@@ -372,6 +372,62 @@ static void test_riscv64_fp_move_to_int(void)
372
372
uc_close (uc );
373
373
}
374
374
375
+ static void test_riscv64_code_patching () {
376
+ uc_engine * uc ;
377
+ char code [] = "\x93\x82\x12\x00" ; // addi t0, t0, 0x1
378
+ uc_common_setup (& uc , UC_ARCH_RISCV , UC_MODE_RISCV64 , code , sizeof (code ) - 1 );
379
+ // Zero out t0 and t1
380
+ uint64_t r_t0 = 0x0 ;
381
+ OK (uc_reg_write (uc , UC_RISCV_REG_T0 , & r_t0 ));
382
+ // emulate the instruction
383
+ OK (uc_emu_start (uc , code_start , code_start + sizeof (code ) - 1 , 0 , 0 ));
384
+ // check value
385
+ OK (uc_reg_read (uc , UC_RISCV_REG_T0 , & r_t0 ));
386
+ TEST_CHECK (r_t0 == 0x1 );
387
+ // patch instruction
388
+ char patch_code [] = "\x93\x82\xf2\x7f" ; // addi t0, t0, 0x7FF
389
+ OK (uc_mem_write (uc , code_start , patch_code , sizeof (patch_code ) - 1 ));
390
+ // zero out t0
391
+ r_t0 = 0x0 ;
392
+ OK (uc_reg_write (uc , UC_RISCV_REG_T0 , & r_t0 ));
393
+ OK (uc_emu_start (uc , code_start , code_start + sizeof (patch_code ) - 1 , 0 , 0 ));
394
+ // check value
395
+ OK (uc_reg_read (uc , UC_RISCV_REG_T0 , & r_t0 ));
396
+ TEST_CHECK (r_t0 != 0x1 );
397
+ TEST_CHECK (r_t0 == 0x7ff );
398
+
399
+ OK (uc_close (uc ));
400
+ }
401
+
402
+ // Need to flush the cache before running the emulation after patching
403
+ static void test_riscv64_code_patching_count () {
404
+ uc_engine * uc ;
405
+ char code [] = "\x93\x82\x12\x00" ; // addi t0, t0, 0x1
406
+ uc_common_setup (& uc , UC_ARCH_RISCV , UC_MODE_RISCV64 , code , sizeof (code ) - 1 );
407
+ // Zero out t0 and t1
408
+ uint64_t r_t0 = 0x0 ;
409
+ OK (uc_reg_write (uc , UC_RISCV_REG_T0 , & r_t0 ));
410
+ // emulate the instruction
411
+ OK (uc_emu_start (uc , code_start , -1 , 0 , 1 ));
412
+ // check value
413
+ OK (uc_reg_read (uc , UC_RISCV_REG_T0 , & r_t0 ));
414
+ TEST_CHECK (r_t0 == 0x1 );
415
+ // patch instruction
416
+ char patch_code [] = "\x93\x82\xf2\x7f" ; // addi t0, t0, 0x7FF
417
+ OK (uc_mem_write (uc , code_start , patch_code , sizeof (patch_code ) - 1 ));
418
+ OK (uc_ctl_remove_cache (uc , code_start , code_start + sizeof (patch_code ) - 1 ));
419
+ // zero out t0
420
+ r_t0 = 0x0 ;
421
+ OK (uc_reg_write (uc , UC_RISCV_REG_T0 , & r_t0 ));
422
+ OK (uc_emu_start (uc , code_start , -1 , 0 , 1 ));
423
+ // check value
424
+ OK (uc_reg_read (uc , UC_RISCV_REG_T0 , & r_t0 ));
425
+ TEST_CHECK (r_t0 != 0x1 );
426
+ TEST_CHECK (r_t0 == 0x7ff );
427
+
428
+ OK (uc_close (uc ));
429
+ }
430
+
375
431
static void test_riscv64_ecall_cb (uc_engine * uc , uint32_t intno , void * data )
376
432
{
377
433
uc_emu_stop (uc );
@@ -492,4 +548,6 @@ TEST_LIST = {{"test_riscv32_nop", test_riscv32_nop},
492
548
{"test_riscv32_mmio_map" , test_riscv32_mmio_map },
493
549
{"test_riscv64_mmio_map" , test_riscv64_mmio_map },
494
550
{"test_riscv32_map" , test_riscv32_map },
551
+ {"test_riscv64_code_patching" , test_riscv64_code_patching },
552
+ {"test_riscv64_code_patching_count" , test_riscv64_code_patching_count },
495
553
{NULL , NULL }};
0 commit comments