diff --git a/src/native/metadata/NativeSectionDirectory.ts b/src/native/metadata/NativeSectionDirectory.ts index dc88f86538..7c2fe417c4 100644 --- a/src/native/metadata/NativeSectionDirectory.ts +++ b/src/native/metadata/NativeSectionDirectory.ts @@ -220,7 +220,7 @@ export function* readElfSectionDirectory( } return { format: "elf", - elf_os_abi: parsed.value.osAbi, + elf_os_abi: header.osAbi, bits, little, machine, diff --git a/tests/boundary/filesystem/elfDeclaredRanges.test.ts b/tests/boundary/filesystem/elfDeclaredRanges.test.ts index 81e0041c40..0437fc359d 100644 --- a/tests/boundary/filesystem/elfDeclaredRanges.test.ts +++ b/tests/boundary/filesystem/elfDeclaredRanges.test.ts @@ -4,6 +4,7 @@ import { join } from "node:path"; import { expect, it } from "vitest"; import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js"; import { elf } from "../../../src/domain/binaryTarget.fixture.js"; +import { expectBinaryTargetError } from "../../fixtures/binaryTargetAdmission.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const image = (bits: 32 | 64, little: boolean) => { @@ -85,8 +86,9 @@ for (const bits of [32, 64] as const) { reason: expect.stringContaining(`truncated ELF ${declaration}`), }, }); - if (result.ok) throw new Error("Expected a declared range refusal"); - expect(result.error.reason).toContain(`${length}-byte file`); + expect(expectBinaryTargetError(result).reason).toContain( + `${length}-byte file`, + ); } }); @@ -117,8 +119,9 @@ for (const bits of [32, 64] as const) { fixture.word(fixture.program + (bits === 64 ? 8 : 4), maximum); const result = await admit(fixture.bytes); expect(result).toMatchObject({ ok: false }); - if (result.ok) throw new Error("Expected an out-of-file range"); - expect(result.error.reason).toContain(`${maximum}..${maximum + 16n}`); + expect(expectBinaryTargetError(result).reason).toContain( + `${maximum}..${maximum + 16n}`, + ); }); } } @@ -153,7 +156,8 @@ it.each(["sections", "programs", "section names"] as const)( expect((await admit(bytes)).ok).toBe(true); const truncated = await admit(bytes.subarray(0, bytes.length - 1)); expect(truncated).toMatchObject({ ok: false }); - if (truncated.ok) throw new Error("Expected an extended table refusal"); - expect(truncated.error.reason).toContain("truncated ELF"); + expect(expectBinaryTargetError(truncated).reason).toContain( + "truncated ELF", + ); }, ); diff --git a/tests/boundary/filesystem/mipsSharedLoad.test.ts b/tests/boundary/filesystem/mipsSharedLoad.test.ts index 9f8665e5b7..ac52cd7936 100644 --- a/tests/boundary/filesystem/mipsSharedLoad.test.ts +++ b/tests/boundary/filesystem/mipsSharedLoad.test.ts @@ -6,6 +6,7 @@ import { it } from "vitest"; import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js"; import { readMipsElfMetadata } from "../../../src/application/MipsElfMetadata.js"; import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js"; +import { expectBinaryTargetError } from "../../fixtures/binaryTargetAdmission.js"; import { ghidraMipsProfileParameters, ghidraMipsUnsupportedReason, @@ -224,8 +225,10 @@ it("does not treat uncaptured ABI declaration headers as an inspected absence", bytes.writeUInt16LE(1, 48); const path = await writeFixture(bytes); const admitted = await parseBinaryTarget(path); - assert.ok(!admitted.ok); - assert.match(admitted.error.reason, /truncated ELF section header table/u); + assert.match( + expectBinaryTargetError(admitted).reason, + /truncated ELF section header table/u, + ); // The specialized inspector still retains unknown source facts if used on // damaged evidence; normal target admission refuses its impossible extent. const declarations = await inspectSource(path); @@ -304,8 +307,10 @@ it.each(malformed)( if (rejectsExtent) { const path = await writeFixture(bytes); const admitted = await parseBinaryTarget(path); - assert.ok(!admitted.ok); - assert.match(admitted.error.reason, /truncated ELF program \d/u); + assert.match( + expectBinaryTargetError(admitted).reason, + /truncated ELF program \d/u, + ); const declarations = await inspectSource(path); assert.ok(declarations.ok); assert.equal(declarations.value.sharedLoad?.status, "unavailable"); diff --git a/tests/fixtures/binaryTargetAdmission.ts b/tests/fixtures/binaryTargetAdmission.ts new file mode 100644 index 0000000000..41e14984e3 --- /dev/null +++ b/tests/fixtures/binaryTargetAdmission.ts @@ -0,0 +1,15 @@ +import assert from "node:assert/strict"; +import type { parseBinaryTarget } from "../../src/application/BinaryTargetResolver.js"; +import { BinaryTargetError } from "../../src/domain/configurationErrors.js"; + +/** Require target admission failure rather than an unrelated cancellation. */ +export const expectBinaryTargetError = ( + result: Awaited>, +): BinaryTargetError => { + assert.ok(!result.ok, "Expected target admission to fail"); + assert.ok( + result.error instanceof BinaryTargetError, + "Expected a target admission error", + ); + return result.error; +};