fix(managed): admit ReadyToRun images built for non-Windows hosts (#1985)

ReadyToRun images compiled for Linux, macOS or the BSDs store the COFF machine XORed with IMAGE_FILE_MACHINE_NATIVE_OS_OVERRIDE (dotnet/runtime src/coreclr/inc/pedecoder.h). Every framework assembly in a Linux .NET install has machine 0xFD1D, so target admission and the managed PE reader rejected them as an unsupported architecture.

Decode the PE machine once in src/domain/peMachine.ts, replacing the two duplicated switches, and accept an OS override only on images with a CLI directory.
This commit is contained in:
Jeremy Levartovsky
2026-10-12 08:41:23 +08:00
committed by GitHub
parent 43f9c39dfb
commit 0d11f71e0d
6 changed files with 168 additions and 40 deletions
@@ -46,6 +46,14 @@ describe("managed artifact failure classification", () => {
]);
});
it("rejects a ReadyToRun OS-override machine without a CLI directory", () => {
const bytes = buildNativePeFixture();
bytes.writeUInt16LE(0x014c ^ 0x7b79, bytes.readUInt32LE(0x3c) + 4);
expect(() =>
inspectManagedArtifactBytes(bytes, managedPeFixtureTarget(bytes)),
).toThrow("Unsupported PE machine 0x7a35");
});
it("keeps malformed reference and attribute issues across inventory callers", () => {
const bytes = buildManagedPeFixture({
references: [
+14 -20
View File
@@ -1,4 +1,5 @@
import type { ManagedParseIssue } from "../domain/managed/managedArtifact.js";
import { decodePeMachine } from "../domain/peMachine.js";
import {
managedFailure,
ManagedReaderFailure,
@@ -100,25 +101,18 @@ const add = (left: number, right: number, scope: string): number => {
return result;
};
const architectureFor = (machine: number): ManagedPeLayout["architecture"] => {
switch (machine) {
case 0x014c:
return "x86";
case 0x8664:
return "x86_64";
case 0x01c0:
case 0x01c2:
case 0x01c4:
return "arm";
case 0xaa64:
return "arm64";
default:
throw managedFailure(
"invalid-directory",
"pe.machine",
`Unsupported PE machine 0x${machine.toString(16).padStart(4, "0")}`,
);
}
const architectureFor = (
machine: number,
managed: boolean,
): ManagedPeLayout["architecture"] => {
const decoded = decodePeMachine(machine);
if (decoded === undefined || (decoded.nativeOsOverride && !managed))
throw managedFailure(
"invalid-directory",
"pe.machine",
`Unsupported PE machine 0x${machine.toString(16).padStart(4, "0")}`,
);
return decoded.architecture;
};
/** Resolve one section identity for both exact reads and available-byte queries. */
@@ -398,7 +392,7 @@ export const readManagedPeLayout = (bytes: Buffer): ManagedPeLayout => {
}
return {
machine: coff.machine,
architecture: architectureFor(coff.machine),
architecture: architectureFor(coff.machine, cliDirectoryPresent),
optionalHeader: optional.optionalHeader,
sectionCount: coff.sectionCount,
characteristics: coff.characteristics,