10 Commits

Author SHA1 Message Date
Ivan FB
51ed83b04a
fix Async constructor cannot specify a timeout (rust.nim:416). new_async hardcodes Duration::from_secs(30) for all subsequent calls on the produced Ctx, while blocking create
accepts a timeout: Duration. Inconsistent and a footgun for any user who picks the async path. Add the parameter.
2026-05-11 21:23:24 +02:00
Ivan FB
24a56032af
Fix Rust callbacks dereference msg without a null check (rust.nim:272, 317). CStr::from_ptr(msg) is UB if msg == nullptr. The C++ side guards with msg ? ... : "". The Nim side
appears to always pass a non-null pointer, but soundness across an FFI boundary shouldn't hinge on a producer's discipline — the Rust receiver should null-check. Especially
  since the C ABI signature here is the one downstream consumers will rely on indefinitely.
2026-05-11 09:40:33 +02:00
Ivan FB
2f6ef08e6c
fix rust leaks 2026-05-11 09:35:47 +02:00
Ivan FB
729801b999
Add ffiDtor concept 2026-05-10 11:38:43 +02:00
Ivan FB
af32a7bce8
Fix cpp vulnerabilities
1. No timeout → wait_for + 30 s default (ffi/codegen/cpp.nim)
ffi_call_ now takes std::chrono::milliseconds timeout and uses cv.wait_for. All factory/method signatures carry a timeout parameter (default std::chrono::seconds{30}), mirroring the Rust blocking API.

2. Stack-allocated state → shared_ptr ownership (ffi/codegen/cpp.nim)
ffi_cb_ now receives a heap-allocated std::shared_ptr<FfiCallState_>* as user_data. The refcount is 2 going in (one for ffi_call_, one for the callback). If ffi_call_ times out and returns, its copy drops — but the state stays alive (refcount 1) until Nim eventually calls back and delete sptr in ffi_cb_ drops the last reference. No more stack UAF.

3. Destructor + Rule of 5 (ffi/codegen/cpp.nim, examples/nim_timer/nim_timer.nim)

Added nimtimer_destroy to nim_timer.nim with {.dynlib, exportc, cdecl, raises: [].} — joins the FFI and watchdog threads, frees the context
Codegen now always emits void {libName}_destroy(void* ctx) in extern "C" and generates a destructor, deleted copy ctor/assignment, and move ctor/assignment for the context class
timeout_ stored in the class; move transfers it, destructor uses it
4. Hardcoded TimerConfig in createAsync (ffi/codegen/cpp.nim)
createAsync now uses the actual ctorParams list (same as create), so it's correct for any library, not just nim_timer.

5. Opaque exceptions → clear error messages (ffi/codegen/cpp.nim)
deserializeFfiResult wraps nlohmann::json::parse + .get<T>() in a catch that rethrows as "FFI response deserialization failed: ...". The stoull in create() is also try-caught with "FFI create returned non-numeric address: " + raw.
2026-05-10 11:38:42 +02:00
Ivan FB
798f5a21ed
simplify auto-generate cpp and rust 2026-05-10 11:38:42 +02:00
Ivan FB
bad1be30e6
enhance async cpp
Co-authored-by: Copilot <copilot@github.com>
2026-05-10 11:38:42 +02:00
Ivan FB
d87fe8b104
enhance cpp and rust tokio examples
Co-authored-by: Copilot <copilot@github.com>
2026-05-10 11:38:41 +02:00
Ivan FB
aa22b982be
allow auto-generate cpp code and add example 2026-05-10 11:38:41 +02:00
Ivan FB
5b908b9c5b
simplify ffi generation and add simple Rust example 2026-05-10 11:35:02 +02:00