`rand()` stub returns 15 bits instead of newlib's 31 bits, which breaks game randomness (particles collapse to one point)

Aperta Adatta ai principianti
#440 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
74/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp

Direzione di ricerca

Il bug si trova nello stub rand in ps2xRuntime/src/lib/Kernel/Stubs/LibC.cpp, che maschera std::rand() con 0x7FFF. Leggi lo stub srand accanto e gli helper che usa (setReturnS32, getRegU32). Sostituisci entrambi con un LCG a 64 bit che mantiene uno stato statico inizializzato a 1, come descritto nell'issue. Completato quando rand restituisce valori a 31 bit, srand imposta lo stesso stato e i test esistenti del runtime continuano a passare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

ps2_stubs::rand (ps2xRuntime/src/lib/Kernel/Stubs/LibC.cpp) returns std::rand() & 0x7FFF. PS2 games link the
SDK's newlib, where rand() returns 31 bits (RAND_MAX = 0x7FFFFFFF). Game code that scales the result with 31-bit
math gets a nearly constant value, so anything random comes out identical.

void rand(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
{
    setReturnS32(ctx, std::rand() & 0x7FFF);
}
Observed in

Jet Li: Rise to Honor (SCUS-97279). Its particle helper RandomNum (0x270CC0) computes
(rand() - 0x3FFFFFFF) / 2^30 to get a value in [-1, 1). With a 15-bit rand() that is always about -1.0, so every
particle in a burst gets the same velocity, spin and fade. The glass-door shatter and smoke in level 1 were missing.
A GS capture showed the particles were drawn, but stacked on about 12 positions for 342 vertices, with alpha 0-5.
After the fix the effect renders, and ambient particles elsewhere had about 3x more live particles and normal alpha.

Why the stub is wrong in general
  • The 15-bit mask matches neither the guest ABI (newlib, 31 bits) nor non-MSVC hosts. glibc's RAND_MAX is
    2^31-1, so & 0x7FFF truncates there too.
  • std::rand() is host state, so the sequence differs between Windows, Linux and macOS builds and from the console.
    The guest's srand seed only reaches std::srand.
Suggested fix

Implement newlib's generator, a 64-bit LCG with seed 1, in the stubs, and have srand set the same state:

static uint64_t s_randNext = 1; // newlib's initial seed

void rand(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
{
    s_randNext = s_randNext * 6364136223846793005ull + 1;
    setReturnS32(ctx, static_cast<int32_t>((s_randNext >> 32) & 0x7FFFFFFF));
}

void srand(uint8_t *rdram, R5900Context *ctx, PS2Runtime *runtime)
{
    s_randNext = getRegU32(ctx, 4);
    setReturnS32(ctx, 0);
}

This is newlib's rand/srand (libc/stdlib/rand.c), so recompiled games then see the same sequence as on the
console. It is deterministic across host platforms too. (newlib keeps the state in the reent struct; one global is
equivalent for the single EE executor.)

How to check whether a game is affected

Search the generated code for ps2_stubs::rand( to find the game's rand address, then look at its callers. A
caller that subtracts about 0x3FFFFFFF, divides by 2^30 or 2^31, or masks with 0x7FFFFFFF expects 31 bits.

Lingua principale
C++
Stelle
3.3k
Fork
149
Merge medio
8h 46m
PR unite (30g)
1

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ran-j/PS2Recomp

Tutte le issue di ran-j/PS2Recomp

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.