refactor(elf): share bounded RISC-V attribute reads (#12515)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 20:41:56 +00:00
1 parent ad515e3531
commit aaa8e1cee0
3 files changed
+17 -19

No files matched your search

@@ -22,7 +22,6 @@
"src/domain/pe/peResourceInventory.ts": 7,
"src/domain/peSectionHeader.ts": 5,
"src/domain/pngVisualDiff.ts": 2,
"src/domain/riscvIsaDeclaration.ts": 1,
"src/dotnet/ManagedMethodBodyReader.ts": 5,
"src/ghidra/GhidraLoadImageValues.ts": 1,
"src/ghidra/extensions/GhidraExtensions.ts": 1,
+8 -7
View File
@@ -93,14 +93,15 @@ describe("RISC-V .riscv.attributes", () => {
});
it.each([true, false])(
"reads lengths in the image's byte order (little=%s)",
"reads lengths within the caller's byte view (little=%s)",
(little) => {
expect(
parseRiscvAttributes(
section([5, ...text("rv32i2p1_c2p0")], { little }),
little,
),
).toEqual({ ok: true, value: "rv32i2p1_c2p0" });
const bytes = section([5, ...text("rv32i2p1_c2p0")], { little });
const enclosing = Uint8Array.of(0, 1, ...bytes, 0);
const selected = enclosing.subarray(2, 2 + bytes.length);
expect(parseRiscvAttributes(selected, little)).toEqual({
ok: true,
value: "rv32i2p1_c2p0",
});
},
);
+9 -11
View File
@@ -1,3 +1,4 @@
import { BinaryReader } from "./binaryContainer.js";
import { readElfAttributeUleb } from "./elfAttributeUleb.js";
import type { RiscvIsaDeclaration } from "./binaryTargetTypes.js";
import {
@@ -64,20 +65,17 @@ export const parseRiscvAttributes = (
): Result<string | null, string> => {
if (bytes.length === 0 || bytes[0] !== FORMAT_VERSION)
return err("unsupported RISC-V attribute section format version");
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const readU32 = (offset: number): number =>
view.getUint32(offset, littleEndian);
const section = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const reader = new BinaryReader(section, littleEndian, "RISC-V attributes");
const readString = (
offset: number,
end: number,
): { readonly value: string; readonly next: number } | undefined => {
const stop = bytes.indexOf(0, offset);
return stop < 0 || stop >= end
const field = section.subarray(offset, end);
const stop = field.indexOf(0);
return stop < 0
? undefined
: {
value: Buffer.from(bytes.subarray(offset, stop)).toString("latin1"),
next: stop + 1,
};
: { value: field.toString("latin1", 0, stop), next: offset + stop + 1 };
};
let arch: string | null = null;
@@ -86,7 +84,7 @@ export const parseRiscvAttributes = (
while (position < bytes.length) {
if (bytes.length - position < 4)
return err("truncated RISC-V attribute subsection length");
const length = readU32(position);
const length = reader.u32(position);
if (length < 4 || length > bytes.length - position)
return err("RISC-V attribute subsection length does not fit the section");
const end = position + length;
@@ -114,7 +112,7 @@ export const parseRiscvAttributes = (
);
if (end - scope.next < 4)
return err("truncated RISC-V attribute scope length");
const scopeLength = readU32(scope.next);
const scopeLength = reader.u32(scope.next);
if (
scopeLength < scope.next + 4 - scopeStart ||
scopeLength > end - scopeStart