Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/user/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ python3 tools/import_audit.py app.registry.json app.libc.prx.guest.prx.registry.
| `absent` | No built library exports the NID: the loader fails. |
| `module` | The import names a file found in `--modules` (the title's own library), which is trusted to provide it. |

An import that names a library requires the literal export name. If its implementation is found only by hashing the import name, it still counts as `implemented` or `stub`, but the report lists the name mismatch, sets `symbol_mismatch` on the JSON import record and counts it in the summary, and exits with code 1. A strong import with a name mismatch is a current stop. Imports without a library retain the audit's name-to-NID fallback.

Module registries mark ELF weak imports with `"weak": true`, allowing the audit's current-stop selection to skip them. Strong imports omit the field. Older module registries do not preserve weak bindings; regenerate them to distinguish optional imports.

ELF modules in `--modules` are audited regardless of filename: their undefined symbols are classified against the built libraries and the other modules' exports, with the module file named in the report, and the libraries they need must have a file in `--libs` or `--modules`. Ordinary non-module sidecars are ignored; malformed ELF, PE/SELF containers and non-ELF files with module extensions remain errors. The audit reads dynamic tables from ELF section headers or, for sectionless files, from `PT_DYNAMIC` and a SysV or GNU hash table.
Expand Down
11 changes: 9 additions & 2 deletions tools/import_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,11 @@ def audit(imports, exports, stubs, libraries, modules, module_imports=None, modu
if not library:
providers = sorted(set(providers) | set((module_exports or {}).get(nid, [])))
resolved_nid = nid
record["symbol_mismatch"] = False
if not providers:
resolved_nid = nid_names.compute_nid(nid)
providers = exports.get(resolved_nid, [])
record["symbol_mismatch"] = bool(library) and library not in modules and bool(providers) and resolved_nid != nid
if not library:
providers = sorted(set(providers) | set((module_exports or {}).get(resolved_nid, [])))
kind = classify(resolved_nid, library, providers, stubs, modules)
Expand Down Expand Up @@ -618,7 +620,7 @@ def current_stop(imports, records):
if weak:
continue
record = classes.get((nid, library))
if record is not None and record["class"] == "absent":
if record is not None and (record["class"] == "absent" or record["symbol_mismatch"]):
return record
return None

Expand Down Expand Up @@ -677,6 +679,7 @@ def summarize(records, references, missing, missing_hosts=()):
"missing_libraries": missing,
"missing_host_dependencies": [{"importer": entry["importer"], "chain": list(entry["chain"])} for entry in missing_hosts],
"library_mismatch": sum(1 for record in records if record["library_mismatch"]),
"symbol_mismatch": sum(1 for record in records if record["symbol_mismatch"]),
}


Expand Down Expand Up @@ -722,6 +725,10 @@ def render(summary, records, missing, stops, missing_hosts=(), located=None):
current = record["library"]
lines.append(f" [{current or 'library not named'}]")
lines.append(f" {label(record)}")
mismatched = [record for record in records if record["symbol_mismatch"]]
if mismatched:
lines.extend(["", f"Qualified import names exported only as their NID ({len(mismatched)}): the loader cannot resolve these names"])
lines.extend(f" {label(record)} -> {nid_names.compute_nid(record['nid'])}" for record in mismatched)
if stops:
lines.append("")
lines.append("Current stop (first unresolved import, in registry order)")
Expand Down Expand Up @@ -800,7 +807,7 @@ def main(argv=None):
return 2
located = locate_missing(missing, {path.resolve().parent for path in args.registries}) if missing else None
print(render(summary, records, missing, stops, missing_hosts, located))
return 1 if summary["unique_by_class"]["absent"] or missing or missing_hosts or (args.strict_providers and summary["library_mismatch"]) or (args.strict_stubs and summary["unique_by_class"]["stub"]) else 0
return 1 if summary["unique_by_class"]["absent"] or summary["symbol_mismatch"] or missing or missing_hosts or (args.strict_providers and summary["library_mismatch"]) or (args.strict_stubs and summary["unique_by_class"]["stub"]) else 0


if __name__ == "__main__":
Expand Down
62 changes: 62 additions & 0 deletions tools/tests/test_import_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,68 @@ def test_named_imports_fall_back_to_nid_exports(self):
self.assertTrue(records["missing_function"]["weak"])
self.assertIsNone(data["stops"][0]["nid"])

def test_nid_only_exports_do_not_resolve_literal_import_names(self):
work = self.work
name, nid = "strlen", nid_names.compute_nid("strlen")
registry, report = work.root / "sdk.registry.json", work.root / "audit.json"
for image in (exporting(nid), build_pe([nid])):
(work.libs / "libc.prx").write_bytes(image)
for stub in (False, True):
write_source(work.source, "libc", (STUB_SOURCE if stub else DONE_SOURCE).format(name=name))
for weak in (False, True):
with self.subTest(pe=image.startswith(b"MZ"), stub=stub, weak=weak):
write_registry(registry, [(name, "libc.prx", weak)])
code, out, err = work.run(registry, "--json", str(report))
self.assertEqual((code, err), (1, ""))
data = json.loads(report.read_text(encoding="utf-8"))
record, = data["imports"]
self.assertEqual(record["class"], "stub" if stub else "implemented")
self.assertEqual(record["providers"], ["libc.prx"])
self.assertTrue(record["symbol_mismatch"])
self.assertEqual(data["symbol_mismatch"], 1)
self.assertEqual(data["stops"][0]["nid"], None if weak else name)
self.assertIn("Qualified import names exported only as their NID", out)
self.assertIn(f"-> {nid}", out)

def test_name_mismatch_requires_a_resolved_host_fallback(self):
name, library = "strlen", "libc.prx"
exports = {nid_names.compute_nid(name): [library]}
for providers, modules, kind, mismatch in (({}, set(), "absent", False),
(exports, {library}, "module", False),
(exports, set(), "implemented", True)):
with self.subTest(kind=kind):
records, _ = import_audit.audit([(name, library, False)], providers, {}, {library}, modules)
self.assertEqual(records[0]["class"], kind)
self.assertEqual(records[0]["symbol_mismatch"], mismatch)

def test_literal_exports_resolve_without_a_name_mismatch(self):
work = self.work
name, nid = "strlen", nid_names.compute_nid("strlen")
registry, report = work.root / "sdk.registry.json", work.root / "audit.json"
for image in (exporting(name, nid), build_pe(sorted([name, nid]))):
with self.subTest(pe=image.startswith(b"MZ")):
(work.libs / "libc.prx").write_bytes(image)
write_registry(registry, [(name, "libc.prx"), (nid, "libc.prx")])
code, out, err = work.run(registry, "--json", str(report))
self.assertEqual((code, err), (0, ""))
data = json.loads(report.read_text(encoding="utf-8"))
self.assertEqual(data["symbol_mismatch"], 0)
self.assertTrue(all(not record["symbol_mismatch"] for record in data["imports"]))
self.assertIsNone(data["stops"][0]["nid"])
self.assertNotIn("Qualified import names exported only as their NID", out)

def test_unscoped_windows_imports_keep_the_loaders_nid_fallback(self):
work = self.work
name, nid = "strlen", nid_names.compute_nid("strlen")
registry, report = work.root / "sdk.registry.json", work.root / "audit.json"
write_registry(registry, [(name, "")])
(work.libs / "libc.prx").write_bytes(build_pe([nid]))
code, out, err = work.run(registry, "--json", str(report))
self.assertEqual((code, err), (0, ""))
data = json.loads(report.read_text(encoding="utf-8"))
self.assertEqual(data["symbol_mismatch"], 0)
self.assertIsNone(data["stops"][0]["nid"])

def test_named_nid_fallback_preserves_stub_and_library_classification(self):
nid = nid_names.compute_nid(MESSAGE_NAME)
imports = [(MESSAGE_NAME, "libSceOther.prx", False)]
Expand Down
Loading