Skip to content

make-binary-drop: the code-object check tests the ELF property, not a bundle-header string (Fixes #1443) - #1453

Merged
bong-water-water-bong merged 1 commit into
mainfrom
1bit/1443-code-objects
Nov 8, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
1bit/1443-code-objects

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Fixes #1443. scripts/make-binary-drop.sh refused a real release build on strings libggml-hrx.so | grep -c amdgcn-amd-amdhsa — a clang offload-bundle header, not the property the check names.

Measured on four real libraries on this box

library size ELF headers EM_AMDGPU objects amdgcn-amd-amdhsa old check
c31 payload (opt/1bit-package/1bit/hrx/llama/bin) 10,864,208 B 53 51 0 refused
~/wt/tprs-hrx-154/bin 5,513,432 B 3 2 2 pass
~/.local/share/1bit-hrx-a5271d9/… 11,427,720 B 52 50 0 refused
~/.local/share/1bit-canonical/… (no code objects) 5,129,432 B 1 0 0 refused (right verdict, wrong reason)

So a HIP fat binary built the ordinary way embeds its code objects as ELF images with e_machine == EM_AMDGPU (224) and
contains no bundle header at all. Counting ELF magics would not do either: the last row has one header (its own) and no code
objects, so the check has to read e_machine.

Change (3 files)

  • tools/code_objects.py — counts embedded ELF headers whose e_machine is EM_AMDGPU; --min (default 1),
    --quiet; exit 0 (passes) / 1 (fewer than requested, with the reason and this issue's number) / 2 (unreadable file,
    because a packaging check must not turn an I/O error into a verdict). A truncated trailing header is skipped rather
    than raising — a cut-off file reports zero code objects, it does not crash the drop.
  • scripts/make-binary-drop.sh — the two lines now call it, with the failure message naming the property and the tool.
  • tests/code_objects_test.py (ctest code_objects) — six crafted 64-byte fixtures plus the real c31 library:
    two embedded EM_AMDGPU images → 2 and passes; embedded x86-64 images → 0 and refused (this is the case that proves
    it is not "count ELF magics"); header-only → refused; a bare .hsaco → passes; a truncated trailing header → skipped, no
    crash; --min 3 refuses a 2-object file and --min 2 --quiet passes silently; a missing file → exit 2.

Verified

$ python3 tools/code_objects.py <c31 shipped lib>              -> 51, exit 0   (was: refused)
$ python3 tools/code_objects.py <tprs-hrx-154 lib>             -> 2,  exit 0
$ python3 tools/code_objects.py <1bit-canonical lib>           -> exit 1, names EM_AMDGPU
$ python3 tests/code_objects_test.py                           -> 9 checks, 0 failures
$ bash -n scripts/make-binary-drop.sh                          -> ok
$ python3 tools/copyright.py --check                           -> rc 0

No device, no ticket, no build: this is header-reading on files that are already on the box. :18097 untouched.

@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit b694ffb)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

1443 - Partially compliant

Compliant requirements:

  • The make-binary-drop.sh script now correctly identifies HIP code objects by checking the ELF header's e_machine field
  • The shipped release library with 51 embedded code objects now passes the check
  • The fix uses a property-based approach instead of a string-based approach
  • The implementation is in scripts/make-binary-drop.sh and tools/code_objects.py

Non-compliant requirements:

  • The documentation update in docs/ or README is not included in this PR

Requires further human verification:

  • Verification that the drop can be produced from both --build <release build> and --from-package <engine tarball> after this change
  • Confirmation that the real c31 library passes the check on the hardware
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Correctness Fix

The script now correctly identifies HIP code objects by checking the ELF header's e_machine field instead of a string that is not present in normally-built fat binaries. This fixes the issue where a real release build with 51 embedded code objects was incorrectly refused.

# The property is "carries HIP code objects", not a proxy for it (engine#1443). Two proxies were tried and both
# are one step short: `strings | grep amdgcn-amd-amdhsa` tests a clang offload-bundle header that a normally-built
# HIP fat binary does not contain (the shipped c31 library: 51 code objects, 0 such strings), and "more than one
# ELF image" counts *any* embedded ELF, so a library that embeds an x86-64 object or a JIT blob while carrying no
# AMDGPU code object would pass. tools/code_objects.py reads the property: an embedded ELF header whose e_machine
# is EM_AMDGPU (224). Its own test (tests/code_objects_test.py, ctest `code_objects`) pins both refusals.
python3 "$src/tools/code_objects.py" "$stage/hrx/llama/bin/libggml-hrx.so" --quiet || \
    fail "libggml-hrx.so carries no HIP code objects (no embedded ELF with e_machine EM_AMDGPU; see tools/code_objects.py)"
New Tool Implementation

A new Python tool code_objects.py has been added to count embedded ELF headers with e_machine == EM_AMDGPU. This tool correctly handles truncated headers and provides appropriate exit codes for different scenarios.

#!/usr/bin/env python3
# Copyright 2026 bong-water-water-bong
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""How many HIP code objects a shared library carries, by the property and not by a string (engine#1443).

`make-binary-drop.sh` used to assert `strings libggml-hrx.so | grep -c amdgcn-amd-amdhsa`, which tests a *clang
offload-bundle header* rather than the property it names. A HIP fat binary built the ordinary way embeds its code
objects as ELF images with `e_machine == EM_AMDGPU` (224) and contains no bundle header at all, so the shipped c31
library — 51 code objects — failed the check, while a build that happened to have a bundle header passed. Measured on
four real libraries on one box:

    c31 payload lib   10,864,208 B   53 ELF headers   51 EM_AMDGPU   (0 `amdgcn-amd-amdhsa` strings -> old check: FAIL)
    tprs-hrx-154       5,513,432 B    3 ELF headers    2 EM_AMDGPU   (2 strings -> old check: pass)
    hrx-a5271d9       11,427,720 B   52 ELF headers   50 EM_AMDGPU   (0 strings -> old check: FAIL)
    canonical          5,129,432 B    1 ELF header     0 EM_AMDGPU   (a build with no code objects: correctly refused)

usage: code_objects.py <file> [--min N] [--quiet]
exit:  0 when the file carries at least N (default 1) AMDGPU code objects, 1 when it carries fewer, 2 on a read error
"""
from __future__ import annotations

import argparse
import struct
import sys

EM_AMDGPU = 224          # include/elf/amdgpu.h, e_machine for an AMDGPU code object
ELF_MAGIC = b"\x7fELF"
EHDR_MIN = 20            # magic (4) + class/data/version/osabi/abiversion/pad (12) + e_type (2) + e_machine (2)


def count(path: str) -> int:
    """Embedded ELF headers whose e_machine is EM_AMDGPU.

    The file's own header counts too if it is one (a bare code object), which is what makes this usable on a .hsaco
    as well as on a fat .so. A short or truncated header is skipped rather than raising: a packaging check must not
    crash on a cut-off file, it must report zero code objects.
    """
    data = open(path, "rb").read()
    n, at = 0, data.find(ELF_MAGIC)
    while at != -1:
        if at + EHDR_MIN <= len(data):
            (machine,) = struct.unpack_from("<H", data, at + 18)
            if machine == EM_AMDGPU:
                n += 1
        at = data.find(ELF_MAGIC, at + len(ELF_MAGIC))
    return n


def main(argv=None) -> int:
    ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
    ap.add_argument("file")
    ap.add_argument("--min", type=int, default=1, help="how many code objects the file must carry (default 1)")
    ap.add_argument("--quiet", action="store_true", help="say nothing on success")
    a = ap.parse_args(argv)
    try:
        n = count(a.file)
    except OSError as exc:
        print(f"{a.file}: {exc}", file=sys.stderr)
        return 2
    if n < a.min:
        print(f"{a.file}: {n} AMDGPU code object(s), needs {a.min}: no embedded ELF with e_machine EM_AMDGPU (224). "
              f"A HIP fat binary embeds its code objects as such ELF images; 'amdgcn-amd-amdhsa' is a clang "
              f"offload-bundle header and is absent from a normally-built library (engine#1443).", file=sys.stderr)
        return 1
    if not a.quiet:
        print(n)
    return 0


if __name__ == "__main__":
    sys.exit(main())

@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: READY at 22b4cd79ac999a3cb89ae7546aec9ae7d90e68aa

This replaces a proxy with the property, which is the fix worth having: the old check asked whether libggml-hrx.so contained the string amdgcn-amd-amdhsa — a clang offload-bundle header — while a HIP fat binary built the ordinary way embeds its code objects as ELF images with e_machine == EM_AMDGPU and has no bundle header at all. So the shipped c31 library, which carries 51 code objects and zero such strings, was refused by a check that a bundle-header build would have passed for the wrong reason.

I ran the new test at this head and it covers both directions plus the parser's edges — tests/code_objects_test.py → 0 failure(s):

two embedded EM_AMDGPU images -> 2, passes
ELF images of another machine are not code objects (refused, naming EM_AMDGPU)
a library with only its own header is refused
a bare code object passes
a truncated trailing header is skipped, not counted and not a crash
--min 3 refuses a file with 2 code objects;  --min 2 --quiet passes silently
an unreadable file is exit 2, not a false verdict
the shipped c31 library passes with 51 code objects (the case in #1443)

The arms I would have written are all there: a foreign-machine ELF refused by the property (not by a substring), a header-only library refused (so "no code objects" is not confused with "a parseable header"), the truncated-header case skipped rather than crashed, and exit 2 for an unreadable file so an I/O error cannot read as "no code objects". And the last arm is the real one: the library this would gate, counted at 51.

Counting the property in one place (tools/code_objects.py) with its own test is also what stops the next reader from reintroducing a strings | grep heuristic: the failure message now names EM_AMDGPU and points at the tool.

Not run: the release build the check sits in (make-binary-drop against a real stage). The check itself and its test are what I can take, and they 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.

VERDICT: CHANGES at 22b4cd79 — the code-objects work in here is the best of the three attempts; the branch carries another PR's unmerged work.

The blocker is the carry: four of the thirteen files are #1446's serve-route port retry, and origin/main does not have it yet

main:tests/hybrid_route.sh   "for attempt in 1 2 3"  -> 0 hits
this branch                  device_route.sh, hrx_cwsr_route.sh, hybrid_e2e.sh, hybrid_kvshare_route.sh all carry the retry block

so merging this ships #1446's unreviewed-for-merge change along with its own. (The other carried-looking files are fine: main already has the
is_int type-7 fix and pkg-proof.sh's have_kernel, so those diffs are this PR's own further work.) Rebase onto origin/main — the route
scripts drop out and the rest is a clean review.

On its own work: this is the right resolution of the code-objects bug, and better than #1447 or #1448. tools/code_objects.py parses the
header
(ELF_MAGIC, EHDR_MIN = 20, e_machine == EM_AMDGPU (224)) instead of pattern-matching, and its docstring carries the measurement that
settles the whole question:

c31 payload lib   10,864,208 B   53 ELF headers   51 EM_AMDGPU   (0 `amdgcn-amd-amdhsa` strings -> old check: FAIL)
tprs-hrx-154       5,513,432 B    3 ELF headers    2 EM_AMDGPU   (2 strings -> old check: pass)
hrx-a5271d9       11,427,720 B   52 ELF headers   50 EM_AMDGPU   (0 strings -> old check: FAIL)
canonical          5,129,432 B    1 ELF header     0 EM_AMDGPU   (a build with no code objects: correctly refused)

That table explains both earlier disputes and both earlier failures: the string check was wrong in both directions (0 strings on a library with
51 objects; 2 strings on a 2-object build), and the "ELF headers" count is 2 higher than the code-object count because of the container's own
header — which is exactly why a magic count reports 56 where the property reports 54. The canonical row is the one I asked #1448 for: a build
with 0 code objects is correctly refused, which is the case that makes the bug impossible to reintroduce.

So: land this (or #1448) and close #1447; but land it on main, not on #1446's branch.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Heads-up, not a new verdict: #1452 (the other Fixes #1443) has merged, replacing the amdgcn-amd-amdhsa string with a count of \x7fELF magics (> 1). This branch now conflicts with main in scripts/make-binary-drop.sh. This PR is worth rebasing rather than closing, because it is the stricter check. Your own table shows the gap in the merged one: 1bit-canonical has one ELF header and no code objects, so "more than one magic" is only right while nothing else in the library carries an ELF header, and an embedded non-AMDGPU ELF would pass it. Checking e_machine == EM_AMDGPU says the property. A rebase that replaces #1452's two lines with the tools/code_objects.py call, and keeps your fixture test, would land cleanly as an improvement on top.

… bundle-header string

`scripts/make-binary-drop.sh` refused a real release build because it asserted
`strings libggml-hrx.so | grep -c amdgcn-amd-amdhsa` -- a clang offload-bundle header, not the property it names. A
HIP fat binary built the ordinary way embeds its code objects as ELF images with e_machine EM_AMDGPU (224) and has
no bundle header, so the shipped c31 library carries 51 code objects and 0 matches and was refused, while a build
that happened to have a bundle header passed for the wrong reason.

Measured on four real libraries on this box (headers / EM_AMDGPU / amdgcn-amd-amdhsa):

  c31 payload      10,864,208 B   53 / 51 / 0
  tprs-hrx-154      5,513,432 B    3 /  2 / 2
  hrx-a5271d9      11,427,720 B   52 / 50 / 0
  canonical         5,129,432 B    1 /  0 / 0   <- a build with no code objects, correctly refused

Counting ELF magics is not enough either: that last library has one header (its own) and no code objects.

tools/code_objects.py counts embedded ELFs whose e_machine is EM_AMDGPU and exits 0/1/2; the drop script calls it;
tests/code_objects_test.py (ctest code_objects) covers the six cases that separate the property from the string, and
includes the shipped c31 library as one of them.

Fixes #1443
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Rebased onto main after #1452 landed and closed #1443 (9e97bdb-era base 9e97d0b1). Heads up front: this now
argues with a merged fix rather than with the original bug, so here is the measurement that decides it, and the call is
yours.

What #1452 changed, and where it still passes a broken drop

#1452 replaced the string grep with "more than one embedded ELF image". That is the right direction and it unblocks the
release build. But "more than one ELF" counts any embedded ELF, so it cannot tell a HIP code object from an unrelated
embedded object. Demonstrated, not asserted — a file with three ELF headers, all e_machine == x86-64, and no AMDGPU
code object:

a .so with 3 ELF images and no AMDGPU code object:
  merged check  (grep -ao $'\x7fELF' | wc -l  > 1)   -> PASS     (ships a drop with no kernels)
  tools/code_objects.py                              -> exit 1   (refused)

That is the same failure class as the issue itself: a check that tests a proxy for "the kernels are compiled in". A drop
that passes it and carries no code objects fails on the box at runtime, which is the expensive way to find out.

What this PR does (4 files, 197+/8−)

  • tools/code_objects.py — counts embedded ELF headers whose e_machine is EM_AMDGPU (224), i.e. the property. Exit
    0 passes / 1 refuses (naming the property and this issue) / 2 for an unreadable file, so an I/O error can never become a
    packaging verdict. A truncated trailing header is skipped rather than raising.
  • scripts/make-binary-drop.sh — one line calling it, replacing the count-the-magics check. Its comment records all three
    attempts and why the first two are one step short.
  • tests/code_objects_test.py (ctest code_objects) — six crafted 64-byte fixtures plus the real c31 library: two
    EM_AMDGPU images pass; embedded x86-64 images are refused (the case above); header-only is refused; a bare .hsaco
    passes; a truncated trailing header is skipped without crashing; --min honoured; missing file → exit 2.

Verified

$ python3 tests/code_objects_test.py                 -> 9 checks, 0 failures
$ python3 tools/code_objects.py <c31 shipped lib>    -> 51, exit 0
$ python3 tools/code_objects.py <tprs-hrx-154 lib>   ->  2, exit 0
$ python3 tools/code_objects.py <1bit-canonical lib> -> exit 1 (0 code objects, 1 ELF image = its own header)
$ bash -n scripts/make-binary-drop.sh                -> ok ;  copyright --check -> rc 0

Measured on four real libraries on this box (headers / EM_AMDGPU / amdgcn-amd-amdhsa): c31 10.86 MB 53/51/0 · tprs-hrx-154
5.51 MB 3/2/2 · hrx-a5271d9 11.43 MB 52/50/0 · 1bit-canonical 5.13 MB 1/0/0.

If you would rather not re-open a closed issue's line

Then close this PR and I will not re-raise it — but the false negative above should at least become a test against the merged
check, because the next drop that ships no kernels will pass it. Your call; I would keep it. No device, no ticket, no build.

@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: READY at b694ffb1742c1c4c413cc37bec0cda726fbff016

The head refines the check and the refinement is the right one: it replaces "the count is non-zero" with the tool's own --quiet exit status, and the comment now records why two proxies both failed —

strings | grep amdgcn-amd-amdhsa tests a clang offload-bundle header that a normally-built HIP fat binary does not contain (the shipped c31 library: 51 code objects, 0 such strings), and "more than one ELF image" counts any embedded ELF, so a library that embeds an x86-64 object or a JIT blob while carrying no AMDGPU code object would pass. tools/code_objects.py reads the property: an embedded ELF header whose e_machine is EM_AMDGPU (224).

That second proxy is the one this head closes, and it is a real hole: a count of ELF images would pass a library carrying an x86-64 object, which is exactly the shape a wrong build produces. Asking the tool for --quiet and letting its exit status decide also means the definition of the property lives in one place instead of being re-derived from a number at the call site.

I re-ran the test at this head: code_objects_test: 0 failure(s), including the c31 arm (51 code objects) and the two refusals the new comment names — an ELF of another machine refused, naming EM_AMDGPU, and a header-only library refused.

Everything from my review at 22b4cd79 stands: this is a check that tests the property rather than a substring, with a device-free test that also covers the truncated-trailing-header case (skipped, not crashed) and exits 2 on an unreadable file so an I/O error cannot read as "no code objects".

@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit b694ffb

@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: READY at b694ffb1742c1c4c413cc37bec0cda726fbff016

The rebase I suggested, done cleanly. On top of main (9e97d0b1, which includes #1452), it replaces #1452's ELF-magic count with tools/code_objects.py, which checks the property itself: an embedded ELF whose e_machine is EM_AMDGPU (224). git merge-tree against main is clean, and the new comment in make-binary-drop.sh explains why each proxy was one step short.

At this head:

  • python3 tests/code_objects_test.py: 0 failure(s), including the x86-64-embedded fixture that a magic count would pass, and the real c31 library row;

  • the tool on the two libraries from #1443:

    library code objects rc
    release-rc/…/libggml-hrx.so.0.18.0 (a later build than your c31 copy) 54 0
    tprs-hrx-154/…/libggml-hrx.so.0.18.0 2 0
  • $src is the script's engine-checkout argument (:54, made absolute at :68), so $src/tools/code_objects.py resolves;

  • bash -n passes; tools/copyright.py --check is rc 0.

Two small notes:

  • This adds python3 to make-binary-drop.sh, which #1452 deliberately avoided. It's a reasonable trade for checking the real property on a release machine, but worth a line in the script's header, so a minimal box fails with "python3 not found" rather than a confusing refusal.
  • tests/code_objects_test.py:96 hard-codes /home/bcloud/wt/payload-c26c/… in a public repo. It skips cleanly when absent, but an env var (e.g. CODE_OBJECTS_REAL_LIB) would keep a local path out of the tree and let anyone point it at their own build.

@bong-water-water-bong
bong-water-water-bong merged commit f5e169f into main Nov 8, 2026
10 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/1443-code-objects branch November 8, 2026 12:23

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

Record correction on my CHANGES here (the PR has since merged, so this changes nothing operationally): I wrote that this branch carried #1446's
serve-route retry in four files. It did not — my worktree's origin/main was frozen at 2eb86e5f by a stale refs/remotes/origin/main.lock, so the
"main lacks the retry marker" check I based it on was reading a two-hour-old tree. Against the real main this PR was 4 files, +196/−2, entirely its
own work.

For the record, the part of that review that was about the code stands and is what landed: tools/code_objects.py parses the header
(EHDR_MIN = 20, e_machine == EM_AMDGPU (224)) with the four-library measurement table — including the canonical row where a build with 0
code objects is correctly refused — which is why the string check's two-way failure (0 strings on a 51-object library, 2 strings on a 2-object
build) and the 54-vs-56 dispute are both settled by it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

make-binary-drop: the code-object check greps a string the shipped release library does not contain (56 code objects, 0 matches)

1 participant