Skip to content

Commit 31f30d4

Browse files
committed
bindings/zig: Fix sample_riscv_zig partial writes and logging
- Use full code length (instead of subtracting 1) when writing instructions. - Uniformly zero-pad addresses in logs and print hexadecimal. - Correct the instruction-hook callback in test_riscv2.
1 parent df75eff commit 31f30d4

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

bindings/zig/sample/sample_riscv_zig.zig

+15-15
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ pub fn main() !void {
1818
fn hook_block(uc: ?*unicornC.uc_engine, address: u64, size: u32, user_data: ?*anyopaque) callconv(.C) void {
1919
_ = user_data;
2020
_ = 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 });
2222
}
2323

2424
fn hook_code(uc: ?*unicornC.uc_engine, address: u64, size: u32, user_data: ?*anyopaque) callconv(.C) void {
2525
_ = user_data;
2626
_ = 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 });
2828
}
2929

3030
fn hook_code3(uc: ?*unicornC.uc_engine, address: u64, size: u32, user_data: ?*anyopaque) callconv(.C) void {
3131
_ = 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 });
3333
if (address == ADDRESS) {
3434
log.info("stop emulation");
3535
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
4141
const algined_address = address & 0xFFFFFFFFFFFFF000;
4242
const aligned_size = (@as(u32, @intCast(size / 0x1000)) + 1) * 0x1000;
4343

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 });
4545

4646
unicorn.uc_mem_map(uc, algined_address, aligned_size, unicornC.UC_PROT_ALL) catch |err| log.err("Error: {}", .{err});
4747

@@ -81,7 +81,7 @@ fn test_recover_from_illegal() !void {
8181
try unicorn.uc_hook_add(uc, &trace2, unicornC.UC_HOOK_CODE, @as(?*anyopaque, @ptrCast(@constCast(&hook_code))), null, 1, 0);
8282

8383
// 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);
8585

8686
// emulate 1 instruction, wrong address, illegal code
8787
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 {
9797
try unicorn.uc_reg_read(uc, unicornC.UC_RISCV_REG_A0, @as(?*anyopaque, @ptrCast(@constCast(&a0))));
9898
try unicorn.uc_reg_read(uc, unicornC.UC_RISCV_REG_A1, @as(?*anyopaque, @ptrCast(@constCast(&a1))));
9999

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});
102102

103103
try unicorn.uc_close(uc);
104104
}
@@ -124,7 +124,7 @@ fn test_riscv_func_return() !void {
124124
try unicorn.uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, unicornC.UC_PROT_ALL);
125125

126126
// 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);
128128

129129
// tracing all basic blocks with customized callback
130130
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 {
143143

144144
try unicorn.uc_reg_read(uc, unicornC.UC_RISCV_REG_PC, @as(?*anyopaque, @ptrCast(@constCast(&pc))));
145145
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 });
147147
if (pc == 0x10004) {
148148
log.info(" PC did not change during execution", .{});
149149
}
@@ -177,7 +177,7 @@ fn test_riscv2() !void {
177177
try unicorn.uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, unicornC.UC_PROT_ALL);
178178

179179
// 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);
181181

182182
// initialize machine registers
183183
try unicorn.uc_reg_write(uc, unicornC.UC_RISCV_REG_A0, @as(?*anyopaque, @ptrCast(@constCast(&a0))));
@@ -187,7 +187,7 @@ fn test_riscv2() !void {
187187
try unicorn.uc_hook_add(uc, &trace1, unicornC.UC_HOOK_BLOCK, @as(?*anyopaque, @ptrCast(@constCast(&hook_block))), null, 1, 0);
188188

189189
// 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);
191191

192192
// emulate 1 instruction
193193
unicorn.uc_emu_start(uc, ADDRESS, ADDRESS + 4, 0, 0) catch |err| {
@@ -197,8 +197,8 @@ fn test_riscv2() !void {
197197
try unicorn.uc_reg_read(uc, unicornC.UC_RISCV_REG_A0, @as(?*anyopaque, @ptrCast(@constCast(&a0))));
198198
try unicorn.uc_reg_read(uc, unicornC.UC_RISCV_REG_A1, @as(?*anyopaque, @ptrCast(@constCast(&a1))));
199199

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});
202202

203203
// emulate one more instruction
204204
unicorn.uc_emu_start(uc, ADDRESS + 4, ADDRESS + 8, 0, 0) catch |err| {
@@ -211,8 +211,8 @@ fn test_riscv2() !void {
211211
try unicorn.uc_reg_read(uc, unicornC.UC_RISCV_REG_A0, @as(?*anyopaque, @ptrCast(@constCast(&a0))));
212212
try unicorn.uc_reg_read(uc, unicornC.UC_RISCV_REG_A1, @as(?*anyopaque, @ptrCast(@constCast(&a1))));
213213

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});
216216

217217
try unicorn.uc_close(uc);
218218
}

0 commit comments

Comments
 (0)