`rand()` stub returns 15 bits instead of newlib's 31 bits, which breaks game randomness (particles collapse to one point)
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
- Ambito
- reverse-engineering
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_MAXis
2^31-1, so& 0x7FFFtruncates there too. std::rand()is host state, so the sequence differs between Windows, Linux and macOS builds and from the console.
The guest'ssrandseed only reachesstd::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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di ran-j/PS2Recomp
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
I maintainer di solito rispondono entro 3 giorni
-
PSLLH/PSRLH/PSRAH: shift amounts 16-31 should use sa[3:0] (SSE gives 0 / sign fill)Forse già presa @dean-k-pearce l’ha presa 4 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 3 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
ran-j/PS2Recomp#393 · 1 commento ·
I maintainer di solito rispondono entro 3 giorni
-
EE sceCdStatus stub always reports READ (6), hanging games that wait for PAUSEForse già presa @ahmedgamalalzatary l’ha presa 30 giorni fa. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
I maintainer di solito rispondono entro 3 giorni
-
VU0 macro VSQI / VSQD: address and source registers swapped in generated codeForse già presa @yanm1103 l’ha presa 38 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 3 giorni
Tutte le issue di ran-j/PS2Recomp
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 80/100
ESPresense/ESPresense#2680 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 Meno di un'ora Idoneità per principianti 75/100
stew675/llama-cpp-rdna-boosts#348 ·
I maintainer di solito rispondono entro 1 giorno
-
Documentation: Add comprehensive newcomer onboarding guide, project structure, and local usage to READMEForse già presa @Ash-Blanc l’ha presa oggi. Apertaarea/other status/bot-triaged status/need-triage
Difficoltà 2/5 Mezza giornata Idoneità per principianti 85/100
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno