feat(libkernel_sys): implement POSIX message queues #9297

Closed
DavidSamir wants to merge 14 commits from upstream-pr/9297 into main
DavidSamir commented 2026-10-21 17:45:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-21T17:45:25Z.


What

Builds on #9284 and implements POSIX message queues in a new libkernel_sys/Mqueue.cpp, following FreeBSD 11 sys/kern/uipc_mqueue.c. Until #9284 merges, this PR's diff also shows #9284's commits. This PR's own change is the last three commits.

Export Behaviour
kmq_open(path, flags, mode, const mq_attr*) see the rules below
kmq_timedsend(fd, msg, len, prio, abstime) EINVAL for prio >= MQ_PRIO_MAX (64); EMSGSIZE for len > mq_msgsize; EAGAIN when full with O_NONBLOCK; EBADF when not open for writing
kmq_timedreceive(fd, buf, len, prio*, abstime) the highest priority first, first in first out within a priority; returns the message length; EMSGSIZE for len < mq_msgsize; EAGAIN when empty with O_NONBLOCK; EBADF when not open for reading
kmq_setattr(fd, attr, oattr) only this descriptor's O_NONBLOCK changes, and other mq_flags bits give EINVAL; oattr gets the flags, maxmsg, msgsize and curmsgs
kmq_unlink(path) removes the name, ENOENT if it is absent; open descriptors keep the queue

kmq_open's rules:

  • EINVAL for (flags & O_ACCMODE) == O_ACCMODE or O_EXEC;
  • with O_CREAT and an attribute, mq_maxmsg must be 1..100 and mq_msgsize 1..16384, or EINVAL; without one, the defaults are 10 and 1024;
  • the name is copied with a 256-byte limit (ENAMETOOLONG) and must be /name with no second / (EINVAL); a NULL name gives EFAULT;
  • ENOENT for a missing name without O_CREAT, and EEXIST with O_CREAT | O_EXCL.

For both send and receive, as in mqueue_send and mqueue_receive, the abstime tv_nsec check (EINVAL) applies only when the call has to wait. A wait ends with ETIMEDOUT at the absolute CLOCK_REALTIME deadline. mq_attr is eight 64-bit fields.

Each open queue is a descriptor reserved on the host null device, carrying its own access mode and O_NONBLOCK, with a process-local name table. #9284's close observer drops a descriptor's entry when it is closed or replaced. kmq_notify follows in a separate PR. A debt line records the limits: process-local names, mode and permissions not modelled, queue descriptors not usable with select, poll or kevent, and dup not giving a queue.

This PR conflicts textually with #9293 at adjacent stubs (kmq_unlink and ksem_close in Export.Discovery.cpp). It is resolved by a merge of main after either lands.

Tested

Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. These are CPU-side exports, so no GPU is involved.

  • New guest_kernel_sys_mqueue (core/libs/tests/GuestKernelSysMqueue.cpp):
    • every kmq_open rule and the default attributes;
    • priority ordering and first in first out within a priority;
    • EMSGSIZE, and EAGAIN after kmq_setattr sets O_NONBLOCK;
    • an invalid abstime, accepted when there is room or a message but EINVAL when the call has to wait;
    • ETIMEDOUT after about 150 ms, and a blocked receiver woken by a send;
    • read-only and write-only descriptors (EBADF), and kmq_unlink while descriptors stay open;
    • closed and regular-file descriptors (EBADF).
  • With #9284's code the test aborts with kmq_open_nid_postfix not implemented. On this branch it passes, 3 of 3 runs. guest_kernel_sys_ksem and guest_kernel_sys still pass, and tools/check_conventions.py passes.
  • Overlap: tools/pr_overlap.py crashes on this machine's partial clone, so it was replaced by a check against an index of every open PR's diff plus git merge-tree. This branch merges cleanly with main and with #9160, #9195, #9162, #9161, #9156, #9171, #9273, #9242, #9019, #9008, #9097, #9141, #9103, #8987 and #7121. The one conflict is with #9293, as described above.

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: #9284
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4955880332 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9297). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-21T17:45:25Z. --- ### What Builds on #9284 and implements POSIX message queues in a new `libkernel_sys/Mqueue.cpp`, following FreeBSD 11 `sys/kern/uipc_mqueue.c`. **Until #9284 merges, this PR's diff also shows #9284's commits.** This PR's own change is the last three commits. | Export | Behaviour | |---|---| | `kmq_open(path, flags, mode, const mq_attr*)` | see the rules below | | `kmq_timedsend(fd, msg, len, prio, abstime)` | `EINVAL` for `prio >= MQ_PRIO_MAX` (64); `EMSGSIZE` for `len > mq_msgsize`; `EAGAIN` when full with `O_NONBLOCK`; `EBADF` when not open for writing | | `kmq_timedreceive(fd, buf, len, prio*, abstime)` | the highest priority first, first in first out within a priority; returns the message length; `EMSGSIZE` for `len < mq_msgsize`; `EAGAIN` when empty with `O_NONBLOCK`; `EBADF` when not open for reading | | `kmq_setattr(fd, attr, oattr)` | only this descriptor's `O_NONBLOCK` changes, and other `mq_flags` bits give `EINVAL`; `oattr` gets the flags, `maxmsg`, `msgsize` and `curmsgs` | | `kmq_unlink(path)` | removes the name, `ENOENT` if it is absent; open descriptors keep the queue | `kmq_open`'s rules: - `EINVAL` for `(flags & O_ACCMODE) == O_ACCMODE` or `O_EXEC`; - with `O_CREAT` and an attribute, `mq_maxmsg` must be 1..100 and `mq_msgsize` 1..16384, or `EINVAL`; without one, the defaults are 10 and 1024; - the name is copied with a 256-byte limit (`ENAMETOOLONG`) and must be `/name` with no second `/` (`EINVAL`); a NULL name gives `EFAULT`; - `ENOENT` for a missing name without `O_CREAT`, and `EEXIST` with `O_CREAT | O_EXCL`. For both send and receive, as in `mqueue_send` and `mqueue_receive`, the `abstime` `tv_nsec` check (`EINVAL`) applies only when the call has to wait. A wait ends with `ETIMEDOUT` at the absolute `CLOCK_REALTIME` deadline. `mq_attr` is eight 64-bit fields. Each open queue is a descriptor reserved on the host null device, carrying its own access mode and `O_NONBLOCK`, with a process-local name table. #9284's close observer drops a descriptor's entry when it is closed or replaced. `kmq_notify` follows in a separate PR. A debt line records the limits: process-local names, `mode` and permissions not modelled, queue descriptors not usable with `select`, `poll` or `kevent`, and `dup` not giving a queue. This PR conflicts textually with #9293 at adjacent stubs (`kmq_unlink` and `ksem_close` in `Export.Discovery.cpp`). It is resolved by a merge of `main` after either lands. ### Tested Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. These are CPU-side exports, so no GPU is involved. - New `guest_kernel_sys_mqueue` (`core/libs/tests/GuestKernelSysMqueue.cpp`): - every `kmq_open` rule and the default attributes; - priority ordering and first in first out within a priority; - `EMSGSIZE`, and `EAGAIN` after `kmq_setattr` sets `O_NONBLOCK`; - an invalid `abstime`, accepted when there is room or a message but `EINVAL` when the call has to wait; - `ETIMEDOUT` after about 150 ms, and a blocked receiver woken by a send; - read-only and write-only descriptors (`EBADF`), and `kmq_unlink` while descriptors stay open; - closed and regular-file descriptors (`EBADF`). - With #9284's code the test aborts with `kmq_open_nid_postfix not implemented`. On this branch it passes, 3 of 3 runs. `guest_kernel_sys_ksem` and `guest_kernel_sys` still pass, and `tools/check_conventions.py` passes. - Overlap: `tools/pr_overlap.py` crashes on this machine's partial clone, so it was replaced by a check against an index of every open PR's diff plus `git merge-tree`. This branch merges cleanly with `main` and with #9160, #9195, #9162, #9161, #9156, #9171, #9273, #9242, #9019, #9008, #9097, #9141, #9103, #8987 and #7121. The one conflict is with #9293, as described above. ### 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: #9284 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-21 17:46:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-21T17:46:19Z.


Progress report (0c6a5bd → d07deaa)

📈 System libraries: 42.21% (+0.11%, +12 implemented, -7 removed functions)

✅ 12 implemented
Library Name
libkernel ksem_destroy_nid_postfix
libkernel ksem_getvalue_nid_postfix
libkernel ksem_init_nid_postfix
libkernel ksem_post_nid_postfix
libkernel ksem_timedwait_nid_postfix
libkernel ksem_trywait_nid_postfix
libkernel ksem_wait_nid_postfix
libkernel_sys kmq_open_nid_postfix
libkernel_sys kmq_setattr_nid_postfix
libkernel_sys kmq_timedreceive_nid_postfix
libkernel_sys kmq_timedsend_nid_postfix
libkernel_sys kmq_unlink_nid_postfix
🗑️ 7 removed
Library Name
libkernel_sys ksem_destroy_nid_postfix
libkernel_sys ksem_getvalue_nid_postfix
libkernel_sys ksem_init_nid_postfix
libkernel_sys ksem_post_nid_postfix
libkernel_sys ksem_timedwait_nid_postfix
libkernel_sys ksem_trywait_nid_postfix
libkernel_sys ksem_wait_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6322645560 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9297#issuecomment-6322645560). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-21T17:46:19Z. --- <!-- progress-report --> ### Progress report (0c6a5bd → d07deaa) 📈 **System libraries**: 42.21% (+0.11%, +12 implemented, -7 removed functions) <details> <summary>✅ 12 implemented</summary> | Library | Name | | - | - | | libkernel | `ksem_destroy_nid_postfix` | | libkernel | `ksem_getvalue_nid_postfix` | | libkernel | `ksem_init_nid_postfix` | | libkernel | `ksem_post_nid_postfix` | | libkernel | `ksem_timedwait_nid_postfix` | | libkernel | `ksem_trywait_nid_postfix` | | libkernel | `ksem_wait_nid_postfix` | | libkernel_sys | `kmq_open_nid_postfix` | | libkernel_sys | `kmq_setattr_nid_postfix` | | libkernel_sys | `kmq_timedreceive_nid_postfix` | | libkernel_sys | `kmq_timedsend_nid_postfix` | | libkernel_sys | `kmq_unlink_nid_postfix` | </details> <details> <summary>🗑️ 7 removed</summary> | Library | Name | | - | - | | libkernel_sys | `ksem_destroy_nid_postfix` | | libkernel_sys | `ksem_getvalue_nid_postfix` | | libkernel_sys | `ksem_init_nid_postfix` | | libkernel_sys | `ksem_post_nid_postfix` | | libkernel_sys | `ksem_timedwait_nid_postfix` | | libkernel_sys | `ksem_trywait_nid_postfix` | | libkernel_sys | `ksem_wait_nid_postfix` | </details>
DavidSamir commented 2026-10-22 02:06:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T02:06:22Z.


Linux: Ubuntu 24.04.1 LTS (WSL2), GCC 13.3.0, Release; head d07deaa18 merged onto main 8c26eac3c.

  • guest_kernel_sys_ksem and guest_kernel_sys_mqueue passes 3/3.
  • No negative control is listed in this PR's text, so none was run.

Run from the PC (Windows host, Linux in WSL2), not from the PR author's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6330943189 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9297#issuecomment-6330943189). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-22T02:06:22Z. --- Linux: Ubuntu 24.04.1 LTS (WSL2), GCC 13.3.0, Release; head `d07deaa18` merged onto `main` `8c26eac3c`. - `guest_kernel_sys_ksem` and `guest_kernel_sys_mqueue` passes 3/3. - No negative control is listed in this PR's text, so none was run. Run from the PC (Windows host, Linux in WSL2), not from the PR author's machine.
harisibrar1 commented 2026-10-24 15:32:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T15:32:47Z.

Review: COMMENTED


Checked against main b607bfdc8b and FreeBSD 11.4.0 sys/kern/uipc_mqueue.c (kern_kmq_open, kern_kmq_setattr, mqueue_send/_mqueue_send, mqueue_receive/_mqueue_recv, sys_kmq_unlink); I reviewed only the mqueue commits:

  • Thanks, the open, setattr, send and receive checks and their order match FreeBSD closely, including the attribute limits and defaults, the 256-byte name limit, priority ordering, and the tv_nsec check only when the call has to wait.
  • kmq_timedreceive (Mqueue.cpp:233) copies into buffer without checking it. After a send, kmq_timedreceive(fd, NULL, 1024, NULL, NULL) writes through a null pointer, where FreeBSD's mqueue_savemsg copyout returns EFAULT. kmq_timedsend already returns EFAULT for NULL at :209; could receive do the same?
  • Deadline (:126-128) turns tv_sec into int64 nanoseconds, so a tv_sec above about 9.2e9 (e.g. {INT64_MAX, 0} used as "forever") is signed overflow. I couldn't run this; reading it suggests the deadline could wrap and time out at once, where FreeBSD subtracts the current time and clamps in tvtohz. Could the conversion saturate?
  • Small debt point: mqueue_alloc returns ENFILE once 100 queues exist (kern.mqueue.maxmq). Here there's no limit, so it may be worth adding to the debt line.
  • With current main, git merge-tree shows a conflict in libkernel_sys/Export.Discovery.cpp, where main added ktimer_* stubs next to the ksem_* ones. It's from the stacked #9293 part, but it will need resolving.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638796236 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9297#pullrequestreview-5638796236). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T15:32:47Z. **Review:** COMMENTED --- Checked against main b607bfdc8b and FreeBSD 11.4.0 `sys/kern/uipc_mqueue.c` (`kern_kmq_open`, `kern_kmq_setattr`, `mqueue_send`/`_mqueue_send`, `mqueue_receive`/`_mqueue_recv`, `sys_kmq_unlink`); I reviewed only the mqueue commits: - Thanks, the open, setattr, send and receive checks and their order match FreeBSD closely, including the attribute limits and defaults, the 256-byte name limit, priority ordering, and the `tv_nsec` check only when the call has to wait. - `kmq_timedreceive` (Mqueue.cpp:233) copies into `buffer` without checking it. After a send, `kmq_timedreceive(fd, NULL, 1024, NULL, NULL)` writes through a null pointer, where FreeBSD's `mqueue_savemsg` copyout returns `EFAULT`. `kmq_timedsend` already returns `EFAULT` for NULL at :209; could receive do the same? - `Deadline` (:126-128) turns `tv_sec` into int64 nanoseconds, so a `tv_sec` above about 9.2e9 (e.g. `{INT64_MAX, 0}` used as "forever") is signed overflow. I couldn't run this; reading it suggests the deadline could wrap and time out at once, where FreeBSD subtracts the current time and clamps in `tvtohz`. Could the conversion saturate? - Small debt point: `mqueue_alloc` returns `ENFILE` once 100 queues exist (`kern.mqueue.maxmq`). Here there's no limit, so it may be worth adding to the debt line. - With current main, `git merge-tree` shows a conflict in `libkernel_sys/Export.Discovery.cpp`, where main added `ktimer_*` stubs next to the `ksem_*` ones. It's from the stacked #9293 part, but it will need resolving. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
CnRJay commented 2026-11-01 20:01:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T20:01:19Z.

Review: COMMENTED


Correction (2026-11-01): this review's claim that maintainers close PRs without a title ID is wrong. CONTRIBUTING allows Unblocks: none; a title is only required when the PR fixes a title. The technical points below are unaffected.

Two points.

  1. No title evidence. The template says Unblocks: none and the body names no title ID and no log line showing a title reaching a kmq_* export on main. Export-only PRs without that tend to be closed, so a title ID plus the log line (queue at 700+ or the import being resolved) is needed here.

  2. kmq_timedreceive_nid_postfix does not check buffer. kmq_timedsend_nid_postfix returns EFAULT for a null message with a non-zero length, but the receive side only checks length < msgsize and then does std::memcpy(buffer, entry.bytes.data(), entry.bytes.size()) after the message has already been dequeued. A null buffer crashes the host instead of failing the call, and the message is lost. The pointer should be validated before the message is removed from queue.messages.

The unsynchronised Open::nonblocking write in kmq_setattr_nid_postfix that oneandonlydean raised is still there in this head as far as I can see: open->nonblocking = ... is assigned outside queue.lock while WaitFor reads it under it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729961663 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9297#pullrequestreview-5729961663). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T20:01:19Z. **Review:** COMMENTED --- **Correction (2026-11-01):** this review's claim that maintainers close PRs without a title ID is wrong. CONTRIBUTING allows `Unblocks: none`; a title is only required when the PR fixes a title. The technical points below are unaffected. Two points. 1. **No title evidence.** The template says `Unblocks: none` and the body names no title ID and no log line showing a title reaching a `kmq_*` export on main. Export-only PRs without that tend to be closed, so a title ID plus the log line (queue at 700+ or the import being resolved) is needed here. 2. **`kmq_timedreceive_nid_postfix` does not check `buffer`.** `kmq_timedsend_nid_postfix` returns `EFAULT` for a null `message` with a non-zero length, but the receive side only checks `length < msgsize` and then does `std::memcpy(buffer, entry.bytes.data(), entry.bytes.size())` after the message has already been dequeued. A null `buffer` crashes the host instead of failing the call, and the message is lost. The pointer should be validated before the message is removed from `queue.messages`. The unsynchronised `Open::nonblocking` write in `kmq_setattr_nid_postfix` that oneandonlydean raised is still there in this head as far as I can see: `open->nonblocking = ...` is assigned outside `queue.lock` while `WaitFor` reads it under it.
MarcZX10 commented 2026-11-23 02:21:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T02:21:15Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

Changes are required before merge: validate kmq_timedreceive output pointers before removing a message from the queue.

What I ran

Reviewed head d07deaa187134d4e2482a5f48aeea9f1f5601ab3 using the prepared diff and repo_read.py at the pinned head. No commands or tests were run by me; reported test results below are from the PR description.

Results

Check Result Note
Source review passed Changed implementation and tests read
Automated checks not run PR description reports passing tests
Console behavior not run CPU-side implementation only

Findings

Blocking

  • core/libs/prx/libkernel_sys/Mqueue.cpp:229 removes the message before checking buffer. If the caller supplies an invalid destination pointer, the subsequent copy at line 233 can fault, and the queued message is already lost. Validate writable output pointers before dequeuing, and return EFAULT without consuming the message when invalid.

Non-blocking

None.

Not verified

No tests or build were run during this review. Windows, other host platforms, and physical-console behavior were not independently verified. All changed files listed in the manifest were read; no files were excluded.

To get this to a good state

Validate the receive buffer before removing a message, then rerun the message queue tests and relevant build checks on the updated head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6015888508 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9297#pullrequestreview-6015888508). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T02:21:15Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested Changes are required before merge: validate `kmq_timedreceive` output pointers before removing a message from the queue. ## What I ran Reviewed head `d07deaa187134d4e2482a5f48aeea9f1f5601ab3` using the prepared diff and `repo_read.py` at the pinned head. No commands or tests were run by me; reported test results below are from the PR description. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Changed implementation and tests read | | Automated checks | not run | PR description reports passing tests | | Console behavior | not run | CPU-side implementation only | ## Findings ### Blocking - `core/libs/prx/libkernel_sys/Mqueue.cpp:229` removes the message before checking `buffer`. If the caller supplies an invalid destination pointer, the subsequent copy at line 233 can fault, and the queued message is already lost. Validate writable output pointers before dequeuing, and return `EFAULT` without consuming the message when invalid. ### Non-blocking None. ## Not verified No tests or build were run during this review. Windows, other host platforms, and physical-console behavior were not independently verified. All changed files listed in the manifest were read; no files were excluded. ## To get this to a good state Validate the receive buffer before removing a message, then rerun the message queue tests and relevant build checks on the updated head.

Pull request closed

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!9297
No description provided.