Skip to content

Dropped requirement for elevation on input report wait event - #431

Merged
nefarius merged 2 commits into
masterfrom
ipc-no-elevation
Apr 5, 2026
Merged

nefarius merged 2 commits into
masterfrom
ipc-no-elevation

Conversation

@nefarius

@nefarius nefarius commented Apr 5, 2026 •

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • Documentation

    • Clarified input-report timeout behavior and privilege guidance: timeout now covers missing per-slot wait objects and admin elevation is no longer required for event-based reads; updated wording around a legacy access-denied message.
  • Refactor

    • Switched to per-device deterministic wait events with improved caching and cleanup for more reliable input-report handling and resource management.

@coderabbitai

coderabbitai Bot commented Apr 5, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bcd9f0d1-adca-4f2b-b752-e326ccebc6d7

📥 Commits

Reviewing files that changed from the base of the PR and between 27ca8e4 and 616574f.

📒 Files selected for processing (1)
  • SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs
🚧 Files skipped from review as they are similar to previous changes (1)
  • SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs

📝 Walkthrough

Walkthrough

Refactors HID input-report wait handling from driver-duplicated handles to per-device named auto-reset events; SDK now opens per-slot named events directly and caches them, driver creates deterministic per-slot event names and exposes no handle-duplication IPC path.

Changes

Cohort / File(s) Summary
SDK Event Handling Refactor
SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs, SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.Commands.cs
Replaced singleton _inputReportEvent with ConcurrentDictionary<int, EventWaitHandle> _inputReportWaitEvents. Added HidReportWaitEventNamePrefix and GetOrOpenHidReportWaitEvent(int). GetRawInputReport now computes per-device message pointer via byteOffset + pMessage, waits on per-slot named event with WaitOne(timeout) and returns false when the named event cannot be opened.
SDK IPC Contracts
SDK/Nefarius.DsHidMini.IPC/Models/Enums.cs, SDK/Nefarius.DsHidMini.IPC/Models/CmdStructs.cs, SDK/Nefarius.DsHidMini.IPC/NativeMethods.txt
Removed IPC enum member for GET_HID_WAIT_HANDLE and its response struct. Removed native declarations related to handle duplication (OpenProcess, DuplicateHandle, GetCurrentProcess, DUPLICATE_HANDLE_OPTIONS, WIN32_ERROR).
SDK Documentation & Exceptions
SDK/Nefarius.DsHidMini.IPC/Exceptions/Exceptions.cs, SDK/Nefarius.DsHidMini.IPC/README.md, SDK/Nefarius.DsHidMini.IPC/docs/nefarius.dshidmini.ipc.dshidminiinterop.md
Changed DsHidMiniInteropAccessDeniedException message/docs to indicate it's reserved/unused. Updated docs to reflect per-slot named event behavior and that timeouts return false when the per-slot event is missing; clarified no elevation required for event-based waits.
Driver Event Creation
driver/Device.c
Stop generating random event names; build deterministic event name in fixed local buffer using sprintf_s with DSHM_IPC_HID_REPORT_EVENT_PREFIX + slot index and create named event via CreateEventA. Explicitly close InputReportWaitHandle in cleanup.
Driver IPC Contracts
driver/Device.h, driver/IPC.h
Removed macros for random HID event naming and WDFMEMORY InputReportWaitEventName field. Added DSHM_IPC_HID_REPORT_EVENT_PREFIX and DSHM_IPC_HID_REPORT_EVENT_NAME_CCH. Removed GET_HID_WAIT_HANDLE command, response struct, and initializer from IPC header.
Driver IPC Handler & Utilities
driver/IPC.Device.c, driver/Driver.h, driver/Util.c
Removed handling for IPC GET_HID_WAIT_HANDLE command and removed GenerateRandomEventName() declaration/implementation (and related <time.h>/PRNG code).

Sequence Diagram(s)

sequenceDiagram
    participant SDK as SDK Process
    participant Shared as Shared Memory
    participant Driver as Kernel Driver
    SDK->>Driver: Open device / discover slot index
    Driver-->>SDK: Slot available + HID buffer name (shared memory)
    Driver->>Driver: Create named auto-reset event "PREFIX<slot>"
    SDK->>Driver: (no longer requests duplicated handle)
    SDK->>Driver: OpenExisting("PREFIX<slot>") -> EventWaitHandle cached
    SDK->>Shared: Read initial HID buffer pointer
    SDK->>SDK: WaitOne(timeout) on per-slot EventWaitHandle
    Driver->>Shared: Write new HID report into shared memory
    Driver->>Driver: SetEvent("PREFIX<slot>")
    SDK->>SDK: WaitOne returns -> read report from Shared Memory
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Suggested labels

enhancement, documentation

Poem

🐰 I hopped from handle-duplication to name,
Per-slot events now call out by frame,
No more random suffix, no secret to find,
Driver names the bell, SDK waits in kind,
A little rabbit clap—synchronized, refined.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 38.46% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the primary architectural change: replacing a handle-duplication approach that required elevation with per-device named wait events that use standard IPC DACLs, removing the elevation requirement.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ipc-no-elevation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs (1)

233-249: Consider documenting the exception behavior.

EventWaitHandle.OpenExisting throws WaitHandleCannotBeOpenedException if the named event doesn't exist (e.g., device slot is empty). While the caller in DsHidMiniInterop.Commands.cs handles this, the XML doc comment should mention this for maintainability.

📝 Suggested documentation update
     /// <summary>
     ///     Opens the driver's named auto-reset event for the given one-based device slot (created with DACL allowing
     ///     authenticated users).
     /// </summary>
+    /// <exception cref="WaitHandleCannotBeOpenedException">
+    ///     The named event does not exist (device slot may be empty or driver unavailable).
+    /// </exception>
     private EventWaitHandle GetOrOpenHidReportWaitEvent(int deviceIndex)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs` around lines 233 - 249,
Update the XML documentation for GetOrOpenHidReportWaitEvent to explicitly state
that EventWaitHandle.OpenExisting may throw WaitHandleCannotBeOpenedException
when the named event does not exist (for example when a device slot is empty),
and note that callers are expected to handle this exception (as done in
DsHidMiniInterop.Commands.cs); reference the method name
GetOrOpenHidReportWaitEvent and the exception type
WaitHandleCannotBeOpenedException in the summary/remarks to improve
maintainability.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs`:
- Line 45: The _inputReportWaitEvents dictionary is accessed concurrently by
device notification handlers (DsHidMiniDeviceRemoved, DsHidMiniDeviceArrived →
RefreshDevices() → DisposeInputReportWaitEvents()) and client code
(GetOrOpenHidReportWaitEvent()), causing a race and possible
InvalidOperationException; fix by replacing the Dictionary<int, EventWaitHandle>
_inputReportWaitEvents with a ConcurrentDictionary<int, EventWaitHandle> or
protect all accesses (reads, writes, iteration/clear) to _inputReportWaitEvents
with a single lock object so DisposeInputReportWaitEvents and
GetOrOpenHidReportWaitEvent use the same synchronization mechanism to avoid
concurrent enumeration/modification.

---

Nitpick comments:
In `@SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs`:
- Around line 233-249: Update the XML documentation for
GetOrOpenHidReportWaitEvent to explicitly state that
EventWaitHandle.OpenExisting may throw WaitHandleCannotBeOpenedException when
the named event does not exist (for example when a device slot is empty), and
note that callers are expected to handle this exception (as done in
DsHidMiniInterop.Commands.cs); reference the method name
GetOrOpenHidReportWaitEvent and the exception type
WaitHandleCannotBeOpenedException in the summary/remarks to improve
maintainability.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 100f64e7-cd73-40de-895d-77c8916e9133

📥 Commits

Reviewing files that changed from the base of the PR and between 0109cb5 and 27ca8e4.

📒 Files selected for processing (14)
  • SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.Commands.cs
  • SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs
  • SDK/Nefarius.DsHidMini.IPC/Exceptions/Exceptions.cs
  • SDK/Nefarius.DsHidMini.IPC/Models/CmdStructs.cs
  • SDK/Nefarius.DsHidMini.IPC/Models/Enums.cs
  • SDK/Nefarius.DsHidMini.IPC/NativeMethods.txt
  • SDK/Nefarius.DsHidMini.IPC/README.md
  • SDK/Nefarius.DsHidMini.IPC/docs/nefarius.dshidmini.ipc.dshidminiinterop.md
  • driver/Device.c
  • driver/Device.h
  • driver/Driver.h
  • driver/IPC.Device.c
  • driver/IPC.h
  • driver/Util.c
💤 Files with no reviewable changes (4)
  • driver/Driver.h
  • driver/IPC.Device.c
  • driver/Device.h
  • SDK/Nefarius.DsHidMini.IPC/NativeMethods.txt

@nefarius
nefarius merged commit 36bfdd8 into master Apr 5, 2026
@nefarius
nefarius deleted the ipc-no-elevation branch April 5, 2026 23:24
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