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 / seed / qualification / om2 / oracle / s5s7 / offsetof.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * OM-2 qualification, lane s5s7 (#748, ledger L836): offsetof ORACLE for contract section 5 |
| 3 | * (spec/omegese-om2.md lines 163-165). Outside the delivered closure. |
| 4 | * Build and run from the worktree root (see PROVENANCE.txt for the recorded run): |
| 5 | * gcc -std=c11 -Wall -Wextra -Werror -I/tmp/zl/main/src \ |
| 6 | * -o build/om2-qual/s5s7-offsetof seed/qualification/om2/oracle/s5s7/offsetof.c |
| 7 | * build/om2-qual/s5s7-offsetof build/om2-qual/s5s7-shactx.bin |
| 8 | * stdout: the offsets and sizes the layout line must equal; argv[1]: the 104-byte image of a |
| 9 | * sha256_ctx with state[i] = 0x10203040 + i, count = 0x0123456789abcdef, buffer[j] = 3j+1. */ |
| 10 | /* fdopen/open are POSIX, and the documented build uses -std=c11 (strict ISO). */ |
| 11 | #define _POSIX_C_SOURCE 200809L |
| 12 | |
| 13 | #include <fcntl.h> |
| 14 | #include <stddef.h> |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 | #include <unistd.h> |
| 18 | |
| 19 | #include "sha256.h" |
| 20 | |
| 21 | int main(int argc, char **argv) |
| 22 | { |
| 23 | sha256_ctx c; |
| 24 | FILE *f; |
| 25 | int i, bad = 0; |
| 26 | |
| 27 | printf("sizeof sha256_ctx %zu\n", sizeof(sha256_ctx)); |
| 28 | printf("state offset %zu size %zu count %zu elem %zu\n", offsetof(sha256_ctx, state), |
| 29 | sizeof c.state, sizeof c.state / sizeof c.state[0], sizeof c.state[0]); |
| 30 | printf("count offset %zu size %zu\n", offsetof(sha256_ctx, count), sizeof c.count); |
| 31 | printf("buffer offset %zu size %zu count %zu elem %zu\n", offsetof(sha256_ctx, buffer), |
| 32 | sizeof c.buffer, sizeof c.buffer / sizeof c.buffer[0], sizeof c.buffer[0]); |
| 33 | printf("alignof %zu\n", _Alignof(sha256_ctx)); |
| 34 | /* the OM-2 line (erratum E5: count is a scalar): layout shactx 104 / state uint 8 at 0 / |
| 35 | * count ulong at 32 / buf uchar 64 at 40, all little */ |
| 36 | bad |= sizeof(sha256_ctx) != 104; |
| 37 | bad |= offsetof(sha256_ctx, state) != 0 || sizeof c.state != 32 || sizeof c.state[0] != 4; |
| 38 | bad |= offsetof(sha256_ctx, count) != 32 || sizeof c.count != 8; |
| 39 | bad |= offsetof(sha256_ctx, buffer) != 40 || sizeof c.buffer != 64; |
| 40 | { |
| 41 | unsigned int one = 1; |
| 42 | unsigned char b; |
| 43 | memcpy(&b, &one, 1); |
| 44 | printf("little endian %d\n", b == 1); |
| 45 | bad |= b != 1; |
| 46 | } |
| 47 | printf("layout matches contract %s\n", bad ? "NO" : "YES"); |
| 48 | memset(&c, 0, sizeof c); |
| 49 | for (i = 0; i < 8; i++) |
| 50 | c.state[i] = 0x10203040u + (unsigned)i; |
| 51 | c.count = 0x0123456789abcdefull; |
| 52 | for (i = 0; i < 64; i++) |
| 53 | c.buffer[i] = (unsigned char)(3 * i + 1); |
| 54 | if (argc > 1) { |
| 55 | /* 0600, never the 0666 fopen would leave under a permissive umask: the image is |
| 56 | * oracle output, not something other users should write to. */ |
| 57 | int fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0600); |
| 58 | if (fd < 0) |
| 59 | return 2; |
| 60 | f = fdopen(fd, "wb"); |
| 61 | if (!f) { |
| 62 | close(fd); |
| 63 | return 2; |
| 64 | } |
| 65 | if (fwrite(&c, 1, sizeof c, f) != sizeof c || fclose(f) != 0) |
| 66 | return 2; |
| 67 | } |
| 68 | return bad; |
| 69 | } |