Repository navigation
Csharp iox2 12 ffi testing #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dkroenke
merged 23 commits into
eclipse-iceoryx:main
from
patdhlk:csharp-iox2-12-ffi-testing
May 27, 2026
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a693826
[#12] Add CallbackContext for GCHandle-pinned FFI state
patdhlk 8195dbb
[#12] CallbackContext: clarify Unpin return-value purpose
patdhlk b598400
[#12] Add CallbackContext edge-case and leak tests
patdhlk 15ee67a
[#12] Stabilize leak test under Debug JIT local-lifetime extension
patdhlk afd721c
[#12] Add FfiAssert test helper for readable FFI failures
patdhlk e4df1c7
[#12] Add tripwire tests for FFI struct sizes
patdhlk 6897459
[#12] Add concurrent Node.List() tests (demonstrates race)
patdhlk 115556e
[#12] Fix IOX2_SERVICE_ID_LENGTH: 32 -> 64
patdhlk bf33601
[#12] Fix Node.List() callback race with CallbackContext
patdhlk c776c81
[#12] WaitAndProcessOnce: pin callback via CallbackContext
patdhlk 514c7a1
[#12] WaitAndProcessOnce(timeout): pin callback via CallbackContext
patdhlk 4a7b3ed
[#12] WaitAndProcess: pin callback context; remove _nativeCallback
patdhlk 0805619
[#12] Add WaitSet concurrent-instance and trampoline tests
patdhlk 014cb90
[#12] Update CHANGELOG with callback-safety bugfix
patdhlk d666dc9
[#12] WaitSet throw test: drop unneeded async/await
patdhlk 8f1c44f
[#12] Apply dotnet format (using order, trailing newline)
patdhlk 7bdd9fd
[#12] Bump new-file copyright headers 2025 -> 2026
patdhlk 3f2d914
[#12] CHANGELOG: split bugfix entry into per-fix bullets
patdhlk 708cc02
[#12] Node.List callback: restore offset/marshal comments
patdhlk 493c2de
[#12] Node.List callback: log exception with stack trace
patdhlk eebaffe
[#12] NativeStructSizesTests: document why sizes are duplicated
patdhlk b2a8498
[#12] CallbackContext: tests for misuse + double-free contract
patdhlk 6ded0f6
[#12] RuntimeTests: use FfiAssert.Ok for raw FFI return codes
patdhlk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Copyright (c) 2026 Contributors to the Eclipse Foundation | ||
| // | ||
| // See the NOTICE file(s) distributed with this work for additional | ||
| // information regarding copyright ownership. | ||
| // | ||
| // This program and the accompanying materials are made available under the | ||
| // terms of the Apache Software License 2.0 which is available at | ||
| // https://www.apache.org/licenses/LICENSE-2.0, or the MIT license | ||
| // which is available at https://opensource.org/licenses/MIT. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace Iceoryx2.Native; | ||
|
|
||
| /// <summary> | ||
| /// Pins managed state so it can be passed through an FFI boundary as an opaque | ||
| /// <see cref="IntPtr"/> context and recovered inside a native callback. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para>Usage:</para> | ||
| /// <code> | ||
| /// var ctx = CallbackContext.Pin(state); | ||
| /// try { NativeCall(..., Trampoline, ctx); } | ||
| /// finally { CallbackContext.Unpin<TState>(ctx); } | ||
| /// | ||
| /// // Inside the static trampoline: | ||
| /// var state = CallbackContext.Peek<TState>(contextPtr); | ||
| /// </code> | ||
| /// <para> | ||
| /// <b>Safety contract.</b> The returned <see cref="IntPtr"/> is a raw handle into | ||
| /// the runtime's GC handle table; treat it like a C pointer to manually managed | ||
| /// storage. Every <see cref="Pin{T}"/> MUST be matched by exactly one | ||
| /// <see cref="Unpin{T}"/>, ideally in a <c>try</c>/<c>finally</c> wrapping the | ||
| /// native call. After <see cref="Unpin{T}"/> the token MUST NOT be used again | ||
| /// (no further <see cref="Peek{T}"/>, no second <see cref="Unpin{T}"/>): the | ||
| /// runtime recycles handle slots aggressively, so a re-used token can silently | ||
| /// resolve to an unrelated pin and double-free it — the .NET analogue of a C | ||
| /// use-after-free / double-free. The <c>IsAllocated</c> preliminary check | ||
| /// inside <see cref="Unpin{T}"/> defends against <see cref="IntPtr.Zero"/> only; | ||
| /// it cannot detect a recycled slot. | ||
| /// </para> | ||
| /// </remarks> | ||
| internal static class CallbackContext | ||
| { | ||
| /// <summary> | ||
| /// Allocates a normal <see cref="GCHandle"/> for <paramref name="state"/> and | ||
| /// returns its <see cref="IntPtr"/> token, suitable for passing through FFI. | ||
| /// </summary> | ||
| public static IntPtr Pin<T>(T state) where T : class | ||
| { | ||
| ArgumentNullException.ThrowIfNull(state); | ||
| var handle = GCHandle.Alloc(state, GCHandleType.Normal); | ||
| return GCHandle.ToIntPtr(handle); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Recovers the pinned state without freeing the handle. Safe from a callback. | ||
| /// Returns <c>null</c> when <paramref name="token"/> is <see cref="IntPtr.Zero"/> | ||
| /// or the target is not of type <typeparamref name="T"/>. | ||
| /// </summary> | ||
| public static T? Peek<T>(IntPtr token) where T : class | ||
| { | ||
| if (token == IntPtr.Zero) | ||
| return null; | ||
| var handle = GCHandle.FromIntPtr(token); | ||
| return handle.IsAllocated ? handle.Target as T : null; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Frees the <see cref="GCHandle"/> and returns the (now unrooted) state. | ||
| /// MUST be called exactly once per <see cref="Pin{T}"/>, in a <c>finally</c>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// The returned value is provided to enable round-trip testing of the | ||
| /// pin/unpin contract. Production callers typically discard it because the | ||
| /// state object is already rooted on the calling stack frame. | ||
| /// </para> | ||
| /// <para> | ||
| /// If <typeparamref name="T"/> does not match the pinned state's actual | ||
| /// type, the underlying <see cref="GCHandle"/> is still freed (no leak) and | ||
| /// <c>null</c> is returned. | ||
| /// </para> | ||
| /// </remarks> | ||
| public static T? Unpin<T>(IntPtr token) where T : class | ||
| { | ||
| if (token == IntPtr.Zero) | ||
| return null; | ||
| var handle = GCHandle.FromIntPtr(token); | ||
| if (!handle.IsAllocated) | ||
| return null; | ||
| var target = handle.Target as T; | ||
| handle.Free(); | ||
|
dkroenke marked this conversation as resolved.
Resolved
|
||
| return target; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.