From 32b20205715e4af02800ace8ac997895f140c474 Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Wed, 18 Nov 2026 22:42:00 +0800 Subject: [PATCH] fix(elf): recover pointers from Android APS2 relocation tables (#12047) --- docs/guides/android/android-analysis.md | 7 +- docs/guides/native/native-initializers.md | 15 +- src/domain/android/androidJniBindings.test.ts | 25 ++ src/domain/android/androidJniLibraryFacts.ts | 5 +- src/domain/elfRelocation.ts | 9 +- .../elfAndroidPackedRelocations.test.ts | 260 ++++++++++++++ src/domain/native/elfPointerRelocations.ts | 316 +++++++++++++----- .../acceptance/nativeInitializersCli.test.ts | 33 ++ .../android/androidJniLibrary.test.ts | 63 +++- .../boundary/il2cpp/il2cppBinaryFile.test.ts | 42 ++- tests/fixtures/android/jni-bindings/README.md | 49 ++- tests/fixtures/android/jni-bindings/aps2.c | 14 + tests/fixtures/android/jni-bindings/build.py | 3 - tests/fixtures/android/jni-bindings/oracle.py | 76 ++++- .../jni-bindings/source-expectations.json | 2 +- .../android/jni-bindings/verify-aps2.py | 102 ++++++ tests/fixtures/android/jniElf.ts | 103 ++++-- tests/fixtures/elfAndroidPacked.ts | 43 +++ tests/fixtures/il2cpp/binary.ts | 66 +++- tests/fixtures/nativeAps2Initializers.ts | 42 +++ .../android-jni-library.mjs | 85 ++--- .../analysis-efficiency/android-jni.mjs | 32 +- 22 files changed, 1150 insertions(+), 242 deletions(-) create mode 100644 src/domain/native/elfAndroidPackedRelocations.test.ts create mode 100644 tests/fixtures/android/jni-bindings/aps2.c create mode 100644 tests/fixtures/android/jni-bindings/verify-aps2.py create mode 100644 tests/fixtures/elfAndroidPacked.ts create mode 100644 tests/fixtures/nativeAps2Initializers.ts diff --git a/docs/guides/android/android-analysis.md b/docs/guides/android/android-analysis.md index ba7bb78447..6bebf0e723 100644 --- a/docs/guides/android/android-analysis.md +++ b/docs/guides/android/android-analysis.md @@ -429,8 +429,11 @@ and their relocation sources. JNI exports follow the failed escaping does not acquire an invented export match. Tables require NUL-terminated [Modified UTF-8 strings](https://docs.oracle.com/en/java/javase/21/docs/specs/jni/types.html#modified-utf-8-strings), valid descriptors and executable load mappings. REL, RELA and RELR retain -separate provenance; unsupported packed relocations and unresolved pointer -spans stay explicit unknowns. +separate provenance. Android APS2-compressed REL/RELA records keep their logical +`encoding` and add `packing: { format: "aps2", group_entry_index: ... }`; +`record_file_offset` identifies the shared group header and the index identifies +the member within it. APS1 and unresolved pointer spans stay explicit unknowns. +APS2 uses the [shared ELF decode-work allowance](../native/native-initializers.md#paging-and-limits). A static `JNINativeMethod` entry names no class. Attribute it uniquely only when exactly one declaration in the complete recorded native inventory has diff --git a/docs/guides/native/native-initializers.md b/docs/guides/native/native-initializers.md index 6883b8897a..16df765115 100644 --- a/docs/guides/native/native-initializers.md +++ b/docs/guides/native/native-initializers.md @@ -99,11 +99,16 @@ Symbol and section names are untrusted file text and carry For PIE executables and shared objects the array slots are usually zero on disk. A slot is reported `unresolved`, never as its stored zero, when the dynamic relocation tables use an encoding this inspection does not decode -(for example Android packed relocations or an unsupported machine). Relative +(for example legacy Android APS1 relocations or an unsupported machine). Relative relocations are decoded for x86-64, i386, AArch64 and 32-bit ARM. A slot is also `unresolved` when a dynamic relocation writes only part of it, starts inside it, or writes it more than once with different values. +Android APS2 packed REL/RELA tables are decoded through the same relocation +rules, including grouped offsets, signed addend deltas and zero-addend groups. +Their relative slots use `relative_addend`; unresolved symbolic or indirect +values remain unresolved. APS2 and standard RELR tables can coexist. + A `COPY` relocation writes as many bytes as the image's own dynamic symbol records, and a TLS descriptor relocation writes two words. A slot outside those bytes keeps its stored word, so a non-PIE executable that copies `stdout` @@ -169,6 +174,14 @@ visible. ## Paging and limits +Each APS2 table has a fixed 16 MiB compressed-input and decode-work allowance. +Before group expansion, the declared count multiplied by the equivalent +unpacked REL/RELA record width must fit 16 MiB. This bounds interpretation work +even when a tiny group encodes millions of records; it is separate from retained +pointer-index and result limits, and has no runtime override. Decoding yields +within groups for cancellation. Only zero linker padding is accepted after the +declared records. + `offset` and `limit` page the ordered list (default limit 64, maximum 4096). `coverage.examined_entries` counts every slot read, including omitted sentinels, and `total_entries` (the listed entries) is set when the inventory diff --git a/src/domain/android/androidJniBindings.test.ts b/src/domain/android/androidJniBindings.test.ts index a2c85fa845..7384d3f6a0 100644 --- a/src/domain/android/androidJniBindings.test.ts +++ b/src/domain/android/androidJniBindings.test.ts @@ -151,6 +151,31 @@ const project = ( return result.value; }; +it("preserves APS2 group/member provenance through binding projection and JSON delivery", () => { + const observed = table(); + const entry = observed.entries[0]; + if (entry === undefined) throw new Error("fixture"); + entry.slots.forEach((slot, index) => { + if (slot.relocation !== null) + slot.relocation = { + ...slot.relocation, + record_file_offset: 256, + packing: { format: "aps2", group_entry_index: index }, + }; + }); + const report = androidJniReportSchema.parse( + JSON.parse( + JSON.stringify( + project([library(0, "arm64-v8a", facts({ tables: [observed] }))]), + ), + ), + ); + expect(report.bindings[0]?.status).toBe("matched"); + expect( + report.candidates[0]?.table?.entry.slots.map((slot) => slot.relocation), + ).toEqual(entry.slots.map((slot) => slot.relocation)); +}); + it.each([ [ "p.q.Native", diff --git a/src/domain/android/androidJniLibraryFacts.ts b/src/domain/android/androidJniLibraryFacts.ts index 88097f38fb..2236442f40 100644 --- a/src/domain/android/androidJniLibraryFacts.ts +++ b/src/domain/android/androidJniLibraryFacts.ts @@ -6,11 +6,14 @@ const address = z.string().regex(/^0x[0-9a-f]+$/u); const index = z.number().int().nonnegative(); const status = coverageStatusSchema; -/** The exact source record that established a link-time pointer value. */ +/** The source record, or packed group/member, establishing a link-time pointer value. */ export const androidJniRelocationSourceSchema = z.strictObject({ encoding: z.enum(["rel", "rela", "relr"]), record_file_offset: index, kind: index.nullable(), + packing: z + .strictObject({ format: z.literal("aps2"), group_entry_index: index }) + .exactOptional(), }); /** A file-backed JNINativeMethod word, before any runtime load bias. */ diff --git a/src/domain/elfRelocation.ts b/src/domain/elfRelocation.ts index 9d304080ad..4b88d8bc7b 100644 --- a/src/domain/elfRelocation.ts +++ b/src/domain/elfRelocation.ts @@ -13,6 +13,12 @@ interface ElfRelocationFailure { readonly message: string; } +/** The class-specific r_info split, shared by fixed and Android packed records. */ +export const decodeElfRelocationInfo = (info: bigint, bits: 32 | 64) => ({ + type: Number(info & (bits === 64 ? 0xffffffffn : 0xffn)), + symbolIndex: Number(info >> (bits === 64 ? 32n : 8n)), +}); + export function readElfRelocationEntry( bytes: Uint8Array, bits: 32 | 64, @@ -49,8 +55,7 @@ export function readElfRelocationEntry( : BigInt(view.getUint32(wordBytes, littleEndian)); const entry = { offset, - type: Number(info & (bits === 64 ? 0xffffffffn : 0xffn)), - symbolIndex: Number(info >> (bits === 64 ? 32n : 8n)), + ...decodeElfRelocationInfo(info, bits), }; if (encoding === "rel") return ok(entry); const addend = diff --git a/src/domain/native/elfAndroidPackedRelocations.test.ts b/src/domain/native/elfAndroidPackedRelocations.test.ts new file mode 100644 index 0000000000..4d0d19cb7f --- /dev/null +++ b/src/domain/native/elfAndroidPackedRelocations.test.ts @@ -0,0 +1,260 @@ +import { expect, it } from "vitest"; +import { + aps2Relocations, + aps2Table, +} from "../../../tests/fixtures/elfAndroidPacked.js"; +import { + ElfPointerRelocationFailure, + readElfRelocationWritesSteps, + readElfPointerRelocationsSteps, + type ElfPointerWrite, +} from "./elfPointerRelocations.js"; + +const packedImage = ( + packed: Buffer, + bits: 32 | 64 = 64, + little = true, + encoding: "rel" | "rela" = "rela", +) => { + const bytes = Buffer.alloc(Math.max(0x900, 0x100 + packed.length)); + const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length); + const word = bits / 8; + const put = (at: number, value: bigint) => { + if (bits === 64) view.setBigUint64(at, value, little); + else view.setUint32(at, Number(value), little); + }; + const tag = encoding === "rela" ? 0x60000011n : 0x6000000fn; + put(0, tag); + put(word, 0x100n); + put(word * 2, tag + 1n); + put(word * 3, BigInt(packed.length)); + bytes.set(packed, 0x100); + for (let index = 0; index < 3; index++) + put(0x500 + index * word, BigInt(0x21 + index)); + return { + bytes, + input: { + bits, + little, + machine: bits === 64 ? 62 : 3, + dynamicSegments: [{ offset: 0n, size: BigInt(word * 6) }], + mappings: [{ address: 0n, offset: 0, size: bytes.length }], + zeroFills: [], + }, + }; +}; + +const decode = ( + packed: Buffer, + bits: 32 | 64 = 64, + little = true, + encoding: "rel" | "rela" = "rela", +) => { + const fixture = packedImage(packed, bits, little, encoding); + const writes: ElfPointerWrite[] = []; + let checkpoints = 0; + const steps = readElfRelocationWritesSteps(fixture.bytes, fixture.input); + let step = steps.next(); + while (!step.done) { + if (step.value === undefined) checkpoints++; + else writes.push(step.value); + step = steps.next(); + } + return { writes, checkpoints, result: step.value }; +}; + +it.each([ + [32, true], + [32, false], + [64, true], + [64, false], +] as const)( + "decodes grouped RELA offsets/info/addend deltas independently of ELF byte order (%s, %s)", + (bits, little) => { + const word = bits / 8; + const packed = aps2Table([3, 0x500 - word, 3, 15, word, 8, 0x20]); + const { writes } = decode(packed, bits, little); + expect( + writes.map(({ address, value, addend }) => [address, value, addend]), + ).toEqual([ + [0x500n, 0x20n, 0x20n], + [0x500n + BigInt(word), 0x40n, 0x40n], + [0x500n + BigInt(2 * word), 0x60n, 0x60n], + ]); + expect(writes.map(({ source }) => source)).toEqual( + [0, 1, 2].map((index) => ({ + encoding: "rela", + record_file_offset: 0x107, + kind: 8, + packing: { format: "aps2", group_entry_index: index }, + })), + ); + }, +); + +it.each([32, 64] as const)( + "keeps REL in-place addends and logical encoding for %s-bit APS2", + (bits) => { + const word = bits / 8; + const { writes } = decode( + aps2Table([3, 0x500 - word, 3, 3, word, 8]), + bits, + true, + "rel", + ); + expect( + writes.map(({ value, addend, source }) => [ + value, + addend, + source.encoding, + ]), + ).toEqual([ + [0x21n, null, "rel"], + [0x22n, null, "rel"], + [0x23n, null, "rel"], + ]); + }, +); + +it("applies ungrouped negative offset/addend deltas without losing group identity", () => { + const { writes } = decode( + aps2Table([2, 0, 2, 8, 0x508, 8, 0x30, -8, 8, -0x10]), + ); + expect( + writes.map(({ address, value, source }) => [ + address, + value, + source.record_file_offset, + source.packing?.group_entry_index, + ]), + ).toEqual([ + [0x508n, 0x30n, 0x106, 0], + [0x500n, 0x20n, 0x106, 1], + ]); +}); + +it("resets RELA addends for a group that declares none", () => { + const { writes } = decode( + aps2Table([3, 0, 1, 8, 0x500, 8, 0x20, 1, 3, 8, 8, 1, 11, 8, 8, 5]), + ); + expect(writes.map(({ value }) => value)).toEqual([0x20n, 0n, 5n]); +}); + +it("retains unknown symbolic writes and exact r_info symbol indices", () => { + const info = (0xffffffffn << 32n) | 1n; + const { writes } = decode( + aps2Table([1, 0, 1, 8, 0x500, BigInt.asIntN(64, info), 7]), + ); + expect(writes[0]).toMatchObject({ + address: 0x500n, + value: null, + symbol_index: 0xffffffff, + addend: 7n, + source: { kind: 1 }, + }); +}); + +it("accepts LLD's zero section padding after the declared records", () => { + expect( + decode(aps2Table([1, 0, 1, 11, 0x500, 8, 9], 32)).writes[0]?.value, + ).toBe(9n); +}); + +it("accepts an empty table without requiring or inventing a group", () => { + const { writes, result } = decode(aps2Relocations([], "rela")); + expect(writes).toEqual([]); + expect(result.pointer_coverage_known).toBe(true); +}); + +it.each([ + { values: [-1, 0], message: "count is negative" }, + { values: [1, 0, 0, 0], message: "group count" }, + { values: [1, 0, -1, 0], message: "group count" }, + { values: [1, 0, 2, 0], message: "group count" }, + { values: [1, 0, 1, 16], message: "group flags" }, + { values: [1, 0, 1, -1], message: "group flags" }, + { values: [1, 0, 1, 4], message: "group flags" }, + { values: [1, 0, 1, 3, 8], message: "integer is truncated" }, + { values: [1, 0, 1, 8, 0x500, 8], message: "integer is truncated" }, +] as const)( + "refuses malformed APS2 groups: $message ($values)", + ({ values, message }) => { + expect(() => decode(aps2Table(values))).toThrow(message); + }, +); + +it("refuses explicit addends in REL groups", () => { + expect(() => decode(aps2Table([1, 0, 1, 8]), 32, true, "rel")).toThrow( + "group flags", + ); +}); + +it.each([ + Buffer.from("CPS2"), + Buffer.from([0xc1, 0xd0, 0xd3, 0xb2]), + Buffer.concat([Buffer.from("APS2"), Buffer.alloc(11, 0x80)]), + Buffer.concat([aps2Table([0, 0]), Buffer.from([1])]), +])( + "refuses invalid magic, overflowing integers and nonzero trailing data (%j)", + (packed) => { + expect(() => decode(packed)).toThrow(ElfPointerRelocationFailure); + }, +); + +it("admits equivalent expanded table work before iterating a compressed group", () => { + expect(() => decode(aps2Table([1_000_000, 0]))).toThrow( + "16 MiB decode-work allowance", + ); +}); + +it.each([ + { + name: "size without address", + at: 0, + value: 21n, + error: "size has no table address", + }, + { + name: "address without size", + at: 16, + value: 21n, + error: "no valid byte size", + }, + { name: "unmapped source", at: 8, value: 0x10000n, error: "not file-backed" }, + { + name: "duplicate declaration", + at: 16, + value: 0x60000011n, + error: "duplicate dynamic tag", + }, +])("refuses incomplete APS2 declarations: $name", ({ at, value, error }) => { + const fixture = packedImage(aps2Table([0, 0])); + fixture.bytes.writeBigUInt64LE(value, at); + expect(() => [ + ...readElfRelocationWritesSteps(fixture.bytes, fixture.input), + ]).toThrow(error); +}); + +it("keeps conflicting members at one destination unresolved with separate provenance", () => { + const fixture = packedImage(aps2Table([2, 0x500, 2, 15, 0, 8, 5])); + const steps = readElfPointerRelocationsSteps(fixture.bytes, fixture.input); + let step = steps.next(); + while (!step.done) step = steps.next(); + expect(step.value.pointerAt(0x500n)).toBeUndefined(); + expect(step.value.unresolved.has(0x500n)).toBe(true); + expect( + step.value.writes.map(({ value, source }) => [ + value, + source.packing?.group_entry_index, + ]), + ).toEqual([ + [5n, 0], + [10n, 1], + ]); +}); + +it("yields cooperatively inside a fully grouped run with no per-record bytes", () => { + const { checkpoints, writes } = decode(aps2Table([8192, 0, 8192, 3, 0, 0])); + expect(writes).toEqual([]); + expect(checkpoints).toBeGreaterThanOrEqual(3); +}); diff --git a/src/domain/native/elfPointerRelocations.ts b/src/domain/native/elfPointerRelocations.ts index dabd168d6c..2146a42610 100644 --- a/src/domain/native/elfPointerRelocations.ts +++ b/src/domain/native/elfPointerRelocations.ts @@ -1,6 +1,10 @@ import type { FileMapping, ZeroFillMapping } from "../virtualFileMapping.js"; import { readElfDynamicEntry } from "../elfDynamicEntry.js"; -import { readElfRelocationEntry } from "../elfRelocation.js"; +import { + decodeElfRelocationInfo, + readElfRelocationEntry, +} from "../elfRelocation.js"; +import { readLeb128, SLEB64 } from "../leb128.js"; import { readElfSymbol } from "../elfSymbol.js"; import { readElfHashSymbolCount, @@ -72,8 +76,14 @@ export class ElfPointerRelocationFailure extends ParseFailureError< } export interface ElfPointerRelocationSource { readonly encoding: "rel" | "rela" | "relr"; + /** Fixed record start, or the APS2 group header identified by `packing`. */ readonly record_file_offset: number; readonly kind: number | null; + /** APS2 records share their group's header offset; the index identifies its member. */ + readonly packing?: { + readonly format: "aps2"; + readonly group_entry_index: number; + }; } export interface ElfPointerWrite { readonly address: bigint; @@ -133,7 +143,16 @@ const DT_SYMTAB = 6n, DT_SYMENT = 11n, DT_HASH = 4n, DT_GNU_HASH = 0x6ffffef5n; -const PACKED_TAGS = [0x6000000fn, 0x60000010n, 0x60000011n, 0x60000012n]; +const DT_ANDROID_REL = 0x6000000fn, + DT_ANDROID_RELSZ = 0x60000010n, + DT_ANDROID_RELA = 0x60000011n, + DT_ANDROID_RELASZ = 0x60000012n; +const PACKED_TAGS = [ + DT_ANDROID_REL, + DT_ANDROID_RELSZ, + DT_ANDROID_RELA, + DT_ANDROID_RELASZ, +]; const ANDROID_RELR_TAGS = [0x6fffe000n, 0x6fffe001n, 0x6fffe003n]; const UNIQUE_TAGS = new Set([ DT_RELA, @@ -157,6 +176,113 @@ const SYMBOL_TABLE_TAGS = new Set([DT_SYMTAB, DT_SYMENT, DT_HASH, DT_GNU_HASH]); const malformed = (message: string): never => { throw new ElfPointerRelocationFailure("malformed-container", message); }; + +/** Bound packed input and equivalent expanded REL/RELA work before iteration. */ +const ANDROID_PACKED_TABLE_BYTES = 16 * 1024 * 1024; +type PackedRelocation = { + readonly offset: bigint; + readonly type: number; + readonly symbolIndex: number; + readonly addend?: bigint; + readonly source: ElfPointerRelocationSource; +}; + +/** LLVM/AOSP APS2 group grammar; yields records without retaining an expanded table. */ +function* androidPackedRelocations( + bytes: Buffer, + fileOffset: number, + bits: 32 | 64, + encoding: "rel" | "rela", +): Generator { + const exhausted = (): never => { + throw new ElfPointerRelocationFailure( + "native-index-capacity", + "Android packed relocation input or expanded table exceeds the 16 MiB decode-work allowance", + ANDROID_PACKED_TABLE_BYTES, + "bytes", + ); + }; + if (bytes.length > ANDROID_PACKED_TABLE_BYTES) exhausted(); + if (bytes.length < 4 || bytes.toString("latin1", 0, 4) !== "APS2") + return malformed("Android packed relocation table lacks an APS2 header"); + let cursor = 4; + const signed = (): bigint => { + const parsed = readLeb128(bytes, cursor, bytes.length, SLEB64); + if ("failure" in parsed) + return malformed( + `Android APS2 integer is ${parsed.failure} at file offset ${String(fileOffset + parsed.at)}`, + ); + cursor = parsed.next; + return parsed.value; + }; + const rawCount = signed(); + if (rawCount < 0n) + return malformed("Android APS2 relocation count is negative"); + const expandedWidth = (bits / 8) * (encoding === "rela" ? 3 : 2); + if (rawCount * BigInt(expandedWidth) > BigInt(ANDROID_PACKED_TABLE_BYTES)) + exhausted(); + const count = Number(rawCount); + let offset = BigInt.asUintN(bits, signed()), + addend = 0n, + read = 0; + while (read < count) { + const groupOffset = fileOffset + cursor; + const groupCount = signed(), + flags = signed(); + if (groupCount <= 0n || groupCount > BigInt(count - read)) + return malformed( + "Android APS2 group count exceeds the remaining records or is empty", + ); + if ( + flags < 0n || + flags > 15n || + ((flags & 4n) !== 0n && (flags & 8n) === 0n) || + (encoding === "rel" && (flags & 12n) !== 0n) + ) + return malformed("Invalid Android APS2 group flags"); + const groupedInfo = (flags & 1n) !== 0n, + groupedOffset = (flags & 2n) !== 0n, + groupedAddend = (flags & 4n) !== 0n, + hasAddend = (flags & 8n) !== 0n; + const offsetDelta = groupedOffset ? signed() : 0n; + const info = groupedInfo ? BigInt.asUintN(bits, signed()) : 0n; + const addendDelta = groupedAddend ? signed() : 0n; + const entriesInGroup = Number(groupCount); + for (let index = 0; index < entriesInGroup; index++, read++) { + if (read % 4096 === 0) yield; + // The loader assigns these values to ELF-word fields, including signed deltas. + offset = BigInt.asUintN( + bits, + offset + (groupedOffset ? offsetDelta : signed()), + ); + const entryInfo = groupedInfo ? info : BigInt.asUintN(bits, signed()); + if (hasAddend) + addend = BigInt.asIntN( + bits, + addend + (groupedAddend ? addendDelta : signed()), + ); + else addend = 0n; + const decoded = decodeElfRelocationInfo(entryInfo, bits); + yield { + offset, + ...decoded, + ...(encoding === "rela" ? { addend } : {}), + source: { + encoding, + kind: decoded.type, + record_file_offset: groupOffset, + packing: { format: "aps2", group_entry_index: index }, + }, + }; + } + } + // LLD keeps zero padding when layout convergence would otherwise shrink a section. + for (; cursor < bytes.length; cursor++) { + if (cursor % 65536 === 0) yield; + if (bytes[cursor] !== 0) + return malformed("Nonzero bytes follow the Android APS2 records"); + } +} const RELOCATION_KINDS = new Map([ [ 183, @@ -572,13 +698,82 @@ export function* readElfRelocationWritesSteps( // Consumers do work across each COPY span, which repeated or overlapping // records would multiply, so the spans together are limited to the file size. let copyAllowance = bytes.length; - if (PACKED_TAGS.some((tag) => tags.has(tag))) - unsupported( - "Android packed APS2 relocation write locations and addends are not decoded", - ); if (ANDROID_RELR_TAGS.some((tag) => tags.has(tag))) unsupported("Android-specific RELR dynamic tags are not decoded"); + /** One owner applies fixed and packed REL/RELA records to loader pointer facts. */ + function* applyRelocation( + parsed: { + readonly type: number; + readonly offset: bigint; + readonly symbolIndex: number; + readonly addend?: bigint; + }, + source: ElfPointerRelocationSource, + ): Generator { + const { type: kind, offset: destination, symbolIndex } = parsed; + if (elfRelocationWidth(input.machine, kind) === 0) return; + const explicitAddend = parsed.addend; + if (source.encoding === "rela" && explicitAddend === undefined) + return malformed("RELA relocation has no decoded addend"); + const record = { + source, + symbol_index: symbolIndex, + addend: explicitAddend ?? null, + }; + if (kind === copy) { + const width = copyWidth(destination, symbolIndex); + if (width === undefined) + unsupported("ELF COPY relocation write size is not established"); + else if (width > copyAllowance) + unsupported( + "ELF COPY relocation sizes exceed the file byte count allowance", + ); + else if (width > 0) { + copyAllowance -= width; + yield write(destination, width, null, record); + } + return; + } + if (kind !== relative || symbolIndex !== 0) { + const globalData = input.machine === 183 ? 1025 : 6; + if ( + options.symbolResolution === "image-definitions" && + input.bits === 64 && + (kind === absolute || kind === globalData) + ) { + const value = yield* symbolLinkValue(symbolIndex); + if (value !== undefined) { + // AArch64 ABS64/GLOB_DAT and x86-64 R_X86_64_64 use S + A. + // R_X86_64_GLOB_DAT uses S, ignoring even a nonzero addend. + const slot = + explicitAddend === undefined ? target(destination, word) : null; + const addend = + input.machine === 62 && kind === globalData + ? 0n + : (explicitAddend ?? (slot === null ? 0n : rawWord(slot))); + yield write( + destination, + word, + BigInt.asUintN(input.bits, value + addend), + record, + ); + return; + } + } + const width = unknownWidths.get(kind); + if (width === undefined || (width > word && !wholeExtents)) { + unsupported(`ELF relocation kind ${kind} is not decoded`); + return; + } + yield write(destination, width, null, record); + return; + } + const slot = target(destination, word); + const addend = explicitAddend ?? (slot === null ? 0n : rawWord(slot)); + yield write(destination, word, BigInt.asUintN(input.bits, addend), record); + } + function* standardTable( address: bigint, size: bigint | undefined, @@ -617,76 +812,11 @@ export function* readElfRelocationWritesSteps( "rel", ); if (!parsed.ok) return malformed(parsed.error.message); - const { type: kind, offset: destination, symbolIndex } = parsed.value; - if (elfRelocationWidth(input.machine, kind) === 0) continue; - const source = { encoding, record_file_offset: at, kind }; - const explicitAddend = - "addend" in parsed.value && typeof parsed.value.addend === "bigint" - ? parsed.value.addend - : undefined; - if (encoding === "rela" && explicitAddend === undefined) - return malformed("RELA relocation has no decoded addend"); - const record = { - source, - symbol_index: symbolIndex, - addend: explicitAddend ?? null, - }; - if (kind === copy) { - const width = copyWidth(destination, symbolIndex); - if (width === undefined) - unsupported("ELF COPY relocation write size is not established"); - else if (width > copyAllowance) - unsupported( - "ELF COPY relocation sizes exceed the file byte count allowance", - ); - else if (width > 0) { - copyAllowance -= width; - yield write(destination, width, null, record); - } - continue; - } - if (kind !== relative || symbolIndex !== 0) { - const globalData = input.machine === 183 ? 1025 : 6; - if ( - options.symbolResolution === "image-definitions" && - input.bits === 64 && - (kind === absolute || kind === globalData) - ) { - const value = yield* symbolLinkValue(symbolIndex); - if (value !== undefined) { - // AArch64 ABS64/GLOB_DAT and x86-64 R_X86_64_64 use S + A. - // R_X86_64_GLOB_DAT uses S, ignoring even a nonzero addend. - const slot = - explicitAddend === undefined ? target(destination, word) : null; - const addend = - input.machine === 62 && kind === globalData - ? 0n - : (explicitAddend ?? (slot === null ? 0n : rawWord(slot))); - yield write( - destination, - word, - BigInt.asUintN(input.bits, value + addend), - record, - ); - continue; - } - } - const width = unknownWidths.get(kind); - if (width === undefined || (width > word && !wholeExtents)) { - unsupported(`ELF relocation kind ${kind} is not decoded`); - continue; - } - yield write(destination, width, null, record); - continue; - } - const slot = target(destination, word); - const addend = explicitAddend ?? (slot === null ? 0n : rawWord(slot)); - yield write( - destination, - word, - BigInt.asUintN(input.bits, addend), - record, - ); + yield* applyRelocation(parsed.value, { + encoding, + record_file_offset: at, + kind: parsed.value.type, + }); } } for (const [addressTag, sizeTag, entryTag, encoding] of [ @@ -706,6 +836,40 @@ export function* readElfRelocationWritesSteps( `DT_${encoding.toUpperCase()}SZ has no DT_${encoding.toUpperCase()}`, ); } + for (const [addressTag, sizeTag, encoding] of [ + [DT_ANDROID_REL, DT_ANDROID_RELSZ, "rel"], + [DT_ANDROID_RELA, DT_ANDROID_RELASZ, "rela"], + ] as const) { + const address = tags.get(addressTag), + size = tags.get(sizeTag); + if (address === undefined) { + if (size !== undefined && size !== 0n) + malformed("Android packed relocation size has no table address"); + continue; + } + if (size === undefined || size > BigInt(bytes.length)) + return malformed( + "Android packed relocation table has no valid byte size", + ); + if (size === 0n) continue; + const start = fileOffset(address, Number(size)); + if (start === null) + return malformed("Android packed relocation table is not file-backed"); + const packed = bytes.subarray(start, start + Number(size)); + if (packed.toString("latin1", 0, 4) === "APS1") { + unsupported("Android packed APS1 relocations are not decoded"); + continue; + } + for (const entry of androidPackedRelocations( + packed, + start, + input.bits, + encoding, + )) { + if (entry === undefined) yield; + else yield* applyRelocation(entry, entry.source); + } + } const plt = tags.get(DT_JMPREL); if (plt !== undefined) { const encoding = diff --git a/tests/acceptance/nativeInitializersCli.test.ts b/tests/acceptance/nativeInitializersCli.test.ts index 4ed5baef48..f4cecc469b 100644 --- a/tests/acceptance/nativeInitializersCli.test.ts +++ b/tests/acceptance/nativeInitializersCli.test.ts @@ -11,9 +11,42 @@ import { } from "../../src/native/initializers/NativeInitializers.fixture.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; import { cliTest } from "../support/cli/cliFixture.js"; +import { aps2InitializerImage } from "../fixtures/nativeAps2Initializers.js"; const contract = toolContract("inspect_native_initializers"); +cliTest( + "reads APS2 RELA alongside RELR through the public CLI without changing the image", + async ({ cli }) => { + const root = await createTestTempDirectory("rea-aps2-initializers-cli-"); + const path = join(root, "library.so"); + const bytes = aps2InitializerImage(); + await writeFile(path, bytes); + const result = await cli.run({ + arguments: ["inspect-native-initializers", path, "--json"], + }); + expect(result.exitCode, result.stderr).toBe(0); + const report = contract.outputSchema.parse(result.json).normalized_result; + expect(report.coverage).toMatchObject({ + status: "complete", + total_entries: 7, + }); + expect( + report.entries + .filter((entry) => entry.kind === "init_array") + .map((entry) => [entry.value_va, entry.resolution]), + ).toEqual([ + ["0x100", "relative_addend"], + ["0x110", "relative_addend"], + [null, "unresolved_import"], + ]); + expect( + report.entries.find((entry) => entry.kind === "fini_array"), + ).toMatchObject({ value_va: "0x120", resolution: "relr" }); + expect((await readFile(path)).equals(bytes)).toBe(true); + }, +); + cliTest( "keeps an unrelocated ELF alias's value and source identity through the CLI", async ({ cli }) => { diff --git a/tests/boundary/android/androidJniLibrary.test.ts b/tests/boundary/android/androidJniLibrary.test.ts index d86ec62ebe..0d587de543 100644 --- a/tests/boundary/android/androidJniLibrary.test.ts +++ b/tests/boundary/android/androidJniLibrary.test.ts @@ -96,18 +96,27 @@ it.each(["name", "descriptor"] as const)( ); it.each([ - [64, 183, "rela"], - [64, 62, "rela"], - [32, 40, "rel"], - [32, 3, "rel"], - [64, 183, "relr"], - [64, 62, "relr"], - [32, 40, "relr"], - [32, 3, "relr"], + [64, 183, "rela", false], + [64, 62, "rela", false], + [32, 40, "rel", false], + [32, 3, "rel", false], + [64, 183, "relr", false], + [64, 62, "relr", false], + [32, 40, "relr", false], + [32, 3, "relr", false], + [64, 183, "rela", true], + [64, 62, "rela", true], + [32, 40, "rel", true], + [32, 3, "rel", true], ] as const)( - "decodes %s-bit machine %s %s static tables with complete slot provenance", - async (bits, machine, encoding) => { - const fixture = jniElfFixture({ bits, machine, encoding }); + "decodes %s-bit machine %s %s static tables (APS2: %s) with complete slot provenance", + async (bits, machine, encoding, packed) => { + const fixture = jniElfFixture({ + bits, + machine, + encoding, + ...(packed ? { packing: "aps2" } : {}), + }); const original = Buffer.from(fixture.bytes); const result = await inspectAndroidJniLibrary(fixture.bytes); if (!result.ok) throw new Error(result.error.message); @@ -161,6 +170,15 @@ it.each([ ], ]); const first = result.value.tables[0]?.entries[0]; + if (packed) + expect(first?.slots.map((slot) => slot.relocation)).toEqual( + [0, 1, 2].map((index) => ({ + encoding, + record_file_offset: 0x806, + kind: machine === 183 ? 1027 : machine === 40 ? 23 : 8, + packing: { format: "aps2", group_entry_index: index }, + })), + ); expect( first?.slots.map((slot) => [ slot.role, @@ -201,11 +219,20 @@ it("does not limit the writable scan to named or read-only relocation data", asy }); }); -it.each([32, 64] as const)( - "keeps later valid entries after a %s-bit unresolved function relocation", - async (bits) => { +it.each([ + [32, undefined], + [64, undefined], + [32, "aps2"], + [64, "aps2"], +] as const)( + "keeps later valid entries after a %s-bit unresolved function relocation (packing: %s)", + async (bits, packing) => { const result = await inspectAndroidJniLibrary( - jniElfFixture({ bits, symbolicFirstFunction: true }).bytes, + jniElfFixture({ + bits, + symbolicFirstFunction: true, + ...(packing === undefined ? {} : { packing }), + }).bytes, ); if (!result.ok) throw new Error(result.error.message); expect(result.value.coverage).toMatchObject({ @@ -221,9 +248,9 @@ it.each([32, 64] as const)( }, ); -it("keeps exports and explicit unknown table coverage for Android packed relocations", async () => { +it("keeps exports and explicit unknown table coverage for unsupported APS1 relocations", async () => { const result = await inspectAndroidJniLibrary( - jniElfFixture({ packed: true }).bytes, + jniElfFixture({ packing: "aps1" }).bytes, ); if (!result.ok) throw new Error(result.error.message); expect(result.value.exports).toHaveLength(2); @@ -233,7 +260,7 @@ it("keeps exports and explicit unknown table coverage for Android packed relocat relocations_status: "unknown", tables_status: "unknown", }); - expect(result.value.unknowns.join("\n")).toContain("APS2"); + expect(result.value.unknowns.join("\n")).toContain("APS1"); }); it("rejects a leading RELR bitmap rather than interpreting it as an address", async () => { diff --git a/tests/boundary/il2cpp/il2cppBinaryFile.test.ts b/tests/boundary/il2cpp/il2cppBinaryFile.test.ts index e519a23355..8ebffc0e00 100644 --- a/tests/boundary/il2cpp/il2cppBinaryFile.test.ts +++ b/tests/boundary/il2cpp/il2cppBinaryFile.test.ts @@ -67,6 +67,18 @@ const link = (bytes: Buffer, architecture?: Il2cppBinaryArchitecture) => metadata, facts, ); +it.each([183, 62])( + "links IL2CPP registration and method pointers through APS2 on machine %d", + (machine) => { + const bytes = createElfFixture(createIl2cppBinaryFixture(), { + machine, + androidPacked: "aps2", + }); + const original = Buffer.from(bytes); + expect(link(bytes).method_addresses).toEqual(expectedAddresses); + expect(bytes.equals(original)).toBe(true); + }, +); it.each([183, 62])( "links past unrelated ABS64 and GLOB_DAT records on ELF machine %d", (machine) => { @@ -501,17 +513,23 @@ it("does not apply symbol relocations as RELATIVE ones", () => { ).toThrow(Il2cppBinaryFormatFailure); }); -it("reads a method pointer array in zero-fill as methods without bodies", () => { - // An iOS-only assembly in an Android build keeps its array in .bss. - const linked = link( - createElfFixture(createIl2cppBinaryFixture(), { zeroFilledTable: true }), - ); - expect(linked.method_addresses).toEqual([ - ...expectedAddresses.slice(0, 3), - expectedAddress("no-body"), - expectedAddress("no-body"), - ]); -}); +it.each([undefined, "aps2"] as const)( + "reads a method pointer array in zero-fill as methods without bodies (packing: %s)", + (androidPacked) => { + // An iOS-only assembly in an Android build keeps its array in .bss. + const linked = link( + createElfFixture(createIl2cppBinaryFixture(), { + zeroFilledTable: true, + ...(androidPacked === undefined ? {} : { androidPacked }), + }), + ); + expect(linked.method_addresses).toEqual([ + ...expectedAddresses.slice(0, 3), + expectedAddress("no-body"), + expectedAddress("no-body"), + ]); + }, +); it("reads a PE pointer array across file bytes and the declared zero-fill tail", () => { const bytes = createSplitPointerPeFixture(); @@ -1106,7 +1124,7 @@ it.each(["outside", "misaligned", "duplicate"] as const)( it.each([ [{ dynamic: false }, "unsupported-container"], [{ machine: 243 }, "unsupported-container"], - [{ androidPacked: true }, "unsupported-pointer-encoding"], + [{ androidPacked: "aps1" }, "unsupported-pointer-encoding"], ] as const)( "refuses ELF relocations it cannot apply (%o)", (options, reason) => { diff --git a/tests/fixtures/android/jni-bindings/README.md b/tests/fixtures/android/jni-bindings/README.md index 870b1e07fc..afeee08561 100644 --- a/tests/fixtures/android/jni-bindings/README.md +++ b/tests/fixtures/android/jni-bindings/README.md @@ -1,5 +1,33 @@ # Static JNI fixture built with the Android NDK +## Portable APS2 compatibility witness + +`aps2.c` is a separate freestanding layout witness with four local +`JNINativeMethod`-shaped entries, three entries depending on an undefined +external function, and a constructor. It has no JNI runtime or registration +calls. GCC emits 32/64-bit x86 objects; LLD links ordinary, APS2-only and mixed +APS2/standard-RELR variants. No multilib C runtime is needed. None of the +produced shared objects is loaded or executed. + +After building REA, run with GCC and an LLD supporting Android packing +(qualified with GCC 14.2 and LLD 19.1.7 on Linux): + +```sh +python3 tests/fixtures/android/jni-bindings/verify-aps2.py --lld /path/to/ld.lld +``` + +The independent Python ELF oracle checks source symbols and exact name, +descriptor, slot and function bytes. The witness requires actual grouped +offsets and zero-addend groups from LLD, compares group/member provenance, +keeps the three external-function dependencies unknown, and verifies the +constructor through the public CLI. It also runs the existing library +qualification checker over all six libraries (24 known entries) and verifies +source digests remain unchanged. Temporary objects and libraries are removed. +`--runtime-root /path/to/checkout` selects another built runtime for a baseline. +This qualifies x86 wire formats; the full NDK/APK workflow below is separate. + +## Android NDK fixture + This fixture contains genuine Java `native` declarations, javac-generated JNI headers, `Java_*` exports and actual `JNI_OnLoad`/`RegisterNatives` source. Its DEX-only APK containers have no Android manifest, resources or signature. They @@ -59,29 +87,28 @@ the resulting DEX `ACC_NATIVE` flags and exact names/prototypes. It then compile two libraries for each of arm64-v8a, armeabi-v7a, x86_64 and x86, with three packing variants. All subprocess arguments and tool versions are recorded in the output. -| APK variant | Observed table relocation encoding | Required interpretation | -| ------------------------- | ------------------------------------------------------ | ------------------------------------------------------------------------------------ | -| `jni-ordinary-static.apk` | AArch64/x86-64 RELA; ARM/i686 REL implicit addends | Every source-known candidate and exact slot value survives | -| `jni-relr-static.apk` | Standard DT_RELR with encoded bitmaps on all four ABIs | Same facts, with RELR source evidence | -| `jni-aps2-static.apk` | Android packed APS2 REL/RELA on all four ABIs | Explicit unsupported/partial table coverage; preserve independently readable exports | +| APK variant | Observed table relocation encoding | Required interpretation | +| ------------------------- | ------------------------------------------------------ | ----------------------------------------------------------------- | +| `jni-ordinary-static.apk` | AArch64/x86-64 RELA; ARM/i686 REL implicit addends | Every source-known candidate and exact slot value survives | +| `jni-relr-static.apk` | Standard DT_RELR with encoded bitmaps on all four ABIs | Same facts, with RELR source evidence | +| `jni-aps2-static.apk` | Android packed APS2 REL/RELA on all four ABIs | Same source-known candidates, with packed group/member provenance | -Ordinary and RELR source expectations have, across four ABIs, 32 matched, +Ordinary, RELR and APS2 source expectations have, across four ABIs, 32 matched, 12 ambiguous and four unmatched bindings. These are source-known outcomes. The report's matched status establishes observed candidate presence; separate partial coverage does not promise complete uniqueness. A source-known unmatched method can remain unknown when GLOB_DAT or NOBITS handling leaves incomplete -coverage. APS2's `expected_methods` retains source-known export facts and marks -unrecovered table declarations unknown. The report must retain readable export -candidates and must not claim that an undecoded table is absent. +coverage. The report must retain readable export candidates and keep unresolved +symbolic table pointers unknown rather than treating them as absent. ## Independent evidence and limits `oracle.py` imports no REA parser. For these small generated artifacts it reads known ELF symbols and source-specified table slots, applies standard relative -REL/RELA/RELR records, checks the exact Modified UTF-8 bytes, and requires each +REL/RELA/RELR and APS2 records, checks the exact Modified UTF-8 bytes, and requires each function pointer to equal the compiled function symbol inside executable PT_LOAD. It is a known-slot oracle, not a general JNI table discovery implementation. -It deliberately does not decode APS2. The separate official `llvm-readelf` +The separate official `llvm-readelf` transcripts establish actual sections, symbols, relocation tags and GNU_RELRO. Generated `full-expectations.json` contains every ABI/library digest, export and diff --git a/tests/fixtures/android/jni-bindings/aps2.c b/tests/fixtures/android/jni-bindings/aps2.c new file mode 100644 index 0000000000..78f910ed23 --- /dev/null +++ b/tests/fixtures/android/jni-bindings/aps2.c @@ -0,0 +1,14 @@ +/* Freestanding static-layout witness: never loaded, registered or invoked. */ +struct NativeMethod { const char *name; const char *signature; void (*function)(void); }; +static void first(void) {} +static void second(void) {} +static void third(void) {} +static void fourth(void) {} +extern void external(void); +__attribute__((visibility("hidden"), used)) const struct NativeMethod methods[] = { + {"first", "()V", first}, {"second", "(I)V", second}, + {"third", "(J)V", third}, {"fourth", "([B)V", fourth}, + {"externalA", "()V", external}, {"externalB", "()V", external}, + {"externalC", "()V", external}, +}; +__attribute__((constructor)) static void initialize(void) {} diff --git a/tests/fixtures/android/jni-bindings/build.py b/tests/fixtures/android/jni-bindings/build.py index a735e43657..e3a20b0358 100644 --- a/tests/fixtures/android/jni-bindings/build.py +++ b/tests/fixtures/android/jni-bindings/build.py @@ -114,9 +114,6 @@ def main(): for abi in triples: for row in expected["native_declarations"]: claim = {**row, "abi": abi, "implementations": []} - if variant == "aps2" and row["basis"] == "table": - claim["per_abi_status"] = "unknown" - claim["reason"] = "unsupported-aps2" for member, facts in libraries.items(): if member.split("/")[1] != abi: continue diff --git a/tests/fixtures/android/jni-bindings/oracle.py b/tests/fixtures/android/jni-bindings/oracle.py index c428ecea8c..713ae75e52 100644 --- a/tests/fixtures/android/jni-bindings/oracle.py +++ b/tests/fixtures/android/jni-bindings/oracle.py @@ -76,6 +76,8 @@ class Elf: self.exports[name] = symbol self.overlay = {} self.encodings = set() + self.packed_records = {} + self.packed_groups = [] relative = {183: 1027, 62: 8, 3: 8, 40: 23}[self.machine] for section in self.sections: if section["type"] in (4, 9): @@ -105,6 +107,43 @@ class Elf: elif section["type"] in (0x60000001, 0x60000002): assert self.section_data(section).startswith(b"APS2") self.encodings.add("APS2") + packed = self.section_data(section) + position = 4 + def integer(): + nonlocal position + value, shift = 0, 0 + while True: + byte = packed[position] + position += 1 + value |= (byte & 127) << shift + shift += 7 + if byte < 128: + return value - (1 << shift) if byte & 64 else value + count, address = integer(), integer() + addend, read = 0, 0 + mask = (1 << (self.width * 8)) - 1 + rela = section["type"] == 0x60000002 + while read < count: + group_offset = section["offset"] + position + number, flags = integer(), integer() + self.packed_groups.append({"file_offset": group_offset, "count": number, "flags": flags}) + delta = integer() if flags & 2 else None + info = integer() if flags & 1 else None + addend_delta = integer() if flags & 4 else None + for index in range(number): + address = (address + (delta if delta is not None else integer())) & mask + entry_info = (info if info is not None else integer()) & mask + if flags & 8: + addend = (addend + (addend_delta if addend_delta is not None else integer())) & mask + else: + addend = 0 + kind = entry_info & (0xFFFFFFFF if self.width == 8 else 255) + symbol = entry_info >> (32 if self.width == 8 else 8) + self.packed_records[address] = {"file_offset": group_offset, "entry_index": index, "symbol": symbol, "encoding": "rela" if rela else "rel"} + if kind == relative and symbol == 0: + self.overlay[address] = (addend if rela else self.pointer(address), "APS2") + read += 1 + assert read == count and not any(packed[position:]), "Only linker zero padding may remain" def unpack(self, fmt, offset): return struct.unpack_from("<" + fmt, self.data, offset) @@ -133,6 +172,10 @@ class Elf: location = address + (index * 3 + slot) * self.width resolved, encoding = self.overlay[location] slots.append({"address": hex(location), "file_offset": hex(self.file_offset(location)), "raw_word": hex(self.pointer(location)), "resolved_word": hex(resolved), "encoding": encoding}) + if encoding == "APS2": + source = self.packed_records[location] + slots[-1].update({"logical_encoding": source["encoding"], "record_file_offset": source["file_offset"], + "packing": {"format": "aps2", "group_entry_index": source["entry_index"]}}) for slot, expected in zip(slots, (name, descriptor)): actual = self.zero(self.data, self.file_offset(int(slot["resolved_word"], 16))) assert actual == mutf8(expected), (actual, expected) @@ -148,23 +191,22 @@ def elf_oracle(path, expected, conscrypt): data = limited(path) elf = Elf(data) tables = [] - if "APS2" not in elf.encodings: - if conscrypt: - assert hashlib.sha256(data).hexdigest() == "a3b78516ca2398d4ab233c71cf5c41ebb14c36ce157ebbaf1e5fd903e8220181" - row = elf.entry(0x2ADA68, 0, "EVP_PKEY_new_RSA", "([B[B[B[B[B[B[B[B)J") - assert row["function_address"] == "0xfa11c" - tables.append(row) - else: - for table in expected["tables"]: - symbol = elf.symbols[table["symbol"]] - section = elf.sections[symbol["section_index"]] - assert section["name"] == table["section"] and section["flags"] & 1 - assert symbol["size"] == table["entries"] * 3 * elf.width - rows = [r for r in expected["native_declarations"] if r.get("table_symbol") == table["symbol"]] - by_index = {r["entry_index"]: r for r in rows} - for index, row in sorted(by_index.items()): - tables.append(elf.entry(symbol["address"], index, row["name"], row["descriptor"], row["function_symbol"])) - return {"path": str(path), "sha256": hashlib.sha256(data).hexdigest(), "machine": elf.machine, "pointer_bytes": elf.width, "relocation_encodings": sorted(elf.encodings), "table_scope": "known source symbols/entries only; APS2 deliberately not independently decoded here", "tables": tables, "exports": {name: {**value, "address": hex(value["address"])} for name, value in sorted(elf.exports.items())}, "loads": elf.loads} + if conscrypt: + assert hashlib.sha256(data).hexdigest() == "a3b78516ca2398d4ab233c71cf5c41ebb14c36ce157ebbaf1e5fd903e8220181" + row = elf.entry(0x2ADA68, 0, "EVP_PKEY_new_RSA", "([B[B[B[B[B[B[B[B)J") + assert row["function_address"] == "0xfa11c" + tables.append(row) + else: + for table in expected["tables"]: + symbol = elf.symbols[table["symbol"]] + section = elf.sections[symbol["section_index"]] + assert section["name"] == table["section"] and section["flags"] & 1 + assert symbol["size"] == table["entries"] * 3 * elf.width + rows = [r for r in expected["native_declarations"] if r.get("table_symbol") == table["symbol"]] + by_index = {r["entry_index"]: r for r in rows} + for index, row in sorted(by_index.items()): + tables.append(elf.entry(symbol["address"], index, row["name"], row["descriptor"], row["function_symbol"])) + return {"path": str(path), "sha256": hashlib.sha256(data).hexdigest(), "machine": elf.machine, "pointer_bytes": elf.width, "relocation_encodings": sorted(elf.encodings), "table_scope": "known source symbols/entries only", "tables": tables, "exports": {name: {**value, "address": hex(value["address"])} for name, value in sorted(elf.exports.items())}, "loads": elf.loads} def dex_oracle(path, expected): diff --git a/tests/fixtures/android/jni-bindings/source-expectations.json b/tests/fixtures/android/jni-bindings/source-expectations.json index cc89c0ec30..2ab82c4454 100644 --- a/tests/fixtures/android/jni-bindings/source-expectations.json +++ b/tests/fixtures/android/jni-bindings/source-expectations.json @@ -122,6 +122,6 @@ "x86": "REL" }, "relr": "DT_RELR; verify actual tags and encoded slots from readelf and bytes", - "aps2": "Android APS2 packed REL/RELA; deliberately unsupported by the REA feature, with explicit coverage unknown" + "aps2": "Android APS2 packed REL/RELA; source-known table candidates and group/member provenance survive; unresolved symbols remain unknown" } } diff --git a/tests/fixtures/android/jni-bindings/verify-aps2.py b/tests/fixtures/android/jni-bindings/verify-aps2.py new file mode 100644 index 0000000000..e9a7896336 --- /dev/null +++ b/tests/fixtures/android/jni-bindings/verify-aps2.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python3 +"""Compare real LLD APS2/ordinary ELF witnesses against source symbols and bytes. + +Requires GCC, LLD and a built REA. Never loads or executes a produced library. +""" +import argparse +import hashlib +import json +import subprocess +import tempfile +from pathlib import Path + +from oracle import Elf + + +def run(*args): + return subprocess.run(args, check=True, capture_output=True, text=True).stdout + + +def verify(root, cc, lld, node): + source = Path(__file__).with_name("aps2.c") + module = (root / "dist/domain/android/androidJniLibrary.js").as_uri() + inspect = ( + 'import {readFile} from "node:fs/promises"; ' + f'import {{inspectAndroidJniLibrary}} from {json.dumps(module)}; ' + 'console.log(JSON.stringify(await inspectAndroidJniLibrary(await readFile(process.argv[1]))));' + ) + results = [] + qualification = {"variants": {}} + with tempfile.TemporaryDirectory(prefix="rea-aps2-") as temporary: + directory = Path(temporary) + for bits, machine in ((32, "elf_i386"), (64, "elf_x86_64")): + obj = directory / f"library{bits}.o" + run(cc, f"-m{bits}", "-fPIC", "-ffreestanding", "-c", str(source), "-o", str(obj)) + for packing in ("none", "android", "android+relr"): + library = directory / f"{packing}{bits}.so" + run(lld, "-m", machine, "-shared", f"--pack-dyn-relocs={packing}", "--no-use-android-relr-tags", str(obj), "-o", str(library)) + data = library.read_bytes() + digest = hashlib.sha256(data).hexdigest() + elf = Elf(data) + if packing == "android": + assert "APS2" in elf.encodings + assert any(group["flags"] & 2 for group in elf.packed_groups), "Producer must emit grouped offsets" + if bits == 64: + assert any(not group["flags"] & 8 for group in elf.packed_groups), "Producer must emit an addend-reset group" + elif packing == "android+relr": + assert {"APS2", "RELR"} <= elf.encodings + expected = [elf.entry(elf.symbols["methods"]["address"], index, name, signature, name) + for index, (name, signature) in enumerate(( + ("first", "()V"), ("second", "(I)V"), ("third", "(J)V"), ("fourth", "([B)V")))] + qualification["variants"].setdefault(packing, {"libraries": {}})["libraries"][str(bits)] = { + "path": str(library), "sha256": digest, "pointer_bytes": elf.width, "machine": elf.machine, + "exports": {name: {**symbol, "address": hex(symbol["address"])} for name, symbol in elf.exports.items()}, + "tables": expected, "loads": elf.loads, + } + observed = json.loads(run(node, "--input-type=module", "-e", inspect, str(library))) + assert observed["ok"], observed + facts = observed["value"] + entries = [entry for table in facts["tables"] for entry in table["entries"]] + assert len(entries) == len(expected), facts + assert facts["coverage"]["relocations_status"] == "partial", facts + assert any("unresolved" in reason for reason in facts["unknowns"]), facts + for actual, wanted in zip(entries, expected): + assert (actual["name"], actual["descriptor"], actual["function_address"]) == ( + wanted["name"], wanted["descriptor"], wanted["function_address"]) + for slot, oracle in zip(actual["slots"], wanted["slots"]): + assert slot["address"] == oracle["address"] + assert slot["file_offset"] == int(oracle["file_offset"], 16) + assert slot["value"] == oracle["resolved_word"] + expected_encoding = ("rela" if bits == 64 else "rel") if oracle["encoding"] == "APS2" else oracle["encoding"].lower() + assert slot["relocation"]["encoding"] == expected_encoding + if oracle["encoding"] == "APS2": + provenance = elf.packed_records[int(slot["address"], 16)] + assert slot["relocation"]["record_file_offset"] == provenance["file_offset"] + assert slot["relocation"]["packing"] == {"format": "aps2", "group_entry_index": provenance["entry_index"]} + else: + assert "packing" not in slot["relocation"] + evidence = json.loads(run(node, str(root / "scripts/rea.mjs"), + "inspect-native-initializers", str(library), "--json")) + report = evidence["normalized_result"] + constructor = next(entry for entry in report["entries"] if entry["kind"] == "init_array") + assert constructor["resolution"] == ("relr" if packing == "android+relr" else "relative_addend"), report + assert constructor["value_va"] == hex(elf.symbols["initialize"]["address"]) + assert hashlib.sha256(library.read_bytes()).hexdigest() == digest + results.append({"bits": bits, "packing": packing, "jni_entries": len(entries), + "initializer": constructor["value_va"], "sha256": digest}) + (directory / "full-expectations.json").write_text(json.dumps(qualification)) + checked = json.loads(run(node, str(root / "tests/support/analysis-efficiency/android-jni-library.mjs"), + "--parser", str(root / "dist/domain/android/androidJniLibrary.js"), + "--fixture-root", str(directory), "--output", str(directory / "qualified.json"))) + assert checked["libraries"] == 6 and checked["verified_entries"] == 24, checked + return {"variants": results, "qualification": checked} + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--runtime-root", type=Path, default=Path(__file__).resolve().parents[4]) + parser.add_argument("--cc", default="gcc") + parser.add_argument("--lld", default="ld.lld") + parser.add_argument("--node", default="node") + args = parser.parse_args() + print(json.dumps(verify(args.runtime_root.resolve(), args.cc, args.lld, args.node), indent=2)) diff --git a/tests/fixtures/android/jniElf.ts b/tests/fixtures/android/jniElf.ts index f76686da5b..1400bb2604 100644 --- a/tests/fixtures/android/jniElf.ts +++ b/tests/fixtures/android/jniElf.ts @@ -1,3 +1,5 @@ +import { aps2Relocations } from "../elfAndroidPacked.js"; + type FixtureEncoding = "rel" | "rela" | "relr"; const fixtureWriter = (bytes: Buffer, bits: 32 | 64, little: boolean) => { @@ -91,6 +93,7 @@ const writeDirectory = ( dynamicBytes: number; functions: readonly bigint[]; unnamedData: boolean; + packed: boolean; }, writer: FixtureWriter, ) => { @@ -105,6 +108,7 @@ const writeDirectory = ( dynamicBytes, functions, unnamedData, + packed, } = input; const { u16, u32, ptr } = writer; const dynstr = Buffer.from("\0JNI_OnLoad\0Java_fixture_Owner_exported\0"); @@ -146,13 +150,21 @@ const writeDirectory = ( [1, 3, 0x500, word * 6, 0, 0, 0], [1, 3, 0x600, word * 3, 0, 0, 0], [ - encoding === "rela" ? 4 : encoding === "rel" ? 9 : 19, + packed + ? encoding === "rela" + ? 0x60000002 + : 0x60000001 + : encoding === "rela" + ? 4 + : encoding === "rel" + ? 9 + : 19, 2, 0x800, relocationSize, 7, 0, - encoding === "relr" ? word : rowWidth, + packed ? 1 : encoding === "relr" ? word : rowWidth, ], [6, 3, 0xc00, dynamicBytes, 8, 0, word * 2], [11, 2, 0x1000, symbolWidth * 3, 8, 1, symbolWidth], @@ -212,7 +224,7 @@ export const jniElfFixture = ( readonly encoding?: "rel" | "rela" | "relr"; readonly little?: boolean; readonly symbolicFirstFunction?: boolean; - readonly packed?: boolean; + readonly packing?: "aps1" | "aps2"; readonly unnamedData?: boolean; } = {}, ) => { @@ -259,53 +271,69 @@ export const jniElfFixture = ( ] as const; const relative = machine === 183 ? 1027 : machine === 40 ? 23 : 8; const rowWidth = word * (encoding === "rela" ? 3 : 2); + const records = slots.map(([at, value], i) => { + const symbolic = options.symbolicFirstFunction === true && i === 2; + const kind = symbolic + ? machine === 183 + ? 1025 + : machine === 40 + ? 21 + : 6 + : relative; + const info = + bits === 64 + ? BigInt(kind) | (symbolic ? 1n << 32n : 0n) + : BigInt(kind | (symbolic ? 1 << 8 : 0)); + return { at, offset: base + BigInt(at), info, addend: value }; + }); if (encoding === "relr") { slots.forEach(([at, value]) => ptr(at, value)); [base + 0x500n, 63n, base + 0x600n, 7n].forEach((value, i) => ptr(0x800 + i * word, value), ); } else - slots.forEach(([at, value], i) => { + records.forEach(({ at, offset, info, addend }, i) => { const row = 0x800 + i * rowWidth; - const symbolic = options.symbolicFirstFunction === true && i === 2; - const kind = symbolic - ? machine === 183 - ? 1025 - : machine === 40 - ? 21 - : 6 - : relative; - ptr(row, base + BigInt(at)); - ptr( - row + word, - bits === 64 - ? BigInt(kind) | (symbolic ? 1n << 32n : 0n) - : BigInt(kind | (symbolic ? 1 << 8 : 0)), - ); - if (encoding === "rela") ptr(row + word * 2, value); - else ptr(at, value); + ptr(row, offset); + ptr(row + word, info); + if (encoding === "rela") ptr(row + word * 2, addend); + else ptr(at, addend); }); - const relocationSize = - encoding === "relr" ? word * 4 : rowWidth * slots.length; + let relocationSize = encoding === "relr" ? word * 4 : rowWidth * slots.length; + if (options.packing !== undefined) { + if (encoding === "relr") + throw new Error("Packed fixture requires REL or RELA"); + const packed = aps2Relocations(records, encoding); + if (options.packing === "aps1") packed.write("APS1", 0); + bytes.set(packed, 0x800); + relocationSize = packed.length; + } const tags: [bigint, bigint][] = - encoding === "relr" + options.packing !== undefined ? [ - [36n, base + 0x800n], - [35n, BigInt(relocationSize)], - [37n, BigInt(word)], + [encoding === "rela" ? 0x60000011n : 0x6000000fn, base + 0x800n], + [ + encoding === "rela" ? 0x60000012n : 0x60000010n, + BigInt(relocationSize), + ], ] - : encoding === "rela" + : encoding === "relr" ? [ - [7n, base + 0x800n], - [8n, BigInt(relocationSize)], - [9n, BigInt(rowWidth)], + [36n, base + 0x800n], + [35n, BigInt(relocationSize)], + [37n, BigInt(word)], ] - : [ - [17n, base + 0x800n], - [18n, BigInt(relocationSize)], - [19n, BigInt(rowWidth)], - ]; - if (options.packed === true) tags.push([0x60000011n, base + 0x800n]); + : encoding === "rela" + ? [ + [7n, base + 0x800n], + [8n, BigInt(relocationSize)], + [9n, BigInt(rowWidth)], + ] + : [ + [17n, base + 0x800n], + [18n, BigInt(relocationSize)], + [19n, BigInt(rowWidth)], + ]; tags.push([0n, 0n]); tags.forEach(([tag, value], i) => { ptr(0xc00 + i * word * 2, tag); @@ -325,6 +353,7 @@ export const jniElfFixture = ( dynamicBytes, functions, unnamedData: options.unnamedData === true, + packed: options.packing !== undefined, }, writer, ); diff --git a/tests/fixtures/elfAndroidPacked.ts b/tests/fixtures/elfAndroidPacked.ts new file mode 100644 index 0000000000..ed9f138080 --- /dev/null +++ b/tests/fixtures/elfAndroidPacked.ts @@ -0,0 +1,43 @@ +/** Independent wire writer for deliberately selected APS2 group fields. */ +export const aps2Table = ( + values: readonly (number | bigint)[], + padding = 0, +): Buffer => { + const bytes = [...Buffer.from("APS2")]; + for (const value of values) { + let remaining = BigInt(value); + for (;;) { + const byte = Number(remaining & 127n); + remaining >>= 7n; + const done = + (remaining === 0n && (byte & 64) === 0) || + (remaining === -1n && (byte & 64) !== 0); + bytes.push(byte | (done ? 0 : 128)); + if (done) break; + } + } + return Buffer.concat([Buffer.from(bytes), Buffer.alloc(padding)]); +}; + +/** Write selected ordinary records as one ungrouped APS2 run. */ +export const aps2Relocations = ( + records: readonly { + readonly offset: bigint; + readonly info: bigint; + readonly addend?: bigint; + }[], + encoding: "rel" | "rela", +): Buffer => { + const fields = [BigInt(records.length), 0n]; + if (records.length > 0) + fields.push(BigInt(records.length), encoding === "rela" ? 8n : 0n); + let previousOffset = 0n, + previousAddend = 0n; + for (const { offset, info, addend = 0n } of records) { + fields.push(offset - previousOffset, BigInt.asIntN(64, info)); + if (encoding === "rela") fields.push(addend - previousAddend); + previousOffset = offset; + previousAddend = addend; + } + return aps2Table(fields); +}; diff --git a/tests/fixtures/il2cpp/binary.ts b/tests/fixtures/il2cpp/binary.ts index aecbf21457..f02b9af8fb 100644 --- a/tests/fixtures/il2cpp/binary.ts +++ b/tests/fixtures/il2cpp/binary.ts @@ -1,3 +1,5 @@ +import { aps2Relocations } from "../elfAndroidPacked.js"; + /** * Synthetic thin arm64 Mach-O holding the IL2CPP registration tables for the * metadata fixture: image names, code-generation modules, their method @@ -485,12 +487,58 @@ export const createSplitPointerPeFixture = (zeroTail = true): Buffer => { return Buffer.concat([header, payload]); }; +const packElfFixture = ({ + file, + rela, + tags, + dynamicOffset, + packing, +}: { + readonly file: Buffer; + readonly rela: Buffer; + readonly tags: readonly (readonly [bigint, bigint])[]; + readonly dynamicOffset: number; + readonly packing: "aps1" | "aps2"; +}): Buffer => { + const packed = aps2Relocations( + Array.from({ length: rela.length / 24 }, (_, index) => ({ + offset: rela.readBigUInt64LE(index * 24), + info: rela.readBigUInt64LE(index * 24 + 8), + addend: rela.readBigInt64LE(index * 24 + 16), + })), + "rela", + ); + if (packing === "aps1") packed.write("APS1"); + // Replace the dead RELA bytes in place so file-backed ranges and any BSS + // pointers remain identical to the unpacked carrier. + if (packed.length > rela.length) + throw new Error( + "Packed relocations exceed the reserved RELA fixture range", + ); + file.fill(0, SIZE, SIZE + rela.length); + packed.copy(file, SIZE); + tags.forEach(([tag], index) => { + const at = dynamicOffset + index * 16; + if (tag === 7n) { + file.writeBigUInt64LE(0x60000011n, at); + } else if (tag === 8n) { + file.writeBigUInt64LE(0x60000012n, at); + file.writeBigUInt64LE(BigInt(packed.length), at + 8); + } else if (tag === 9n) { + // A harmless ignored tag keeps the remaining directory positions stable. + file.writeBigUInt64LE(21n, at); + file.writeBigUInt64LE(0n, at + 8); + } + }); + return file; +}; + /** Android ELF relocation tables appended after the fixture image. */ export interface ElfFixtureOptions { /** e_machine; 183 is AArch64. */ readonly machine?: number; - /** Add DT_ANDROID_RELA, the packed APS2 encoding. */ - readonly androidPacked?: boolean; + /** Replace RELA with a packed table; APS1 deliberately stays unsupported. */ + readonly androidPacked?: "aps1" | "aps2"; /** Omit PT_DYNAMIC. */ readonly dynamic?: boolean; /** Give every relocation a symbol index, so none is RELATIVE-only. */ @@ -628,7 +676,6 @@ export const createElfFixture = ( const tagCount = 4 + (options.duplicateRela === true ? 1 : 0) + - (options.androidPacked === true ? 1 : 0) + symbolTableTags(symbols, 0n).length + (hash.length === 0 ? 0 : 1); const fileSize = @@ -661,9 +708,6 @@ export const createElfFixture = ( : []), [8n, options.relaSize ?? BigInt(rela.length)], [9n, options.relaEntry ?? 24n], - ...(options.androidPacked === true - ? [[0x60000011n, 0n] as [bigint, bigint]] - : []), ...symbolTableTags( symbols, FIXTURE_BINARY_BASE + BigInt(fileSize - symbols.length - hash.length), @@ -718,7 +762,15 @@ export const createElfFixture = ( file.writeBigUInt64LE(BigInt(dynamic.length), 152); file.writeBigUInt64LE(BigInt(dynamic.length), 160); } - return file; + return options.androidPacked === undefined + ? file + : packElfFixture({ + file, + rela, + tags, + dynamicOffset: SIZE + rela.length, + packing: options.androidPacked, + }); }; /** A code-loaded type table plus isolated one-entry candidate runs in disjoint ELF mappings. */ diff --git a/tests/fixtures/nativeAps2Initializers.ts b/tests/fixtures/nativeAps2Initializers.ts new file mode 100644 index 0000000000..f1ade1c16c --- /dev/null +++ b/tests/fixtures/nativeAps2Initializers.ts @@ -0,0 +1,42 @@ +import { elf64PieInitializers } from "../../src/native/initializers/NativeInitializers.fixture.js"; +import { aps2Table } from "./elfAndroidPacked.js"; + +/** Same known PIE slots as the ordinary fixture, with RELA packed as APS2. */ +export const aps2InitializerImage = (): Buffer => { + const base = elf64PieInitializers(); + const packed = aps2Table([ + 4, + 0, + 4, + 8, + 0, + 0, + 0, + 0x200, + 8, + 0x100, + 8, + 8, + 0x10, + 8, + (1n << 32n) | 1n, + -0x110, + ]); + const bytes = Buffer.concat([base, packed]); + bytes.writeBigUInt64LE(BigInt(bytes.length), 64 + 32); + bytes.writeBigUInt64LE(BigInt(bytes.length), 64 + 40); + const dynamic = 0x300 + 8 * 16; + bytes.writeBigUInt64LE(0x60000011n, dynamic); + bytes.writeBigUInt64LE(BigInt(base.length), dynamic + 8); + bytes.writeBigUInt64LE(0x60000012n, dynamic + 16); + bytes.writeBigUInt64LE(BigInt(packed.length), dynamic + 24); + bytes.writeBigUInt64LE(21n, dynamic + 32); + bytes.writeBigUInt64LE(0n, dynamic + 40); + const section = 0x800 + 6 * 64; + bytes.writeUInt32LE(0x60000002, section + 4); + bytes.writeBigUInt64LE(BigInt(base.length), section + 16); + bytes.writeBigUInt64LE(BigInt(base.length), section + 24); + bytes.writeBigUInt64LE(BigInt(packed.length), section + 32); + bytes.writeBigUInt64LE(1n, section + 56); + return bytes; +}; diff --git a/tests/support/analysis-efficiency/android-jni-library.mjs b/tests/support/analysis-efficiency/android-jni-library.mjs index ae457f5e35..89f97e90cc 100644 --- a/tests/support/analysis-efficiency/android-jni-library.mjs +++ b/tests/support/analysis-efficiency/android-jni-library.mjs @@ -45,52 +45,57 @@ async function verify(expected, variant) { for (const row of facts.exports) assert.equal(row.address, expected.exports[row.name].address); const rows = facts.tables.flatMap((table) => table.entries); - if (variant === "aps2") { - assert.equal(facts.coverage.tables_status, "unknown"); - assert.equal(rows.length, 0); - assert.ok(facts.unknowns.some((unknown) => unknown.includes("APS2"))); - } else - for (const row of expected.tables) { - const actual = rows.find( - (candidate) => candidate.slots[0]?.address === row.slots[0]?.address, - ); - assert.ok( - actual, - `missing ${expected.path} ${row.name} at ${row.slots[0]?.address}`, - ); - assert.equal(actual.name, row.name); - assert.equal(actual.descriptor, row.descriptor); - assert.equal(actual.function_address, row.function_address); - const code = - expected.machine === 40 - ? BigInt(row.function_address) & ~1n - : BigInt(row.function_address); - assert.equal(actual.code_address, `0x${code.toString(16)}`); - const load = expected.loads.find( - (load) => - load.type === 1 && - (load.flags & 1) !== 0 && - code >= BigInt(load.address) && - code < BigInt(load.address + load.file_bytes), - ); - assert.ok(load, "independent executable segment must cover the function"); + for (const row of expected.tables) { + const actual = rows.find( + (candidate) => candidate.slots[0]?.address === row.slots[0]?.address, + ); + assert.ok( + actual, + `missing ${expected.path} ${row.name} at ${row.slots[0]?.address}`, + ); + assert.equal(actual.name, row.name); + assert.equal(actual.descriptor, row.descriptor); + assert.equal(actual.function_address, row.function_address); + const code = + expected.machine === 40 + ? BigInt(row.function_address) & ~1n + : BigInt(row.function_address); + assert.equal(actual.code_address, `0x${code.toString(16)}`); + const load = expected.loads.find( + (load) => + load.type === 1 && + (load.flags & 1) !== 0 && + code >= BigInt(load.address) && + code < BigInt(load.address + load.file_bytes), + ); + assert.ok(load, "independent executable segment must cover the function"); + assert.equal( + actual.function_file_offset, + Number(code - BigInt(load.address) + BigInt(load.offset)), + ); + for (const [index, slot] of row.slots.entries()) { + assert.equal(actual.slots[index]?.address, slot.address); assert.equal( - actual.function_file_offset, - Number(code - BigInt(load.address) + BigInt(load.offset)), + actual.slots[index]?.file_offset, + Number(BigInt(slot.file_offset)), ); - for (const [index, slot] of row.slots.entries()) { - assert.equal(actual.slots[index]?.address, slot.address); - assert.equal( - actual.slots[index]?.file_offset, - Number(BigInt(slot.file_offset)), + assert.equal(actual.slots[index]?.value, slot.resolved_word); + assert.equal( + actual.slots[index]?.relocation?.encoding, + slot.logical_encoding ?? slot.encoding.toLowerCase(), + ); + if (slot.packing !== undefined) { + assert.deepEqual( + actual.slots[index]?.relocation?.packing, + slot.packing, ); - assert.equal(actual.slots[index]?.value, slot.resolved_word); assert.equal( - actual.slots[index]?.relocation?.encoding, - slot.encoding.toLowerCase(), + actual.slots[index]?.relocation?.record_file_offset, + slot.record_file_offset, ); } } + } receipts.push({ path: expected.path, sha256: expected.sha256, @@ -98,7 +103,7 @@ async function verify(expected, variant) { elapsed_ms: elapsed, exports: facts.exports.length, entries: rows.length, - verified_entries: variant === "aps2" ? 0 : expected.tables.length, + verified_entries: expected.tables.length, coverage: facts.coverage, unknowns: facts.unknowns, }); diff --git a/tests/support/analysis-efficiency/android-jni.mjs b/tests/support/analysis-efficiency/android-jni.mjs index ccee3b6faa..92edb2dcfb 100644 --- a/tests/support/analysis-efficiency/android-jni.mjs +++ b/tests/support/analysis-efficiency/android-jni.mjs @@ -118,7 +118,17 @@ const expectedSlots = (candidate, row) => { assert.equal(slot.address, gold.address); assert.equal(slot.file_offset, Number(BigInt(gold.file_offset))); assert.equal(slot.value, gold.resolved_word); - assert.equal(slot.relocation?.encoding, gold.encoding.toLowerCase()); + assert.equal( + slot.relocation?.encoding, + gold.logical_encoding ?? gold.encoding.toLowerCase(), + ); + if (gold.packing !== undefined) { + assert.deepEqual(slot.relocation?.packing, gold.packing); + assert.equal( + slot.relocation?.record_file_offset, + gold.record_file_offset, + ); + } } }; @@ -157,14 +167,9 @@ function checkFixture(report, query) { assert.equal(library.sha256, gold.sha256); assert.equal(library.abi, library.path.split("/")[1]); assert.equal(library.failure, null); - if (values.variant === "aps2") { - assert.notEqual(library.coverage.tables_status, "complete"); - assert.ok(library.unknowns.some((text) => /APS2|packed/i.test(text))); - } else { - assert.ok(["complete", "partial"].includes(library.coverage.status)); - if (library.coverage.status === "partial") - assert.ok(library.unknowns.length > 0); - } + assert.ok(["complete", "partial"].includes(library.coverage.status)); + if (library.coverage.status === "partial") + assert.ok(library.unknowns.length > 0); } const selectedMethods = report.declarations.filter( (row) => @@ -671,11 +676,10 @@ async function qualifyRetainedRemoval(invoke) { (row) => row.table != null, ).length; } - if (values.variant !== "aps2") - assert.ok( - tableEntries > 0, - "Relocated slot evidence must remain available after APK removal", - ); + assert.ok( + tableEntries > 0, + "Relocated slot evidence must remain available after APK removal", + ); const proof = join(output, "retained-removal-evidence.json"); await invoke("export_evidence_bundle", { path: proof }); const bundle = JSON.parse(await readFile(proof, "utf8"));