|
| 1 | +#include "wasm_i32_c.h" |
| 2 | +#include "../../cavp_driver.h" |
| 3 | + |
| 4 | +#include <errno.h> |
| 5 | + |
| 6 | +typedef struct { |
| 7 | + const char *name; |
| 8 | + const char *path; |
| 9 | + unsigned arity; |
| 10 | + uint32_t args[2]; |
| 11 | + uint32_t fuel; |
| 12 | + uint32_t expected; |
| 13 | +} wasm_case_t; |
| 14 | + |
| 15 | +static int hex_value(int ch) { |
| 16 | + if (ch >= '0' && ch <= '9') { |
| 17 | + return ch - '0'; |
| 18 | + } |
| 19 | + if (ch >= 'a' && ch <= 'f') { |
| 20 | + return ch - 'a' + 10; |
| 21 | + } |
| 22 | + if (ch >= 'A' && ch <= 'F') { |
| 23 | + return ch - 'A' + 10; |
| 24 | + } |
| 25 | + return -1; |
| 26 | +} |
| 27 | + |
| 28 | +static uint8_t *read_hex_body(const char *path, uint32_t *length) { |
| 29 | + FILE *file = fopen(path, "r"); |
| 30 | + if (file == NULL) { |
| 31 | + fprintf(stderr, "FAIL open %s: %s\n", path, strerror(errno)); |
| 32 | + exit(2); |
| 33 | + } |
| 34 | + |
| 35 | + size_t capacity = 64; |
| 36 | + size_t used = 0; |
| 37 | + uint8_t *bytes = malloc(capacity); |
| 38 | + if (bytes == NULL) { |
| 39 | + fprintf(stderr, "FAIL allocate hex buffer\n"); |
| 40 | + exit(2); |
| 41 | + } |
| 42 | + |
| 43 | + int high = -1; |
| 44 | + int in_comment = 0; |
| 45 | + for (;;) { |
| 46 | + int ch = fgetc(file); |
| 47 | + if (ch == EOF) { |
| 48 | + break; |
| 49 | + } |
| 50 | + if (in_comment) { |
| 51 | + if (ch == '\n') { |
| 52 | + in_comment = 0; |
| 53 | + } |
| 54 | + continue; |
| 55 | + } |
| 56 | + if (ch == '#') { |
| 57 | + in_comment = 1; |
| 58 | + continue; |
| 59 | + } |
| 60 | + if (isspace((unsigned char)ch)) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + int nibble = hex_value(ch); |
| 65 | + if (nibble < 0) { |
| 66 | + fprintf(stderr, "FAIL invalid hex character %c in %s\n", ch, path); |
| 67 | + exit(2); |
| 68 | + } |
| 69 | + if (high < 0) { |
| 70 | + high = nibble; |
| 71 | + continue; |
| 72 | + } |
| 73 | + if (used == capacity) { |
| 74 | + capacity *= 2; |
| 75 | + uint8_t *grown = realloc(bytes, capacity); |
| 76 | + if (grown == NULL) { |
| 77 | + fprintf(stderr, "FAIL grow hex buffer\n"); |
| 78 | + exit(2); |
| 79 | + } |
| 80 | + bytes = grown; |
| 81 | + } |
| 82 | + bytes[used++] = (uint8_t)((high << 4) | nibble); |
| 83 | + high = -1; |
| 84 | + } |
| 85 | + fclose(file); |
| 86 | + |
| 87 | + if (high >= 0) { |
| 88 | + fprintf(stderr, "FAIL odd number of hex digits in %s\n", path); |
| 89 | + exit(2); |
| 90 | + } |
| 91 | + if (used > UINT32_MAX) { |
| 92 | + fprintf(stderr, "FAIL %s too large for PVS bytestring\n", path); |
| 93 | + exit(2); |
| 94 | + } |
| 95 | + *length = (uint32_t)used; |
| 96 | + return bytes; |
| 97 | +} |
| 98 | + |
| 99 | +static wasm_i32__exec_result_t run_case_body(const wasm_case_t *test, |
| 100 | + const uint8_t *body, |
| 101 | + uint32_t length) { |
| 102 | + bytestrings__bytestring_t pvs_body = cavp_make_bytestring(body, length); |
| 103 | + if (test->arity == 1) { |
| 104 | + return wasm_i32__invoke1(pvs_body, test->fuel, test->args[0]); |
| 105 | + } |
| 106 | + if (test->arity == 2) { |
| 107 | + return wasm_i32__invoke2(pvs_body, test->fuel, test->args[0], test->args[1]); |
| 108 | + } |
| 109 | + return wasm_i32__invoke0(pvs_body, test->fuel); |
| 110 | +} |
| 111 | + |
| 112 | +static int run_case(const wasm_case_t *test) { |
| 113 | + uint32_t length = 0; |
| 114 | + uint8_t *body = read_hex_body(test->path, &length); |
| 115 | + wasm_i32__exec_result_t result = run_case_body(test, body, length); |
| 116 | + free(body); |
| 117 | + |
| 118 | + uint8_t halt = wasm_i32__halt_status(); |
| 119 | + int ok = result->status == halt && result->value == test->expected; |
| 120 | + printf("%s wasm %s", ok ? "PASS" : "FAIL", test->name); |
| 121 | + if (test->arity == 1) { |
| 122 | + printf("(%u)", test->args[0]); |
| 123 | + } else if (test->arity == 2) { |
| 124 | + printf("(%u, %u)", test->args[0], test->args[1]); |
| 125 | + } else { |
| 126 | + printf("()"); |
| 127 | + } |
| 128 | + printf(" => %u\n", result->value); |
| 129 | + if (!ok) { |
| 130 | + printf(" expected status=%u value=%u\n", halt, test->expected); |
| 131 | + printf(" actual status=%u value=%u pc=%u stack=%u\n", |
| 132 | + result->status, |
| 133 | + result->value, |
| 134 | + result->final_pc, |
| 135 | + result->final_stack_height); |
| 136 | + } |
| 137 | + release_wasm_i32__exec_result(result); |
| 138 | + return ok ? 0 : 1; |
| 139 | +} |
| 140 | + |
| 141 | +int main(void) { |
| 142 | + static const wasm_case_t cases[] = { |
| 143 | + {"add", "examples/add.body.hex", 2, {40, 2}, 32, 42}, |
| 144 | + {"affine", "examples/affine.body.hex", 2, {5, 9}, 64, 73}, |
| 145 | + {"factorial", "examples/factorial.body.hex", 1, {0, 0}, 128, 1}, |
| 146 | + {"factorial", "examples/factorial.body.hex", 1, {5, 0}, 512, 120}, |
| 147 | + }; |
| 148 | + |
| 149 | + int failures = 0; |
| 150 | + for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { |
| 151 | + failures += run_case(&cases[i]); |
| 152 | + } |
| 153 | + return failures == 0 ? 0 : 1; |
| 154 | +} |
0 commit comments