Skip to content

Csharp iox2 12 ffi testing - #20

Merged
dkroenke merged 23 commits into
eclipse-iceoryx:mainfrom
patdhlk:csharp-iox2-12-ffi-testing
May 27, 2026
Merged

dkroenke merged 23 commits into
eclipse-iceoryx:mainfrom
patdhlk:csharp-iox2-12-ffi-testing

Conversation

@patdhlk

@patdhlk patdhlk commented Apr 24, 2026 •

Copy link
Copy Markdown
Contributor

Notes for Reviewer

  • Core change: introduces Iceoryx2.Native.CallbackContext (GCHandle-pinned token) and routes three FFI callback sites (Node.List, WaitSet.WaitAndProcessOnce, WaitSet.WaitAndProcessOnce(timeout), WaitSet.WaitAndProcessInternal) through it.
  • Bugs fixed:
    • Node.List() used a static _tempServiceList field — two concurrent callers on the same process could share a list. Regression test: ServiceDiscoveryConcurrentTests.
    • WaitSet._nativeCallback was a single instance field rewritten on every WaitAndProcessOnce*/WaitAndProcessInternal call — a second overlapping call would overwrite the first's closure. Now replaced by two static readonly trampolines + per-call pinned context.
    • IOX2_SERVICE_ID_LENGTH was 32 but the cbindgen header defines it as 64 (iceoryx2.h line 69). Node.List()'s manual marshal was reading the name field at the wrong offset. No existing test caught this because ServiceDiscoveryTests deliberately avoided the name field.
  • Focus points when reviewing:
    • CallbackContext.Pin/Unpin pairing in every caller (Pin outside try, Unpin in finally).
    • Trampoline catch { return STOP; } — intentionally silent; swallowing user-callback exceptions is required at the FFI boundary. Pinned in tests (WaitSet_TrampolineReturnsStop_WhenCallbackThrows).
    • NativeStructSizesTests hardcodes 9104 / 18696 with header references — treat as a tripwire, update in lockstep with cbindgen output.

Pre-Review Checklist for the PR Author

  • Add sensible notes for the reviewer
  • PR title is short, expressive and meaningful
  • Consider switching the PR to a draft (Convert to draft)
    • as draft PR, the CI will be skipped for pushes
  • Relevant issues are linked in the References section
  • Branch follows the naming format (csharp-iox2-123-short-description)
  • Commits messages are according to this guideline
    • Commit messages have the issue ID ([#123] Add feature description)
  • Tests have been added/updated for new functionality
  • XML documentation comments added to public APIs
  • Changelog updated in the unreleased section including API breaking changes
  • Assign PR to reviewer
  • All checks have passed

PR Reviewer Reminders

  • Commits are properly organized and messages are according to the guideline
  • Unit tests have been written for new behavior
  • Public API is documented with XML comments
  • PR title describes the changes
  • Code follows C# conventions (PascalCase for public APIs)
    • dotnet format has been exectued before submitting

References

Closes #12

patdhlk added 16 commits April 23, 2026 08:27
Introduces an internal helper that pins managed state against GC moves,
returns an IntPtr token for the FFI `context` parameter, and recovers
the state in native callbacks. Foundation for removing the static
callback state that races in Node.List() and WaitSet callbacks.
The C# constant was out of sync with the cbindgen-generated header
(iceoryx2.h line 69: `#define IOX2_SERVICE_ID_LENGTH 64`). Node.List()'s
manual marshaling was reading 32 bytes of service ID and then misreading
the name field at offset 32 instead of 64, returning truncated /
cross-contaminated strings.

Discovered while fixing the static-field race in Node.List(). The
existing ServiceDiscoveryTests avoided the name field (the "service
names might be stored in a different format" note) which is why the
pre-existing bug wasn't flagged sooner.
Removes the static `_tempServiceList` field that two concurrent callers
could clobber. Each List() invocation now pins its own result list via
CallbackContext and passes the IntPtr token through the native
`context` parameter; the static callback recovers it via Peek.

The concurrent tests added in the previous commit now pass reliably.
…tiveCallback

Converts the third WaitSet callback site to CallbackContext using a
small record (Callback, DisposeAttachments) as the pinned state. Drops
the instance `_nativeCallback` field: all three sites now use static
trampolines held as `s_*Trampoline` static readonly fields.
@dkroenke
dkroenke self-requested a review April 24, 2026 11:11
@patdhlk

patdhlk commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

@dkroenke do you have time to review this one?

@dkroenke

Copy link
Copy Markdown
Contributor

@dkroenke do you have time to review this one?

@patdhlk Yes sorry for the delay, I was out of office the last days.

Comment thread src/Iceoryx2/Native/CallbackContext.cs Resolved
Comment thread CHANGELOG.md Resolved
Comment thread src/Iceoryx2/Node.cs Resolved
Comment thread src/Iceoryx2/Node.cs Resolved
Comment thread tests/Ffi/NativeStructSizesTests.cs Resolved
patdhlk added 5 commits May 19, 2026 22:55
Files added in this PR were authored in 2026; reviewer asked the
headers be dated accordingly. Existing repo files keep 2025 since
that reflects their creation year.
Reviewer asked for better readability. The merged paragraph hid the
three distinct fixes (Node.List race, WaitSet cross-call overwrite,
SERVICE_ID_LENGTH header mismatch); separate bullets each linking to
the same issue make the scope of eclipse-iceoryx#12 easier to scan.
The cleanup commit (bf33601) dropped the inline comments that explained
why ServiceListCallback walks the iox2_static_config_t payload by hand
(union avoidance) and what each Marshal.Copy offset corresponds to.
Reviewer flagged the loss; restoring them so future readers do not have
to reverse-engineer the offset arithmetic.
Reviewer asked why the silent catch dropped the previous diagnostic
output. The WaitSet trampolines stay silent because they wrap arbitrary
user callbacks at the FFI boundary, but ServiceListCallback runs only
our own marshalling code — if it throws, that is a binding bug and the
stack trace is exactly what a maintainer needs. Routing through stderr
keeps it out of the normal stdout-driven console logger contract.
…uplicated

Reviewer suggested sharing the storage-size literals between
Iox2NativeMethods.cs and this test file. Doing so would erase the
tripwire: Marshal.SizeOf<T> reads the StructLayout(Size = ...) attribute,
so if both sides derive from the same const, the assert collapses to
const-equals-const and trivially passes. The double-entry bookkeeping is
the point — a maintainer who updates one side but forgets the other
gets a failing test. Adding an in-line note so the design intent is
visible at the literals, not buried in the class docstring.
@patdhlk
patdhlk requested a review from dkroenke May 19, 2026 21:03
Comment thread src/Iceoryx2/Native/CallbackContext.cs Resolved
Comment thread src/Iceoryx2/Native/CallbackContext.cs Resolved
Comment thread tests/Ffi/FfiAssert.cs Resolved
…contract

Reviewer asked whether GCHandle has C/C++-style double-free UB and
requested tests on the preliminary checks. Investigation:

  Probe shows that after Free, GCHandle.FromIntPtr on the original
  token returns a struct with IsAllocated=true (the runtime recycles
  handle slots, and the token resolves to whatever pin now owns that
  slot). Calling Free on the stale token then silently frees the new
  owner's pin. The `if (!handle.IsAllocated)` check inside Unpin only
  defends against IntPtr.Zero in practice.

Document the actual safety contract (token-as-raw-pointer; double-Unpin
and Peek-after-Unpin are UB) and add tests covering the boundaries we
do defend:

  - Pin(null) -> ArgumentNullException
  - Peek<WrongType>(token) -> null (original pin still intact)
  - Unpin<WrongType>(token) -> null but GCHandle still Freed (no leak)

The leak-free type-mismatch test uses the same WeakReference tripwire
as the existing 10k Pin/Unpin loop.
…n codes

Reviewer noticed FfiAssert was unused. Wire it into the two
RuntimeTests sites that previously asserted IOX2_OK via Assert.Equal,
so the helper has a concrete caller and failures carry the operation
name. The newer tests under tests/Ffi/ go through Iox2.Run() / .IsOk
rather than raw FFI codes, so they don't need it.
@patdhlk
patdhlk requested a review from dkroenke May 27, 2026 10:30
@dkroenke
dkroenke merged commit c8e0571 into eclipse-iceoryx:main May 27, 2026
6 checks passed
@patdhlk
patdhlk deleted the csharp-iox2-12-ffi-testing branch May 27, 2026 14:25
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.

Testing of C# <-> Rust FFI

2 participants