ACPI AML decompiler w/ CFG recovery and structured pseudocode
1#include "cf.h"
2#include "p.h"
3
4#include <dirent.h>
5#include <stdint.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <sys/stat.h>
10
11#ifndef TOBI_SRC_DIR
12#define TOBI_SRC_DIR "."
13#endif
14
15static int run_one(const uint8_t *data, size_t len) {
16 tobi_parse_result r;
17 (void)tobi_parse(data, len, 0, &r);
18 if (r.root) {
19 (void)tobi_cf_recover(r.root, &r.diag);
20 }
21 tobi_parse_result_free(&r);
22 return 0;
23}
24
25static void file_one(const char *path) {
26 FILE *fp = fopen(path, "rb");
27 if (!fp) {
28 return;
29 }
30 (void)fseek(fp, 0, SEEK_END);
31 long n = ftell(fp);
32 rewind(fp);
33 if (n < 0) {
34 fclose(fp);
35 return;
36 }
37 uint8_t *buf = malloc((size_t)n ? (size_t)n : 1);
38 if (!buf) {
39 fclose(fp);
40 abort();
41 }
42 if ((size_t)n == fread(buf, 1, (size_t)n, fp)) {
43 (void)run_one(buf, (size_t)n);
44 for (size_t i = 0; i <= (size_t)n; i++) {
45 (void)run_one(buf, i);
46 }
47 }
48 free(buf);
49 fclose(fp);
50}
51
52static void seed_dir(const char *path) {
53 DIR *dir = opendir(path);
54 if (!dir) {
55 return;
56 }
57 struct dirent *de = NULL;
58 while ((de = readdir(dir)) != NULL) {
59 if (de->d_name[0] == '.') {
60 continue;
61 }
62 char file[1024];
63 snprintf(file, sizeof(file), "%s/%s", path, de->d_name);
64 struct stat st;
65 if (stat(file, &st) == 0 && S_ISREG(st.st_mode)) {
66 file_one(file);
67 }
68 }
69 closedir(dir);
70}
71
72int main(void) {
73 seed_dir(TOBI_SRC_DIR "/fuzz/seed");
74 uint32_t x = 0x12345678u;
75 for (size_t n = 0; n < 512; n++) {
76 uint8_t buf[256];
77 for (size_t i = 0; i < sizeof(buf); i++) {
78 x = x * 1664525u + 1013904223u;
79 buf[i] = (uint8_t)(x >> 24);
80 }
81 (void)run_one(buf, n % sizeof(buf));
82 }
83 return 0;
84}