Skip to content

Build fails to link on musl targets: undefined symbol malloc_trim / mallopt #645

Closed

Description

@jenujari

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_allocator calls mallopt
  • crates/jcode-base/src/process_memory.rs:492 — release_retained_heap calls malloc_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 fallback
  • crates/jcode-base/src/process_memory.rs — purge_allocator and release_retained_heap, glibc branch plus fallback

Tradeoffs

  • musl loses the allocator tuning entirely. M_ARENA_MAX / M_MMAP_THRESHOLD pinning and the idle malloc_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:purge on 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_tuning should keep its any(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:

jenujari@60cecab

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

  1. added a commit that references this issue on Jul 28, 2026
    3631f63
  2. added
    bug
    priority: high
    triage: reproducible
    autonomous: clear
    on Jul 28, 2026
  3. 1jehuang commented on Jul 28, 2026

    @1jehuang
    Owner

    Confirmed and fixed in #643, exactly as you suggested. Your diagnosis was precise, including the observation that glibc_arena_retention_bytes in 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 from not(target_os = "linux") to not(all(target_os = "linux", target_env = "gnu")):

    • src/main.rs: configure_system_allocator, parse_alloc_tuning_env, parse_alloc_tuning, and the no-op fallback
    • crates/jcode-base/src/process_memory.rs: purge_allocator, release_retained_heap, and both fallbacks

    I 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_os will 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:purge on musl correctly reports the --features jemalloc suggestion, and glibc/macOS/Windows/jemalloc behavior is unchanged.

    If you are able to confirm a clean cargo build --release on 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

  4. 1jehuang commented on Jul 28, 2026

    @1jehuang
    Owner

    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-musl as the C toolchain, I compiled the actual committed release_retained_heap body for both targets and inspected the resulting object's undefined symbols:

    # real code, glibc target -> glibc symbol referenced (tuning still works)
    $ nm librealprobe_gnu.a | grep ' U mallo'
                     U malloc_trim
    
    # real code, musl target -> no glibc symbols at all (link failure gone)
    $ nm librealprobe.a | grep ' U mallo'
    (empty)
    

    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:

    $ nm liboldlib.a | grep ' U mallo'
                     U malloc_trim
                     U mallopt
    

    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 --release on 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

  5. jenujari commented on Jul 30, 2026

    @jenujari
    Author

    Confirmed on Chimera Linux (x86_64 musl). Built PR #643 at head 111ee842, pre-merge:

    $ OPENSSL_DIR=/usr CARGO_PROFILE_RELEASE_LTO=false \
      CARGO_PROFILE_RELEASE_CODEGEN_UNITS=256 CARGO_PROFILE_RELEASE_DEBUG=0 \
      cargo build --release
        Finished `release` profile [optimized] target(s) in 23m 23s
    
    $ ./target/release/jcode --version
    jcode v0.61.2-dev (111ee842)
    

    Clean whole-workspace release build, zero errors. That covers the aws-lc-sys assembly 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:

    $ nm -D target/release/jcode | grep -E ' U (mallopt|malloc_trim)'
    (empty)
    
    $ readelf -p .interp target/release/jcode
    could not find section '.interp'
    

    No .interp because 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:

    test process_memory::tests::glibc_only_allocator_paths_are_gated_on_gnu_not_just_linux ... ok
    test result: ok. 12 passed; 0 failed
    

    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. The assert_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.

  6. added 4 commits that reference this issue on Aug 30, 2026
    9cf0844
    555b073
    81b7195
    e789dee
  7. added 2 commits that reference this issue on Sep 18, 2026
    cee58b0
    ad32ea8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    autonomous: clearbugpriority: hightriage: reproducible

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions