polyplug 0.1.1

Universal high-performance zero-overhead cross-language plugin runtime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#![allow(clippy::expect_used)]

//! Integration test: verify the generated catch_unwind ABI wrapper catches a panic
//! and returns Panic (= 3) WITHOUT aborting the process.
//!
//! This test crate is the crate root for the `integration_panic` test binary.

use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::process::ExitStatus;

use polyplug_abi::AbiError;
use polyplug_abi::AbiErrorCode;
use polyplug_abi::BundleInitContext;
use polyplug_abi::GuestContractInterface;
use polyplug_abi::HostApi;
use polyplug_abi::PluginDescriptor;
use polyplug_abi::StringView;
use polyplug_abi::ffi::polyplug_host_alloc;
use polyplug_abi::ffi::polyplug_host_free;

mod common;

use common::polyplugc_bin;

// ─── Host functions for integration tests ─────────────────────────────────────

/// Global storage for the interface pointer captured during `polyplug_init`.
///
/// # Safety
/// Only written by `capture_register_callback` which is called once during
/// `polyplug_init`, before the interface pointer is read in the test.
static mut CAPTURED_INTERFACE_PTR: *const GuestContractInterface = core::ptr::null();

/// A register_guest_contract callback that captures the interface pointer.
///
/// # Safety
/// `this`, `_descriptor`, and `interface` must be valid for the duration of the call.
unsafe extern "C" fn capture_register_callback(
    _this: *const HostApi,
    _descriptor: *const PluginDescriptor,
    interface: *const GuestContractInterface,
    out_err: *mut AbiError,
) {
    // SAFETY: CAPTURED_INTERFACE_PTR is only written here, during polyplug_init,
    // before the test reads it. Single-threaded test execution ensures no data race.
    unsafe {
        CAPTURED_INTERFACE_PTR = interface;
    }
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe {
            out_err.write(AbiError {
                code: AbiErrorCode::Ok as u32,
                message: StringView::null(),
            })
        };
    }
}

/// No-op alloc callback.
unsafe extern "C" fn noop_alloc(_this: *const HostApi, size: usize, align: usize) -> *mut u8 {
    polyplug_host_alloc(size, align)
}

/// No-op free callback.
unsafe extern "C" fn noop_free(_this: *const HostApi, ptr: *mut u8, size: usize, align: usize) {
    // SAFETY: polyplug_host_free is a safe wrapper around the system allocator.
    unsafe { polyplug_host_free(ptr, size, align) }
}

/// No-op find_guest_contract callback.
unsafe extern "C" fn noop_find_guest_contract(
    _this: *const HostApi,
    _contract_id: u64,
    _min_version: u32,
) -> polyplug_abi::GuestContractHandle {
    polyplug_abi::GuestContractHandle::null()
}

/// No-op find_all_by_contract callback.
unsafe extern "C" fn noop_find_all_guest_contracts(
    _this: *const HostApi,
    _contract_id: u64,
    _min_version: u32,
) -> polyplug_abi::Array<polyplug_abi::GuestContractHandle> {
    polyplug_abi::Array::empty()
}

/// No-op resolve_guest_contract callback.
unsafe extern "C" fn noop_resolve_guest_contract(
    _this: *const HostApi,
    _handle: polyplug_abi::GuestContractHandle,
) -> *const GuestContractInterface {
    core::ptr::null()
}

/// No-op get_host_contract callback.
unsafe extern "C" fn noop_get_host_contract(
    _this: *const HostApi,
    _contract_id: u64,
    _min_version: u32,
) -> polyplug_abi::HostContractInstance {
    polyplug_abi::HostContractInstance::null()
}

/// No-op list_bundles callback.
unsafe extern "C" fn noop_list_bundles(
    _this: *const HostApi,
) -> polyplug_abi::Array<polyplug_utils::BundleId> {
    polyplug_abi::Array::empty()
}

/// No-op get_dependencies callback.
unsafe extern "C" fn noop_get_dependencies(
    _this: *const HostApi,
) -> polyplug_abi::Array<polyplug_abi::DependencyInfo> {
    polyplug_abi::Array::empty()
}

/// No-op resolve_host_contract_interface callback.
unsafe extern "C" fn noop_resolve_host_contract_interface(
    _this: *const HostApi,
    _contract_id: u64,
    _min_version: u32,
) -> *const polyplug_abi::HostContractInterface {
    core::ptr::null()
}

/// No-op load_bundle callback.
unsafe extern "C" fn noop_load_bundle(
    _this: *const HostApi,
    _path: *const u8,
    _path_len: usize,
    out_err: *mut AbiError,
) {
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(AbiError::ok()) };
    }
}

/// No-op reload_bundle callback.
unsafe extern "C" fn noop_reload_bundle(
    _this: *const HostApi,
    _path: *const u8,
    _path_len: usize,
    out_err: *mut AbiError,
) {
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(AbiError::ok()) };
    }
}

/// No-op register_host_contract callback.
unsafe extern "C" fn noop_register_host_contract(
    _this: *const HostApi,
    _interface: *const polyplug_abi::HostContractInterface,
    out_err: *mut AbiError,
) {
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(AbiError::ok()) };
    }
}

/// No-op register_loader callback.
unsafe extern "C" fn noop_register_loader(
    _this: *const HostApi,
    _loader_ptr: *mut core::ffi::c_void,
    out_err: *mut AbiError,
) {
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(AbiError::ok()) };
    }
}

/// No-op get_last_error callback.
unsafe extern "C" fn noop_get_last_error(
    _this: *const HostApi,
    _buf: *mut u8,
    _buf_len: usize,
) -> usize {
    0
}

/// No-op get_error_len callback.
unsafe extern "C" fn noop_get_error_len(_this: *const HostApi) -> usize {
    0
}

unsafe extern "C" fn noop_unload_bundle(
    _this: *const HostApi,
    _bundle_id: polyplug_utils::BundleId,
    out_err: *mut AbiError,
) {
    if !out_err.is_null() {
        // SAFETY: out_err is non-null (just checked) and writable per the ABI contract.
        unsafe { out_err.write(AbiError::ok()) };
    }
}

// ─── Test ─────────────────────────────────────────────────────────────────────

#[test]
fn test_panic_returns_abi_error_panic() {
    // -- Step 1: Locate workspace root and polyplugc binary --
    let manifest_dir: &Path = Path::new(env!("CARGO_MANIFEST_DIR"));
    // CARGO_MANIFEST_DIR = crates/polyplug; workspace root is two up.
    let workspace_root: &Path = manifest_dir
        .parent()
        .expect("crates parent")
        .parent()
        .expect("workspace root");

    let api_toml: PathBuf = workspace_root
        .join("tests")
        .join("fixtures")
        .join("test_panic_api.toml");

    // -- Step 2: Create a temp directory for the panic plugin crate --
    let tmp_dir: PathBuf = std::env::temp_dir().join("polyplug_panic_plugin_test");
    std::fs::create_dir_all(&tmp_dir).expect("create tmp dir");
    std::fs::create_dir_all(tmp_dir.join("src")).expect("create tmp src dir");

    // -- Step 2b: Create a minimal bundle.toml referencing the API --
    let bundle_toml_content: String = format!(
        "[bundle]\n\
         name = \"panic_plugin\"\n\
         version = \"1.0.0\"\n\
         loader = \"native\"\n\
         api = \"{}\"\n\
         \n\
         [bundle.file]\n\
         linux.x86_64 = \"libpanic_plugin.so\"\n\
         \n\
         [[plugin]]\n\
         name = \"panic_plugin\"\n\
         implements = [\"test.panic@1.0\"]\n",
        // Backslashes are invalid TOML escape sequences; forward slashes are valid
        // path separators on every platform (including Windows).
        api_toml.to_string_lossy().replace('\\', "/")
    );
    let bundle_toml_path: PathBuf = tmp_dir.join("bundle.toml");
    std::fs::write(&bundle_toml_path, bundle_toml_content).expect("write bundle.toml");

    // -- Step 3: Run polyplugc generate into tmp_dir/src --
    let gen_status: ExitStatus = Command::new(polyplugc_bin())
        .arg("generate")
        .arg("--bundle")
        .arg(&bundle_toml_path)
        .arg("--lang")
        .arg("rust")
        .arg("--out")
        .arg(tmp_dir.join("src"))
        .status()
        .expect("polyplugc generate failed to run");

    assert!(
        gen_status.success(),
        "polyplugc generate exited with non-zero status"
    );

    // -- Step 4: Write Cargo.toml for the cdylib crate --
    // Only depend on polyplug_abi/polyplug_guest/polyplug_utils; polyplug is an indirect dep.
    // We do NOT add polyplug as a direct dep to avoid duplicate
    // `polyplug_abi_version` symbol (it is defined in polyplug/src/lib.rs).
    let guest_lib_path: PathBuf = workspace_root.join("sdks").join("rust").join("guest");
    let abi_lib_path: PathBuf = workspace_root.join("crates").join("polyplug_abi");
    let utils_lib_path: PathBuf = workspace_root.join("crates").join("polyplug_utils");
    let cargo_toml_content: String = format!(
        "[package]\n\
         name = \"panic_plugin\"\n\
         version = \"0.1.0\"\n\
         edition = \"2024\"\n\
         \n\
         [lib]\n\
         name = \"panic_plugin\"\n\
         crate-type = [\"cdylib\"]\n\
         \n\
         [dependencies]\n\
         polyplug_abi = {{ path = \"{}\" }}\n\
         polyplug_guest = {{ path = \"{}\" }}\n\
         polyplug_utils = {{ path = \"{}\" }}\n",
        // Backslashes are invalid TOML escape sequences; forward slashes are valid
        // path separators on every platform (including Windows).
        abi_lib_path.to_string_lossy().replace('\\', "/"),
        guest_lib_path.to_string_lossy().replace('\\', "/"),
        utils_lib_path.to_string_lossy().replace('\\', "/")
    );
    std::fs::write(tmp_dir.join("Cargo.toml"), &cargo_toml_content).expect("write Cargo.toml");

    // -- Step 5: Write src/lib.rs implementing TestPanicPlugin --
    // The generated src/guest/interfaces.rs, src/guest/contracts.rs, src/guest/types.rs already exist.
    // We provide the crate root lib.rs that declares the guest submodule and implements the trait.
    // We do NOT include mod guest::init -- we write our own polyplug_init here.
    // We do NOT define polyplug_abi_version -- it comes from polyplug rlib.
    let lib_rs_content: &str = concat!(
        "// THIS FILE IS WRITTEN BY THE integration_panic TEST -- NOT generated by polyplugc.\n",
        "// It implements the TestPanicPlugin trait with a function that always panics.\n",
        "\n",
        "mod guest {\n",
        "    pub mod types;\n",
        "    pub mod contracts;\n",
        "    pub mod interfaces;\n",
        "}\n",
        "\n",
        "use polyplug_abi::AbiError;\n",
        "use polyplug_abi::HostApi;\n",
        "use polyplug_abi::BundleInitContext;\n",
        "use polyplug_abi::PluginDescriptor;\n",
        "use polyplug_guest::GuestError;\n",
        "use polyplug_abi::GuestContractInterface;\n",
        "use polyplug_abi::StringView;\n",
        "use polyplug_abi::AbiErrorCode;\n",
        "use guest::interfaces::PANIC_PLUGIN_INTERFACE;\n",
        "use guest::contracts::TestPanicGuestContract;\n",
        "use polyplug_guest::HostContext;\n",
        "\n",
        "struct PanicPlugin;\n",
        "\n",
        "impl TestPanicGuestContract for PanicPlugin {\n",
        "    fn do_panic(&self) -> Result<(), GuestError> {\n",
        "        panic!(\"intentional test panic\");\n",
        "    }\n",
        "}\n",
        "\n",
        "/// Factory called by the generated create_instance for every instance.\n",
        "#[unsafe(no_mangle)]\n",
        "pub fn polyplug_create_panic_plugin(_host: HostContext) -> Box<dyn TestPanicGuestContract> {\n",
        "    Box::new(PanicPlugin)\n",
        "}\n",
        "\n",
        "/// Register the panic plugin interface with the host.\n",
        "///\n",
        "/// # Safety\n",
        "/// `host` and `ctx` must be valid pointers.\n",
        "#[unsafe(no_mangle)]\n",
        "pub unsafe extern \"C\" fn polyplug_init(\n",
        "    host: *const HostApi,\n",
        "    ctx: *const BundleInitContext,\n",
        ") -> AbiError {\n",
        "    if host.is_null() || ctx.is_null() {\n",
        "        return AbiError {\n",
        "            code: AbiErrorCode::Generic as u32,\n",
        "            message: StringView::null(),\n",
        "        };\n",
        "    }\n",
        "    // SAFETY: host is non-null and valid per ABI contract.\n",
        "    let host_iface: &HostApi = unsafe { &*host };\n",
        "    let desc: PluginDescriptor = PluginDescriptor {\n",
        "        name: StringView {\n",
        "            ptr: b\"panic_plugin\".as_ptr(),\n",
        "            len: 12_usize,\n",
        "        },\n",
        "        contract_name: StringView {\n",
        "            ptr: b\"test.panic\".as_ptr(),\n",
        "            len: 10_usize,\n",
        "        },\n",
        "        version: polyplug_abi::Version { major: 1, minor: 0, patch: 0 },\n",
        "    };\n",
        "    // Out-param ABI: register_guest_contract writes its AbiError through a\n",
        "    // trailing pointer and returns void; init still surfaces it by value.\n",
        "    let mut err: AbiError = AbiError { code: AbiErrorCode::Ok as u32, message: StringView::null() };\n",
        "    // SAFETY: desc and interface are valid for the duration of the call; &mut err is valid.\n",
        "    unsafe {\n",
        "        (host_iface.register_guest_contract)(\n",
        "            host,\n",
        "            &desc as *const PluginDescriptor,\n",
        "            &PANIC_PLUGIN_INTERFACE as *const GuestContractInterface,\n",
        "            &mut err as *mut AbiError,\n",
        "        );\n",
        "    }\n",
        "    err\n",
        "}\n",
    );
    std::fs::write(tmp_dir.join("src").join("lib.rs"), lib_rs_content).expect("write src/lib.rs");

    // -- Step 6: Build the cdylib --
    let build_status: ExitStatus = Command::new(env!("CARGO"))
        .arg("build")
        .arg("--manifest-path")
        .arg(tmp_dir.join("Cargo.toml"))
        .arg("--release")
        .status()
        .expect("cargo build failed to run");

    assert!(
        build_status.success(),
        "cargo build for panic_plugin cdylib failed"
    );

    // -- Step 7: Locate the compiled .so --
    let lib_filename: &str = if cfg!(target_os = "macos") {
        "libpanic_plugin.dylib"
    } else if cfg!(target_os = "windows") {
        "panic_plugin.dll"
    } else {
        "libpanic_plugin.so"
    };

    let so_path: PathBuf = tmp_dir.join("target").join("release").join(lib_filename);

    // -- Step 8: Load with libloading --
    // SAFETY: so_path is a compiled cdylib.
    let library: libloading::Library = unsafe {
        libloading::Library::new(&so_path).expect("failed to load panic_plugin shared library")
    };

    // -- Step 9: Resolve and call polyplug_init --
    // SAFETY: polyplug_init matches the expected ABI signature (2-arg).
    let init_fn: libloading::Symbol<
        '_,
        unsafe extern "C" fn(*const HostApi, *const BundleInitContext) -> AbiError,
    > = unsafe {
        library
            .get(b"polyplug_init\0")
            .expect("polyplug_init symbol not found")
    };

    let host_interface: HostApi = HostApi {
        runtime: core::ptr::null_mut(),
        register_guest_contract: capture_register_callback,
        alloc: noop_alloc,
        free: noop_free,
        find_guest_contract: noop_find_guest_contract,
        find_all_guest_contracts: noop_find_all_guest_contracts,
        resolve_guest_contract: noop_resolve_guest_contract,
        get_host_contract: noop_get_host_contract,
        resolve_host_contract_interface: noop_resolve_host_contract_interface,
        list_bundles: noop_list_bundles,
        get_dependencies: noop_get_dependencies,
        load_bundle: noop_load_bundle,
        reload_bundle: noop_reload_bundle,
        register_host_contract: noop_register_host_contract,
        register_loader: noop_register_loader,
        get_last_error: noop_get_last_error,
        get_error_len: noop_get_error_len,
        unload_bundle: noop_unload_bundle,
        log: stub_host_log,
        create_guest_instance: stub_create_guest_instance,
        destroy_guest_instance: stub_destroy_guest_instance,
        revision_counter: stub_revision_counter,
        reserved: core::ptr::null(),
    };

    let ctx: BundleInitContext = BundleInitContext {
        bundle_path: StringView::null(),
        bundle_id: 0,
    };
    // SAFETY: init_fn is valid; host_interface and ctx live for the duration of this call.
    let init_result: AbiError = unsafe {
        init_fn(
            &host_interface as *const HostApi,
            &ctx as *const BundleInitContext,
        )
    };
    assert_eq!(
        init_result.code,
        AbiErrorCode::Ok as u32,
        "polyplug_init must succeed (code Ok)"
    );

    // SAFETY: CAPTURED_INTERFACE_PTR was written by capture_register_callback above.
    // Single-threaded; no race condition.
    let interface_ptr: *const GuestContractInterface = unsafe { CAPTURED_INTERFACE_PTR };
    assert!(
        !interface_ptr.is_null(),
        "interface pointer must be non-null"
    );

    // SAFETY: interface_ptr is valid (plugin library is loaded, not yet dropped).
    let interface: &GuestContractInterface = unsafe { &*interface_ptr };

    // -- Step 10: Call function_id 0 (do_panic) through the interface --
    // The generated ABI wrapper uses catch_unwind internally, so the panic is
    // caught inside the extern "C" boundary. The host sees AbiError { code: Panic }.

    // The generated create_instance constructs the implementation via the
    // author factory and carries it in instance.data — dispatch needs a real
    // instance.
    let mut instance: polyplug_abi::GuestContractInstance =
        polyplug_abi::GuestContractInstance::null();
    // SAFETY: host_interface outlives the instance; create_instance is the
    // generated factory thunk on the captured interface.
    unsafe {
        (interface.create_instance)(
            polyplug_abi::VmLoaderData::null(),
            &host_interface as *const HostApi,
            core::ptr::null(),
            &mut instance,
        )
    };
    assert!(
        !instance.data.is_null(),
        "create_instance must produce a non-null instance payload"
    );

    // SAFETY: fn_ptr is function 0 in the interface (do_panic).
    let fn_ptr: *const () = unsafe { *interface.dispatch.native.functions.add(0) };
    let dispatch_fn: unsafe extern "C" fn(
        polyplug_abi::GuestContractInstance,
        *const (),
        *mut (),
        *mut AbiError,
    ) =
        // SAFETY: fn_ptr is the do_panic ABI wrapper -- the canonical 4-arg
        // native dispatch signature (trailing out_err). The catch_unwind wrapper
        // inside the plugin catches the panic before it crosses the FFI boundary.
        unsafe { core::mem::transmute(fn_ptr) };

    let mut call_result: AbiError = AbiError::ok();
    // SAFETY: do_panic ignores args and out entirely (void function, no params);
    // instance was created by the generated create_instance above.
    unsafe {
        dispatch_fn(
            instance,
            core::ptr::null(),
            core::ptr::null_mut(),
            &mut call_result,
        )
    };

    // -- Step 11: Assert panic was caught and returned Panic --
    assert_eq!(
        call_result.code,
        AbiErrorCode::Panic as u32,
        "do_panic ABI wrapper must return AbiErrorCode::Panic, got {:?}",
        call_result.code
    );

    // Process continues here -- no abort occurred. Test completing IS the proof.

    // Leak the library to avoid dlclose issues on some platforms.
    core::mem::forget(library);
}

/// `HostApi.log` stub for test hosts — drops the record.
unsafe extern "C" fn stub_host_log(
    _this: *const polyplug_abi::HostApi,
    _level: u32,
    _scope: polyplug_abi::StringView,
    _message: polyplug_abi::StringView,
) {
}

unsafe extern "C" fn stub_create_guest_instance(
    _this: *const polyplug_abi::HostApi,
    _interface: *const polyplug_abi::GuestContractInterface,
    _args: *const core::ffi::c_void,
    out_instance: *mut polyplug_abi::GuestContractInstance,
) {
    if !out_instance.is_null() {
        // SAFETY: out_instance is non-null (just checked) and writable per the ABI contract.
        unsafe { out_instance.write(polyplug_abi::GuestContractInstance::null()) };
    }
}

unsafe extern "C" fn stub_destroy_guest_instance(
    _this: *const polyplug_abi::HostApi,
    _interface: *const polyplug_abi::GuestContractInterface,
    _instance: polyplug_abi::GuestContractInstance,
) {
}

unsafe extern "C" fn stub_revision_counter(_this: *const polyplug_abi::HostApi) -> *const u64 {
    core::ptr::null()
}