From b65d24c0adb4f2e222a8a70639ce5af36819c15f Mon Sep 17 00:00:00 2001 From: ChinYikMing Date: Mon, 17 Jun 2024 22:36:53 +0800 Subject: [PATCH] Initialize ir->branch_table->PC with safe value If the ra(return address) is 0x0, the LOOKUP_OR_UPDATE_BRANCH_HISTORY_TABLE will bahave abnormally since calloc initializes ir->branch_table->PC[i] to 0x0. The address 0x0 might be not yet translated to a valid block, thus ir->branch_table->target[i] might be NULL, accessing a NULL pointer causes segmentation fault. It can be solved by initializing ir->branch_table->PC with other value than 0x0. Here, I choose unsigned integer of -1. Close #461 --- src/emulate.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/emulate.c b/src/emulate.c index 0152896c..3e6bf1e3 100644 --- a/src/emulate.c +++ b/src/emulate.c @@ -617,6 +617,8 @@ static void block_translate(riscv_t *rv, block_t *block) ) { ir->branch_table = calloc(1, sizeof(branch_history_table_t)); assert(ir->branch_table); + memset(ir->branch_table->PC, -1, + sizeof(uint32_t) * HISTORY_SIZE); } break; }