omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / research / atemporal / at1 / model / at1_io.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* The only stdio of the engine outside at1_main.c: read a whole file (up to |
| 18 | * AT1_FILE_MAX bytes) and the fatal error exit. The compute objects stay free |
| 19 | * of stdio so the isolation gate scans them strictly (AT-1 D2). */ |
| 20 | #include "at1_model.h" |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | at1_status at1_read_file(const char *path, uint8_t **bytes, size_t *len) |
| 26 | { |
| 27 | *bytes = NULL; *len = 0; |
| 28 | FILE *f = fopen(path, "rb"); |
| 29 | if (!f) return AT1_ERR_ARGUMENT; |
| 30 | size_t cap = 65536, n = 0; |
| 31 | uint8_t *b = at1_xmalloc(cap); |
| 32 | for (;;) { |
| 33 | if (n == cap) { |
| 34 | if (cap > AT1_FILE_MAX) break; |
| 35 | cap *= 2; |
| 36 | b = at1_xrealloc(b, cap); |
| 37 | } |
| 38 | size_t got = fread(b + n, 1, cap - n, f); |
| 39 | n += got; |
| 40 | if (got == 0) break; |
| 41 | } |
| 42 | int bad = ferror(f); |
| 43 | fclose(f); |
| 44 | if (bad) { free(b); return AT1_ERR_ARGUMENT; } |
| 45 | if (n > AT1_FILE_MAX) { free(b); return AT1_ERR_RESOURCE; } |
| 46 | *bytes = b; *len = n; |
| 47 | return AT1_OK; |
| 48 | } |
| 49 | |
| 50 | /* Fatal engine error: one stderr line, exit 1. Lives here, not in a compute |
| 51 | * object: gcc lowers fputs(constant, stream) to fwrite, which the isolation |
| 52 | * gate bans (omega#371 D2); fprintf is not lowered and at1_io.o is the one |
| 53 | * object the gate exempts for file I/O anyway. */ |
| 54 | void at1_fatal(const char *reason) |
| 55 | { |
| 56 | fprintf(stderr, "AT1_ENGINE_ERROR %s\n", reason); |
| 57 | exit(1); |
| 58 | } |