nki-kernel-bench / tools /scrub_paths.py
jburtoft's picture
nki-kernel-bench v0.1
0fef070
Raw History Blame Contribute Delete
3.47 kB
"""Scrub build-host paths from shipped binary/text artifacts (NEFF payloads, compile logs).
NEFF = 1024-byte header + tar(.gz) payload. Paths like /home/ubuntu/nkb/dataset/... are rewritten
to /nkb/dataset/... inside every payload member; the header data_size (offset 16) and payload hash
(offset 172; md5 when pkg_version == 2, else sha256) are rewritten so the runtime accepts it. Compile logs (*.log-neuron-cc.txt) are rewritten in place.
python tools/scrub_paths.py dataset [--check]
"""
import hashlib, io, os, re, struct, sys, tarfile
PAT = re.compile(rb"/(?:home|local/home|Users)/[^\s\x00\"']*?/(?:nkb|nki_kernel_bench)/") # any build-host checkout path
def REPL(m):
# SAME LENGTH: NEFF debug-info members are binary with length-prefixed strings
return b"/" * (len(m.group(0)) - 4) + b"nkb/"
TXT = re.compile(r"/(?:home|local/home|Users)/[^\s\"']*?/(?:nkb|nki_kernel_bench)/")
def scrub_neff(p, check):
raw = open(p, "rb").read()
head, payload = raw[:1024], raw[1024:]
if not PAT.search(payload) and b"\x1f\x8b" != payload[:2]:
return False
gz = payload[:2] == b"\x1f\x8b"
src = tarfile.open(fileobj=io.BytesIO(payload), mode="r:gz" if gz else "r:")
members = [(m, src.extractfile(m).read() if m.isfile() else None) for m in src.getmembers()]
if not any(d is not None and PAT.search(d) for _, d in members):
return False
if check:
return True
out = io.BytesIO()
with tarfile.open(fileobj=out, mode="w:gz" if gz else "w") as dst:
for m, d in members:
if d is not None:
d = PAT.sub(REPL, d); m.size = len(d)
dst.addfile(m, io.BytesIO(d))
else:
dst.addfile(m)
import hashlib, struct
new = out.getvalue()
pkg = struct.unpack("<Q", head[:8])[0]
digest = hashlib.md5(new).digest() if pkg == 2 else hashlib.sha256(new).digest()
h = bytearray(head)
h[16:24] = struct.pack("<Q", len(new)) # data_size
h[172:204] = digest.ljust(32, b"\0") # payload hash, validated at load
open(p, "wb").write(bytes(h) + new)
return True
IID = re.compile(rb"i-0(?!0{16})[0-9a-f]{16}")
def scrub_ntff(p, check):
"""NTFF: protobuf-framed; the build-host NEFF path and the instance id are rewritten in place,
same length (length prefixes stay valid)."""
b = open(p, "rb").read()
if not (PAT.search(b) or IID.search(b)):
return False
if check:
return True
b = PAT.sub(REPL, b)
b = IID.sub(lambda m: b"i-" + b"0" * (len(m.group(0)) - 2), b)
open(p, "wb").write(b)
return True
def main(root, check):
n = {"neff": 0, "ntff": 0, "log": 0, "json": 0}
for dp, _, fs in os.walk(root):
for f in fs:
p = os.path.join(dp, f)
if f.endswith(".neff"):
n["neff"] += scrub_neff(p, check)
elif f.endswith(".ntff"):
n["ntff"] += scrub_ntff(p, check)
elif f.endswith((".log-neuron-cc.txt", ".log", ".json")) and "/results/" in p:
t = open(p, errors="ignore").read()
if TXT.search(t):
n["log" if not f.endswith(".json") else "json"] += 1
if not check:
open(p, "w").write(TXT.sub("/nkb/", t))
print(("would scrub " if check else "scrubbed ") + str(n))
if __name__ == "__main__":
main(sys.argv[1], "--check" in sys.argv)