|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdint.h> |
| 3 | +#include <stddef.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | +#include <assert.h> |
| 7 | +#include <stdbool.h> |
| 8 | + |
| 9 | +#define LLSC_TEST_FILE "LLSC_TEST_FILE" |
| 10 | + |
| 11 | +int32_t is_key(char* key, char* name_prefix, size_t size) { |
| 12 | + char* p = strstr(key, name_prefix); |
| 13 | + if (p == NULL) return -1; |
| 14 | + assert(strlen(name_prefix) < size); |
| 15 | + char* n = p + strlen(name_prefix); |
| 16 | + int32_t v = strtol(n, (char**)NULL, 10); |
| 17 | + assert(v >= 0); |
| 18 | + return v; |
| 19 | +} |
| 20 | + |
| 21 | +// name can only be alphabet letters, containing no numbers |
| 22 | +void make_symbolic(void* addr, size_t byte_size, char* name_prefix) { |
| 23 | + const char* path = getenv(LLSC_TEST_FILE); |
| 24 | + char* line = NULL; |
| 25 | + size_t len = 0; |
| 26 | + ssize_t read; |
| 27 | + FILE* fp = fopen(path, "r"); |
| 28 | + if (fp == NULL) { |
| 29 | + fprintf(stderr, "Test file %s not exists\n", path); |
| 30 | + exit(-1); |
| 31 | + } |
| 32 | + fprintf(stdout, "Reading test case from %s\n", path); |
| 33 | + |
| 34 | + uint8_t* reified_data = (uint8_t*)malloc(byte_size); |
| 35 | + while ((read = getline(&line, &len, fp)) != -1) { |
| 36 | + char* value_ptr = strstr(line, "="); |
| 37 | + if (value_ptr == NULL) continue; |
| 38 | + size_t key_size = value_ptr - line; |
| 39 | + line[key_size] = '\0'; |
| 40 | + //printf("%s, %s", line, value_ptr+1); |
| 41 | + int idx = is_key(line, name_prefix, key_size); |
| 42 | + if (idx == -1) continue; |
| 43 | + assert(idx >= 0 && idx < byte_size); |
| 44 | + uint8_t v = (uint8_t) strtol(value_ptr+1, (char **)NULL, 10); |
| 45 | + reified_data[idx] = v; |
| 46 | + //printf("%s%d -> %d\n", name_prefix, idx, v); |
| 47 | + } |
| 48 | + memcpy(addr, (void*)reified_data, byte_size); |
| 49 | + free(reified_data); |
| 50 | + free(line); |
| 51 | +} |
| 52 | + |
| 53 | +void make_symbolic_whole(void* addr, size_t byte_size, char* name) { |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +/* |
| 58 | +int main() { |
| 59 | + int x; |
| 60 | + make_symbolic(&x, 4, "x"); |
| 61 | + if (x <= 10) { |
| 62 | + printf("%d\n", x); |
| 63 | + } else { |
| 64 | + if (x <= 15) { |
| 65 | + printf("%d\n", x); |
| 66 | + } else { |
| 67 | + printf("%d\n", x); |
| 68 | + } |
| 69 | + } |
| 70 | + return 0; |
| 71 | +} |
| 72 | +*/ |
0 commit comments