Repository navigation
Description
Problem
cargo build --release fails at link time on musl targets. Chimera Linux defaults to x86_64-unknown-linux-musl, so this is a plain cargo build --release in a clean checkout of master (a3a24cd):
x86_64-chimera-linux-musl-ld: error: undefined symbol: malloc_trim
>>> referenced by jcode_base::process_memory::release_retained_heap
>>> in archive target/release/deps/libjcode_base-*.rlib
x86_64-chimera-linux-musl-ld: error: undefined symbol: mallopt
>>> referenced by jcode::main
cc: error: linker command failed with exit code 1
error: could not compile `jcode` (bin "jcode") due to 1 previous error
mallopt and malloc_trim are glibc extensions. musl does not provide them.
Both call sites are gated on target_os = "linux" alone, which is true for musl, so musl builds compile the glibc path and then fail to link:
src/main.rs:30—configure_system_allocatorcallsmalloptcrates/jcode-base/src/process_memory.rs:492—release_retained_heapcallsmalloc_trim
Reproduction
On any musl host, or on a glibc host with an explicit target:
rustup target add x86_64-unknown-linux-musl
cargo build --release --target x86_64-unknown-linux-musl
Suggested fix
Add target_env = "gnu" to the gates guarding the glibc-only calls, and widen the corresponding fallback arms from not(target_os = "linux") to not(all(target_os = "linux", target_env = "gnu")) so musl selects the no-op arm instead of no arm at all.
Notably, process_memory.rs:238 (glibc_arena_retention_bytes) already uses the all(target_os = "linux", target_env = "gnu", not(feature = "jemalloc")) gate — so this is really just the other sites in the same file not having been given the same treatment.
Sites needing the change:
src/main.rs—configure_system_allocator,parse_alloc_tuning_env,parse_alloc_tuning, and the no-op fallbackcrates/jcode-base/src/process_memory.rs—purge_allocatorandrelease_retained_heap, glibc branch plus fallback
Tradeoffs
- musl loses the allocator tuning entirely.
M_ARENA_MAX/M_MMAP_THRESHOLDpinning and the idlemalloc_trim(0)become no-ops. musl's allocator exposes no equivalent API, so there is nothing to substitute — this is unavailable rather than removed. The RSS behavior the tuning targets (glibc arena retention) is a glibc-specific problem to begin with. allocator:purgeon a musl build takes the "no purge mechanism available" arm and returns the existing error suggesting--features jemalloc. That suggestion is correct for musl — jemalloc builds are unaffected by this and keep full purge support.- No behavior change on glibc, macOS, or Windows. Every gate true before is still true; only musl moves, from "does not link" to "no-op".
parse_alloc_tuningshould keep itsany(test, ...)gate so its unit tests still compile everywhere.
Reference implementation
I could not open a PR (this repo limits PRs to collaborators), so the branch is on a fork:
Two files, four cfg predicates. Diff:
master...jenujari:jcode:chimera-changes
Written with Claude Code assistance (noted in the commit trailer) — per CONTRIBUTING.md, treat it as a reference rather than something to merge.
Verification
Release build on Chimera Linux (musl) with the change applied:
$ cargo build --release
Finished `release` profile [optimized] target(s) in 1m 22s
$ ./target/release/jcode --version
jcode v0.61.2-dev (a3a24cdb)
Environment
- Chimera Linux,
x86_64-unknown-linux-musl(default target) - rustc 1.96.0, cargo 1.96.0
- jcode master @ a3a24cd
- Built with
OPENSSL_DIR=/usr CARGO_PROFILE_RELEASE_LTO=false CARGO_PROFILE_RELEASE_CODEGEN_UNITS=256(LTO off for memory reasons; unrelated to the failure — it reproduces with default profile settings)
Activity
fix(build): gate glibc-only allocator calls on target_env = gnu (fixes …
1jehuang commentedon Jul 28, 2026
Confirmed and fixed in #643, exactly as you suggested. Your diagnosis was precise, including the observation that
glibc_arena_retention_bytesin that same file already had the correct gate, which made this a consistency fix rather than a judgement call.All six sites now gate on
target_env = "gnu", and the matching fallbacks widen fromnot(target_os = "linux")tonot(all(target_os = "linux", target_env = "gnu")):src/main.rs:configure_system_allocator,parse_alloc_tuning_env,parse_alloc_tuning, and the no-op fallbackcrates/jcode-base/src/process_memory.rs:purge_allocator,release_retained_heap, and both fallbacksI could not link a full musl binary here (no musl C toolchain on this machine), so I verified the cfg logic directly against both targets instead, which is the actual claim: with the old gates a musl target selects the glibc arm; with the new gates glibc selects the glibc arm and musl selects the no-op arm, exactly one each. There is also a new unit test asserting that invariant, so re-widening the gate to bare
target_oswill fail CI rather than silently break musl again.Your tradeoffs section is accurate and I did not deviate from it: musl loses allocator tuning entirely (no equivalent API exists, so unavailable rather than removed),
allocator:purgeon musl correctly reports the--features jemallocsuggestion, and glibc/macOS/Windows/jemalloc behavior is unchanged.If you are able to confirm a clean
cargo build --releaseon Chimera once this lands, that would be useful, since it is the one step I could not run end to end.— Jcode agent (automated triage), on behalf of @1jehuang
style: rustfmt the widened allocator cfg gate
1jehuang commentedon Jul 28, 2026
Follow-up: I found a way to verify this properly after all, so disregard my caveat about not being able to link for musl.
Using
zig cc -target x86_64-linux-muslas the C toolchain, I compiled the actual committedrelease_retained_heapbody for both targets and inspected the resulting object's undefined symbols:And to confirm the old gates were genuinely the cause rather than a plausible theory, the same comparison with the pre-fix gating on a musl target reproduces exactly the two symbols from your report:
So: glibc keeps the tuning, musl no longer emits the undefined references, and the failure mode you reported is reproduced and then eliminated. That is the substantive half of
cargo build --releaseon musl; a full end-to-end release build still needs a real musl host because an unrelated transitive dependency (aws-lc-sys) has assembly that zig will not build. So a confirmation from you on Chimera is still welcome, but the specific defect in #645 is now verified fixed rather than assumed.— Jcode agent (automated triage), on behalf of @1jehuang
jenujari commentedon Jul 30, 2026
Confirmed on Chimera Linux (x86_64 musl). Built PR #643 at head
111ee842, pre-merge:Clean whole-workspace release build, zero errors. That covers the
aws-lc-sysassembly that blocked your zig path, so the end-to-end link is now verified on a real musl host rather than assumed.Confirming your symbol result against the linked binary rather than a single object:
No
.interpbecause this is a static-pie musl binary, which is also relevant to the second half of the original report: a natively built binary has no dynamic loader path to get wrong.Your regression test passes here:
One thing you may want to know: the musl-specific assertion in that test — the
if cfg!(all(target_os = "linux", target_env = "musl"))block asserting!glibc_arm_active— is unreachable on a glibc host, so this is its first actual execution. Theassert_ne!on exactly-one-arm does run everywhere, but the musl branch only means something on a musl target. Unless CI has a musl job, that branch stays dormant there too.fix(build): gate glibc-only allocator calls on target_env = gnu (fixes …
style: rustfmt the widened allocator cfg gate
fix(build): gate glibc-only allocator calls on target_env = gnu (fixes …
style: rustfmt the widened allocator cfg gate
fix(build): gate glibc-only allocator calls on target_env = gnu (fixes …
style: rustfmt the widened allocator cfg gate