feat(libc): implement timingsafe_bcmp #12586

Merged
voidlakhan merged 2 commits from upstream-pr/12586 into main 2026-11-02 07:54:59 +00:00
voidlakhan commented 2026-10-28 21:30:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by voidlakhan on 2026-10-28T21:30:56Z.


What

Implement timingsafe_bcmp (NID aWl82o9ammc) in libc matching FreeBSD 11.4 behavior, and add test coverage in GuestStrings.cpp.

Reference:
https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/string/timingsafe_bcmp.c

Tested

Windows 10, core/libs/tests/GuestStrings.cpp checking equal, non-equal at start/middle/end, reversed order, sub-lengths, empty buffers, and high-bit byte sequences.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5058006927 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12586). Originally posted by [voidlakhan](https://github.com/voidlakhan) on 2026-10-28T21:30:56Z. --- ### What Implement `timingsafe_bcmp` (NID `aWl82o9ammc`) in `libc` matching FreeBSD 11.4 behavior, and add test coverage in `GuestStrings.cpp`. Reference: https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/string/timingsafe_bcmp.c ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 10, `core/libs/tests/GuestStrings.cpp` checking equal, non-equal at start/middle/end, reversed order, sub-lengths, empty buffers, and high-bit byte sequences. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
rooklz commented 2026-10-28 22:15:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T22:15:50Z.

Review: COMMENTED


I ran a host-side check on macOS arm64 (Apple clang, -fsanitize=address,undefined). The harness holds the function body from 33b5bee04d and the 11 new Require lines word for word. They all pass, and the NID aWl82o9ammc recomputes from timingsafe_bcmp.

Mutants against the new assertions:

Mutant Result
loop starts at 1 killed (safeD)
loop stops one byte early killed
& instead of ^ killed
ret == 0 inverted killed
p1[i] - p2[i] instead of ^ equivalent (non-zero exactly when the bytes differ)
return ret; (raw OR of the XORs) survives
return -(ret != 0); survives

The body follows FreeBSD 11.4, where the function returns (ret != 0), so it gives exactly 1 when the buffers differ. The tests only check != 0, so nothing would catch a change that returns some other non-zero value. If 1 is the intended contract, two lines pin it down and kill both survivors:

     Require(timingsafe_bcmp_nid_postfix(highB, highA, 2) != 0);
+    Require(timingsafe_bcmp_nid_postfix(safeA, safeB, 6) == 1);
+    Require(timingsafe_bcmp_nid_postfix(highA, highB, 2) == 1);

Apart from that, and the licence notice @oneandonlydean asked for, this looks right to me. I did not build guest_strings itself (the macOS lane here has no submodules), and I did not test on a console.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5690409567 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12586#pullrequestreview-5690409567). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-28T22:15:50Z. **Review:** COMMENTED --- I ran a host-side check on macOS arm64 (Apple clang, `-fsanitize=address,undefined`). The harness holds the function body from 33b5bee04d and the 11 new `Require` lines word for word. They all pass, and the NID `aWl82o9ammc` recomputes from `timingsafe_bcmp`. Mutants against the new assertions: | Mutant | Result | | - | - | | loop starts at 1 | killed (`safeD`) | | loop stops one byte early | killed | | `&` instead of `^` | killed | | `ret == 0` inverted | killed | | `p1[i] - p2[i]` instead of `^` | equivalent (non-zero exactly when the bytes differ) | | `return ret;` (raw OR of the XORs) | **survives** | | `return -(ret != 0);` | **survives** | The body follows FreeBSD 11.4, where the function returns `(ret != 0)`, so it gives exactly 1 when the buffers differ. The tests only check `!= 0`, so nothing would catch a change that returns some other non-zero value. If 1 is the intended contract, two lines pin it down and kill both survivors: ```diff Require(timingsafe_bcmp_nid_postfix(highB, highA, 2) != 0); + Require(timingsafe_bcmp_nid_postfix(safeA, safeB, 6) == 1); + Require(timingsafe_bcmp_nid_postfix(highA, highB, 2) == 1); ``` Apart from that, and the licence notice @oneandonlydean asked for, this looks right to me. I did not build `guest_strings` itself (the macOS lane here has no submodules), and I did not test on a console. AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!12586
No description provided.