diff options
| author | Narek Shaghoyan <narekshagoyan7@gmail.com> | 2026-11-24 14:53:03 +0000 |
|---|---|---|
| committer | Narek Shaghoyan <narekshagoyan7@gmail.com> | 2026-11-24 14:53:03 +0000 |
| commit | 1ebe352f11d96bdd86a3dbd68baeeb8dcd70b002 (patch) | |
| tree | 0e56077323f7397d8b51e67401fa4c6191659cc5 | |
| parent | 8f05e09e224656740e8a53f5047e1338d7a52588 (diff) | |
script_qtable: sort _BinFolderSource hex offsets numerically, skip .bin subdirs, and sort type_query histograms numerically
| -rw-r--r-- | tests/test_script_qtable.py | 45 | ||||
| -rw-r--r-- | tools/script_qtable.py | 32 |
2 files changed, 68 insertions, 9 deletions
diff --git a/tests/test_script_qtable.py b/tests/test_script_qtable.py index a9176d889..c405620c7 100644 --- a/tests/test_script_qtable.py +++ b/tests/test_script_qtable.py | |||
| @@ -333,6 +333,51 @@ class ScriptQtableTests(unittest.TestCase): | |||
| 333 | self.assertFalse(rep["valid"]) | 333 | self.assertFalse(rep["valid"]) |
| 334 | self.assertEqual(rep["cross_tree_edges"], n - 1) | 334 | self.assertEqual(rep["cross_tree_edges"], n - 1) |
| 335 | 335 | ||
| 336 | def test_bin_folder_sorts_hex_offsets_numerically_and_skips_bin_subdirs(self): | ||
| 337 | # Build a 2-chunk qtable (> 64 KiB) split across unpadded hex names 90000.bin and 100000.bin. | ||
| 338 | # Lexicographic sort puts 100000.bin before 90000.bin, and a subdir ending in .bin used to crash. | ||
| 339 | table_end = 40 + 2 * 8 | ||
| 340 | first_off = (table_end + 15) & ~15 | ||
| 341 | off0 = first_off | ||
| 342 | off1 = 65536 + 16 # second record starts in chunk 2 so chunk 1 alone is incomplete | ||
| 343 | root_rec = struct.pack("<4I2Q", 0x11111111, 0, 0xAAAA0001, 0, Q_PTR_HIGH | (off1 + 8), 0) + bytes(off1 - off0 - 32) | ||
| 344 | child_rec = struct.pack("<4I", 0, 0, 0xAAAA0002, 0) + bytes(16) | ||
| 345 | blob = ( | ||
| 346 | struct.pack("<10I", 0, 0, 0, 0, 0, 2, 0, 0, 0, 0) | ||
| 347 | + struct.pack("<2Q", off0, off1) | ||
| 348 | + bytes(first_off - table_end) | ||
| 349 | + root_rec | ||
| 350 | + child_rec | ||
| 351 | ) | ||
| 352 | self.assertGreater(len(blob), 65536) | ||
| 353 | with tempfile.TemporaryDirectory() as d: | ||
| 354 | os.mkdir(os.path.join(d, "000000.bin")) | ||
| 355 | with open(os.path.join(d, "90000.bin"), "wb") as f: | ||
| 356 | f.write(blob[:65536]) | ||
| 357 | with open(os.path.join(d, "100000.bin"), "wb") as f: | ||
| 358 | f.write(blob[65536:]) | ||
| 359 | loaded, meta = script_qtable.load_qtable_payload(d) | ||
| 360 | self.assertEqual(loaded, blob) | ||
| 361 | self.assertEqual((meta["offset"], meta["chunks"]), (0x90000, 2)) | ||
| 362 | |||
| 363 | def test_type_query_sorts_histograms_numerically(self): | ||
| 364 | # Two structs sharing the same type hash with sizes 32 and 112: "32" must come before "112". | ||
| 365 | table_end = 40 + 2 * 8 | ||
| 366 | first_off = (table_end + 15) & ~15 | ||
| 367 | off0 = first_off | ||
| 368 | off1 = off0 + 32 | ||
| 369 | r0 = struct.pack("<4I2Q", 0x22222222, 0, 0xBBBB0001, 0, Q_PTR_HIGH | (off1 + 8), 0) | ||
| 370 | r1 = struct.pack("<4I", 0, 0, 0xBBBB0001, 0) + bytes(112 - 16) | ||
| 371 | blob = ( | ||
| 372 | struct.pack("<10I", 0, 0, 0, 0, 0, 2, 0, 0, 0, 0) | ||
| 373 | + struct.pack("<2Q", off0, off1) | ||
| 374 | + bytes(first_off - table_end) | ||
| 375 | + r0 | ||
| 376 | + r1 | ||
| 377 | ) | ||
| 378 | rep = script_qtable.parse_qtable(blob, type_hash=0xBBBB0001) | ||
| 379 | self.assertEqual(list(rep["type_query"]["struct_sizes"].keys()), ["32", "112"]) | ||
| 380 | |||
| 336 | 381 | ||
| 337 | if __name__ == "__main__": | 382 | if __name__ == "__main__": |
| 338 | unittest.main() | 383 | unittest.main() |
diff --git a/tools/script_qtable.py b/tools/script_qtable.py index afa037245..6c29a5260 100644 --- a/tools/script_qtable.py +++ b/tools/script_qtable.py | |||
| @@ -400,13 +400,15 @@ def parse_qtable(blob: bytes, *, root_hash: int | None = None, type_hash: int | | |||
| 400 | matching_arrays = [r for r in records if r["kind"] == "array" and r["element_type_hash"] == type_hash] | 400 | matching_arrays = [r for r in records if r["kind"] == "array" and r["element_type_hash"] == type_hash] |
| 401 | matching_structs = [r for r in records if r["kind"] == "struct" and r["struct_type_hash"] == type_hash] | 401 | matching_structs = [r for r in records if r["kind"] == "struct" and r["struct_type_hash"] == type_hash] |
| 402 | owner_roots = {tree_owner[r["index"]] for r in matching_arrays + matching_structs if tree_owner[r["index"]] >= 0} | 402 | owner_roots = {tree_owner[r["index"]] for r in matching_arrays + matching_structs if tree_owner[r["index"]] >= 0} |
| 403 | tq_strides = Counter(r["stride"] for r in matching_arrays) | ||
| 404 | tq_sizes = Counter(r["bytes"] for r in matching_structs) | ||
| 403 | result["type_query"] = { | 405 | result["type_query"] = { |
| 404 | "hash": f"{type_hash:#010x}", | 406 | "hash": f"{type_hash:#010x}", |
| 405 | "array_matches": len(matching_arrays), | 407 | "array_matches": len(matching_arrays), |
| 406 | "struct_matches": len(matching_structs), | 408 | "struct_matches": len(matching_structs), |
| 407 | "distinct_trees": len(owner_roots), | 409 | "distinct_trees": len(owner_roots), |
| 408 | "array_strides": dict(sorted(Counter(str(r["stride"]) for r in matching_arrays).items())), | 410 | "array_strides": {str(k): tq_strides[k] for k in sorted(tq_strides)}, |
| 409 | "struct_sizes": dict(sorted(Counter(str(r["bytes"]) for r in matching_structs).items())), | 411 | "struct_sizes": {str(k): tq_sizes[k] for k in sorted(tq_sizes)}, |
| 410 | } | 412 | } |
| 411 | 413 | ||
| 412 | return result | 414 | return result |
| @@ -465,20 +467,31 @@ def _join_qtable_from_source(source) -> tuple[bytes, dict]: | |||
| 465 | 467 | ||
| 466 | 468 | ||
| 467 | class _BinFolderSource: | 469 | class _BinFolderSource: |
| 468 | """Minimal sorted *.bin reader when a test/carve directory has no index.json.""" | 470 | """Minimal offset-ordered *.bin reader when a test/carve directory has no index.json.""" |
| 469 | 471 | ||
| 470 | def __init__(self, directory: str): | 472 | def __init__(self, directory: str): |
| 471 | names = sorted(n for n in os.listdir(directory) if n.lower().endswith(".bin")) | ||
| 472 | self.directory = directory | 473 | self.directory = directory |
| 473 | self.rows = [] | 474 | hex_rows = [] |
| 474 | for idx, name in enumerate(names): | 475 | other_rows = [] |
| 476 | for name in sorted(os.listdir(directory)): | ||
| 477 | if not name.lower().endswith(".bin"): | ||
| 478 | continue | ||
| 475 | full = os.path.join(directory, name) | 479 | full = os.path.join(directory, name) |
| 480 | if not os.path.isfile(full): | ||
| 481 | continue | ||
| 476 | sz = os.path.getsize(full) | 482 | sz = os.path.getsize(full) |
| 477 | try: | 483 | try: |
| 478 | off = int(os.path.splitext(name)[0], 16) | 484 | off = int(os.path.splitext(name)[0], 16) |
| 479 | except ValueError: | 485 | except ValueError: |
| 480 | off = idx * 0x10000 | 486 | other_rows.append((name, sz)) |
| 481 | self.rows.append({"off": off, "stored": sz, "size": sz, "_name": name}) | 487 | else: |
| 488 | hex_rows.append((off, name, sz)) | ||
| 489 | hex_rows.sort(key=lambda item: (item[0], item[1])) | ||
| 490 | next_off = (hex_rows[-1][0] + 0x10000) if hex_rows else 0 | ||
| 491 | self.rows = [{"off": off, "stored": sz, "size": sz, "_name": name} for off, name, sz in hex_rows] | ||
| 492 | for name, sz in other_rows: | ||
| 493 | self.rows.append({"off": next_off, "stored": sz, "size": sz, "_name": name}) | ||
| 494 | next_off += 0x10000 | ||
| 482 | 495 | ||
| 483 | def read(self, row: dict) -> bytes: | 496 | def read(self, row: dict) -> bytes: |
| 484 | with open(os.path.join(self.directory, row["_name"]), "rb") as f: | 497 | with open(os.path.join(self.directory, row["_name"]), "rb") as f: |
| @@ -533,7 +546,8 @@ def check_report_path(out_path: str, target: str, rows: list[dict] | None = None | |||
| 533 | raise UsageError(f"--json would overwrite the input {target}") | 546 | raise UsageError(f"--json would overwrite the input {target}") |
| 534 | else: | 547 | else: |
| 535 | for name in os.listdir(target): | 548 | for name in os.listdir(target): |
| 536 | if name.lower().endswith(".bin") and same_report_path(out_path, os.path.join(target, name)): | 549 | full = os.path.join(target, name) |
| 550 | if name.lower().endswith(".bin") and os.path.isfile(full) and same_report_path(out_path, full): | ||
| 537 | raise UsageError(f"--json would overwrite the input {name}") | 551 | raise UsageError(f"--json would overwrite the input {name}") |
| 538 | elif same_report_path(out_path, target): | 552 | elif same_report_path(out_path, target): |
| 539 | raise UsageError(f"--json would overwrite the input {target}") | 553 | raise UsageError(f"--json would overwrite the input {target}") |