-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrdtest_helpers.c
More file actions
322 lines (251 loc) · 9.11 KB
/
Copy pathrdtest_helpers.c
File metadata and controls
322 lines (251 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "rdtest_helpers.h"
#include "rdtest.h"
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define RDTEST_PROJECT_FILE "project.rdx"
static const char RDTEST_BUFFER[RDTEST_BUFFER_SIZE] = {0};
static const char* g_samples_dir = NULL;
static RDContext* g_context = NULL;
static bool _rdtest_ext_matches(RDContext* ctx, const RDExternal* ext,
const RDTestExternal* e) {
if(ext->kind != e->kind) return false;
if(ext->address != e->address) return false;
if(e->name) {
const char* name = rd_get_name(ctx, ext->address);
if(!name || strcmp(e->name, name) != 0) return false;
}
if(ext->ordinal.has_value != e->ordinal.has_value) return false;
if(e->ordinal.has_value && ext->ordinal.value != e->ordinal.value)
return false;
return true;
}
static void _rdtest_load_plugins() {
char paths[] = REDASM_PLUGIN_PATHS;
char* path = strtok(paths, "|");
while(path) {
rd_module_load(path);
path = strtok(NULL, "|");
}
}
static int _rdtest_check_graph(RDContext* ctx, RDAddress address,
u32 expected_hash) {
const RDFunction* f = rd_find_function(ctx, address);
if(!f) {
fprintf(stderr, " TEST FAIL no function at 0x%08" PRIx64 "\n",
address);
return RDTEST_FAIL;
}
u32 hash = rd_function_get_hash(f);
if(hash != expected_hash) {
fprintf(stderr,
" TEST FAIL graph hash mismatch at 0x%08" PRIx64
": expected 0x%08x, got 0x%08x\n",
address, expected_hash, hash);
return RDTEST_FAIL;
}
return RDTEST_PASS;
}
static int _rdtest_run(RDTestSample* sample) {
rdtest_assert_notnull(sample->ctx);
if(sample->entry_point.has_value) {
RDAddress ep;
rdtest_assert_true(rd_get_entry_point(sample->ctx, &ep));
if(ep != sample->entry_point.value) {
fprintf(stderr,
" TEST FAIL address mismatch: expected %" PRIx64
", got %" PRIx64 "\n",
sample->entry_point.value, ep);
return RDTEST_FAIL;
}
const RDFunction* f =
rd_find_function(sample->ctx, sample->entry_point.value);
rdtest_assert_notnull(f);
rdtest_assert_eq(rd_function_is_noret(f), sample->entry_point.no_ret);
}
rdtest_assert_pass(rdtest_check_names(sample->ctx, sample->names));
rdtest_assert_pass(rdtest_check_types(sample->ctx, sample->types));
rdtest_assert_pass(rdtest_check_graphs(sample->ctx, sample->graphs));
rdtest_assert_pass(rdtest_check_xrefs(sample->ctx, sample->xrefs));
rdtest_assert_pass(rdtest_check_externals(sample->ctx, sample->externals));
RDAddressSpace aspace = rd_get_address_space(sample->ctx);
rdtest_assert_ne(aspace.start, aspace.end);
rdtest_assert_true(aspace.size > 0);
if(!sample->skip_rendering) {
RDSurface* s = rd_surface_create(sample->ctx, RD_RF_DEFAULT);
rdtest_assert_notnull(s);
rd_surface_set_max_rows(s, SIZE_MAX); // render everything
rdtest_assert_true(rd_surface_jump_to(s, aspace.start));
rd_surface_destroy(s);
}
return RDTEST_PASS;
}
void rdtest_init(int argc, char** argv) {
for(int i = 1; i < argc; i++) {
if(!strcmp(argv[i], "--samples") && i + 1 < argc)
g_samples_dir = argv[++i];
}
if(!g_samples_dir) g_samples_dir = getenv("REDASM_SAMPLES");
const char* kb_paths[] = {REDASM_KB_PATH, NULL};
RDInitParams params = {.kb_paths = kb_paths};
rd_init(¶ms);
_rdtest_load_plugins();
}
void rdtest_deinit(void) {
if(g_context) rd_destroy(g_context);
rd_deinit();
}
RDContext* rdtest_context_create(void) {
RDTestResultSlice slice = rd_test_data(RDTEST_BUFFER, RDTEST_BUFFER_SIZE);
if(rd_slice_is_empty(slice)) return NULL;
RDAcceptParams params = {.mode = RD_AM_NEW};
RDAcceptResult res = rd_accept(rd_slice_at(slice, 0), ¶ms);
return res.context; // NULL on failure
}
RDContext* rdtest_load_sample(const char* relpath, const char* loaderid,
const char* processorid) {
if(!g_samples_dir) return NULL;
if(g_context) {
rd_destroy(g_context);
g_context = NULL;
}
RDTestResultSlice slice =
rd_test(rd_format("%s/%s", g_samples_dir, relpath));
if(rd_slice_is_empty(slice)) return NULL;
RDAcceptParams params = {.mode = RD_AM_NEW};
RDAcceptResult res = rd_accept(rd_slice_at(slice, 0), ¶ms);
if(res.status != RD_ACCEPT_OK) return NULL;
const RDLoaderPlugin* loader = rd_get_loader_plugin(res.context);
if(!loader || strcmp(loader->id, loaderid) != 0) {
fprintf(stderr,
" TEST FAIL loader mismatch: expected '%s', got '%s'\n",
loaderid, loader ? loader->id : "(null)");
rd_destroy(res.context);
return NULL;
}
const RDProcessorPlugin* processor = rd_get_processor_plugin(res.context);
if(!processor || strcmp(processor->id, processorid) != 0) {
fprintf(stderr,
" TEST FAIL processor mismatch: expected '%s', got '%s'\n",
processorid, processor ? processor->id : "(null)");
rd_destroy(res.context);
return NULL;
}
return res.context;
}
int rdtest_check_names(RDContext* ctx, const RDTestName* names) {
const RDTestName* n = names;
while(n && n->name) {
const char* name = rd_get_name(ctx, n->address);
if(!name || strcmp(n->name, name) != 0) {
fprintf(stderr,
" TEST FAIL name mismatch at 0x%08" PRIx64
": expected '%s', got '%s'\n",
n->address, n->name, name ? name : "(null)");
return RDTEST_FAIL;
}
n++;
}
return RDTEST_PASS;
}
int rdtest_check_types(RDContext* ctx, const RDTestType* types) {
const RDTestType* tt = types;
while(tt && tt->name) {
RDType currt;
if(!rd_get_type(ctx, tt->address, &currt)) {
fprintf(stderr, " TEST FAIL no type at 0x%08" PRIx64 "\n",
tt->address);
return RDTEST_FAIL;
}
RDType t;
if(!rd_type_init(&t, tt->name, tt->count, tt->mod, ctx)) {
fprintf(stderr,
" TEST FAIL type creation failed at 0x%08" PRIx64 "\n",
tt->address);
return RDTEST_FAIL;
}
if(!rd_type_equals(&currt, &t)) {
fprintf(stderr, " TEST FAIL type mismatch at 0x%08" PRIx64 "\n",
tt->address);
return RDTEST_FAIL;
}
tt++;
}
return RDTEST_PASS;
}
int rdtest_check_graphs(RDContext* ctx, const RDTestGraph* graphs) {
const RDTestGraph* g = graphs;
while(g && g->hash) {
if(_rdtest_check_graph(ctx, g->address, g->hash) != RDTEST_PASS)
return RDTEST_FAIL;
g++;
}
return RDTEST_PASS;
}
int rdtest_check_xrefs(RDContext* ctx, const RDTestXRef* xrefs) {
const RDTestXRef* r = xrefs;
while(r && r->ref.type) {
RDXRefSlice slice = rd_get_xrefs_to(ctx, r->address, r->ref.type);
bool found = false;
const RDXRef* xref;
rd_slice_each(xref, slice) {
if(xref->address == r->ref.address) {
found = true;
break;
}
}
if(!found) {
fprintf(stderr,
" TEST FAIL xref missing: 0x%08" PRIx64 " -> 0x%08" PRIx64
"\n",
r->ref.address, r->address);
return RDTEST_FAIL;
}
r++;
}
return RDTEST_PASS;
}
int rdtest_check_externals(RDContext* ctx, const RDTestExternal* externals) {
if(!externals) return RDTEST_PASS;
RDExternalSlice slice = rd_get_all_externals(ctx, RD_EXT_NONE);
const RDTestExternal* e = externals;
while(e && e->kind != RD_EXT_NONE) {
bool found = false;
const RDExternal* ext;
rd_slice_each(ext, slice) {
if(_rdtest_ext_matches(ctx, ext, e)) {
found = true;
break;
}
}
if(!found) {
fprintf(stderr, " TEST FAIL external mismatch: 0x%08" PRIx64 "\n",
e->address);
return RDTEST_FAIL;
}
e++;
}
return RDTEST_PASS;
}
int rdtest_check_sample(RDTestSample* sample) {
sample->ctx = rdtest_load_sample(sample->rel_path, sample->loader_id,
sample->processor_id);
g_context = sample->ctx;
rd_disassemble(sample->ctx);
rdtest_assert_pass(_rdtest_run(sample));
// run again in project mode
bool ok = rd_project_save(sample->ctx, RDTEST_PROJECT_FILE);
rdtest_assert_true(ok);
rd_destroy(sample->ctx);
sample->ctx = NULL;
g_context = NULL;
RDAcceptResult res = rd_project_load(RDTEST_PROJECT_FILE, NULL);
rdtest_assert_eq(res.status, RD_ACCEPT_OK);
sample->ctx = res.context;
g_context = res.context;
remove(RDTEST_PROJECT_FILE);
rdtest_assert_pass(_rdtest_run(sample));
return RDTEST_PASS;
}