fix(artifacts): report S-record data after the final count (#14260)

* fix(artifacts): reject stale S-record data counts

* docs(artifacts): explain the final S-record count check

* fix(artifacts): retain data beyond the final S-record count

---------

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
Co-authored-by: ShuaixinHuang <ShuaixinHuang@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-11-23 23:37:06 -05:00
1 parent 78d2593a45
commit 90d774ffed
3 files changed
+94 -33

No files matched your search

+18 -17
View File
@@ -53,7 +53,7 @@ Root `format_metadata` (`hex-records`) records:
| `start_address`, `start_address_record` | The Intel `05` EIP or the `S7`/`S8`/`S9` address, with its record type |
| `start_segment_cs`, `start_segment_ip` | The Intel `03` CS:IP pair |
| `header_hex`, `header_text` | S-record `S0` data as hex, and as text only when every byte is printable ASCII |
| `declared_data_record_count` | The last `S5`/`S6` count, validated against the data records before it |
| `declared_data_record_count` | The last `S5`/`S6` count, validated against preceding data records |
`variant` is `null` for an S-record file in which no data or termination
record establishes an address width. Start addresses are declared facts, not
@@ -61,22 +61,23 @@ 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 data records before it | Fails |
| 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 |
| 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 |
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
+52 -2
View File
@@ -2,8 +2,8 @@ import { createHash } from "node:crypto";
import { buffer } from "node:stream/consumers";
import { artifactPreparationRecoveryCases } from "../../tests/fixtures/artifactReaderLifecycle.js";
import { openRegularFile } from "../filesystem/RegularFile.js";
import { stat, utimes, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { readFile, stat, utimes, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { setImmediate } from "node:timers/promises";
import { expect, it, vi } from "vitest";
@@ -19,6 +19,7 @@ import {
SREC_S19,
intelHexInterleavedRuns,
intelHexRecord,
srecRecord,
} from "../domain/firmware/hexRecords.fixture.js";
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
import { scanCanonicalArtifactInventory } from "../../tests/fixtures/artifactInventory.js";
@@ -85,6 +86,55 @@ it("reports unknown coverage and termination facts as limitations", async () =>
);
});
it.each([
[5, false],
[6, false],
[5, true],
] as const)(
"retains data after an S%i count (later final count: %s)",
async (type, finalCount) => {
const text = SREC_S19.replace(
"S5030002FA",
[
srecRecord(type, 2, []),
srecRecord(1, 0x20, [0x2a]),
...(finalCount ? [srecRecord(type, 3, [])] : []),
].join("\n"),
);
const path = await writeImage("count.s19", text);
const result = await scanCanonicalArtifactInventory(path);
const warning = expect.stringMatching(
/line 4.*count covers 2 of 3 data records/iu,
);
if (finalCount) expect(result.limitations).not.toContainEqual(warning);
else expect(result.limitations).toContainEqual(warning);
expect(result.coverage.status).toBe("complete");
// GNU objcopy 2.44 retained these exact bytes for the S5 inputs.
const resourceScope = new ArtifactResourceScope();
try {
const extracted = await extractArtifact({
inputPath: path,
inputFormat: "srec",
outputRoot: join(dirname(path), "out"),
environment: process.env,
integrityPolicy: "fail",
resourceScope,
});
const bytes = await readFile(
join(extracted.output_root, "0x00000000-0x00000020.bin"),
);
const expected = Buffer.from([
...Array.from({ length: 32 }, (_, index) => index),
0x2a,
]);
expect(bytes.equals(expected)).toBe(true);
} finally {
await resourceScope.close();
}
},
);
it("refuses one over-limit physical run even when all addresses could merge", async () => {
await expect(
scanCanonicalArtifactInventory(
+24 -14
View File
@@ -122,7 +122,7 @@ class StreamingHexRecordDecoder implements RegionDecoder<HexRecordImage> {
#start: (DeclaredValue<number> & { readonly record: string }) | undefined;
#startSegment: DeclaredValue<readonly [number, number]> | undefined;
#header: DeclaredValue<Uint8Array> | undefined;
#declaredCount: number | null = null;
#declaredCount: DeclaredValue<number> | undefined;
readonly #dataTypes = new Set<string>();
constructor(encoding: HexRecordEncoding, fileBytes: number) {
@@ -279,7 +279,7 @@ class StreamingHexRecordDecoder implements RegionDecoder<HexRecordImage> {
`record type ${record.type} declares ${String(record.address)} data records, but ${String(this.#dataRecordCount)} precede it`,
),
);
this.#declaredCount = record.address;
this.#declaredCount = { value: record.address, line: this.#lineNumber };
return ok(undefined);
default:
this.#termination = { value: record.type, line: this.#lineNumber };
@@ -393,18 +393,28 @@ class StreamingHexRecordDecoder implements RegionDecoder<HexRecordImage> {
}
#limitations(): string[] {
const label = HEX_RECORD_ENCODING_LABELS[this.#encoding];
if (this.#termination === undefined)
return [`${label} has no termination record; the file may be truncated.`];
const termination = this.#termination.value;
const mismatched = [...this.#dataTypes].filter(
(type) => SREC_WIDTH_OF[type] !== SREC_WIDTH_OF[termination],
);
return this.#encoding === "srec" && mismatched.length > 0
? [
const limitations: string[] = [];
const count = this.#declaredCount;
if (count !== undefined && count.value < this.#dataRecordCount)
limitations.push(
`S-record line ${String(count.line)}: the final count covers ${String(count.value)} of ${String(this.#dataRecordCount)} data records. Data after the count is retained.`,
);
const termination = this.#termination?.value;
if (termination === undefined) {
const label = HEX_RECORD_ENCODING_LABELS[this.#encoding];
limitations.push(
`${label} has no termination record; the file may be truncated.`,
);
} else if (this.#encoding === "srec") {
const mismatched = [...this.#dataTypes].filter(
(type) => SREC_WIDTH_OF[type] !== SREC_WIDTH_OF[termination],
);
if (mismatched.length > 0)
limitations.push(
`S-record termination ${termination} does not match the address width of ${mismatched.sort().join(", ")} data records.`,
]
: [];
);
}
return limitations;
}
#metadata(
@@ -438,7 +448,7 @@ class StreamingHexRecordDecoder implements RegionDecoder<HexRecordImage> {
header?.every((byte) => byte >= 0x20 && byte <= 0x7e) === true
? String.fromCharCode(...header)
: null,
declared_data_record_count: this.#declaredCount,
declared_data_record_count: this.#declaredCount?.value ?? null,
};
}
}