mirror of
https://github.com/morluto/rea.git
synced 2026-11-18 20:53:00 +00:00
fix(native): index only address-bearing Mach-O initializer symbols (#12070)
This commit is contained in:
1 parent
cb8096d522
commit
884fda99f0
4 files changed
+117
-3
No files matched your search
@@ -320,8 +320,10 @@ const readSymbols = (
|
||||
for (let index = 0; index < count; index += 1) {
|
||||
const at = table.symoff + index * width;
|
||||
const type = scan.bytes[at + 4] ?? 0;
|
||||
// Skip debugger stabs and undefined (N_UNDF) entries.
|
||||
if ((type & 0xe0) !== 0 || (type & 0x0e) === 0) continue;
|
||||
// Only N_ABS and N_SECT carry addresses. N_INDR's n_value is a string
|
||||
// table index, and N_PBUD is still undefined in this image.
|
||||
const kind = type & 0x0e;
|
||||
if ((type & 0xe0) !== 0 || (kind !== 0x02 && kind !== 0x0e)) continue;
|
||||
const strx = little
|
||||
? scan.bytes.readUInt32LE(at)
|
||||
: scan.bytes.readUInt32BE(at);
|
||||
|
||||
@@ -673,6 +673,55 @@ export const machoX8664ObjectInitializers = (): Buffer => {
|
||||
return file;
|
||||
};
|
||||
|
||||
/**
|
||||
* LLVM 19.1.7 golden: llvm-mc -triple=x86_64-apple-macosx -filetype=obj input.s.
|
||||
* Producer source (Lstartup is a temporary label, absent from the symbol table):
|
||||
* .text
|
||||
* .space 8, 0x90
|
||||
* Lstartup: ret
|
||||
* .globl _alias
|
||||
* .set _alias, _external
|
||||
* .section __DATA,__mod_init_func,mod_init_funcs
|
||||
* .p2align 3
|
||||
* .quad Lstartup
|
||||
* llvm-readobj --symbols --relocations confirms N_INDR _alias has n_value=8
|
||||
* (the string index of _external), and the slot has a section-relative relocation.
|
||||
*/
|
||||
export const machoIndirectInitializerGolden = (): Buffer =>
|
||||
Buffer.from(
|
||||
"z/rt/gcAAAEDAAAAAQAAAAMAAABQAQAAAAAAAAAAAAAZAAAA6AAAAAAAAAAAAAAAAAAAAAAAAAAA" +
|
||||
"AAAAAAAAABgAAAAAAAAAcAEAAAAAAAAYAAAAAAAAAAcAAAAHAAAAAgAAAAAAAABfX3RleHQAAAAA" +
|
||||
"AAAAAAAAX19URVhUAAAAAAAAAAAAAAAAAAAAAAAACQAAAAAAAABwAQAAAAAAAAAAAAAAAAAAAAQA" +
|
||||
"gAAAAAAAAAAAAAAAAF9fbW9kX2luaXRfZnVuYwBfX0RBVEEAAAAAAAAAAAAAEAAAAAAAAAAIAAAA" +
|
||||
"AAAAAIABAAADAAAAiAEAAAEAAAAJAAAAAAAAAAAAAAAAAAAAAgAAABgAAACQAQAAAgAAALABAAAY" +
|
||||
"AAAACwAAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
|
||||
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACQkJCQkJCQkMMAAAAAAAAACAAAAAAAAAAAAAAAAQAA" +
|
||||
"BgEAAAALAAAACAAAAAAAAAAIAAAAAQAAAAAAAAAAAAAAAF9hbGlhcwBfZXh0ZXJuYWwAAAAAAAAA",
|
||||
"base64",
|
||||
);
|
||||
|
||||
/** An n_value string index collides with a real initializer address. */
|
||||
export const machoInitializerSymbolKinds = (type: number): Buffer => {
|
||||
const file = Buffer.alloc(0x400);
|
||||
machoX8664ObjectInitializers().copy(file);
|
||||
// The second, section-relative pointer also selects _startup at 0x10.
|
||||
file.writeBigUInt64LE(0x10n, 0x228);
|
||||
// First symbol: an indirect alias to the unresolved _external, whose string
|
||||
// index (0x10) is not the address of _startup. Defined kinds serve as controls.
|
||||
file.writeUInt8(type | 1, 0x254);
|
||||
file.writeUInt8(type === 0x0e ? 1 : 0, 0x255);
|
||||
file.writeBigUInt64LE(0x10n, 0x258);
|
||||
file.writeUInt32LE(0x20, 0x260);
|
||||
const strings = Buffer.alloc(0x30);
|
||||
strings.write("_alias", 1);
|
||||
strings.write("_external", 0x10);
|
||||
strings.write("_startup", 0x20);
|
||||
file.writeUInt32LE(0x300, 280);
|
||||
file.writeUInt32LE(strings.length, 284);
|
||||
strings.copy(file, 0x300);
|
||||
return file;
|
||||
};
|
||||
|
||||
/** Sparse bind runs bracket the initializer slots without binding any of them. */
|
||||
export const machoSparseBindInitializers = (count: number): Buffer => {
|
||||
const uleb = (value: number): Buffer => {
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
elf64AliasedInitializers,
|
||||
elfUnmappedInitializerTargets,
|
||||
machoArm64Initializers,
|
||||
machoIndirectInitializerGolden,
|
||||
} from "../../src/native/initializers/NativeInitializers.fixture.js";
|
||||
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../support/cli/cliFixture.js";
|
||||
@@ -47,6 +48,33 @@ cliTest(
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"does not name an initializer from an LLVM indirect-symbol string index",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-native-init-indirect-cli-");
|
||||
const path = join(root, "program.o");
|
||||
const bytes = machoIndirectInitializerGolden();
|
||||
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.entries).toMatchObject([
|
||||
{
|
||||
value_va: "0x8",
|
||||
value_file_offset: 376,
|
||||
symbol: null,
|
||||
resolution: "raw",
|
||||
},
|
||||
]);
|
||||
expect(report.artifact.sha256).toBe(
|
||||
"dc67f456f3b98a7e2279971a5049b089668fadbbd75f0c4e630383d413f713bf",
|
||||
);
|
||||
expect((await readFile(path)).equals(bytes)).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"keeps an unrelocated ELF alias's value and source identity through the CLI",
|
||||
async ({ cli }) => {
|
||||
|
||||
@@ -2,10 +2,45 @@ import { symlink, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createNativeInitializersService } from "../../../src/composition/binaryDiagnostics.js";
|
||||
import { elf64PieInitializers } from "../../../src/native/initializers/NativeInitializers.fixture.js";
|
||||
import {
|
||||
elf64PieInitializers,
|
||||
machoInitializerSymbolKinds,
|
||||
} from "../../../src/native/initializers/NativeInitializers.fixture.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
describe("native initializer stable filesystem boundary", () => {
|
||||
it.each([
|
||||
{ kind: "N_INDR", type: 0x0a, symbol: "_startup" },
|
||||
{ kind: "N_PBUD", type: 0x0c, symbol: "_startup" },
|
||||
{ kind: "N_ABS", type: 0x02, symbol: "_alias" },
|
||||
{ kind: "N_SECT", type: 0x0e, symbol: "_alias" },
|
||||
])(
|
||||
"uses address-bearing symbols for initializer names ($kind)",
|
||||
async ({ type, symbol }) => {
|
||||
const root = await createTestTempDirectory("rea-native-init-symbol-");
|
||||
const path = join(root, "program.o");
|
||||
await writeFile(path, machoInitializerSymbolKinds(type));
|
||||
const result = await createNativeInitializersService().inspect({ path });
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.normalized_result).toMatchObject({
|
||||
format: "mach-o",
|
||||
entries: [
|
||||
{
|
||||
value_va: "0x10",
|
||||
symbol: { name: "_startup", trust: "observed-untrusted" },
|
||||
resolution: "symbol_addend",
|
||||
},
|
||||
{
|
||||
value_va: "0x10",
|
||||
symbol: { name: symbol, trust: "observed-untrusted" },
|
||||
resolution: "raw",
|
||||
},
|
||||
],
|
||||
coverage: { status: "complete", returned_entries: 2 },
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("reports non-file and malformed selections as caller input errors", async () => {
|
||||
const root = await createTestTempDirectory("rea-native-init-files-");
|
||||
const path = join(root, "program");
|
||||
|
||||
Reference in new issue
Block a user