@@ -18,18 +18,18 @@ pub fn main() !void {
18
18
fn hook_block (uc : ? * unicornC.uc_engine , address : u64 , size : u32 , user_data : ? * anyopaque ) callconv (.C ) void {
19
19
_ = user_data ;
20
20
_ = uc ;
21
- log .info (">>> Tracing basic block at 0x{}, block size = 0x{}" , .{ address , size });
21
+ log .info (">>> Tracing basic block at 0x{x:0>4 }, block size = 0x{x:0>4 }" , .{ address , size });
22
22
}
23
23
24
24
fn hook_code (uc : ? * unicornC.uc_engine , address : u64 , size : u32 , user_data : ? * anyopaque ) callconv (.C ) void {
25
25
_ = user_data ;
26
26
_ = uc ;
27
- log .info (">>> Tracing instruction at 0x{}, instruction size = 0x{}" , .{ address , size });
27
+ log .info (">>> Tracing instruction at 0x{x:0>4 }, instruction size = 0x{x:0>4 }" , .{ address , size });
28
28
}
29
29
30
30
fn hook_code3 (uc : ? * unicornC.uc_engine , address : u64 , size : u32 , user_data : ? * anyopaque ) callconv (.C ) void {
31
31
_ = user_data ;
32
- log .info (">>> Tracing instruction at 0x{}, instruction size = 0x{}" , .{ address , size });
32
+ log .info (">>> Tracing instruction at 0x{x:0>4 }, instruction size = 0x{x:0>4 }" , .{ address , size });
33
33
if (address == ADDRESS ) {
34
34
log .info ("stop emulation" );
35
35
unicorn .uc_emu_stop (uc ) catch | err | log .err ("Error: {}" , .{err });
@@ -41,7 +41,7 @@ fn hook_memalloc(uc: ?*unicornC.uc_engine, @"type": unicornC.uc_mem_type, addres
41
41
const algined_address = address & 0xFFFFFFFFFFFFF000 ;
42
42
const aligned_size = (@as (u32 , @intCast (size / 0x1000 )) + 1 ) * 0x1000 ;
43
43
44
- log .info (">>> Allocating block at 0x{} (0x{}), block size = 0x{} (0x{})" , .{ address , algined_address , size , aligned_size });
44
+ log .info (">>> Allocating block at 0x{x:0>4 } (aligned: 0x{x:0>4 }), block size = 0x{x:0>4 } (aligned: 0x{x:0>4 })" , .{ address , algined_address , size , aligned_size });
45
45
46
46
unicorn .uc_mem_map (uc , algined_address , aligned_size , unicornC .UC_PROT_ALL ) catch | err | log .err ("Error: {}" , .{err });
47
47
@@ -81,7 +81,7 @@ fn test_recover_from_illegal() !void {
81
81
try unicorn .uc_hook_add (uc , & trace2 , unicornC .UC_HOOK_CODE , @as (? * anyopaque , @ptrCast (@constCast (& hook_code ))), null , 1 , 0 );
82
82
83
83
// write machine code to be emulated to memory
84
- try unicorn .uc_mem_write (uc , ADDRESS , RISCV_CODE , RISCV_CODE .len - 1 );
84
+ try unicorn .uc_mem_write (uc , ADDRESS , RISCV_CODE , RISCV_CODE .len );
85
85
86
86
// emulate 1 instruction, wrong address, illegal code
87
87
unicorn .uc_emu_start (uc , 0x1000 , @as (u64 , @bitCast (@as (i64 , -1 ))), 0 , 1 ) catch | err |
@@ -97,8 +97,8 @@ fn test_recover_from_illegal() !void {
97
97
try unicorn .uc_reg_read (uc , unicornC .UC_RISCV_REG_A0 , @as (? * anyopaque , @ptrCast (@constCast (& a0 ))));
98
98
try unicorn .uc_reg_read (uc , unicornC .UC_RISCV_REG_A1 , @as (? * anyopaque , @ptrCast (@constCast (& a1 ))));
99
99
100
- log .info (">>> A0 = 0x{}" , .{a0 });
101
- log .info (">>> A1 = 0x{}" , .{a1 });
100
+ log .info (">>> A0 = 0x{x:0>4 }" , .{a0 });
101
+ log .info (">>> A1 = 0x{x:0>4 }" , .{a1 });
102
102
103
103
try unicorn .uc_close (uc );
104
104
}
@@ -124,7 +124,7 @@ fn test_riscv_func_return() !void {
124
124
try unicorn .uc_mem_map (uc , ADDRESS , 2 * 1024 * 1024 , unicornC .UC_PROT_ALL );
125
125
126
126
// write machine code to be emulated to memory
127
- try unicorn .uc_mem_write (uc , ADDRESS , CODE , CODE .len - 1 );
127
+ try unicorn .uc_mem_write (uc , ADDRESS , CODE , CODE .len );
128
128
129
129
// tracing all basic blocks with customized callback
130
130
try unicorn .uc_hook_add (uc , & trace1 , unicornC .UC_HOOK_BLOCK , @as (? * anyopaque , @ptrCast (@constCast (& hook_block ))), null , 1 , 0 );
@@ -143,7 +143,7 @@ fn test_riscv_func_return() !void {
143
143
144
144
try unicorn .uc_reg_read (uc , unicornC .UC_RISCV_REG_PC , @as (? * anyopaque , @ptrCast (@constCast (& pc ))));
145
145
if (pc != ra ) {
146
- log .info ("Error after execution: PC is: 0x{}, expected was 0x{}" , .{ pc , ra });
146
+ log .info ("Error after execution: PC is: 0x{x:0>4 }, expected was 0x{x:0>4 }" , .{ pc , ra });
147
147
if (pc == 0x10004 ) {
148
148
log .info (" PC did not change during execution" , .{});
149
149
}
@@ -177,7 +177,7 @@ fn test_riscv2() !void {
177
177
try unicorn .uc_mem_map (uc , ADDRESS , 2 * 1024 * 1024 , unicornC .UC_PROT_ALL );
178
178
179
179
// write machine code to be emulated to memory
180
- try unicorn .uc_mem_write (uc , ADDRESS , RISCV_CODE , RISCV_CODE .len - 1 );
180
+ try unicorn .uc_mem_write (uc , ADDRESS , RISCV_CODE , RISCV_CODE .len );
181
181
182
182
// initialize machine registers
183
183
try unicorn .uc_reg_write (uc , unicornC .UC_RISCV_REG_A0 , @as (? * anyopaque , @ptrCast (@constCast (& a0 ))));
@@ -187,7 +187,7 @@ fn test_riscv2() !void {
187
187
try unicorn .uc_hook_add (uc , & trace1 , unicornC .UC_HOOK_BLOCK , @as (? * anyopaque , @ptrCast (@constCast (& hook_block ))), null , 1 , 0 );
188
188
189
189
// tracing all instruction
190
- try unicorn .uc_hook_add (uc , & trace2 , unicornC .UC_HOOK_CODE , @as (? * anyopaque , @ptrCast (@constCast (& hook_block ))), null , 1 , 0 );
190
+ try unicorn .uc_hook_add (uc , & trace2 , unicornC .UC_HOOK_CODE , @as (? * anyopaque , @ptrCast (@constCast (& hook_code ))), null , 1 , 0 );
191
191
192
192
// emulate 1 instruction
193
193
unicorn .uc_emu_start (uc , ADDRESS , ADDRESS + 4 , 0 , 0 ) catch | err | {
@@ -197,8 +197,8 @@ fn test_riscv2() !void {
197
197
try unicorn .uc_reg_read (uc , unicornC .UC_RISCV_REG_A0 , @as (? * anyopaque , @ptrCast (@constCast (& a0 ))));
198
198
try unicorn .uc_reg_read (uc , unicornC .UC_RISCV_REG_A1 , @as (? * anyopaque , @ptrCast (@constCast (& a1 ))));
199
199
200
- log .info (">>> A0 = 0x{}" , .{a0 });
201
- log .info (">>> A1 = 0x{}" , .{a1 });
200
+ log .info (">>> A0 = 0x{x:0>4 }" , .{a0 });
201
+ log .info (">>> A1 = 0x{x:0>4 }" , .{a1 });
202
202
203
203
// emulate one more instruction
204
204
unicorn .uc_emu_start (uc , ADDRESS + 4 , ADDRESS + 8 , 0 , 0 ) catch | err | {
@@ -211,8 +211,8 @@ fn test_riscv2() !void {
211
211
try unicorn .uc_reg_read (uc , unicornC .UC_RISCV_REG_A0 , @as (? * anyopaque , @ptrCast (@constCast (& a0 ))));
212
212
try unicorn .uc_reg_read (uc , unicornC .UC_RISCV_REG_A1 , @as (? * anyopaque , @ptrCast (@constCast (& a1 ))));
213
213
214
- log .info (">>> A0 = 0x{}" , .{a0 });
215
- log .info (">>> A1 = 0x{}" , .{a1 });
214
+ log .info (">>> A0 = 0x{x:0>4 }" , .{a0 });
215
+ log .info (">>> A1 = 0x{x:0>4 }" , .{a1 });
216
216
217
217
try unicorn .uc_close (uc );
218
218
}
0 commit comments