host: Supply loaded image digests
117807f86fa3781c48307a3f38bae4cb606397abcfdadafe22d7ec054955e7d4
Hash the immutable image snapshot with BLAKE3 and append the digest to the hosted process environment. Load from that same snapshot so the digest describes exactly the bytes that execute. Use the BLAKE3 C library through pkg-config. The emulator builds and make fmt passes. Tests have not been run. Assisted-by: Codex:gpt-6 Change-Id: I5c07b1a24520e029ee5c3643ce346d48da9684f5
1 parent
a9d70099
Makefile
+3 -1
| 12 | 12 | -Wunused -Wuninitialized \ |
|
| 13 | 13 | -Wmissing-field-initializers \ |
|
| 14 | 14 | -fno-common -fstack-protector-all \ |
|
| 15 | 15 | -mcmodel=medium \ |
|
| 16 | 16 | -march=native -flto |
|
| 17 | + | BLAKE3_CFLAGS := $(shell pkg-config --cflags libblake3) |
|
| 18 | + | BLAKE3_LIBS := $(shell pkg-config --libs libblake3) |
|
| 17 | 19 | LDFLAGS := -fuse-ld=lld -Wl,-z,stack-size=33554432 |
|
| 18 | 20 | EMULATOR_SRCS := emulator.c abi_host.c abi_linux.c image_native.c image_rad0.c \ |
|
| 19 | 21 | image_raw.c host_posix.c jit.c riscv/debug.c io.c riscv.c \ |
|
| 20 | 22 | vector.c machine.c machine_cpu.c machine_csr.c machine_devices.c machine_fdt.c |
|
| 21 | 23 | ||
| 22 | 24 | default: bin/emulator |
|
| 23 | 25 | ||
| 24 | 26 | bin/emulator: $(EMULATOR_SRCS) jit.h bitmanip.h vector.h muldiv.h color.h types.h io.h host.h hosted.h riscv.h riscv/debug.h machine.h machine_internal.h |
|
| 25 | 27 | @echo "cc emulator => $@" |
|
| 26 | 28 | @mkdir -p bin |
|
| 27 | - | @$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(EMULATOR_SRCS) -o $@ |
|
| 29 | + | @$(CC) $(CPPFLAGS) $(CFLAGS) $(BLAKE3_CFLAGS) $(LDFLAGS) $(EMULATOR_SRCS) $(BLAKE3_LIBS) -o $@ |
|
| 28 | 30 | ||
| 29 | 31 | install: bin/emulator |
|
| 30 | 32 | @echo "copy bin/emulator => $(PREFIX)/emulator" |
|
| 31 | 33 | @mkdir -p $(PREFIX) |
|
| 32 | 34 | @cp bin/emulator $(PREFIX)/emulator |
README
+2 -1
| 9 | 9 | ||
| 10 | 10 | BUILDING |
|
| 11 | 11 | ||
| 12 | 12 | $ make |
|
| 13 | 13 | ||
| 14 | - | Requires `clang` and `lld`. The binary is written to `bin/emulator`. |
|
| 14 | + | Requires `clang`, `lld`, `pkg-config`, and the BLAKE3 C library |
|
| 15 | + | (`libblake3`). The binary is written to `bin/emulator`. |
|
| 15 | 16 | ||
| 16 | 17 | INSTALLATION |
|
| 17 | 18 | ||
| 18 | 19 | $ make install |
|
| 19 | 20 |
emulator.c
+30 -3
| 1 | + | #define _POSIX_C_SOURCE 200809L |
|
| 2 | + | ||
| 1 | 3 | #include <errno.h> |
|
| 2 | 4 | #include <fcntl.h> |
|
| 3 | 5 | #include <limits.h> |
|
| 4 | 6 | #include <stdint.h> |
|
| 5 | 7 | #include <stdio.h> |
| 7 | 9 | #include <string.h> |
|
| 8 | 10 | #include <sys/ioctl.h> |
|
| 9 | 11 | #include <termios.h> |
|
| 10 | 12 | #include <unistd.h> |
|
| 11 | 13 | ||
| 14 | + | #include <blake3.h> |
|
| 15 | + | ||
| 12 | 16 | #include "bitmanip.h" |
|
| 13 | 17 | #include "color.h" |
|
| 14 | 18 | #include "host.h" |
|
| 15 | 19 | #include "hosted.h" |
|
| 16 | 20 | #include "io.h" |
| 111 | 115 | /* Loaded section sizes, used for bounds checking and diagnostics. */ |
|
| 112 | 116 | static u32 program_base = 0; |
|
| 113 | 117 | static u32 program_bytes = 0; |
|
| 114 | 118 | static u32 data_bytes = 0; |
|
| 115 | 119 | ||
| 120 | + | /* BLAKE3 digest of the immutable file snapshot used by the image loader. */ |
|
| 121 | + | static u8 image_digest[BLAKE3_OUT_LEN]; |
|
| 122 | + | ||
| 116 | 123 | /* Snapshot buffer. */ |
|
| 117 | 124 | static struct snapshot_buffer snapshots; |
|
| 118 | 125 | ||
| 119 | 126 | /* File descriptor table for guest file operations. */ |
|
| 120 | 127 | static int guest_fds[MAX_OPEN_FILES]; |
| 1294 | 1301 | read, |
|
| 1295 | 1302 | size |
|
| 1296 | 1303 | ); |
|
| 1297 | 1304 | } |
|
| 1298 | 1305 | ||
| 1299 | - | /* Prepare the environment block (argv) on the guest stack. */ |
|
| 1306 | + | /* Prepare the argument slice and image digest on the guest stack. */ |
|
| 1300 | 1307 | static void prepare_env(struct cpu *cpu, int argc, char **argv) { |
|
| 1301 | 1308 | if (argc < 0 || argv == NULL) |
|
| 1302 | 1309 | argc = 0; |
|
| 1303 | 1310 | ||
| 1304 | 1311 | usize bytes = 0; |
| 1306 | 1313 | bytes += strlen(argv[i]) + 1; /* Include terminating NUL. */ |
|
| 1307 | 1314 | ||
| 1308 | 1315 | /* In RV64, slices are 16 bytes (8-byte ptr + 4-byte len + 4 padding). */ |
|
| 1309 | 1316 | u32 slice_size = 16; |
|
| 1310 | 1317 | u32 slice_array_size = (u32)argc * slice_size; |
|
| 1311 | - | u32 base_size = slice_size + slice_array_size; |
|
| 1318 | + | u32 env_size = slice_size + BLAKE3_OUT_LEN; |
|
| 1319 | + | u32 base_size = env_size + slice_array_size; |
|
| 1312 | 1320 | u32 total_size = align(base_size + (u32)bytes, 16); |
|
| 1313 | 1321 | ||
| 1314 | 1322 | /* Place the env block below the current stack pointer so it doesn't |
|
| 1315 | 1323 | * overlap with uninitialized static data. The image only |
|
| 1316 | 1324 | * contains initialized statics; undefined statics occupy memory after |
| 1324 | 1332 | ||
| 1325 | 1333 | /* Move SP below the env block so the program's stack doesn't overwrite it. |
|
| 1326 | 1334 | */ |
|
| 1327 | 1335 | cpu->regs[SP] = env_addr; |
|
| 1328 | 1336 | ||
| 1329 | - | u32 slices_addr = env_addr + slice_size; |
|
| 1337 | + | u32 slices_addr = env_addr + env_size; |
|
| 1330 | 1338 | u32 strings_addr = slices_addr + (argc > 0 ? slice_array_size : 0); |
|
| 1331 | 1339 | ||
| 1332 | 1340 | /* Write the Env slice header. */ |
|
| 1333 | 1341 | memory_store_u64(env_addr, argc > 0 ? slices_addr : 0); |
|
| 1334 | 1342 | memory_store_u32(env_addr + 8, (u32)argc); |
|
| 1335 | 1343 | ||
| 1344 | + | memcpy(&memory[env_addr + slice_size], image_digest, BLAKE3_OUT_LEN); |
|
| 1345 | + | ||
| 1336 | 1346 | /* Copy argument strings and populate slices. */ |
|
| 1337 | 1347 | u32 curr = strings_addr; |
|
| 1338 | 1348 | for (int i = 0; i < argc; i++) { |
|
| 1339 | 1349 | size_t len = strlen(argv[i]); |
|
| 1340 | 1350 | if (curr + len >= g_opts.memory_size) |
| 1462 | 1472 | if (fseek(file, 0, SEEK_SET) != 0) { |
|
| 1463 | 1473 | fclose(file); |
|
| 1464 | 1474 | bail("failed to rewind program '%s'", filepath); |
|
| 1465 | 1475 | } |
|
| 1466 | 1476 | ||
| 1477 | + | /* Load and hash one snapshot so the digest identifies the loaded bytes. */ |
|
| 1478 | + | u8 *snapshot = malloc((size_t)size); |
|
| 1479 | + | if (!snapshot) |
|
| 1480 | + | bail("cannot allocate image snapshot"); |
|
| 1481 | + | if (fread(snapshot, 1, (size_t)size, file) != (size_t)size) |
|
| 1482 | + | bail("cannot read image snapshot"); |
|
| 1483 | + | fclose(file); |
|
| 1484 | + | blake3_hasher hasher; |
|
| 1485 | + | blake3_hasher_init(&hasher); |
|
| 1486 | + | blake3_hasher_update(&hasher, snapshot, (size_t)size); |
|
| 1487 | + | blake3_hasher_finalize(&hasher, image_digest, sizeof(image_digest)); |
|
| 1488 | + | file = fmemopen(snapshot, (size_t)size, "rb"); |
|
| 1489 | + | if (!file) |
|
| 1490 | + | bail("cannot open image snapshot"); |
|
| 1491 | + | ||
| 1467 | 1492 | u32 data_limit = DATA_MEMORY_START + g_opts.data_memory_size; |
|
| 1468 | 1493 | if (data_limit > g_opts.memory_size) |
|
| 1469 | 1494 | data_limit = g_opts.memory_size; |
|
| 1470 | 1495 | ||
| 1471 | 1496 | /* The format is recognized by its magic; see hosted.h. */ |
| 1478 | 1503 | } |
|
| 1479 | 1504 | image_native_load_machine( |
|
| 1480 | 1505 | file, size, g_opts.memory_size, &machine_options |
|
| 1481 | 1506 | ); |
|
| 1482 | 1507 | fclose(file); |
|
| 1508 | + | free(snapshot); |
|
| 1483 | 1509 | return; |
|
| 1484 | 1510 | } |
|
| 1485 | 1511 | struct hosted_image image; |
|
| 1486 | 1512 | if (image_native_match(header, header_read)) |
|
| 1487 | 1513 | image = image_native_load(file, size, data_limit); |
|
| 1488 | 1514 | else if (image_rad0_match(header, header_read)) |
|
| 1489 | 1515 | image = image_rad0_load(file, size, data_limit); |
|
| 1490 | 1516 | else |
|
| 1491 | 1517 | image = image_raw_load(file, size, filepath, data_limit); |
|
| 1492 | 1518 | fclose(file); |
|
| 1519 | + | free(snapshot); |
|
| 1493 | 1520 | ||
| 1494 | 1521 | program_base = image.code_base; |
|
| 1495 | 1522 | program_bytes = image.code_bytes; |
|
| 1496 | 1523 | data_bytes = image.data_bytes; |
|
| 1497 | 1524 | cpu->programsize = program_bytes / sizeof(instr_t); |