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

オープン 初心者向け
#440 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
74/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
cpp

調査の方向性

バグは ps2xRuntime/src/lib/Kernel/Stubs/LibC.cpp の rand スタブにあり、std::rand() を 0x7FFF でマスクしています。隣の srand スタブと、それが使うヘルパー(setReturnS32、getRegU32)を読んでください。両方を、issue に記載のとおり 1 で初期化された静的状態を保持する 64 ビット LCG に置き換えてください。rand が 31 ビットの値を返し、srand が同じ状態を設定し、既存のランタイムテストが引き続き通れば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
C++
スター
3.3k
フォーク
149
平均マージ
8時間 46分
マージ済み PR(30日)
1

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ran-j/PS2Recomp のほかの issue

ran-j/PS2Recomp の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。