Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mod real_fn instead of impl real_fn (for cdylib) #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

galkinvv
Copy link

Change real_fn to be defined in mod instead of impl,
so it is exported while building as cdylib.

This allows building crates using redhook not only as dylib but as cdylib too.
Tested on linux, not on mac (but no mac changes).
For my (simple) use case dylib is 3.6MB while cdylib is 2.1MB.

Actually I don't know any doc details describing why impl function is not exported in cdylib,
but it's looking natural for me (new to rust with c/c++ experience) that "methods" are not exported when building a library with a C-compatible interface, but free functions are exported.

Change real_fn to be defined in mod instead of impl,
so it is exported while building as cdylib.
@galkinvv
Copy link
Author

TLDR: I want suggest another change, see changest at last link

I'm new to rust, so I'm not sure that the change is for example compatible with older compiler versions, Ive tested only rust 2018 v1.38

By the way I used redhook for a very corner case - hooking the clock_gettime on linux. It was running fine for most apps, but failed with an obscure crash when running rustc itself.

The problem was very obscure: call to dlsym (to get real function) lead to recursive call clock_gettime.
the call sequence of calls was

  • clock_gettime(hook)
  • real_fn macro
  • dlsym (system lib)
  • malloc (hooked by rustc execuatbale!)
  • clock_gettime(hook)(nested)
  • real_fn macro(nested)
  • call_once is not ready to nedted usage and preparing waiting which would cause a dead lock, but... calling thread::current() doesn't retun
  • thread::current()
  • malloc
  • clock_gettime(hook)(nested 2)`
  • real_fn-call_once-thread::current()-malloc-clock_gettime until a stack overflow

So the top stack near overflow does NOT show that dlsym was a first recursive call initiator!

I developed a code to abort with a clear message in a case of a recursion during dlsym call instead of this complex crash.
Unfortunately it's a bit too hacky to be called "fine-in-production".

galkinvv@b048100

Should I create separate MR for it to discuss details (maybe adding tests, etc) or it is looking too hacky? (I'm discussing it here instead of a separate merge request since code changes are not auto-mergeable)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant