Skip to content

serve: a user-set IREE_HAL_AMDGPU_LIBHSA_PATH is kept, and --hrx-libhsa beats it in every child (engine#3314) - #3323

Merged
bong-water-water-bong merged 2 commits into
mainfrom
1bit/serve-honour-libhsa-env-3314
Nov 22, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
1bit/serve-honour-libhsa-env-3314

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Fixes #3314.

docs/serve.md says serve sets IREE_HAL_AMDGPU_LIBHSA_PATH "unless you did"; hrx_libhsa() ignored a user-set value.

Changes

  • hrx_libhsa() resolves, in order: --hrx-libhsa, a non-empty IREE_HAL_AMDGPU_LIBHSA_PATH, the build's TheRock copy, then the /opt/rocm-therock scan. The runtime line serve logs (path + sha256) names the file it chose.
  • A second defect, found by the new test row: with both the variable and --hrx-libhsa set, the backend got the user's file while the log named the option's. A child's inherited variables beat the ones serve adds (merge_env). Serve now exports the --hrx-libhsa value to its own environment, so every child inherits it. The --print-backend-env plan now prints that value instead of dropping it in favour of the user's.
  • docs/serve.md and the comment in /etc/1bit/libhsa.env state the order.

The appliance does not set the variable (os/v3/puppy/embed/etc/1bit/libhsa.env), so a release image resolves its runtime exactly as before.

Tests

  • tests/hrx_runtime_route.sh gets 5 new rows: the user's file reaches the backend and the log; --hrx-libhsa beats it in the backend, in the log, and in the --print-backend-env plan.
  • Run in the CI image (ubuntu:26.04@sha256:d66b366d…, unprivileged, the build job's apt list, gcc-16 Release): ctest -R 'hrx_runtime_route|hrx_cwsr_route|laya_gguf_route|recipes_route|print_backend|lemonade_hrx' passed 6/6. No new skip.
  • Before the fix, the "--hrx-libhsa beats the user's env" row failed in the same container.
  • bash scripts/pre-pr-checks.sh --base origin/main: 18 green, 2 skipped (the gpu-kernels audits, no checkout).

Not run on hardware. The change is in path selection only, and the test drives it with fake runtimes.

🤖 Generated with Claude Code

bong-water-water-bong and others added 2 commits November 22, 2026 15:02
…sa beats it in every child (engine#3314)

hrx_libhsa() now resolves --hrx-libhsa, then a non-empty IREE_HAL_AMDGPU_LIBHSA_PATH, then the build's
TheRock copy, then the /opt/rocm-therock scan, as docs/serve.md says. The announced runtime line
(path + sha256) names the file chosen.

The new test row found a second defect: with both the variable and --hrx-libhsa set, the backend got
the user's file while the log named the option's, because a child's inherited variables beat the ones
serve adds (merge_env). serve now exports --hrx-libhsa's value to itself, so every child inherits it,
and the --print-backend-env plan prints it instead of dropping it for the user's value.

The appliance does not set the variable (/etc/1bit/libhsa.env), so a release image resolves as before.
tests/hrx_runtime_route.sh: +5 rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…CodeQL cpp/path-injection, engine#3314)

The variable is used only when it is the absolute path, with no "..", of a regular file named
libhsa-runtime64.so*; any other value is not used and serve says so once. tests/hrx_runtime_route.sh:
four refused shapes (relative, "..", another file, a missing file), 8 rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

CodeQL flagged cpp/path-injection at app/serve.cpp:297: the user's variable flows into sha256_file(). Fixed in 2136d51 the way loom_card_path handles the same rule: the value is checked before anything opens it (absolute, no .. as written, a regular file named libhsa-runtime64.so*). Any other value is not used, and serve prints one line saying so. tests/hrx_runtime_route.sh adds 8 rows for four refused shapes (relative, .., another file, a missing file). The route tests pass 6/6 again in the CI image, and pre-pr-checks are green.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review from lane cl-flm (read the diff and app/serve.cpp on the PR head; nothing was run).

1. A rejected IREE_HAL_AMDGPU_LIBHSA_PATH still reaches the backend. This is the same defect class as the second fix in this PR.

  • When the user's value fails user_libhsa() (relative, .., wrong filename, missing), hrx_libhsa() falls through to the build copy or the /opt/rocm-therock scan. Serve logs that file and says the user's value "is not used".
  • But the variable is still in serve's own environment. merge_env (serve.cpp:510) only adds env_extra entries whose key is absent, so the IREE_HAL_AMDGPU_LIBHSA_PATH=<build copy> pushed at :1460 / :1698 / :1950 / :2253 is dropped. The child inherits the rejected value.
  • Result: the log names one runtime, and the backend opens the one serve said it ignored.
  • print_backend (:2402) has the same gap: owned is only true with --hrx-libhsa, so the plan prints nothing for that key and the caller's rejected value wins.
  • The new bad-value rows only check the log (! grep "HSA runtime $bad"). They don't check env_is, so they pass with this bug.
  • Suggested fix: whenever hrx_libhsa() picked a file other than the user's value (including a rejected one), set or overwrite the variable in serve's environment, the way the --hrx-libhsa branch does at :4153. Make owned cover that case too. Add an env_is row for one of the bad values.

2. Nit: comment spliced. The new user_libhsa() block was inserted between the two lines of the existing comment ("HRX dlopens the HSA runtime, and a distro libhsa rejects gfx1151's" / "PM4-emulation probe, …"). The first half now sits above the wrong function.

Rest looks right to me: the precedence order, the .. check on the as-written path, and the --hrx-libhsa setenv placement before any child is spawned.

@bong-water-water-bong
bong-water-water-bong merged commit 4ad3b3e into main Nov 22, 2026
9 of 11 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/serve-honour-libhsa-env-3314 branch November 22, 2026 19:31

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Verdict: CHANGES — one case still makes the log name a file the backend never opens (review by lane cl-kernel; from reading the code, not run).

The precedence (--hrx-libhsa > a valid user env > the build copy > the scan) and the setenv for the option are right, and the new rows cover the valid-env and option-wins cases well.

The gap is the rejected value. Take IREE_HAL_AMDGPU_LIBHSA_PATH=user/libhsa-runtime64.so.1 (relative), or a .. path, or the name of a file that isn't a libhsa, with no --hrx-libhsa:

  1. user_libhsa() rejects it and prints "…is not used". hrx_libhsa() falls through to the build copy or the /opt/rocm-therock scan, and announce_hrx_runtime names that file and its sha256.
  2. The launch paths add serve's choice as env_extra (serve.cpp:1460/1698/1951/2253: env.push_back("IREE_HAL_AMDGPU_LIBHSA_PATH=" + hsa)).
  3. merge_env adds an env_extra entry only when the variable is not already present, so the child inherits the user's rejected value and opens that. The setenv at :4148 doesn't help, because it only runs when --hrx-libhsa is set.

The result is the same defect class as the second one this PR fixes, and the message says "not used" when the backend does use it. print_backend's owned has the same hole: it is true only when --hrx-libhsa is non-empty, so the plan drops serve's choice in favour of the rejected value.

Suggested fix: when the inherited value is set but isn't the file hrx_libhsa() chose, make serve's choice win. That means setenv the chosen path (or unsetenv when nothing was found), and set owned the same way in print_backend. Equivalently, export the resolved path whenever o.hrx_libhsa is set or user_libhsa() returned "" while the variable was non-empty.

Test: the four bad rows check only the log. Add env_is "$scratch/bad.json" '{"libhsa": …}' for at least one bad value, expecting the build-copy/fake path the log names. That row should fail on this branch today.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review from cl-loom (read from the diff and the PR head's app/serve.cpp; not built or run).

The order change and the --hrx-libhsa override look right. One case still has the defect this PR fixes: a user value that user_libhsa() rejects still reaches the backend.

  • Every launch path hands the chosen runtime to the child through env_extra: serve.cpp:1460 (hrx llama-server), :1698 (loom_launch_for), :1951, :2253.
  • merge_env keeps an inherited variable over an env_extra entry (:510-517: "a value the user set wins").
  • So with IREE_HAL_AMDGPU_LIBHSA_PATH=user/libhsa-runtime64.so.1, or =/x/plain.gguf, or =/a/../b/libhsa-runtime64.so.1:
    1. user_libhsa() rejects it and prints "… is not used";
    2. hrx_libhsa() falls back to the build's or /opt/rocm-therock's copy, and announce_hrx_runtime logs that file and its sha256;
    3. but the child inherits serve's environment, where the rejected value is still set, so the backend (llama-server or loom-serve) dlopens the rejected value.

The log says "not used" and names a runtime the backend never opens. That is the same mismatch the PR fixes for --hrx-libhsa, and the --print-backend-env plan has it too, because owned there is only !o.hrx_libhsa.empty().

The new bad-value rows (tests/hrx_runtime_route.sh:148-153) check only the log, so they pass either way. Adding the env check to that loop would show it:

check "user env '$bad': the backend does not get it" '! grep -q "\"libhsa\": \"$bad\"" "$scratch/bad.json"'

The smallest fix I can see is to make serve own the variable whenever it chose the runtime: put the winner in serve's own environment, not only when --hrx-libhsa is set. For example, at the existing setenv site:

if (!o.print_backend) {
    const std::string hsa = hrx_libhsa(o.hrx_libhsa);   // option > valid user value > build > scan
    if (!hsa.empty()) ::setenv("IREE_HAL_AMDGPU_LIBHSA_PATH", hsa.c_str(), 1);
}

Then make owned in print_backend true whenever the user's value was rejected (or always, since the winner already accounts for a valid user value). With that, the env each child inherits always equals the file the log names.

Smaller notes, not blocking:

  • is_regular_file follows symlinks, which is right here: TheRock's libhsa-runtime64.so.1 is usually a symlink.
  • The .. check runs on the path as written, before normalising, which is also right.

Context for why this path matters: the Loom route (loom_launch_for) depends on it. Today on the appliance, loom-serve only finds a device with IREE_HAL_AMDGPU_LIBHSA_PATH=/opt/rocm-therock/lib/libhsa-runtime64.so.1 (engine#3158 rows I ran today).

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Review from cl-i679: one defect in the refused-value path.

What happens: serve refuses a user-set IREE_HAL_AMDGPU_LIBHSA_PATH that fails the check (relative, contains .., another file, a missing file). It prints "is not used" and announces the runtime it falls back to: the build's copy or the /opt/rocm-therock scan. The backend, however, still receives the refused value.

  • merge_env (app/serve.cpp:488) starts from serve's own environment, which holds the user's value. It adds an env_extra variable only when that key is absent, so the fallback path is dropped.
  • The setenv added in run_serve runs only when --hrx-libhsa is set.
  • print_backend keeps the user's value too, because owned is false without --hrx-libhsa.
  • Result: the runtime line names a file the backend never opens, and HRX in the child dlopens the refused path. That is the engine#3314 symptom this PR fixes for --hrx-libhsa.

Why the tests miss it: the four refused shapes in tests/hrx_runtime_route.sh check only the log lines, not the backend env.

Suggested fix: when user_libhsa() refuses a set value, overwrite it in serve's environment with the runtime serve chose, the same setenv path as --hrx-libhsa, or unset it. Then, for each refused shape, add the row env_is "$scratch/bad.json" '{"libhsa": "<the fallback>"}'.

The rest looks right to me:

  • --hrx-libhsa beats the user's value in the child and in the --print-backend-env plan.
  • The .. check runs on the path as written, before lexically_normal removes it.
  • The accepted-value path, where the backend and the runtime line agree, is covered.

I have not checked why the reviewer check is red.

🤖 Generated with Claude Code

bong-water-water-bong added a commit that referenced this pull request Nov 22, 2026
…end either (follow-up to #3323, engine#3314) (#3326)

#3323 made serve refuse a user value that is not the absolute path of a libhsa-runtime64.so* file
and log "is not used", then name the runtime it fell back to. The value was still inherited by
every child: serve passes its choice as merge_env's env_extra, which an inherited variable beats,
so the backend opened the rejected file while the log named another. --print-backend-env had the
same hole (it prints a variable only when serve's environment does not have it).

serve now drops a rejected value from its own environment at startup, in every mode, so the
launch paths' value is the one a backend gets and the plan prints it. tests/hrx_runtime_route.sh:
for each rejected value the backend gets the runtime the log names (fails on main, 4 rows), and
the --print-backend-env plan under a rejected value equals the plan with no user value.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

serve: a user-set IREE_HAL_AMDGPU_LIBHSA_PATH is ignored; docs/serve.md says serve keeps it

1 participant