v4: mkf18a, the host tool that builds a train's F18 artifact (ADR step 3, task 3)
Build / Image RASPI (push) Successful in 3m59s
Build / Image PC (push) Successful in 3m58s
Build / Image MILKV (push) Successful in 4m7s
Build / v4 tests (32- and 64-bit cells) (push) Successful in 16m5s

The artifact's layout and its check (artifact.h, artifact.c), the tool
(tools/f18a.c, mkf18a.c) and make -C v4 artifacts. test_mkf18a, 83
checks at both widths, failed against a stub first. Gates: test,
test-32 and sanitize pass, POST 990/990. Plan section 11.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-19 21:09:22 +00:00
parent 84f119a1b3
commit dc3803ab47
12 changed files with 2924 additions and 3 deletions
+90
View File
@@ -0,0 +1,90 @@
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* artifact.c -- the check of an F18 artifact (artifact.h, ARTIFACT.md section 7). */
#include "v4/artifact.h"
#include "v4/iword.h"
#include "v4/opcode.h"
#define LIMIT 0x01000000u /* no count is larger: it keeps every sum below in range */
uint32_t v4_f18a_unit(const unsigned char *a, size_t u)
{
const unsigned char *p = a + u * 4u;
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
}
static int bad(const char **why, const char *what)
{
*why = what;
return 0;
}
/* a counted string at pool offset `off`, of 1 to `max` characters */
static int string_ok(const unsigned char *a, const v4_f18a_layout *l, uint32_t off, unsigned max)
{
unsigned len;
if (off == 0 || off >= l->s) return 0;
len = a[(l->pool + off) * 4u];
if (len == 0 || len > max) return 0;
return off + (1u + len + 3u) / 4u <= l->s;
}
int v4_f18a_check(const unsigned char *a, size_t len, unsigned cell_bits, v4_f18a_layout *l, const char **why)
{
size_t u, cell_bytes = cell_bits / 8u;
uint32_t k;
*why = NULL;
if (len < V4_F18A_HEADER * 4u) return bad(why, "shorter than a header");
if (v4_f18a_unit(a, 0) != V4_F18A_MAGIC) return bad(why, "not an F18 artifact: the magic");
if (v4_f18a_unit(a, 1) != V4_F18A_ABI) return bad(why, "an ABI version this does not take");
l->cell_bits = v4_f18a_unit(a, 2);
if (l->cell_bits != cell_bits || (cell_bits != 32u && cell_bits != 64u)) return bad(why, "another cell width");
l->node_words = v4_f18a_unit(a, 3);
l->e = v4_f18a_unit(a, 4); l->i = v4_f18a_unit(a, 5); l->r = v4_f18a_unit(a, 6);
l->s = v4_f18a_unit(a, 7); l->c = v4_f18a_unit(a, 8);
if (l->e >= LIMIT || l->i >= LIMIT || l->r >= LIMIT || l->s >= LIMIT || l->c >= LIMIT) return bad(why, "a count out of range");
if (v4_f18a_unit(a, 9) != V4_F18A_HEADER + V4_F18A_PROV) return bad(why, "the exports are not after the provenance");
for (u = 10; u < V4_F18A_HEADER; u++) if (v4_f18a_unit(a, u) != 0) return bad(why, "a reserved unit of the header is not 0");
l->exports = V4_F18A_HEADER + V4_F18A_PROV;
l->imports = l->exports + (size_t)V4_F18A_EXPORT * l->e;
l->relocs = l->imports + (size_t)V4_F18A_IMPORT * l->i;
l->verify = l->relocs + (size_t)V4_F18A_RELOC * l->r;
l->pool = l->verify + V4_F18A_VERIFY;
l->code = l->pool + l->s;
if (len != l->code * 4u + (size_t)l->c * cell_bytes) return bad(why, "its length is not what its counts make");
if (l->s == 0 || v4_f18a_unit(a, l->pool) != 0) return bad(why, "the string pool does not start with 0");
for (k = 0; k < l->e; k++) {
size_t x = l->exports + (size_t)V4_F18A_EXPORT * k;
uint32_t caps = v4_f18a_unit(a, x + 5) & ~V4_F18A_PORTS;
if (!string_ok(a, l, v4_f18a_unit(a, x), 31u)) return bad(why, "an export's name is not a counted string of the pool");
if (v4_f18a_unit(a, x + 1) >= l->c) return bad(why, "an export's entry is outside the code");
if (caps != 0 && (caps >= l->s || caps + 1u + v4_f18a_unit(a, l->pool + caps) > l->s))
return bad(why, "an export's requests are not a list of the pool");
if (v4_f18a_unit(a, x + 6) != 0 || v4_f18a_unit(a, x + 7) != 0) return bad(why, "a reserved unit of an export is not 0");
}
for (k = 0; k < l->i; k++) {
size_t x = l->imports + (size_t)V4_F18A_IMPORT * k;
if (!string_ok(a, l, v4_f18a_unit(a, x + 2), 31u)) return bad(why, "an import's name is not a counted string of the pool");
if (v4_f18a_unit(a, x + 5) != 0) return bad(why, "a reserved unit of an import is not 0");
}
for (k = 0; k < l->r; k++) {
size_t x = l->relocs + (size_t)V4_F18A_RELOC * k;
uint32_t u0 = v4_f18a_unit(a, x), to = v4_f18a_unit(a, x + 1);
uint32_t cell = u0 & 0x1FFFFFFFu, slot = u0 >> 29;
if (cell >= l->c) return bad(why, "a relocation's cell is outside the code");
if (slot < 4u) {
/* an instruction word is the cell's low 32 bits, its first unit */
v4_iword w = (v4_iword)v4_f18a_unit(a, l->code + (size_t)cell * (cell_bytes / 4u));
if (!v4_op_is_branch(v4_iword_op(w, slot))) return bad(why, "a relocation's slot holds no branch");
} else if (slot != V4_F18A_WHOLE) return bad(why, "a relocation's slot is not 0-3 or 7");
if (to != V4_F18A_INTERNAL && to >= l->i) return bad(why, "a relocation's import is beyond the imports");
}
if (v4_f18a_unit(a, l->verify + 16) != 0 && !string_ok(a, l, v4_f18a_unit(a, l->verify + 16), 255u))
return bad(why, "the proof's assumptions are not a counted string of the pool");
if (v4_f18a_unit(a, l->verify + 17) != 0) return bad(why, "a reserved unit of the verification is not 0");
return 1;
}