Skip to content

Commit 54ca374

Browse files
committed
update benchmark for new api
1 parent 4258b5f commit 54ca374

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

tests/benchmarks/cow/benchmark.c

+21-20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
struct data {
1010
gsl_rstat_workspace *rstat_p;
1111
struct timespec start;
12+
size_t nc;
13+
uc_context **c;
1214
};
1315

1416

@@ -22,11 +24,8 @@ void update_stats(gsl_rstat_workspace *rstat_p, struct timespec *start, struct t
2224
static uint64_t CODEADDR = 0x1000;
2325
static uint64_t DATABASE = 0x40000000;
2426
static uint64_t BLOCKSIZE = 0x10000;
27+
static size_t NRUNS = 200;
2528

26-
/*static void callback_mem(uc_engine *uc, uc_mem_type type, uint64_t addr, uint32_t size, uint64_t value, void *data)
27-
{
28-
printf("callback mem valid: 0x%lX, value: 0x%lX\n", addr, value);
29-
}*/
3029
static int callback_mem_prot(uc_engine *uc, uc_mem_type type, uint64_t addr, uint32_t size, int64_t value, void *data)
3130
{
3231
printf("callback mem prot: 0x%lX, type: %X\n", addr, type);
@@ -50,27 +49,21 @@ static void callback_block(uc_engine *uc, uint64_t addr, uint32_t size, void *da
5049
d->rstat_p = gsl_rstat_alloc();
5150
}
5251
run = gsl_rstat_n(d->rstat_p);
53-
if ((run >> 4) >= 20) {
52+
if (run && !(run % 128)) {
5453
uc_emu_stop(uc);
5554
return;
56-
} else if (run > 0 && run % 16 == 0) {
57-
uc_snapshot(uc);
5855
}
59-
/* if (run > 0 && run % 16 == 0) {
60-
uc_emu_stop(uc);
61-
return;
62-
}*/
6356
rsi = random();
6457
memblock = random() & 15;
6558
offset = random() & (BLOCKSIZE - 1) & (~0xf);
66-
// memblock = 0;
67-
// offset = 0;
6859
if (memblock == 15 && (offset + 0x1000) > BLOCKSIZE) {
6960
offset -= 0x1000;
7061
}
7162
rbx += (memblock * BLOCKSIZE) + offset;
63+
#ifndef NDEBUG
7264
printf("write at 0x%lX\n", rbx);
7365
printf("[%li] callback block: 0x%lX\n", run, addr);
66+
#endif
7467
uc_reg_write(uc, UC_X86_REG_RBX, &rbx);
7568
uc_reg_write(uc, UC_X86_REG_RAX, &rax);
7669
uc_reg_write(uc, UC_X86_REG_RSI, &rsi);
@@ -80,7 +73,9 @@ static void callback_block(uc_engine *uc, uint64_t addr, uint32_t size, void *da
8073
static void prepare_mapping(uc_engine *uc)
8174
{
8275
for (size_t i = 0; i < 16; i++) {
76+
#ifndef NDEBUG
8377
printf("mem map: 0x%lX\n", DATABASE+i*BLOCKSIZE);
78+
#endif
8479
uc_mem_map(uc, DATABASE+i*BLOCKSIZE, BLOCKSIZE, UC_PROT_READ|UC_PROT_WRITE);
8580
}
8681
}
@@ -145,6 +140,7 @@ int main(int argc, char *argv[])
145140
uc_err err;
146141
uc_hook hook_block;
147142
uc_hook hook_mem;
143+
uc_context **con = calloc(NRUNS, sizeof(*con));
148144
struct data d;
149145
uint64_t rax = 5;
150146
uint64_t rbx = DATABASE;
@@ -156,9 +152,13 @@ int main(int argc, char *argv[])
156152
}
157153

158154
d.rstat_p = NULL;
155+
d.c = con;
156+
d.nc = 0;
159157
srandom(time(NULL));
160158

161159
uc_open(UC_ARCH_X86, UC_MODE_64, &uc);
160+
uc_ctl_context_mode(uc, UC_CTL_CONTEXT_MEMORY);
161+
uc_ctl_tlb_mode(uc, UC_TLB_VIRTUAL);
162162
prepare_code(uc, argv[1], &bin_mmap);
163163
prepare_mapping(uc);
164164
err = uc_hook_add(uc, &hook_block, UC_HOOK_BLOCK, &callback_block, &d, CODEADDR, 0x1000);
@@ -168,18 +168,19 @@ int main(int argc, char *argv[])
168168
uc_hook_add(uc, &hook_mem, UC_HOOK_MEM_INVALID, &callback_mem_prot, NULL, CODEADDR, 0x1000);
169169
uc_reg_write(uc, UC_X86_REG_RBX, &rbx);
170170
uc_reg_write(uc, UC_X86_REG_RAX, &rax);
171-
/* err = uc_hook_add(uc, &hook_mem, UC_HOOK_MEM_VALID, &callback_mem, NULL, DATABASE, 16*BLOCKSIZE);
172-
if (err) {
173-
printf("err: %s\n", uc_strerror(err));
174-
return 1;
175-
}*/
176-
for (int i = 0; i < 1; i++) {
171+
172+
for (int i = 0; i < NRUNS; i++) {
173+
#ifndef NDEBUG
174+
printf("============ run: %i\n", i);
175+
#endif
177176
err = uc_emu_start(uc, CODEADDR, -1, 0, 0);
178177
if (err) {
179178
printf("err: %s\n", uc_strerror(err));
180179
return 1;
181180
}
182-
uc_snapshot(uc);
181+
uc_context_alloc(uc, &d.c[d.nc]);
182+
uc_context_save(uc, d.c[d.nc]);
183+
d.nc++;
183184
}
184185
print_stats(d.rstat_p);
185186
return 0;

0 commit comments

Comments
 (0)