Repository navigation
fix(launch-rt): resolve glibc libc_nonshared.a symbols after dlsym miss on Linux - #2900
mteacher360 wants to merge 5 commits into
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex (@codex) review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8020664609
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "Codex (@codex) review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "Codex (@codex) address that feedback".
| "pthread_atfork" | "__pthread_atfork" => (c"__register_atfork", libc::pthread_atfork as *const ()), | ||
| "atexit" => (c"__cxa_atexit", libc::atexit as *const ()), | ||
| "at_quick_exit" => (c"__cxa_at_quick_exit", at_quick_exit as *const ()), |
There was a problem hiding this comment.
Link the nonshared stubs into the guest image
When guest code calls pthread_atfork, atexit, or at_quick_exit, these pointers select stubs linked into the launcher, so each stub passes the launcher's embedded __dso_handle rather than the guest image's handle. Consequently, guest calls such as __unregister_atfork(__dso_handle) or __cxa_finalize(__dso_handle) cannot remove the registrations, and unloading would leave callbacks targeting guest code; glibc places these wrappers in libc_nonshared.a precisely so every DSO gets its own handle. Link/copy the corresponding archive objects into the generated native image instead of silently taking a second lookup route after dlsym fails.
AGENTS.md reference: AGENTS.md:L121-L123
Useful? React with 👍 / 👎.
|
Addressed Codex R1 P1 finding in
Codex (@codex) review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 56282a949a
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "Codex (@codex) review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "Codex (@codex) address that feedback".
| if let Some(found) = crate::native_lib::dlsym_addr(address) { | ||
| return Ok(found); | ||
| } | ||
| #[cfg(all(target_os = "linux", target_env = "gnu"))] | ||
| if let Some(fallback) = crate::native_lib::jit_glibc_nonshared(name) { |
There was a problem hiding this comment.
Bind JIT nonshared wrappers before global dlsym
When a pure-JIT Linux run has an executable, preload, or other globally loaded image exporting pthread_atfork, atexit, or at_quick_exit, this early return accepts that image's wrapper and never reaches jit_glibc_nonshared; registrations then use the foreign image's __dso_handle, so __cxa_finalize(GUEST_DSO_HANDLE) or __unregister_atfork(GUEST_DSO_HANDLE) cannot remove them. Loaded::outside repeats the same ordering. Resolve these known JIT-owned wrappers before RTLD_DEFAULT lookup rather than as a second route after failure.
AGENTS.md reference: AGENTS.md:L121-L123
Useful? React with 👍 / 👎.
| static DSO_HANDLE: u8 = 0; | ||
| if let Some(addr) = match name { | ||
| "__dso_handle" => Some(&raw const DSO_HANDLE as u64), | ||
| "__dso_handle" => u64::try_from((&raw const crate::native_lib::GUEST_DSO_HANDLE).addr()).ok(), |
There was a problem hiding this comment.
Return the native image handle when a guest .so is loaded
When a run has a guest native .so, NativeMemory::outside still calls this interception before DlNatives::lookup, so JIT guest references to __dso_handle receive GUEST_DSO_HANDLE while the newly synthesized .so stubs register callbacks with the ELF base. A guest call such as __cxa_finalize(__dso_handle) therefore cannot finalize handlers registered through its resolved atexit stub. Make the interception native-image-aware and return the loaded image's base in this context.
AGENTS.md reference: AGENTS.md:L116-L119
Useful? React with 👍 / 👎.
| self.plan | ||
| .objects | ||
| .get(usize::try_from(o).unwrap_or(usize::MAX)) | ||
| .and_then(|pulled| self.inputs.get(usize::try_from(pulled.input).unwrap_or(usize::MAX))) | ||
| .map_or(Origin { package: "libc_nonshared", file: "libc_nonshared.a" }, |input| input.from) |
There was a problem hiding this comment.
Keep invalid plan indices from masquerading as libc input
If an internal Plan contains an out-of-range object or input index, this now silently reports libc_nonshared.a as the origin instead of exposing the violated layout precondition, so unrelated linker defects produce misleading user-facing errors and can continue through a second path. Use the synthetic origin only at the explicit no-object stub call sites and preserve a named failure or panic for invalid indices.
AGENTS.md reference: AGENTS.md:L121-L123
Useful? React with 👍 / 👎.
| None if NonsharedStub::of(self.platform, name).is_some() | ||
| && (has_native_content || state.member_ref || state.queued) => |
There was a problem hiding this comment.
Preserve weak undefined nonshared references
When a pulled ELF member has only a weak undefined reference to one of these symbols, member_ref is true while strong_ref is false, but this condition still resolves it to a synthesized stub; has_native_content makes the same happen merely because unrelated native content exists. That contradicts this resolver's ELF rule that weak undefined references do not pull archive members, turning optional probes such as if (pthread_atfork != NULL) into non-null calls and adding strong backing imports. Gate archive-style synthesis on a strong reference or an explicit runtime root, leaving weak references weak.
AGENTS.md reference: AGENTS.md:L116-L119
Useful? React with 👍 / 👎.
…s, and plan bounds
|
Addressed all 4 Codex R2 findings in
Verification
Codex (@codex) review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9f5b05d57b
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "Codex (@codex) review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "Codex (@codex) address that feedback".
| Some(addr) => match (name, self.natives.as_ref().and_then(crate::native_lib::DlNatives::base)) { | ||
| ("__dso_handle", Some(native_base)) => native_base, | ||
| _ => addr, |
There was a problem hiding this comment.
Preserve one DSO handle across lazy native loading
In the supported NativeStart::Lazy path, the entry can resolve __dso_handle and atexit before Act::Native, so self.natives is None here and self.outside caches GUEST_DSO_HANDLE and the JIT wrapper. load_library_opened later assigns memory.natives = Some(natives) without invalidating that cache; JIT registrations therefore remain attached to the synthetic handle while the guest .so stubs use its ELF base, so __cxa_finalize(__dso_handle) or atfork unregistration can miss half the program's registrations depending solely on lazy versus eager loading. Fresh evidence beyond the earlier comment is this cache surviving the supported lazy-load transition; keep one handle across that transition or rebind the affected cached entries when installing the library.
AGENTS.md reference: AGENTS.md:L116-L119
Useful? React with 👍 / 👎.
| let Ok(adrp) = adrp_to(ADRP_X2, nonshared_cur + 4, 0) else { | ||
| return Err(me.far_nonshared(b"(atexit stub)", Reach::Distance { place: nonshared_cur + 4, to: 0 })); | ||
| }; | ||
| me.put32(nonshared_cur + 4, adrp); |
There was a problem hiding this comment.
Rebase nonshared DSO-handle instructions during ELF merge
When this image is merged into an exported Linux executable with a nonzero native_merge::place_blocks shift, the native block's bytes move but its PC-relative instructions are deliberately copied unchanged. This adrp_to(..., 0) therefore stops addressing the executable's ELF header at address zero: after the move the unchanged immediate produces shift instead, so atexit registers callbacks under a pointer that differs from the executable's synthesized __dso_handle; the analogous pthread_atfork and at_quick_exit instructions have the same problem. As a result, exported programs cannot finalize or unregister those callbacks by __dso_handle; relocate these header references during merge_elf or encode the handle through a merge-aware cell.
AGENTS.md reference: AGENTS.md:L116-L119
Useful? React with 👍 / 👎.
| imports.try_push(stub.backing()).map_err(|_| ResolveError::Alloc)?; | ||
| Resolution::Nonshared { stub, backing_import: 0 } |
There was a problem hiding this comment.
Resolve nonshared backing names against program definitions
When the program itself defines one of these backing symbols—such as a Rust #[no_mangle] __cxa_atexit supplied by a custom runtime—this unconditionally creates a dynamic import instead of resolving the synthetic stub's undefined reference through the existing native-definition/Rust-word precedence. The Rust export consequently never enters Plan::words, and atexit calls the launcher's glibc implementation rather than the program definition; __register_atfork, __cxa_at_quick_exit, and __stack_chk_fail have the same issue. A normal link resolves the undefined reference from the pulled libc_nonshared.a member against the program first, so route each backing name through the ordinary resolver instead of forcing an import.
AGENTS.md reference: AGENTS.md:L116-L119
Useful? React with 👍 / 👎.
…dence, and lazy-load handle consistency
|
Addressed all 3 Codex R3 findings in
Verification
Codex (@codex) review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 048c8784fd
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "Codex (@codex) review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "Codex (@codex) address that feedback".
| // Address 0 with zero tag is the image's ELF header (`Synthetic::DsoHandle`, | ||
| // `Cell::DsoHandle`): the merged executable's ELF header stays at vaddr 0 | ||
| // (matching `elf.rs`'s `__dso_handle`), not shifted into the native segment. | ||
| let target = if address.get() == 0 && high8 == 0 { |
There was a problem hiding this comment.
Distinguish DSO handles from zero-valued rebases
When an ordinary native relocation evaluates to an untagged image-relative zero—for example, a pointer to a symbol at 0x10000 with addend -0x10000—it produces the same Rebase(0) encoding as the new DSO-handle cell. This value-based check now preserves zero for both, although the ordinary target must move by shift; after merging, that pointer therefore targets the executable header instead of the relocated native image base. Preserve the DSO-handle identity explicitly rather than inferring it from the numeric rebase value.
AGENTS.md reference: AGENTS.md:L116-L119
Useful? React with 👍 / 👎.
| } | ||
| } | ||
| Resolution::Common { .. } => Target::Common { entry: entry as u32, offset: 0 }, | ||
| Resolution::Nonshared { stub } => Target::Nonshared { stub, offset: 0 }, |
There was a problem hiding this comment.
Export every alias of the synthesized atfork member
When only pthread_atfork or only __pthread_atfork appears in Plan::names, this loop exports the synthesized stub solely under that referenced name. However, NonsharedStub::export_names() records both aliases as definitions of the same archive member, so a runtime lookup of the omitted alias misses the guest's DSO-bound wrapper and may fail or bind elsewhere, unlike pulling the real libc_nonshared.a member. Emit every non-overridden alias belonging to the synthesized stub.
AGENTS.md reference: AGENTS.md:L116-L119
Useful? React with 👍 / 👎.
|
Addressed both P2 findings from Round 4 in
Verified: Codex (@codex) review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ef0b366bda
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "Codex (@codex) review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "Codex (@codex) address that feedback".
| if self.lazy_native_pending && self.natives.is_none() && is_dso_bound_symbol(name) { | ||
| return Err(RunModeError::NoNativeLibrary { symbol }); |
There was a problem hiding this comment.
Allow DSO-bound symbols before a lazy native load
Under NativeStart::Lazy, the entry intentionally starts before Act::Native, so a valid JIT prefix can call atexit/pthread_atfork or reference __dso_handle before it first touches the native library. This new early return makes that program fail with NoNativeLibrary, although the same symbols work in pure-JIT and eager-native runs. Fresh evidence beyond the prior lazy-handle finding is that the remediation now rejects the pre-load call outright; load the pending image on this demand or otherwise provide one handle without refusing valid calls.
AGENTS.md reference: AGENTS.md:L116-L119
Useful? React with 👍 / 👎.
| fn is_dso_handle(&self, t: Target) -> bool { | ||
| let Target::Linker { entry, offset: 0 } = t else { | ||
| return false; |
There was a problem hiding this comment.
Carry DSO-handle addends through merge
When an Abs64 or GOT relocation targets __dso_handle + N with a nonzero addend, this predicate classifies it as an ordinary Rebase(N). merge_elf/merge then adds the native block's shift, so an exported executable points at native_block + N rather than its own header plus N; only the zero-addend spelling gets the new identity-preserving behavior. Represent the DSO handle together with its addend so every relocation derived from that linker symbol remains relative to the enclosing image.
AGENTS.md reference: AGENTS.md:L116-L119
Useful? React with 👍 / 👎.
Summary
Fixes
blaze test/blaze runfailures on glibc Linux when guest code referenceslibc_nonshared.asymbols such aspthread_atfork(observed failing all 62 tests incodex-utils-ptywithneither the program's native library nor a library it loads defines it ('dlsym' on its handle)).Root Cause
On glibc Linux (
#[cfg(all(target_os = "linux", target_env = "gnu"))]),libc.sois a GNU ld script groupinglibc.so.6with static archivelibc_nonshared.a. Symbols inlibc_nonshared.a(pthread_atfork,__pthread_atfork,atexit,at_quick_exit,__stack_chk_fail_local, plus pre-glibc-2.33stat/lstat/fstat/fstatat/stat64/lstat64/fstat64/fstatat64/mknod/mknodatwrappers) areSTV_HIDDENstatic stubs forwarding to dynamic symbols inlibc.so.6with__dso_handle, solibc::dlsym(handle, name)andlibc::dlsym(RTLD_DEFAULT, name)returnNULL.Changes
native_lib::glibc_nonshared_fallback(handle, name)(gated on#[cfg(all(target_os = "linux", target_env = "gnu"))]) as the single home for glibclibc_nonshared.afallback resolution, plusnative_lib::dlsym_addrto avoid integerascasts.libc::dlsymreturnsNULLacross all three outside-symbol lookup paths inblaze-launch-rt:DlNatives::lookup(crates/blaze-launch-rt/src/native_lib.rs)Dlsym::address(crates/blaze-launch-rt/src/run_mode/bind.rs)Loaded::outside(crates/blaze-launch-rt/src/records.rs)dlsym(handle, <backing dynamic symbol>)thathandleactually loadslibc.so.6(__register_atfork,__cxa_atexit,__cxa_at_quick_exit,__stack_chk_fail,__xstat, etc.), preserving handle isolation while letting any native-library-defined or interposed symbol win viadlsymfirst.WeakOutsidevsOutsidebehavior and comptimeTableisolation.pthread_atfork/atexit/at_quick_exitinclassify_outside_symbol(they remain impureNotKept::Foreign).Verification
cargo test -p blaze-launch-rt -- --test-threads=4: 146 passed, 0 failed (135 unit + 6native_lib+ 5native_load), including new live tests that resolve and callpthread_atfork(NULL, NULL, NULL)viaDlNativesandDlsym, verifyatexit/at_quick_exit/__stack_chk_fail_local, verifyWeakOutsidevsOutside, and verify native-library interposition takes precedence over the fallback.cargo check -p blaze-launch-rt --no-default-features,cargo check -p blaze-launcher,cargo check -p blaze-cli --bin blaze --no-default-features: all clean.stops,casts,clones,allocs,ambient,homes,dups,domains,rustcsites): 0 net new sites inblaze-launch-rt(castsreduced by 3 from 457 → 454); 0rustc-invoking tests; cleangit merge-tree --write-tree origin/main HEAD.