sharpemu icon indicating copy to clipboard operation
sharpemu copied to clipboard

[HLE] Add strchr/strrchr/memchr/strcat/strncat/strstr libc exports

Open j92580498-max opened this issue 4 months ago • 0 comments

Summary

Adds six missing libc string/memory search exports to the libc module so that titles which rely on standard C string handling get correct HLE behavior instead of hitting an unresolved import:

  • strchr — locate first occurrence of a byte (NUL matches the terminator)
  • strrchr — locate last occurrence of a byte
  • memchr — locate a byte within a bounded buffer
  • strcat — append source string to destination, returns destination
  • strncat — append at most n bytes of source, always NUL-terminates, returns destination
  • strstr — locate first occurrence of a substring (empty needle returns the haystack)

These are pure, self-contained functions that games and middleware use constantly (metadata parsing, path handling, string building). They follow the exact pattern of the existing strcpy/strncpy/strcmp/memcmp exports in KernelMemoryCompatExports.cs: guest memory is accessed through the CpuContext compat helpers, arguments come from the SysV AMD64 registers (rdi, rsi, rdx), and results are returned in rax.

Background

The PS5 runs an x86-64 AMD "Zen 2" CPU with a FreeBSD-derived kernel, so its user-mode libc follows the standard System V AMD64 calling convention (integer args in rdi/rsi/rdx/..., return value in rax). The exports here mirror that convention and match the semantics documented for standard C <string.h> functions.

NIDs

Each export's NID was produced with the project's own symbol-hashing scheme (SHA-1 over symbol + suffix key, first 8 bytes, base64url), the same generator used by scripts/generate_aerolib_binary.py. Verified against the already-shipped exports (strlen, strcmp, strcpy, memcpy all reproduce their in-tree NIDs with this generator) and confirmed unique across the codebase — no collisions with existing entries.

Export NID
strchr ob5xAW4ln-0
strrchr 9yDWMxEFdJU
memchr 8u8lPzUEq+U
strcat Ls4tzzhimqQ
strncat kHg45qPC6f0
strstr viiwFMaNamA

All are registered for both Generation.Gen4 and Generation.Gen5 under LibraryName = "libc", consistent with the neighboring string exports.

Testing

  • dotnet build SharpEmu.slnx -c Release — succeeds with 0 warnings / 0 errors.
  • Wrote a standalone harness that implements ICpuMemory over a flat guest heap, drives each export through a real CpuContext, and asserts both the returned rax pointer/value and the resulting guest memory against native libc semantics. 17 cases pass, covering the important edge cases:
    • strchr matching the trailing NUL; missing char returns NULL.
    • strrchr last-match and missing-char (NULL).
    • memchr honoring the count bound (no over-read) and finding a NUL byte.
    • strcat / strncat returning the destination pointer, correct concatenation, strncat stopping at the source terminator and always NUL-terminating.
    • strstr locating a substring, missing needle returning NULL, empty needle returning the haystack, and a match at the very end.

Notes

Bounds and fault handling use the same TryRead*/TryWrite* compat helpers as the surrounding code, so an out-of-range guest pointer yields the project's standard memory-fault result rather than a host crash.

j92580498-max avatar Jul 14 '26 11:07 j92580498-max