Skip to content

fix(launch-rt): resolve glibc libc_nonshared.a symbols after dlsym miss on Linux - #2900

Open
mteacher360 wants to merge 5 commits into
mainfrom
fix/linux-glibc-nonshared-dlsym
Open

mteacher360 wants to merge 5 commits into
mainfrom
fix/linux-glibc-nonshared-dlsym

Conversation

@mteacher360

Copy link
Copy Markdown
Collaborator

Summary

Fixes blaze test / blaze run failures on glibc Linux when guest code references libc_nonshared.a symbols such as pthread_atfork (observed failing all 62 tests in codex-utils-pty with neither 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.so is a GNU ld script grouping libc.so.6 with static archive libc_nonshared.a. Symbols in libc_nonshared.a (pthread_atfork, __pthread_atfork, atexit, at_quick_exit, __stack_chk_fail_local, plus pre-glibc-2.33 stat/lstat/fstat/fstatat/stat64/lstat64/fstat64/fstatat64/mknod/mknodat wrappers) are STV_HIDDEN static stubs forwarding to dynamic symbols in libc.so.6 with __dso_handle, so libc::dlsym(handle, name) and libc::dlsym(RTLD_DEFAULT, name) return NULL.

Changes

  • Added native_lib::glibc_nonshared_fallback(handle, name) (gated on #[cfg(all(target_os = "linux", target_env = "gnu"))]) as the single home for glibc libc_nonshared.a fallback resolution, plus native_lib::dlsym_addr to avoid integer as casts.
  • Wired the fallback strictly after libc::dlsym returns NULL across all three outside-symbol lookup paths in blaze-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)
  • Every fallback entry verifies via dlsym(handle, <backing dynamic symbol>) that handle actually loads libc.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 via dlsym first.
  • Preserved WeakOutside vs Outside behavior and comptime Table isolation.
  • Did not whitelist pthread_atfork/atexit/at_quick_exit in classify_outside_symbol (they remain impure NotKept::Foreign).

Verification

  • cargo test -p blaze-launch-rt -- --test-threads=4: 146 passed, 0 failed (135 unit + 6 native_lib + 5 native_load), including new live tests that resolve and call pthread_atfork(NULL, NULL, NULL) via DlNatives and Dlsym, verify atexit/at_quick_exit/__stack_chk_fail_local, verify WeakOutside vs Outside, 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.
  • Quality gates (stops, casts, clones, allocs, ambient, homes, dups, domains, rustcsites): 0 net new sites in blaze-launch-rt (casts reduced by 3 from 457 → 454); 0 rustc-invoking tests; clean git merge-tree --write-tree origin/main HEAD.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T05:35:01.660458Z ef0b366 Manual request
ℹ️ About Codex in GitHub

Your 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.

@mteacher360

Copy link
Copy Markdown
Collaborator Author

Codex (@codex) review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +591 to +593
"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 ()),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

@mteacher360

Copy link
Copy Markdown
Collaborator Author

Addressed Codex R1 P1 finding in 56282a949:

  1. Synthesized DSO-bound glibc libc_nonshared.a stubs directly inside Linux ELF guest images (blaze-native):
    • Added NonsharedStub (pthread_atfork/__pthread_atfork, atexit, at_quick_exit, __stack_chk_fail_local) in crates/blaze-native/src/synthetic.rs, Resolution::Nonshared in crates/blaze-native/src/resolve.rs, and AArch64 stub emission in crates/blaze-native/src/layout.rs (bumping LAYOUT_VERSION to 3).
    • Each stub passes the guest ELF image's own __dso_handle (ELF header at address 0, matching Synthetic::DsoHandle) and tail-calls its backing dynamic symbol in libc.so.6 (__register_atfork, __cxa_atexit, __cxa_at_quick_exit, __stack_chk_fail) through .got (R_AARCH64_GLOB_DAT).
    • Any pulled C object or Rust export defining the symbol still wins over the synthesized stub; pure-Rust images with no native content and no nonshared roots stay empty (Text.len == 0).
  2. Removed the silent launcher fallback from DlNatives::lookup:
    • DlNatives::lookup now resolves strictly through libc::dlsym(self.handle, name) with zero second lookup route.
  3. Bound pure-JIT (no-.so) Dlsym / Loaded::outside wrappers to the JIT guest's own GUEST_DSO_HANDLE:
    • Unified GUEST_DSO_HANDLE across LauncherOwn, signals::own, and jit_glibc_nonshared so pure-JIT runs without a guest .so pass the guest's own synthetic __dso_handle (never the launcher's) to __register_atfork / __cxa_atexit / __cxa_at_quick_exit.
  4. Verified live with __cxa_finalize on both paths:
    • blaze-launch-rt (146 passed), blaze-native (31 passed), blaze-export (all passed), blaze-ladder --lib native_links (4 passed): live Linux E2E test builds an ELF .so via blaze_native::layout::lay + blaze_export::native_so::write, dlopens via DlNatives, calls pthread_atfork/atexit/at_quick_exit, proves __cxa_finalize(guest_base) invokes & unregisters the guest atexit handler (while an unrelated handle does not), and proves a control .so lacking stubs is refused by DlNatives::lookup.

Codex (@codex) review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +100 to +104
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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Comment thread crates/blaze-native/src/layout.rs Outdated
Comment on lines +466 to +470
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Comment thread crates/blaze-native/src/resolve.rs Outdated
Comment on lines +470 to +471
None if NonsharedStub::of(self.platform, name).is_some()
&& (has_native_content || state.member_ref || state.queued) =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

@mteacher360

Copy link
Copy Markdown
Collaborator Author

Addressed all 4 Codex R2 findings in 9f5b05d57:

  1. Bind pure-JIT nonshared wrappers BEFORE dlsym(RTLD_DEFAULT, ...) (crates/blaze-launch-rt/src/run_mode/bind.rs, crates/blaze-launch-rt/src/records.rs):
    • Both Dlsym::address and Loaded::outside now check jit_glibc_nonshared(name) before global dlsym(RTLD_DEFAULT, ...), ensuring pure-JIT runs always bind pthread_atfork/__pthread_atfork/atexit/at_quick_exit against GUEST_DSO_HANDLE even if another loaded image or LD_PRELOAD exports them globally.
  2. Return the loaded guest .so's base address for __dso_handle (crates/blaze-launch-rt/src/native_lib.rs, crates/blaze-launch-rt/src/run_mode.rs):
    • DlNatives records and exposes base(&self) -> Option<u64> (dli_fbase).
    • NativeMemory::outside returns the loaded guest image's base for __dso_handle when a native library is loaded (matching Synthetic::DsoHandle and the guest .so's synthesized stubs), falling back to GUEST_DSO_HANDLE only when no guest .so is loaded.
    • Added live test run_mode::weak_outside_tests::native_memory_dso_handle_matches_loaded_guest_so_atexit proving __dso_handle and atexit resolved via NativeMemory::outside agree on the guest .so handle and __cxa_finalize(__dso_handle) finalizes the registered handler.
  3. Keep invalid plan indices from masquerading as libc_nonshared.a (crates/blaze-native/src/layout.rs):
    • Restored strict Lay::origin(o) bounds indexing and isolated NONSHARED_ORIGIN strictly to explicit Target::Nonshared / nonshared stub emission sites.
  4. Preserve ELF weak undefined probes for nonshared symbols (crates/blaze-native/src/resolve.rs):
    • Removed the blanket has_native_content synthesis loop and gated Resolution::Nonshared strictly on state.strong_ref || state.root_ref (strong undefined ref from a pulled member OR explicit runtime root in Wanted::roots).
    • Added ELF object tests in crates/blaze-native/tests/resolve.rs verifying: weak undefined STB_WEAK reference to pthread_atfork stays Resolution::Import { weak: true, .. } (not synthesized), while strong STB_GLOBAL reference or explicit Wanted::roots synthesizes Resolution::Nonshared.

Verification

  • blaze-native (31 passed), blaze-launch-rt (147 passed), blaze-export (68 passed, 1 ignored), blaze-ladder --lib native_links (4 passed); --no-default-features checks for blaze-launch-rt and blaze-cli + blaze-launcher all clean.
  • Quality gates (stops, casts, clones, allocs, ambient, homes, dups, domains, rustcsites): 0 net new sites vs origin/main (blaze-launch-rt casts reduced 457 → 452, blaze-native 0 unreviewed dups), clean git merge-tree --write-tree origin/main HEAD.

Codex (@codex) review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +2008 to +2010
Some(addr) => match (name, self.natives.as_ref().and_then(crate::native_lib::DlNatives::base)) {
("__dso_handle", Some(native_base)) => native_base,
_ => addr,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Comment thread crates/blaze-native/src/layout.rs Outdated
Comment on lines +1639 to +1642
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Comment thread crates/blaze-native/src/resolve.rs Outdated
Comment on lines +478 to +479
imports.try_push(stub.backing()).map_err(|_| ResolveError::Alloc)?;
Resolution::Nonshared { stub, backing_import: 0 }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

@mteacher360

Copy link
Copy Markdown
Collaborator Author

Addressed all 3 Codex R3 findings in 048c8784f:

  1. Preserve one unified __dso_handle across NativeStart::Lazy (crates/blaze-launch-rt/src/run_mode.rs):
    • Added NativeMemory::lazy_native_pending (armed on NativeStart::Lazy, cleared by load_library_opened) so pure symbols bind freely before lazy load while DSO-bound symbols (__dso_handle, pthread_atfork, __pthread_atfork, atexit, at_quick_exit) are refused (RunModeError::NoNativeLibrary) until Act::Native loads the guest .so.
    • load_library_opened also rejects installing a guest .so if any DSO-bound symbol was already cached in memory.outside, making split GUEST_DSO_HANDLE vs guest_base handles impossible by construction.
    • Extended run_mode::weak_outside_tests::native_memory_dso_handle_matches_loaded_guest_so_atexit to verify pure pre-load binding, pre-load refusal of __dso_handle/atexit, post-load agreement (__cxa_finalize(__dso_handle) finalizing guest atexit), and control refusal on load_library_opened if __dso_handle was pre-cached.
  2. Load __dso_handle through a rebased GOT cell invariant under native_merge::merge_elf shifts (crates/blaze-native/src/layout.rs, crates/blaze-native/src/synthetic.rs, crates/blaze-export/src/native_merge.rs):
    • Added Cell::DsoHandle (FixTo::rebase(0)) so synthesized pthread_atfork/atexit/at_quick_exit stubs load __dso_handle via adrp xN, got_dso_handle@PAGE; ldr xN, [xN, got_dso_handle@PAGEOFF] (100% invariant under merge_elf's page-aligned shift) instead of raw adrp xN, 0.
    • Updated native_merge::merge_elf to preserve FixTo::Rebase { address: 0, high8: 0 } (the image's ELF header / __dso_handle) at vaddr 0 (matching elf.rs's __dso_handle), rather than shifting it to 0 + shift.
    • Added blaze_export::tests::merge_elf_preserves_dso_handle_at_vaddr_zero_for_shifted_nonshared_stubs verifying shift > 0, R_AARCH64_RELATIVE addend 0 on the shifted GOT cell, decoded adrp; ldr target at shifted PC, and full check::elf.
  3. Resolve nonshared backing symbols through normal program-definition precedence (crates/blaze-native/src/resolve.rs, crates/blaze-native/src/layout.rs):
    • Removed hardcoded backing_import from Resolution::Nonshared; Resolver::pull_nonshared_backings records each active stub's backing symbol (__register_atfork, __cxa_atexit, __cxa_at_quick_exit, __stack_chk_fail) as a strong undefined reference and drains the archive queue so pulled C objects -> Rust #[no_mangle] exports (Plan::words) -> dynamic imports follow standard ELF linker precedence.
    • Added tests in crates/blaze-native/tests/resolve.rs proving both a Rust #[no_mangle] __cxa_atexit export (Plan::words, zero dynamic imports) and a pulled archive member defining __cxa_atexit satisfy atexit's backing symbol without forcing a dynamic import.

Verification

  • blaze-native (31 passed), blaze-launch-rt (147 passed), blaze-export (69 passed, 1 ignored), blaze-ladder --lib native_links (4 passed); --no-default-features checks for blaze-launch-rt and blaze-cli + blaze-launcher all clean.
  • Quality gates (stops, casts, clones, allocs, ambient, homes, dups, domains, rustcsites): 0 net new sites vs origin/main across all 3 touched crates (blaze-native, blaze-launch-rt, blaze-export), clean git merge-tree --write-tree origin/main HEAD.

Codex (@codex) review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread crates/blaze-export/src/native_merge.rs Outdated
// 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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Comment thread crates/blaze-native/src/layout.rs Outdated
}
}
Resolution::Common { .. } => Target::Common { entry: entry as u32, offset: 0 },
Resolution::Nonshared { stub } => Target::Nonshared { stub, offset: 0 },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@mteacher360

Copy link
Copy Markdown
Collaborator Author

Addressed both P2 findings from Round 4 in ef0b366bd:

  1. Explicit FixTo::DsoHandle instead of value-based address == 0 check (blaze-native/src/image.rs, blaze-native/src/layout.rs, blaze-export/src/native_{merge,so,dylib}.rs):

    • Added FixTo::DsoHandle, emitted for Cell::DsoHandle and offset-0 Synthetic::DsoHandle linker targets.
    • native_merge::merge_elf (and merge on Mach-O) keeps FixTo::DsoHandle at vaddr 0, while unconditionally shifting ordinary FixTo::Rebase { address, high8 } by shift even when address == 0 && high8 == 0.
    • Extended merge_elf_preserves_dso_handle_at_vaddr_zero_for_shifted_nonshared_stubs to verify both FixTo::DsoHandle (stays 0) and an ordinary zero-valued Abs64 relocation (text_sym - 0x10000 -> FixTo::Rebase { address: 0, high8: 0 }, shifts to merged.shift).
  2. Export every non-overridden alias of synthesized NonsharedStub members (blaze-native/src/resolve.rs, blaze-native/src/layout.rs):

    • When NonsharedStub::PthreadAtfork is synthesized (whether via pthread_atfork or __pthread_atfork), every non-overridden alias in stub.export_names() is resolved and exported at the same stub address, while respecting explicit C or Rust overrides of either alias.
    • Added unit tests in blaze-native/tests/layout.rs (nonshared_atfork_exports_all_non_overridden_aliases) and blaze-native/tests/resolve.rs covering single-alias requests and explicit C/Rust overrides.

Verified: blaze-native (32 passed), blaze-launch-rt (147 passed), blaze-export (69 passed, 1 ignored), blaze-ladder --lib native_links (4 passed), --no-default-features / blaze-launcher / blaze-cli checks clean, 0 net new sites across all 9 quality gates, clean git merge-tree origin/main HEAD.

Codex (@codex) review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +2007 to +2008
if self.lazy_native_pending && self.natives.is_none() && is_dso_bound_symbol(name) {
return Err(RunModeError::NoNativeLibrary { symbol });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Comment on lines +1072 to +1074
fn is_dso_handle(&self, t: Target) -> bool {
let Target::Linker { entry, offset: 0 } = t else {
return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

This branch has not been deployed

No deployments
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