9
9
struct data {
10
10
gsl_rstat_workspace * rstat_p ;
11
11
struct timespec start ;
12
+ size_t nc ;
13
+ uc_context * * c ;
12
14
};
13
15
14
16
@@ -22,11 +24,8 @@ void update_stats(gsl_rstat_workspace *rstat_p, struct timespec *start, struct t
22
24
static uint64_t CODEADDR = 0x1000 ;
23
25
static uint64_t DATABASE = 0x40000000 ;
24
26
static uint64_t BLOCKSIZE = 0x10000 ;
27
+ static size_t NRUNS = 200 ;
25
28
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
- }*/
30
29
static int callback_mem_prot (uc_engine * uc , uc_mem_type type , uint64_t addr , uint32_t size , int64_t value , void * data )
31
30
{
32
31
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
50
49
d -> rstat_p = gsl_rstat_alloc ();
51
50
}
52
51
run = gsl_rstat_n (d -> rstat_p );
53
- if (( run >> 4 ) >= 20 ) {
52
+ if (run && !( run % 128 ) ) {
54
53
uc_emu_stop (uc );
55
54
return ;
56
- } else if (run > 0 && run % 16 == 0 ) {
57
- uc_snapshot (uc );
58
55
}
59
- /* if (run > 0 && run % 16 == 0) {
60
- uc_emu_stop(uc);
61
- return;
62
- }*/
63
56
rsi = random ();
64
57
memblock = random () & 15 ;
65
58
offset = random () & (BLOCKSIZE - 1 ) & (~0xf );
66
- // memblock = 0;
67
- // offset = 0;
68
59
if (memblock == 15 && (offset + 0x1000 ) > BLOCKSIZE ) {
69
60
offset -= 0x1000 ;
70
61
}
71
62
rbx += (memblock * BLOCKSIZE ) + offset ;
63
+ #ifndef NDEBUG
72
64
printf ("write at 0x%lX\n" , rbx );
73
65
printf ("[%li] callback block: 0x%lX\n" , run , addr );
66
+ #endif
74
67
uc_reg_write (uc , UC_X86_REG_RBX , & rbx );
75
68
uc_reg_write (uc , UC_X86_REG_RAX , & rax );
76
69
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
80
73
static void prepare_mapping (uc_engine * uc )
81
74
{
82
75
for (size_t i = 0 ; i < 16 ; i ++ ) {
76
+ #ifndef NDEBUG
83
77
printf ("mem map: 0x%lX\n" , DATABASE + i * BLOCKSIZE );
78
+ #endif
84
79
uc_mem_map (uc , DATABASE + i * BLOCKSIZE , BLOCKSIZE , UC_PROT_READ |UC_PROT_WRITE );
85
80
}
86
81
}
@@ -145,6 +140,7 @@ int main(int argc, char *argv[])
145
140
uc_err err ;
146
141
uc_hook hook_block ;
147
142
uc_hook hook_mem ;
143
+ uc_context * * con = calloc (NRUNS , sizeof (* con ));
148
144
struct data d ;
149
145
uint64_t rax = 5 ;
150
146
uint64_t rbx = DATABASE ;
@@ -156,9 +152,13 @@ int main(int argc, char *argv[])
156
152
}
157
153
158
154
d .rstat_p = NULL ;
155
+ d .c = con ;
156
+ d .nc = 0 ;
159
157
srandom (time (NULL ));
160
158
161
159
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 );
162
162
prepare_code (uc , argv [1 ], & bin_mmap );
163
163
prepare_mapping (uc );
164
164
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[])
168
168
uc_hook_add (uc , & hook_mem , UC_HOOK_MEM_INVALID , & callback_mem_prot , NULL , CODEADDR , 0x1000 );
169
169
uc_reg_write (uc , UC_X86_REG_RBX , & rbx );
170
170
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
177
176
err = uc_emu_start (uc , CODEADDR , -1 , 0 , 0 );
178
177
if (err ) {
179
178
printf ("err: %s\n" , uc_strerror (err ));
180
179
return 1 ;
181
180
}
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 ++ ;
183
184
}
184
185
print_stats (d .rstat_p );
185
186
return 0 ;
0 commit comments