-
Notifications
You must be signed in to change notification settings - Fork 381
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[SCP-184] Fix incompatible library check when there's no libruby.so #2250
Merged
ivoanjo
merged 1 commit into
master
from
ivoanjo/scp-184-handle-ruby-without-shared-object
Aug 31, 2022
Merged
[SCP-184] Fix incompatible library check when there's no libruby.so #2250
ivoanjo
merged 1 commit into
master
from
ivoanjo/scp-184-handle-ruby-without-shared-object
Aug 31, 2022
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The logic in `ddtrace_profiling_loader.c` follows Ruby's `dln.c`; see the big comment at the top of the former for why it needs to exist. The `incompatible_library` sanity check exists to try to catch situations where the library being loaded is linked to a different `libruby.so` (or equivalent in other OSs) than the one that is doing the loading. BUT when we implemented `incompatible_library` we missed an check that upstream Ruby does. From https://github.com/ruby/ruby/blob/v3_1_2/dln.c#L295: ```c static bool dln_incompatible_library_p(void *handle) { void *ex = dlsym(handle, EXTERNAL_PREFIX"ruby_xmalloc"); void *const fp = (void *)ruby_xmalloc; return ex && ex != fp; } ``` Ruby actually checks that the symbol found is **not NULL** (e.g. with the `ex && ` check). This is important, because there's a valid situation in which the symbol is expected to be NULL: when Ruby is built with `--disable-shared` at compilation time. What `--disable-shared` does is that it builds the Ruby VM logic inside the `ruby` executable itself INSTEAD OF splitting the logic between the `ruby` executable and the `libruby.so` shared library. To properly support this configuration, we should, like upstream Ruby does, consider a library compatible if the symbol lookup returns `NULL`. Extra note -- here's a few ways of telling if there's a `libruby`: * `RbConfig::CONFIG['configure_args']` will contain `--disable-shared` * `RbConfig::CONFIG['LIBRUBY']` will be `libruby-static.a` instead of, for instance `libruby.so.3.1.1` * Ruby will not be linked to libruby: ``` # with libruby $ ldd `which ruby` | grep libruby libruby.so.3.1 => /usr/local/lib/libruby.so.3.1 (0x00007fb37e260000) # without libruby $ ldd `which ruby` | grep libruby (no output) ```
r1viollet
approved these changes
Aug 31, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
If upstream ruby relies on this, it sounds good to me
Nice reproducing and checking the behaviour !
TonyCTHsu
approved these changes
Aug 31, 2022
ivoanjo
added a commit
that referenced
this pull request
Feb 6, 2025
**What does this PR do?** This PR removes the `datadog_profiling_loader` native extension, as it's no longer needed. The profiling native loader was added in #2003 . Quoting from that PR's description: > When Ruby loads a native extension (using `require`), it uses > `dlopen(..., RTLD_LAZY | RTLD_GLOBAL)` > (https://github.com/ruby/ruby/blob/67950a4c0a884bdb78d9beb4405ebf7459229b21/dln.c#L362). > > This means that every symbol exposed directly or indirectly by that > native extension becomes visible to every other extension in the > Ruby process. This can cause issues, see > rubyjs/mini_racer#179 . > > Ruby's extension loading mechanism is not configurable -- there's > no way to tell it to use different flags when calling `dlopen`. > To get around this, this commit introduces introduces another > extension (profiling loader) which has only a single responsibility: > mimic Ruby's extension loading mechanism, but when calling > `dlopen` use a different set of flags. > > This idea was shamelessly stolen from @lloeki's work in > rubyjs/mini_racer#179, big thanks! ...and importantly ends with: > Note that, that we know of, the profiling native extension only > exposes one potentially problematic symbol: > `rust_eh_personality` (coming from libddprof/libdatadog). > > Future versions of Rust have been patched not to expose this > (see rust-lang/rust#95604 (comment)) > so we may want to revisit the need for this loader in the future, > and perhaps delete it if we no longer require its services :) And we have reached the situation predicted in that description: 1. Nowadays libdatadog no longer exposes `rust_eh_personality` 2. We have a test that validates that only expected symbols are exported by the libdatadog library (see DataDog/libdatadog#573 ). Any new symbols that show up would break shipping new libdatadog versions to rubygems.org until we review them. 3. The `libdatadog_api` extension, which we've been shipping for customers since release 2.3.0 back in July 2024 has always been loaded directly without a loader without issues. Thus, I think it's the right time to get rid of the loader. **Motivation:** Having the loader around is not zero cost; we've run into/caused a few issues ( #2250 and #3582 come to mind). It also adds overhead to development: every time we need to rebuild the extensions, it's an extra extension that needs to be prepared, rebuilt, copied, etc. I've been meaning to get rid of the loader for some time now, and this came up again this week so I decided it was time to do it. **Additional Notes:** In the future, we can always ressurrect this approach if we figure out we need it again. We've also discussed internally about proposing upstream to the Ruby VM to maybe add an API to do what the loader was doing, so that we wouldn't need the weird workaround. We haven't yet decided if we're going to do that (help welcome!). **How to test the change?** The loader was responsible for loading the rest of profiling. Thus, any test that uses profiling was also validating the loader and now will validate that we're doing fine without it. TL;DR green CI is good.
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?:
Add a missing check for a
NULL
result ofdlsym
that happens when Ruby is not built withlibruby.so
.Motivation:
The logic in
ddtrace_profiling_loader.c
follows Ruby'sdln.c
; see the big comment at the top of the former for why it needs to exist.The
incompatible_library
sanity check exists to try to catch situations where the library being loaded is linked to a differentlibruby.so
(or equivalent in other OSs) than the one that is doing the loading.BUT when we implemented
incompatible_library
we missed an check that upstream Ruby does.From https://github.com/ruby/ruby/blob/v3_1_2/dln.c#L295:
Ruby actually checks that the symbol found is not NULL (e.g. with the
ex &&
check). This is important, because there's a valid situation in which the symbol is expected to be NULL: when Ruby is built with--disable-shared
at compilation time.What
--disable-shared
does is that it builds the Ruby VM logic inside theruby
executable itself INSTEAD OF splitting the logic between theruby
executable and thelibruby.so
shared library.To properly support this configuration, we should, like upstream Ruby does, consider a library compatible if the symbol lookup returns
NULL
.Additional Notes:
Here's a few ways of telling if there's a
libruby
:RbConfig::CONFIG['configure_args']
will contain--disable-shared
RbConfig::CONFIG['LIBRUBY']
will belibruby-static.a
instead of, for instancelibruby.so.3.1.1
How to test the change?:
I couldn't find a prebuilt docker image that had Ruby compiled with
--disable-shared
.To reproduce the issue, I downloaded the latest Ruby version, configured it with
./configure --disable-shared
, compiled and installed it, and then installeddd-trace-rb
, and tried to start the profiler.Without this fix, profiling did not work, I would get:
With the fix, the profiler starts up successfully.