|
| 1 | +# FFM transport build strategy |
| 2 | + |
| 3 | +Design notes for adding `java.lang.foreign` (FFM) transports (starting with kqueue on macOS) |
| 4 | +alongside the existing JNI transports: module layout, how the jextract-generated bindings are |
| 5 | +produced and committed, and the build-integration work required. See [Jextract.md](Jextract.md) for |
| 6 | +the generator plugin itself. |
| 7 | + |
| 8 | +## Summary |
| 9 | + |
| 10 | +- Each FFM transport mirrors the existing `transport-classes-*` / `transport-native-*` split, except |
| 11 | + the "native" half holds **committed jextract-generated Java** instead of a compiled library. |
| 12 | +- Bindings are **per-OS** (jextract bakes ABI constants and struct layouts into the output) and |
| 13 | + **arch-invariant within an OS**, so one plain jar per OS serves all its architectures. There are no |
| 14 | + per-arch classifier jars. |
| 15 | +- Generated sources are committed and treated as a build artifact: pinned jextract, pinned SDK, a |
| 16 | + provenance manifest, and a regeneration-diff CI check. jextract is never on a consumer's build path. |
| 17 | +- The only hand-written binding code is a small errno-capture shim, needed until jextract can emit |
| 18 | + `captureCallState` handles. |
| 19 | + |
| 20 | +## Relationship to the JNI transports |
| 21 | + |
| 22 | +The JNI transports split platform-neutral Java from a native module that ships a compiled library per |
| 23 | +architecture: |
| 24 | + |
| 25 | +``` |
| 26 | +transport-classes-kqueue platform-neutral Java (channels, IoHandler, config) |
| 27 | +transport-native-kqueue per-arch classifier jars (osx-x86_64, osx-aarch_64); .jnilib in META-INF/native |
| 28 | +``` |
| 29 | + |
| 30 | +FFM resolves libc symbols at runtime via `Linker.nativeLinker().defaultLookup()`, so there is no |
| 31 | +compiled library. The platform-varying artifact becomes the jextract-generated Java, which we commit. |
| 32 | + |
| 33 | +## Module layout |
| 34 | + |
| 35 | +| JNI module | FFM module | Contents | |
| 36 | +|---|---|---| |
| 37 | +| `transport-native-unix-common` | `transport-ffm-unix-common` | OS-agnostic hand-written support. Currently only the errno capture-state helper (see below). | |
| 38 | +| `transport-native-kqueue` | `transport-ffm-native-kqueue` | Committed jextract output for macOS, the provenance manifest, and the per-OS errno-capture call sites. No C headers. | |
| 39 | +| `transport-classes-kqueue` | `transport-ffm-classes-kqueue` | Hand-written kqueue transport: `IoHandler`, channels, configs. | |
| 40 | + |
| 41 | +Linux follows the same shape (`transport-ffm-{native,classes}-epoll`, likewise `io_uring`). |
| 42 | + |
| 43 | +**Dependency direction.** The JNI native jar is a runtime-only, per-arch classifier the user selects. |
| 44 | +The FFM bindings are Java types the transport calls, so `transport-ffm-classes-kqueue` has a plain |
| 45 | +compile dependency on `transport-ffm-native-kqueue` (no classifier). Consumers depend on the classes |
| 46 | +module and get the bindings transitively; there is no platform-classifier selection step and jextract |
| 47 | +is never required to build. |
| 48 | + |
| 49 | +**One module per OS, not a cross-OS common module.** Generated sources bake in per-OS ABI values, and |
| 50 | +committed source has no classifier idiom the way a compiled `transport-native-unix-common` does. |
| 51 | +Per-OS modules avoid shipping other OSes' classes or profile-gating source roots. If a second same-OS |
| 52 | +FFM consumer appears (e.g. a macOS DNS resolver), split out a per-OS common bindings module then. |
| 53 | + |
| 54 | +**Package naming.** Generated code lives in an OS-named package, e.g. |
| 55 | +`io.netty.channel.unix.ffm.generated.macos`, so the target OS is visible at the call site and in stack |
| 56 | +traces and a cross-OS import stands out in review. |
| 57 | + |
| 58 | +## Bindings are per-OS, arch-invariant within an OS |
| 59 | + |
| 60 | +jextract bakes constant values (`AF_INET`), struct layouts and offsets (`sockaddr_in`), and type |
| 61 | +sizes into the generated code, and the API itself differs (`kqueue` vs `epoll`). A binding set is |
| 62 | +therefore valid for exactly one OS. Within an OS the supported architectures share an ABI (x86_64 and |
| 63 | +aarch64 are both LP64), so the output is identical across them and one no-classifier jar per OS |
| 64 | +suffices. A CI check (below) regenerates on both arches and asserts byte-identical output; a future |
| 65 | +non-LP64 architecture would need its own binding set. |
| 66 | + |
| 67 | +## Hand-written code: errno capture |
| 68 | + |
| 69 | +jextract does not build its downcall handles with `Linker.Option.captureCallState("errno")`, so the |
| 70 | +generated `socket`/`bind`/etc. methods cannot read errno atomically (a separate read is racy). Until |
| 71 | +jextract can emit capture-state handles, errno-sensitive calls are hand-written: |
| 72 | + |
| 73 | +- An OS-agnostic helper in `transport-ffm-unix-common` decorates a generated `FunctionDescriptor` with |
| 74 | + `captureCallState` and owns the cached errno `VarHandle`. It runs once per symbol at handle |
| 75 | + construction, not per call. |
| 76 | +- Per-OS typed call sites in the bindings module invoke the resulting `static final MethodHandle`. |
| 77 | + |
| 78 | +Everything above the bindings (channels, IoHandler, config) is ordinary hand-written transport code. |
| 79 | + |
| 80 | +Guidelines for the wrappers: invoke a `static final MethodHandle` via `invokeExact` (not |
| 81 | +`invoke`/`Object[]`, which box and adapt at runtime); reuse a loop-confined capture segment rather |
| 82 | +than allocating per call; return `(result, errno)` bit-packed in a `long`. `Linker.Option.critical()` |
| 83 | +is worth evaluating for short non-blocking calls, but blocking calls (`read`/`write`/`kevent`) must |
| 84 | +not use it, and its composition with `captureCallState` needs checking on the target JDK. |
| 85 | + |
| 86 | +## Code generation and determinism |
| 87 | + |
| 88 | +We author no C headers. jextract reads the OS's system headers directly from the platform SDK (e.g. |
| 89 | +`<sys/socket.h>` from `$(xcrun --show-sdk-path)/usr/include`), and `--include-*` flags select the |
| 90 | +symbols. It runs on-demand, never in the normal build, and the output is committed. |
| 91 | + |
| 92 | +Generation is inherently per-OS: jextract parses with the host clang's predefined macros (`__APPLE__`, |
| 93 | +`__linux__`, arch macros), so macOS bindings must be generated on macOS and Linux bindings in a |
| 94 | +matching Linux toolchain (a pinned container works, including via Docker on a Mac). No `-I` flag |
| 95 | +changes that. |
| 96 | + |
| 97 | +Determinism is treated as a property of the artifact: |
| 98 | + |
| 99 | +- Pin jextract; the plugin verifies `jextract --version` and fails on mismatch. |
| 100 | +- Pin the SDK/toolchain and record its identity in the provenance manifest. |
| 101 | +- A `GENERATED.properties` manifest sits beside the sources (jextract version, host OS, SDK id) and is |
| 102 | + deterministic (no timestamps or paths), so it reproduces byte-for-byte. |
| 103 | +- A CI regeneration-diff job regenerates on the canonical pinned toolchain and fails on any diff. That |
| 104 | + job, not a developer's laptop, is the source of truth; it can be a manually-triggered workflow that |
| 105 | + opens the PR. |
| 106 | + |
| 107 | +The POSIX socket / kqueue / epoll ABIs are frozen for binary compatibility, so cross-SDK-version drift |
| 108 | +is unlikely in practice; the diff gate catches any exception. |
| 109 | + |
| 110 | +See [Jextract.md](Jextract.md) for the plugin configuration and the recommended `<bindings>` layout. |
| 111 | + |
| 112 | +## Build integration |
| 113 | + |
| 114 | +- **Checkstyle / forbidden-apis.** The committed generated tree under `src/main/java` is scanned at |
| 115 | + `validate` and fails it (no copyright header, no `package-info.java`, line length, and so on). Exempt |
| 116 | + it, preferably module-locally with `<checkstyle.excludes>**/generated/**/*</checkstyle.excludes>` |
| 117 | + (and the forbidden-apis equivalent), or by adding a `generated/` entry to the shared |
| 118 | + `SuppressionFilter` in `netty-build-common` (a cross-artifact change). Needed before the first FFM |
| 119 | + module builds. |
| 120 | +- **Java version.** FFM requires Java 25; gate the modules behind a JDK-25-activated profile (as the |
| 121 | + existing `java25` profile does) so older JDKs skip them. No multi-release jar needed. |
| 122 | +- **`module-info`** via the `io.github.dmlloyd.module-info` plugin. No `provides`: transport selection |
| 123 | + stays explicit (`KQueue.isAvailable() ? ... : NioIoHandler.newFactory()`), as with the other |
| 124 | + transports. |
| 125 | +- **Runtime flags** for consumers and the testsuite: `--enable-native-access=ALL-UNNAMED` plus the |
| 126 | + usual `--add-opens java.base/sun.nio.ch=ALL-UNNAMED`. |
| 127 | +- **Aggregation.** Add the modules to the root `<modules>`, the BOM, and the `all` aggregator. |
| 128 | + |
| 129 | +## Open questions |
| 130 | + |
| 131 | +- Module naming: `transport-ffm-native-kqueue` vs `transport-ffm-bindings-kqueue`. |
| 132 | +- JDK gating: profile-skip on JDK < 25 vs. hard-require 25 to build. |
| 133 | +- Exact jextract option for pinning the SDK sysroot (may be `SDKROOT` or a clang-arg passthrough |
| 134 | + rather than a first-class flag). |
| 135 | +- `critical()` / `captureCallState` composition; benchmark before relying on it. |
| 136 | + |
| 137 | +## Prerequisites for the first FFM module (not yet done) |
| 138 | + |
| 139 | +- The checkstyle / forbidden-apis exemption above. |
| 140 | +- CI: the per-OS regeneration-diff check and the arch-invariance byte-identical check. |
| 141 | +- Migrate the exploratory `ffm/` prototype into the real modules and delete it. |