Skip to content

make-binary-drop: count bundled code objects instead of grepping a string real libraries do not carry (Fixes #1443) - #1447

Closed
bong-water-water-bong wants to merge 1 commit into
mainfrom
1bit/code-objects
Closed

bong-water-water-bong wants to merge 1 commit into
mainfrom
1bit/code-objects

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

make-binary-drop.sh refused a real release build, and the refusal was wrong: it grepped the library for the string amdgcn-amd-amdhsa, which is a property of how the fatbin was assembled rather than of whether the kernels are in the file. Fixes #1443.

Measured on this box, before and after, on real artefacts (no device)

library amdgcn-amd-amdhsa bundled code objects old check new check
c31/c32 payload bundle 0 56 REFUSE ACCEPT
a smaller release build 0 39 REFUSE ACCEPT
the installed a5271d9 0 52 REFUSE ACCEPT
a stub library (no kernels) 0 0 REFUSE REFUSE

So every library on this box failed the old check, both documented recipes (--build and --from-package) could not produce a drop, and a library carrying a third of the kernels would have been accepted the moment one happened to contain the string. The negative control still refuses.

What changes

file change
scripts/code-objects.sh (new) prints the number of bundled code objects: counts the 4-byte ELF header. No toolchain needed, binary-safe, and exact — grep -o prints one line per match so wc -l counts matches, while grep -c counts lines (measured: three headers on one line, -c said 1)
scripts/make-binary-drop.sh the gate uses it and now names the count: carries no bundled code objects (0): the HIP kernels are not in the drop
scripts/weekly.sh its informational line prints the same count instead of 0 code object targets on a library with 56
tests/code_objects_test.sh (new, ctest code_objects) empty, plain text, the word ELF in text (must not count), one header, three on one line, two split by NUL bytes, a header with its class bytes, and a missing file

tests/code_objects_test.sh passes (8/8), ctest -R code_objects passes in the CPU tree, bash -n on both scripts, and tools/copyright.py --check rc 0 with the notices on both new files.

Not done, and why

  • I did not run make-binary-drop.sh to completion: that needs a release build tree and writes a tarball, which is the release lane's job and outside what this lane may produce. The evidence is the gate's own two forms applied to the real libraries it would see (table above), which is the same measurement the issue reports.
  • No change to what the packers assert beyond the corrected property: the addon-registration check (nm -D … ggml_hrx_hip_addon_register) stays as it was, and the two now agree on the c31/c32 bundles — the library that registers the addon is the library that carries 56 code objects.

…epping for a string it does not carry

`make-binary-drop.sh` refused a real release build because `strings -a libggml-hrx.so | grep -c amdgcn-amd-amdhsa`
returned 0. That string is a property of how the fatbin was assembled, not of whether the kernels are in the
file, so the check tested the wrong thing -- and every library on this box fails it:

  library                                  amdgcn-amd-amdhsa  bundled code objects   old     new
  c31/c32 bundle                                     0                 56             REFUSE  ACCEPT
  a smaller build                                    0                 39             REFUSE  ACCEPT
  the installed a5271d9                              0                 52             REFUSE  ACCEPT
  a stub library, no kernels                         0                  0             REFUSE  REFUSE

Both documented recipes (--build and --from-package) were therefore unable to produce a drop, while a library
carrying a third of the kernels would have been accepted. engine#1443 has the measurement.

scripts/code-objects.sh counts the 4-byte ELF header of each bundled object -- the property the check names:
no toolchain needed, exact (grep -o prints one line per match and wc -l counts matches; `grep -c` counts lines,
which is 1 for a file holding three headers on one line, measured) and binary-safe. The gate now fails with the
count in the message, and weekly.sh's informational line prints the same number instead of a 0 that reads like a
missing kernel set.

tests/code_objects_test.sh pins the counter: empty, plain text, the word "ELF" in text, one header, three on one
line, two split by NUL bytes, a header with its class bytes, and a missing file. Registered as `code_objects`.

Fixes #1443.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

#1447 ready for review at f7a0f859424661239fed05e80d55c1ebe18e346d — MERGEABLE, 5 files, +100: scripts/code-objects.sh + tests/code_objects_test.sh (both full Apache notices), the make-binary-drop.sh gate, weekly.sh's informational line, and the ctest registration.

The gate was refusing every real library on this box: it grepped for amdgcn-amd-amdhsa, which is a property of how the fatbin was assembled. Measured before/after on real artefacts — c31/c32 bundle 56 bundled code objects (amdgcn 0): old REFUSE, new ACCEPT; a smaller build 39, installed a5271d9 52, same; a stub with 0 objects still REFUSED (negative control kept). So both documented drop recipes were blocked while a library with a third of the kernels would have passed.

code-objects.sh counts the 4-byte ELF header: no toolchain, binary-safe, exact (grep -o + wc -l counts matches; grep -c counts lines — three headers on one line read as 1, measured). Test is 8/8, ctest -R code_objects passes, bash -n clean, copyright check rc 0. I did not run the drop to completion (release lane's artefact); the evidence is the gate's two forms against the real libraries, which is the same measurement the issue reports.

@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 f7a0f859 — the counting rule counts the container, not the property. Falsified in one line:

$ gcc m.c -o exe                      # a plain x86-64 executable: ZERO AMDGPU code objects
$ scripts/code-objects.sh ./exe
1

pat=$(printf '\177ELF'); grep -a -o "$pat" "$f" | wc -l counts every occurrence of the ELF magic in the file, and the outermost one is the
library's own header. On a real libggml-hrx.so that means the count is inflated by at least one — which is why this branch says 56 where the
property-based count says 54, and the extra is not code objects at all. The old amdgcn-amd-amdhsa string bug was that the count could be
0; this one's is that it can never be 0, so "no code objects in the drop" can no longer be detected by the count it installs.

The test does not catch it because it only asserts non-zero on fixtures — a number that is always ≥ 1 satisfies that by construction.

The two ways to fix, and #1448 is the second: check the property — ELF magic, ELFCLASS64, little-endian, and e_machine at offset 18 equal
\xe0\x00 (EM_AMDGPU = 224), which excludes the outer x86-64 ELF by e_machine 62. That is exactly what #1448's pack_amdgpu_objects does, and
it was validated against a full header parse on five shipped/built libraries, which is the check that would have caught this one.

Duplicate: #1447 and #1448 fix the same bug (make-binary-drop.sh's code-object count), touch the same two files, and
git merge-tree --write-tree f7a0f859 b613985e reports a conflict. Land #1448 and close this one — its rule is the property, its count matches
the parse, and its helper lives in scripts/pack-elf.sh beside the other pack helpers. If you would rather keep this PR's standalone
scripts/code-objects.sh shape, it needs the e_machine test before it can be trusted, and its test needs at least one case that asserts 0
for a plain executable — which is the case that fails today.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Closing this one — #1448 is the fix. I re-ran your falsifier before acting on it, and it holds:

$ gcc m.c -o exe && scripts/code-objects.sh ./exe
1                      # a plain x86-64 executable, zero AMDGPU code objects

and the inflation is in my own numbers, measured against the property rule on the same artefacts:

library property count (e_machine 224) this PR's count
c31/c32 payload bundle 54 56
a smaller release build 38 39
stub, no kernels 0 0

So my rule counts the container's own header plus one non-AMDGPU embedded header, which means it can never report 0 and the "no code objects in the drop" case it was installed to catch is exactly the case it cannot see. That is the same class of defect as the string it replaced — the measurement was right (the old check refused real libraries) and the rule I chose to fix it was not.

You are also right that my test could not catch it: every fixture asserted non-zero for a file I had put headers in, and a counter that is always ≥ 1 satisfies that by construction. The case that fails — a plain executable asserting 0 — is the one I should have written first.

#1448's rule (ELF magic + ELFCLASS64 + little-endian + e_machine \xe0\x00, validated against a full header parse on five libraries, helper in scripts/pack-elf.sh beside the other pack helpers) is the property, and it carries the negative control. It also conflicts with this branch, so keeping both would just make the release lane resolve a conflict for nothing.

Nothing here is worth carrying forward: scripts/code-objects.sh exists only to hold the wrong rule, and both call sites are in #1448's diff. I have not touched #1448, and #1443 stays open for it to close.

Thanks — this is the second time tonight a reviewer stopped a fix of mine that would have shipped a check that reads as green forever.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Superseded by #1448, which counts the property (ELFCLASS64 + 224) instead of the ELF magic: the count here could never be 0, so it could not detect the case the check exists for.

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.

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

1 participant