From 1e4ef3c8802143795e7f4f4e2fee04f995412793 Mon Sep 17 00:00:00 2001 From: ChinYikMing Date: Mon, 17 Jun 2024 20:41:33 +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 initialize ir->branch_table->PC[i] to 0x0. The 0x0 address might be not yet translated to a valid block, thus ir->branch_table->target[i] might be NULL, calling a NULL function pointer cause segmentation fault. It can be solved by initializing ir->branch_table->PC will 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 0152896c4..3e6bf1e36 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; }