dragonflyoss / client

Dragonfly client written in Rust
Apache License 2.0
31 stars 19 forks source link

Backend plugin api is not FFI safe #404

Open imtsuki opened 6 months ago

imtsuki commented 6 months ago

Currently Rust compiler does not provide any guarantee for ABI stability across FFI boundaries, so the calling convention, trait object layout, etc. might be different across compiler versions or even different builds. Some issues in current backend plugin implementation include

  1. register_plugin is not extern "C";
  2. register_plugin returns a trait object, which is not FFI-safe;
  3. async_trait desugars to something like fn () -> Box<dyn Future>, which also returns a trait object;
  4. parameters like HeadRequest are not repr(C).

https://github.com/dragonflyoss/client/blob/b2ff71c7b93fba5cf5411142ab34232a4e1f9015/dragonfly-client-backend/examples/plugin/src/lib.rs#L31-L51

I suggest to use some libraries like https://docs.rs/async-ffi/latest/async_ffi/ and https://docs.rs/abi_stable/latest/abi_stable/; or if we want to support plugins written in other languages, we might need to re-consider the interface design, particularly regarding how should we handle async operations like Futures.

gaius-qi commented 6 months ago

@imtsuki No need to support plugin written in other languages. It is necessary to provide a build image of the plugin to make a consistent environment, refer to https://d7y.io/docs/next/contribute/development-guide/plugin-builder/.

xujihui1985 commented 6 months ago

@gaius-qi I think what @imtsuki mean is if we want to load .so dynamically as what the example doing now, we need to take ffi safety into consideration.