Skip to content

fix(libSceNgs2.native): safely probe outputs in sceNgs2RackGetVoiceHandle and sceNgs2CustomRackGetModuleInfo - #20418

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
milescoviello:fix/ngs2-probe-rack-voice-handle-and-module-info
Nov 15, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
milescoviello:fix/ngs2-probe-rack-voice-handle-and-module-info

Conversation

@milescoviello

@milescoviello milescoviello commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What

sceNgs2RackGetVoiceHandle stored the voice handle with *handle = ... and sceNgs2CustomRackGetModuleInfo copied the module info with *info = ..., so a non-null output pointing at unmapped memory faulted the host once the rack and index had checked out.

Both now write through the guest copy helpers and return SCE_NGS2_ERROR_INVALID_OUT_ADDRESS (0x804A8010, what each already returns for a null output) when the write fails, with errno left as the caller had it. The rack, voice index, module index and info size checks keep their order, and valid outputs get the same values.

Uses the helpers that #20401 (merged) moved into Ngs2Internal.hpp (independent of #20402). The debt entry, next to the other Rack.cpp entry, records the unmapped-memory result as a host-safety fallback; console behaviour there is not measured. It is placed away from the custom-rack entries that #18501 and #18464 edit.

Tested

Linux (Ubuntu 26.04.1), GCC 15.2, Release.

  • ngs2_voicehandles: sceNgs2RackGetVoiceHandle on a live sampler rack with the unmapped output 0x10 returns 0x804A8010 and leaves errno unchanged.
  • ngs2_custom: sceNgs2CustomRackGetModuleInfo on a live custom rack with the unmapped info 0x10 returns 0x804A8010 and leaves errno unchanged; the existing module-info and null-pointer cases still pass.
  • ctest -R '^ngs2_(voicehandles|custom|handleerrors|report|atrac9)$': 5/5 pass.
  • tools/check_conventions.py passes on the branch and this body.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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

milescoviello and others added 2 commits November 15, 2026 00:14
…ResetOption and sceNgs2SystemQueryBufferSize

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ndle and sceNgs2CustomRackGetModuleInfo

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 6f42c1ccc6:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: ngs2_core_custom_tests, ngs2_custom_tests, ngs2_handleerrors_tests, ngs2_voicehandles_tests built cleanly.
  • CTest (ngs2_core_custom_safety_custom_callback, ngs2_core_custom_safety_upstream_callback, ngs2_core_custom_safety_configured_gain, ngs2_core_custom_safety_routing_one_buffer, ngs2_core_custom_safety_routing_two_buffers, ngs2_core_custom, ngs2_custom_safety_custom_callback, ngs2_custom_safety_upstream_callback, ngs2_custom_safety_configured_gain, ngs2_custom_safety_routing_one_buffer, ngs2_custom_safety_routing_two_buffers, ngs2_custom, ngs2_handleerrors, ngs2_voicehandles):
    4/14 Test #4227: ngs2_core_custom_safety_routing_one_buffer ....   Passed    0.27 sec
    5/14 Test #4228: ngs2_core_custom_safety_routing_two_buffers ...   Passed    0.28 sec
    6/14 Test #4236: ngs2_custom_safety_custom_callback ............   Passed    0.24 sec
    7/14 Test #4237: ngs2_custom_safety_upstream_callback ..........   Passed    0.23 sec
    8/14 Test #4238: ngs2_custom_safety_configured_gain ............   Passed    0.22 sec
    9/14 Test #4239: ngs2_custom_safety_routing_one_buffer .........   Passed    0.21 sec
    10/14 Test #4240: ngs2_custom_safety_routing_two_buffers ........   Passed    0.25 sec
    11/14 Test #4235: ngs2_core_custom ..............................   Passed    0.69 sec
    12/14 Test #4264: ngs2_voicehandles .............................   Passed    0.31 sec
    13/14 Test #4263: ngs2_handleerrors .............................   Passed    0.23 sec
    14/14 Test #4245: ngs2_custom ...................................   Passed    1.77 sec
    100% tests passed, 0 tests failed out of 14
    
  • Negative control: with core/libs/prx/libSceNgs2.native/src/Atrac9.cpp, core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp, core/libs/prx/libSceNgs2.native/src/Rack.cpp, core/libs/prx/libSceNgs2.native/src/System.cpp reverted to main and the new tests kept, the suite fails:
    8/14 Test #4236: ngs2_custom_safety_custom_callback ............   Passed    0.03 sec
    9/14 Test #4238: ngs2_custom_safety_configured_gain ............   Passed    0.02 sec
    10/14 Test #4239: ngs2_custom_safety_routing_one_buffer .........   Passed    0.02 sec
    11/14 Test #4225: ngs2_core_custom_safety_upstream_callback .....   Passed    0.01 sec
    12/14 Test #4264: ngs2_voicehandles .............................***Exception: SegFault  0.58 sec
    13/14 Test #4224: ngs2_core_custom_safety_custom_callback .......   Passed    0.02 sec
    14/14 Test #4263: ngs2_handleerrors .............................***Exception: SegFault  0.66 sec
    71% tests passed, 4 tests failed out of 14
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified in core/libs/prx/libSceNgs2.native/src/Rack.cpp that sceNgs2RackGetVoiceHandle validates the rack handle and voice_id bounds before attempting WriteGuest, ensuring out-of-range voice indices return SCE_NGS2_ERROR_INVALID_VOICE_INDEX (0x804A8305) without probing or mutating the output pointer.
    • Verified in core/libs/prx/libSceNgs2.native/src/Rack.cpp that sceNgs2CustomRackGetModuleInfo checks for null output, validates the rack handle, verifies module index bounds and exact info_size before safely writing through WriteGuest, returning SCE_NGS2_ERROR_INVALID_OUT_ADDRESS on unmapped addresses.
    • Verified in core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp that ReadGuestBytes, WriteGuestBytes, ReadGuest, and WriteGuest preserve errno across copy faults using savedErrno and are correctly declared with inline template linkage.
    • Verified in core/libs/prx/libSceNgs2.native/src/Rack.cpp that Ngs2Lock is held across the find, validate, and copy operations in both functions, preventing concurrent modification or destruction races.
    • Verified in core/libs/prx/libSceNgs2.native/tests/Ngs2VoiceHandles.cpp and Ngs2Custom.cpp that unit tests exercise unmapped outputs (0x10) against live racks, asserting SCE_NGS2_ERROR_INVALID_OUT_ADDRESS and checking errno preservation (errno == 71).
    • Verified in docs/dev/debt/libSceNgs2.native.md that technical debt documentation records the host-safety fallback on unmapped addresses in accordance with repository conventions, with no comments added in source code.
  • Minor: please drop the Generated with footer from the description; the AI-assisted checkbox can stay.

Looks Good To Me

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@milescoviello

Copy link
Copy Markdown
Contributor Author

Thanks. Dropped the footer. I also moved the debt entry into docs/dev/debt/changes/fix-ngs2-probe-rack-voice-handle-and-module-info.md in a docs-only commit, 9efae65c28, per the fragment rule from #17217, which landed after this branch was cut. No code changed. tools/debt_fold.py --check passes on main (c802c51261) with it applied.

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 9efae65c28:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: ngs2_core_custom_tests, ngs2_custom_tests, ngs2_handleerrors_tests, ngs2_voicehandles_tests built cleanly.
  • CTest (ngs2_core_custom_safety_custom_callback, ngs2_core_custom_safety_upstream_callback, ngs2_core_custom_safety_configured_gain, ngs2_core_custom_safety_routing_one_buffer, ngs2_core_custom_safety_routing_two_buffers, ngs2_core_custom, ngs2_custom_safety_custom_callback, ngs2_custom_safety_upstream_callback, ngs2_custom_safety_configured_gain, ngs2_custom_safety_routing_one_buffer, ngs2_custom_safety_routing_two_buffers, ngs2_custom, ngs2_handleerrors, ngs2_voicehandles):
    4/14 Test #4224: ngs2_core_custom_safety_custom_callback .......   Passed    0.04 sec
    5/14 Test #4226: ngs2_core_custom_safety_configured_gain .......   Passed    0.05 sec
    6/14 Test #4228: ngs2_core_custom_safety_routing_two_buffers ...   Passed    0.04 sec
    7/14 Test #4225: ngs2_core_custom_safety_upstream_callback .....   Passed    0.03 sec
    8/14 Test #4245: ngs2_custom ...................................   Passed    0.30 sec
    9/14 Test #4239: ngs2_custom_safety_routing_one_buffer .........   Passed    0.04 sec
    10/14 Test #4240: ngs2_custom_safety_routing_two_buffers ........   Passed    0.04 sec
    11/14 Test #4237: ngs2_custom_safety_upstream_callback ..........   Passed    0.03 sec
    12/14 Test #4238: ngs2_custom_safety_configured_gain ............   Passed    0.02 sec
    13/14 Test #4236: ngs2_custom_safety_custom_callback ............   Passed    0.03 sec
    14/14 Test #4264: ngs2_voicehandles .............................   Passed    0.03 sec
    100% tests passed, 0 tests failed out of 14
    
  • Negative control: with core/libs/prx/libSceNgs2.native/src/Atrac9.cpp, core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp, core/libs/prx/libSceNgs2.native/src/Rack.cpp, core/libs/prx/libSceNgs2.native/src/System.cpp reverted to main and the new tests kept, the suite fails:
    8/14 Test #4224: ngs2_core_custom_safety_custom_callback .......   Passed    0.15 sec
    9/14 Test #4225: ngs2_core_custom_safety_upstream_callback .....   Passed    0.15 sec
    10/14 Test #4237: ngs2_custom_safety_upstream_callback ..........   Passed    0.13 sec
    11/14 Test #4236: ngs2_custom_safety_custom_callback ............   Passed    0.14 sec
    12/14 Test #4238: ngs2_custom_safety_configured_gain ............   Passed    0.14 sec
    13/14 Test #4263: ngs2_handleerrors .............................   Passed    0.14 sec
    14/14 Test #4264: ngs2_voicehandles .............................***Exception: SegFault  3.25 sec
    79% tests passed, 3 tests failed out of 14
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libSceNgs2.native/src/Rack.cpp: Verified that sceNgs2RackGetVoiceHandle writes voice handles via WriteGuest, returning SCE_NGS2_ERROR_INVALID_OUT_ADDRESS (0x804A8010) on unwritable outputs while maintaining rack handle, null check, and voice index validation precedence.
    • core/libs/prx/libSceNgs2.native/src/Rack.cpp: Verified that sceNgs2CustomRackGetModuleInfo writes Ngs2CustomModuleInfo through WriteGuest, returning SCE_NGS2_ERROR_INVALID_OUT_ADDRESS on write failure while maintaining module index and size checks.
    • core/libs/prx/libSceNgs2.native/src/Rack.cpp: Verified concurrency and thread safety under Ngs2Lock, ensuring rack lookup and access to rack->voices and rack->customModules remain protected without deadlocks.
    • core/libs/prx/libSceNgs2.native/tests/Ngs2VoiceHandles.cpp and Ngs2Custom.cpp: Verified that unit tests test unmapped destination pointer 0x10, assert SCE_NGS2_ERROR_INVALID_OUT_ADDRESS, and confirm caller errno is preserved unchanged.
    • AGENTS.md & technical debt: Verified zero code comments were added, and host-safety fallback behavior on unmapped memory is properly documented in docs/dev/debt/changes/fix-ngs2-probe-rack-voice-handle-and-module-info.md.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

@oneandonlydean
oneandonlydean merged commit 097c389 into boykopovar:main Nov 15, 2026
16 checks passed
@github-actions

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1384 core/libs/prx/libSceNgs2.native/src/Rack.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants