Skip to content

Commit

Permalink
Initialize ir->branch_table->PC with safe value
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ChinYikMing committed Jun 17, 2024
1 parent ef151e2 commit b65d24c
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit b65d24c

Please # to comment.