Skip to content

Commit 522b861

Browse files
committed
[BPF] fix print_btf.py test script on bigendian machines
Make print_btf.py correctly detect endianness of the BTF input. Input endianness is inferred from BTF magic word [2], which is a 2-byte integer at offset 0 of the input: - sequence `EB 9F` signals big-endian input; - sequence `9F EB` signals little-endian input. Before this commit the magic sequence was read using "H" format for `unpack` method of python's `struct` module: - if magic is `0xEB9F` assume little-endian; - if magic is `0x9FEB` assume big-endian. However, format `H` reads data in native endianness. Thus the above logic would only be correct on little endian hosts: - byte sequence `9F EB` read as `0xEB9F` -> little-endian input; - byte sequence `EB 9F` read as `0x9FEB` -> big-endian input. On the big-endian host the relation should be inverse. Fix this by always reading magic in big-endian (format `>H`). This fixes CI error reported for a BPF test using print_btf.py script in [1]. The error happens on a s390 host, which is big-endian. [1] https://lab.llvm.org/buildbot/#/builders/42/builds/1192 [2] https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-type-and-string-encoding
1 parent 7883b02 commit 522b861

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

llvm/test/CodeGen/BPF/BTF/print_btf.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def print_btf(filename):
8888
buf = file.read()
8989

9090
fmt_cache = {}
91-
endian_pfx = ""
91+
endian_pfx = ">" # big endian
9292
off = 0
9393

9494
def unpack(fmt):
@@ -104,9 +104,9 @@ def unpack(fmt):
104104
# Use magic number at the header start to determine endianness
105105
(magic,) = unpack("H")
106106
if magic == 0xEB9F:
107-
endian_pfx = "<"
107+
endian_pfx = ">" # big endian
108108
elif magic == 0x9FEB:
109-
endian_pfx = ">"
109+
endian_pfx = "<" # little endian
110110
else:
111111
warn(f"Unexpected BTF magic: {magic:02x}")
112112
return
@@ -290,6 +290,6 @@ def warn_nonzero(val, name):
290290

291291
if __name__ == "__main__":
292292
if len(sys.argv) != 2:
293-
warn("Usage: {sys.argv[0]} <btf_file>")
293+
warn(f"Usage: {sys.argv[0]} <btf_file>")
294294
sys.exit(1)
295295
print_btf(sys.argv[1])

0 commit comments

Comments
 (0)