Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(loader): preserve preferred module base across exact-alloc failure - #815

Open
semirkabir wants to merge 1 commit into
sharpemu:mainfrom
semirkabir:fix/module-preferred-base-clobber
Open

semirkabir wants to merge 1 commit into
sharpemu:mainfrom
semirkabir:fix/module-preferred-base-clobber

Conversation

@semirkabir

Copy link
Copy Markdown

Summary

Fixes a loader bug that sends additional modules (libc.prx, libSceNpCppWebApi.prx, etc.) to host memory instead of the guest window — and lies about it in the log.

The bug

SelfLoader.LoadCore calls:

else if (!TryAllocateAdditionalImageAtExact(physicalVm, imageBase, totalImageSize, isNextGen, out imageBase))

TryAllocateAdditionalImageAtExact only writes its out allocatedBase on success; the failure path does allocatedBase = 0; return false;. Because the caller passes imageBase itself as the out argument, a failed exact-placement attempt clobbers the preferred base to 0.

Two consequences:

  1. Fallback allocates anywhere instead of retrying the guest window. The fallback then calls AllocateAt(imageBase /* = 0 */, ...), and AllocateAt deliberately skips its fixed-address attempt when desiredAddress == 0 (the if (desiredAddress != 0) guard in PhysicalVirtualMemory.AllocateAt) — so it goes straight to "any address", landing the module in host memory. The original code clearly intended AllocateAt(imageBase) to retry the preferred base first (that's what the desiredAddress != 0 branch is for); the clobber silently disabled that intent.
  2. The diagnostic reports the wrong base. The log prints Could not allocate module at preferred base 0x0000000000000000 — but DetermineRequestedImageBase cannot return 0 for Gen5 (it returns Ps5MainImageBase or a search-range candidate ≥ Ps5ModuleSearchStart = 0x804000000). The 0x0 in the log is purely the clobber artifact, hiding the base that was actually requested and making real failures much harder to diagnose.

The fix

Preserve the requested base in a local before the call, pass a separate variable as the out argument, and use the preserved base for both the fallback allocation (so guest-window placement is retried before giving up) and the log line.

Verification

  • dotnet build SharpEmu.slnx -c Release → clean, 0 errors.
  • dotnet test SharpEmu.slnx -c Release --no-build891 passed, 0 failed.
  • Code-read verification: DetermineRequestedImageBase provably cannot return 0 for Gen5, so the preferred base 0x0 log lines seen in the wild (e.g. EA UFC 5 compatibility reports SharpEmuLog-PPSA03541-*, where libc.prx loaded at 0x2781CBF0000 host memory) are the clobber artifact this PR removes.

Testing

N/A — no game testing; the change affects the module-placement fallback path and its diagnostic only. On hosts where exact placement succeeds (the normal case), behavior is unchanged — the out param is written and the new else branch simply adopts it, identical to before.

Checklist

  • I have read and followed CONTRIBUTING.md.
  • I tested my changes or marked the testing section as N/A.
  • I wrote this pull request description myself and did not paste AI-generated explanations.
  • I listed the game(s) I tested (or marked the testing section as N/A).

TryAllocateAdditionalImageAtExact only writes its out-param on success,
so passing imageBase as `out` clobbered the preferred base to 0 on the
failure path. The fallback then called AllocateAt(0, ...), which skips
its fixed-address attempt entirely (the desiredAddress != 0 branch) and
allocates anywhere - putting additional modules (libc.prx, etc.) in host
memory instead of the guest window. The diagnostic also reported
'preferred base 0x0000000000000000', hiding the base that was actually
requested.

Preserve the requested base and use it for both the fallback (so the
guest-window placement is retried before giving up) and the log line.
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.

1 participant