Skip to content

Recompiler: VCALLMSR reads CMSAR0 instead of out-of-bounds vi[27] - #444

Open
Miquelzorno wants to merge 1 commit into
ran-j:mainfrom
Miquelzorno:fix/vcallmsr-cmsar0
Open

Miquelzorno wants to merge 1 commit into
ran-j:mainfrom
Miquelzorno:fix/vcallmsr-cmsar0

Conversation

@Miquelzorno

Copy link
Copy Markdown

Fixes #262.

vcallmsr is always written as vcallmsr $vi27, so the emitter formatted ctx->vi[27]. But vi is uint16_t vi[16], which makes that an out-of-bounds read (undefined behaviour). It returns whatever sits after the array, so the microprogram starts at a garbage address.

On hardware, VCALLMSR always starts at CMSAR0, whatever the operand says. The $vi27 is just CMSAR0's number in the control-register space. CTC2/CFC2 already keep CMSAR0 in ctx->vu0_cmsar0, so VCALLMSR now reads it from there:

uint16_t instr_index = static_cast<uint16_t>(ctx->vu0_cmsar0 & 0x1FF);

About the vi[32] approach suggested in the issue: making vi 32 entries wide would also fix this, and it matches how PCSX2 lays out the register file. But every other control register in this codebase lives in a named field (vu0_status, vu0_fbrst, vu0_cmsar0…), and CFC2/CTC2 apply per-register semantics on top of them (masks, TPC >> 3, read-only registers). Moving to vi[32] means changing the R5900Context layout and keeping two copies of each register in sync. That is a bigger change, better done as its own PR if you want it. This PR is the minimal fix that matches the current design: vu0_cmsar0 is already the single source of truth that ctc2 $rt, $vi27 writes.

Changes

  • ps2xRecomp/src/lib/vu_translation_helpers.cpp: translateVU_VCALLMSR reads ctx->vu0_cmsar0 instead of ctx->vi[inst.rd].
  • ps2xTest/src/code_generator_tests.cpp: new test checking that VCALLMSR emits a vu0_cmsar0 read and no vi[ index.

Testing

vcallmsr is always written "vcallmsr $vi27", so the emitter formatted
ctx->vi[27] on the 16-entry vi array: an out-of-bounds read. On hardware
VCALLMSR always starts at CMSAR0, which CTC2/CFC2 already keep in
ctx->vu0_cmsar0, so read it from there.

Fixes ran-j#262.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recompiler: vcallmsr reads ctx->vi[27], out of bounds on uint16_t vi[16], instead of ctx->vu0_cmsar0

1 participant