From 0ca87a4807b4d26baef3b7813e1b948b88dddf79 Mon Sep 17 00:00:00 2001 From: David Guillen Fandos Date: Mon, 5 Jul 2021 18:19:19 +0200 Subject: Fix conditional ARM instructions at the end of a translation block This fixes issue #133 The explanation is as follows. Most blocks end on an inconditional jump/branch, but there's two cases where this doesn't happen: translation gates and when we hit MAX_EXITS. These are very uncommon cases and therefore more prone to hidden bugs. When this happens, the last instruction emits a conditional jump (via arm_conditional_block_header macro) which is patched by a later instruction via generate_branch_patch_conditional. Typically the last unconditional branch will trigger the patching condition (which is aproximately condition != last_condition), but in these two cases it might not happen, leaving an unpatched branch. This makes x86 and ARM dynarecs crash in interesting ways (although it might not crash depending on $stuff and make the bug even harder to track). --- cpu_threaded.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cpu_threaded.c b/cpu_threaded.c index 557b197..2d61b4a 100644 --- a/cpu_threaded.c +++ b/cpu_threaded.c @@ -3115,6 +3115,12 @@ s32 translate_block_arm(u32 pc, translation_region_type generate_cycle_update(); } } + + /* This can happen if the last instruction is *not* inconditional */ + if ((last_condition & 0x0F) != 0x0E) { + generate_branch_patch_conditional(backpatch_address, translation_ptr); + } + for(i = 0; i < translation_gate_targets; i++) { if(pc == translation_gate_target_pc[i]) -- cgit v1.2.3