Ivan FB 0305c1ace7
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-04 00:36:52 +02:00
..
2026-05-04 00:36:52 +02:00
2026-05-04 00:36:52 +02:00
2026-05-03 15:48:47 +02:00

nim_timer example

This example is a self-contained Nimble project demonstrating how to import nim-ffi and use the .ffiCtor. / .ffi. abstraction.

Usage

  1. Change into the example directory:

    cd examples/nim_timer
    
  2. Install the local ffi dependency:

    nimble install -y ../..
    
  3. Build the example library:

    nimble build
    
  4. Generate bindings:

    nimble genbindings_rust
    nimble genbindings_cpp
    

Rust example clients

The Rust client lives in examples/nim_timer/rust_client.

  • Run the sync example:

    cd examples/nim_timer/rust_client
    cargo run --bin rust_client
    
  • Run the Tokio example:

    cd examples/nim_timer/rust_client
    cargo run --bin tokio_client
    

C++ example

The generated C++ example lives in examples/nim_timer/cpp_bindings.

Build and run it with:

cd examples/nim_timer/cpp_bindings
cmake -S . -B build
cmake --build build
./build/example