fix(firmware): decode Intel HEX linear address crossings (#15074)

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
This commit is contained in:
ShuaixinHuangandShuaixinHuang authored and GitHub committed 2026-11-28 20:37:34 -05:00
1 parent adf61c7aba
commit 858a9cf51e
4 files changed
+58 -26

No files matched your search

+18 -17
View File
@@ -61,23 +61,24 @@ proven entry points.
## Facts, limitations and failures
| Situation | Outcome |
| ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ |
| Gaps between regions | Separate regions; never zero-filled |
| Out-of-order records | Sorted into regions |
| An address written twice with the same value | Accepted and counted in `duplicate_bytes` |
| An address written twice with different values | Fails, naming the address and the line spans of both writes |
| Checksum mismatch, odd hex length, non-hex character, byte count disagreeing with the line | Fails with the line, declared and computed values, and record type |
| Line longer than 521 (Intel) or 514 (S-record) characters, excluding the line ending | Fails before the line is decoded |
| Intel type `06` or higher, `S4`, wrong fixed length for Intel `01`–`05` or `S5`–`S9` | Fails |
| Missing Intel `01` or `S7`–`S9` | Regions are returned; coverage is unknown and the file may be truncated |
| A non-blank record after termination, or a second `S0` | Fails; blank lines are allowed anywhere |
| `S5`/`S6` count different from the preceding data records | Fails |
| Data records after the final `S5`/`S6` count | Regions are retained and a limitation identifies the count that does not cover all records |
| Termination type not matching the data address width | Reported as a limitation; the declared fact is kept |
| Conflicting repeated `03`/`05` start records | Fails; identical repeats are allowed |
| An Intel data record whose 16-bit offset wraps past `0xFFFF` | Fails: producers disagree on whether the address wraps or continues linearly |
| I16HEX data above `0xFFFFF`, or data crossing the S1/S2/S3 address width | Fails |
| Situation | Outcome |
| ------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
| Gaps between regions | Separate regions; never zero-filled |
| Out-of-order records | Sorted into regions |
| An address written twice with the same value | Accepted and counted in `duplicate_bytes` |
| An address written twice with different values | Fails, naming the address and the line spans of both writes |
| Checksum mismatch, odd hex length, non-hex character, byte count disagreeing with the line | Fails with the line, declared and computed values, and record type |
| Line longer than 521 (Intel) or 514 (S-record) characters, excluding the line ending | Fails before the line is decoded |
| Intel type `06` or higher, `S4`, wrong fixed length for Intel `01`–`05` or `S5`–`S9` | Fails |
| Missing Intel `01` or `S7`–`S9` | Regions are returned; coverage is unknown and the file may be truncated |
| A non-blank record after termination, or a second `S0` | Fails; blank lines are allowed anywhere |
| `S5`/`S6` count different from the preceding data records | Fails |
| Data records after the final `S5`/`S6` count | Regions are retained and a limitation identifies the count that does not cover all records |
| Termination type not matching the data address width | Reported as a limitation; the declared fact is kept |
| Conflicting repeated `03`/`05` start records | Fails; identical repeats are allowed |
| Intel data under an extended linear address (type `04`) that crosses `0xFFFF` in its offset | Continues linearly and wraps to address zero only at the 32-bit limit |
| Other Intel data records whose 16-bit offset wraps past `0xFFFF` | Fails: producers disagree on whether the address wraps or continues linearly |
| I16HEX data above `0xFFFFF`, or data crossing the S1/S2/S3 address width | Fails |
Lowercase hex, LF and CRLF line endings, and trailing spaces or tabs are
accepted. A CR not followed by LF is an invalid character. Failures apply to
+2 -2
View File
@@ -186,8 +186,8 @@ describe("Intel HEX malformed records", () => {
/line 1.*02.*0x0000.*0x0010/iu,
],
[
"offset wrap",
":020000040000FA\n:10FFF800000102030405060708090A0B0C0D0E0F81\n:00000001FF\n",
"segmented offset wrap",
":020000020000FC\n:10FFF800000102030405060708090A0B0C0D0E0F81\n:00000001FF\n",
/line 2.*0xfff8.*16 bytes.*wraps.*0xffff/iu,
],
[
+14 -6
View File
@@ -116,7 +116,7 @@ class StreamingHexRecordDecoder implements RegionDecoder<HexRecordImage> {
#dataRecordCount = 0;
#termination: DeclaredValue<string> | undefined;
#base = 0;
#baseFromSegment = false;
#baseRecord: "02" | "04" | undefined;
#segmentRecords = false;
#linearRecords = false;
#start: (DeclaredValue<number> & { readonly record: string }) | undefined;
@@ -219,7 +219,7 @@ class StreamingHexRecordDecoder implements RegionDecoder<HexRecordImage> {
const value = readBigEndian(record.data);
this.#segmentRecords ||= record.type === "02";
this.#linearRecords ||= record.type === "04";
this.#baseFromSegment = record.type === "02";
this.#baseRecord = record.type;
this.#base = record.type === "02" ? value * 16 : value * 0x1_0000;
return ok(undefined);
}
@@ -238,16 +238,24 @@ class StreamingHexRecordDecoder implements RegionDecoder<HexRecordImage> {
#intelData({ address, data }: HexRecord): Result<void, RegionDecodeFailure> {
this.#dataRecordCount += 1;
// The specification wraps within 64 KiB, but some producers continue
// linearly, so the address of the wrapped bytes is ambiguous.
const start = this.#base + address;
if (this.#baseRecord === "04") {
// Extended linear addresses wrap modulo 4 GiB. Only split a run
// when the record crosses the end of that address space.
const beforeWrap = 0x1_0000_0000 - start;
if (data.length <= beforeWrap) return this.#addData(start, data);
const first = this.#addData(start, data.subarray(0, beforeWrap));
return first.ok ? this.#addData(0, data.subarray(beforeWrap)) : first;
}
// Without an extended linear base, keep rejecting records whose
// interpretation differs between segmented and linear consumers.
if (address + data.length > 0x1_0000)
return err(
this.#failure(
`data at offset 0x${hex(address, 4)} with ${String(data.length)} bytes wraps past 0xffff; producers disagree on the wrapped address`,
),
);
const start = this.#base + address;
if (this.#baseFromSegment && start + data.length - 1 > 0xf_ffff)
if (this.#baseRecord === "02" && start + data.length - 1 > 0xf_ffff)
return err(
this.#failure(
`I16HEX data at 0x${hex(start, 8)} with ${String(data.length)} bytes exceeds the 20-bit address 0xfffff`,
+24 -1
View File
@@ -3,7 +3,10 @@ import { join } from "node:path";
import { expect, onTestFinished } from "vitest";
import { cliTest } from "../support/cli/cliFixture.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { INTEL_HEX_I32_GAP } from "../../src/domain/firmware/hexRecords.fixture.js";
import {
INTEL_HEX_I32_GAP,
intelHexRecord,
} from "../../src/domain/firmware/hexRecords.fixture.js";
import { parseEvidence } from "../../src/domain/evidence.js";
import { artifactFormatDescriptor } from "../../src/artifacts/artifactFormats.js";
import { artifactInspectionResultSchema } from "../../src/domain/artifactInspection.js";
@@ -25,6 +28,24 @@ const cases = [
"0x08000100-0x08000103.bin": Buffer.from("deadbeef", "hex"),
},
},
{
encoding: "intel-hex",
name: "linear-crossing.hex",
text: [
intelHexRecord(4, 0, [0x08, 0x00]),
intelHexRecord(0, 0xfffe, [1, 2, 3, 4]),
intelHexRecord(4, 0, [0xff, 0xff]),
intelHexRecord(0, 0xfffe, [5, 6, 7, 8]),
intelHexRecord(1, 0),
"",
].join("\n"),
root: { variant: "I32HEX" },
regions: {
"0x00000000-0x00000001.bin": Buffer.from([7, 8]),
"0x0800fffe-0x08010001.bin": Buffer.from([1, 2, 3, 4]),
"0xfffffffe-0xffffffff.bin": Buffer.from([5, 6]),
},
},
{
encoding: "srec",
name: "firmware.s37",
@@ -77,6 +98,8 @@ cliTest.for(cases)(
/not establish.*CPU.*byte order.*memory map.*start address.*executable/iu,
);
expect(limitation).toMatch(/gaps.*unwritten.*not zero/iu);
}
if (name === "firmware.hex") {
expect(
graph.occurrences.find((row) => row.logical_path === "."),
).toMatchObject({