-
Notifications
You must be signed in to change notification settings - Fork 0
20 C ABI FFI and Native Language Bindings
Corresponding Specifications:
sys-arch/19-c-abi-ffi-architecture.md,sys-arch/27-rust-driven-android-native-build-packaging-automation.md
Key Modules:apps/android/rust-jni-glue,apps/android/messaging-jni
To allow integration into Kotlin/Java, Swift/Objective-C, Python, and C/C++ applications without memory leaks or runtime overhead, the SIAR core exposes a pure C-ABI foreign function interface:
// Opaque handle to the internal Tokio async runtime and state engine
pub struct SiarEngineHandle {
pub(crate) runtime: tokio::runtime::Runtime,
pub(crate) inner: Arc<SiarEngine>,
}
#[no_mangle]
pub unsafe extern "C" fn siar_engine_create(
db_path: *const c_char,
out_handle: *mut *mut SiarEngineHandle,
) -> i32 {
std::panic::catch_unwind(|| {
// Safe string extraction, engine initialization, and raw pointer packaging
}).unwrap_or(-1)
}
#[no_mangle]
pub unsafe extern "C" fn siar_engine_destroy(handle: *mut SiarEngineHandle) {
if !handle.is_null() {
drop(Box::from_raw(handle));
}
}Crossing the FFI boundary requires strict adherence to safety rules:
[Kotlin Android Application (JVM)]
|
JNI Call | (Java Native Interface)
v
+---------------------------------------------------------------+
| JNI C-ABI Layer |
| 1. Convert jstring -> CStr -> Rust &str |
| 2. std::panic::catch_unwind Barrier (Never unwind past FFI!) |
| 3. Dispatch to internal async Tokio task pool |
| 4. Convert Rust Result<T, E> -> C-ABI error status code |
+-------------------------------+-------------------------------+
|
v
[Rust Core Engine (siar-messaging)]
-
No Uncaught Panics: All exported FFI functions wrap logic in
catch_unwind. A panic in Rust returns an error code rather than crashing the host JVM / iOS process. - Explicit Memory Lifetime: Any buffer allocated by Rust is freed exclusively via a paired Rust cleanup function.
The workspace includes automated build scripts to compile and strip native shared libraries (libsiar_jni.so) across all standard Android ABIs:
# Target Android ABIs
aarch64-linux-android # Modern 64-bit ARM (Primary phones & tablets)
armv7-linux-androideabi # Legacy 32-bit ARM (Older smartphones)
x86_64-linux-android # 64-bit Intel/AMD (Android Studio Emulators)SIAR — Survivable Identity & Autonomous Routing
Open Source Mesh & DTN Communications Platform | Dual Licensed under MIT / Apache-2.0 / Commercial
Documentation Index • GitHub Repository • System Specifications
- 04-Autonomous-Routing-and-Policy-Engine
- 05-Proximity-and-Hardware-Transports
- 06-Delay-Tolerant-Networking-and-Bundle-Forwarding
- 07-Battery-Aware-Scheduling-and-Emergency-Mesh
- 08-Offline-Event-Log-and-Outbox-Engine
- 09-Robust-Blob-Storage-and-Chunk-Transfers
- 10-Crash-Recovery-and-Data-Portability
- 11-Realtime-Audio-Video-Calling-Architecture
- 12-Cross-Platform-Client-Architecture
- 13-Messaging-Timeline-Composer-and-Inbox
- 14-Contacts-Groups-and-Security-Center
- 15-Nearby-Discovery-and-Out-of-Band-Pairing
- 16-Notifications-Presence-and-Background-Lifecycle
- 17-Local-Knowledge-Retrieval-and-Search
- 25-Design-System-Tokens-and-Responsive-Layouts
- 26-UI-UX-Performance-Testing-and-Quality-Gates
- 18-Protocol-Extensions-and-WASM-Plugins
- 19-Headless-Daemons-and-Embedded-Nodes
- 20-C-ABI-FFI-and-Native-Language-Bindings
- 21-Testing-Fuzzing-and-Network-Diagnostics
- 22-Getting-Started-and-Developer-Guide
- 23-Off-Grid-Survival-and-Field-Operations-Guide
- 24-System-Comparison-and-Benchmarking