Skip to content

Add partial support for safe shared structs via the #[safe_shared_extern] attribute - #1715

Open
kolayne wants to merge 3 commits into
dtolnay:masterfrom
kolayne:safe-shared-extern
Open

Add partial support for safe shared structs via the #[safe_shared_extern] attribute#1715
kolayne wants to merge 3 commits into
dtolnay:masterfrom
kolayne:safe-shared-extern

Conversation

@kolayne

@kolayne kolayne commented May 25, 2026

Copy link
Copy Markdown

Adds the safe_shared_extern attribute.

The new attribute allows to define a shared struct that is automatically verified for compatibility with the struct defined in C++.

This only works for structs that have no padding in their layout. This also currently requires that the C++ struct is trivial, though this requirement may be relaxed.

The syntax is as follows:

mod ffi {
    #[safe_shared_extern]
    struct S {
        pub a: i32,
        pub b: i32,
    }

    extern "C++" {
        include!("include.hpp");

        // Note: no need to include `type S;` (the legacy unsafe syntax)
        unsafe fn f() -> S;
    }
}

Partially addresses #871.

@kolayne
kolayne force-pushed the safe-shared-extern branch 6 times, most recently from 3c9eb41 to cdfe829 Compare May 25, 2026 15:49
@kolayne
kolayne force-pushed the safe-shared-extern branch 2 times, most recently from a24c8fb to 30a6c11 Compare May 26, 2026 02:30
kolayne added a commit to kolayne/xgrammar-rs that referenced this pull request May 28, 2026
Rewrites the library to rely on `cxx` rather than `autocxx` as the
bindings back-end. Updates the building script accordingly.

Note that a custom version of cxx is used: it includes the patch
dtolnay/cxx#1715 for the `#[safe_shared_extern]` attribute.

Motivation for the change: wasm32 support (trymirai#8).

xgrammar-rs now compiles for `wasm32-wasip1` (and, supposedly, other
wasi targets), though for now it still requires the `WASI_SYSROOT`
environment variable to be set to the path to
[wasi-sdk](https://github.com/WebAssembly/wasi-sdk) version 33 or newer
compiled with exception support (`-DWASI_SDK_EXCEPTIONS=ON`).
kolayne added a commit to kolayne/xgrammar-rs that referenced this pull request May 28, 2026
Rewrites the library to rely on `cxx` rather than `autocxx` as the
bindings back-end. Updates the building script accordingly.

Note that a custom version of cxx is used: it includes the patch
dtolnay/cxx#1715 for the `#[safe_shared_extern]` attribute.

Motivation for the change: wasm32 support (trymirai#8).

xgrammar-rs now compiles for `wasm32-wasip1` (and, supposedly, other
wasi targets), though for now it still requires the `WASI_SYSROOT`
environment variable to be set to the path to
[wasi-sdk](https://github.com/WebAssembly/wasi-sdk) version 33 or newer
compiled with exception support (`-DWASI_SDK_EXCEPTIONS=ON`).
kolayne added a commit to kolayne/xgrammar-rs that referenced this pull request May 28, 2026
Rewrites the library to rely on `cxx` rather than `autocxx` as the
bindings back-end. Updates the building script accordingly.

Note that a custom version of cxx is used: it includes the patch
dtolnay/cxx#1715 for the `#[safe_shared_extern]` attribute.

Motivation for the change: wasm32 support (trymirai#8).

xgrammar-rs now compiles for `wasm32-wasip1` (and, supposedly, other
wasi targets), though for now it still requires the `WASI_SYSROOT`
environment variable to be set to the path to
[wasi-sdk](https://github.com/WebAssembly/wasi-sdk) version 33 or newer
compiled with exception support (`-DWASI_SDK_EXCEPTIONS=ON`).
kolayne added a commit to kolayne/xgrammar-rs that referenced this pull request Jun 3, 2026
Rewrites the library to rely on `cxx` rather than `autocxx` as the
bindings back-end. Updates the building script accordingly.

Note that a custom version of cxx is used: it includes the patch
dtolnay/cxx#1715 for the `#[safe_shared_extern]` attribute.

Motivation for the change: wasm32 support (trymirai#8).

xgrammar-rs now compiles for `wasm32-wasip1` (and, supposedly, other
wasi targets), though for now it still requires the `WASI_SYSROOT`
environment variable to be set to the path to
[wasi-sdk](https://github.com/WebAssembly/wasi-sdk) version 33 or newer
compiled with exception support (`-DWASI_SDK_EXCEPTIONS=ON`).
@kolayne
kolayne force-pushed the safe-shared-extern branch from 4896534 to b367fd1 Compare June 25, 2026 05:15
kolayne added 3 commits June 25, 2026 14:16
Adds the `safe_shared_extern` attribute.

The new attribute allows to define a shared struct that is automatically
verified for compatibility with the struct defined in C++.

This only works for structs that have no padding in their layout.
This also currently requires that the C++ struct is trivial, though
this may be relaxed.

The syntax is as follows:
```rust
mod ffi {
    #[safe_shared_extern]
    struct S {
        pub a: i32,
        pub b: i32,
    }

    extern "C++" {
        include!("include.hpp");

        // Note: no need to include `type S;` (the legacy unsafe syntax)
        unsafe fn f() -> S;
    }
}
```

TODO: add tests
Implement tests that various declaration mismatches are detected,
as well as that the positive case works.
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