-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dynamic_modules: scaffolds Rust SDK #35914
Merged
mattklein123
merged 16 commits into
envoyproxy:main
from
mathetake:dynamicmodulerustsdkscaffolds2
Sep 4, 2024
+199
−2
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7b798a7
dynamic_modules: scaffolds Rust SDK
mathetake 3cd1bdc
Adds README and check
mathetake 65af05c
more
mathetake efd1d4e
manual bindgen
mathetake beac95e
comment
mathetake e974278
add readme
mathetake 1add34e
trailing space
mathetake 21053c5
more
mathetake 7b4bd30
review: use LazyLock which requires 1.80+
mathetake 7a6e10f
1.80.0
mathetake a9e7a3c
more
mathetake 5a24889
use Atomic instead of LazyLock
mathetake fff449c
try using the releases of rules_rust
mathetake 6923b75
format
mathetake ee43ed3
Revert "format"
mathetake ef12250
Revert "try using the releases of rules_rust"
mathetake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
## Dynamic Modules SDKs | ||
|
||
This directory contains the SDKs for the Dynamic Modules feature. Each SDK passes the same set of tests and | ||
is guaranteed to provide the same functionality. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
load("@rules_rust//rust:defs.bzl", "rust_library") | ||
load( | ||
"//bazel:envoy_build_system.bzl", | ||
"envoy_extension_package", | ||
) | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
envoy_extension_package() | ||
|
||
rust_library( | ||
name = "envoy_proxy_dynamic_modules_rust_sdk", | ||
srcs = glob(["src/**/*.rs"]), | ||
edition = "2021", | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "envoy-proxy-dynamic-modules-rust-sdk" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors = ["Envoy Proxy Authors <[email protected]>"] | ||
description = "Envoy Proxy Dynamic Modules Rust SDK" | ||
license = "Apache-2.0" | ||
repository = "https://github.com/envoyproxy/envoy" | ||
|
||
[dependencies] | ||
|
||
[lib] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Envoy Dynamic Modules Rust SDK | ||
|
||
This directory contains the Rust SDK for the Dynamic Modules feature. The SDK passes the same set of tests and is guaranteed to provide the same functionality as the other SDKs. This directory is organized in the way that it can be used as a standalone Rust crate. The SDK is basically the high-level abstraction layer for the Dynamic Modules ABI defined in the [abi.h](../../abi.h). | ||
|
||
Currently, the ABI binding ([src/abi.rs](./src/abi.rs)) is manually generated by [`bindgen`](https://github.com/rust-lang/rust-bindgen) for the ABI header. Ideally, we should be able to do the bindgen in the build.rs file. | ||
|
||
TODO(@mathetake): figure out how to properly setup the bindgen in build.rs with rules_rust. The most recommended way is to use [crate_universe](https://bazelbuild.github.io/rules_rust/crate_universe.html#setup) and use bindgen as a dev-dependency. However, it seems that crate_universe tries to use the underlying gcc system linker which we cannot assume always available. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* automatically generated by rust-bindgen 0.70.1 */ | ||
|
||
pub type wchar_t = ::std::os::raw::c_int; | ||
#[repr(C)] | ||
#[repr(align(16))] | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct max_align_t { | ||
pub __clang_max_align_nonce1: ::std::os::raw::c_longlong, | ||
pub __bindgen_padding_0: u64, | ||
pub __clang_max_align_nonce2: u128, | ||
} | ||
#[doc = " envoy_dynamic_module_type_abi_version represents a null-terminated string that contains the ABI\n version of the dynamic module. This is used to ensure that the dynamic module is built against\n the compatible version of the ABI."] | ||
pub type envoy_dynamic_module_type_abi_version = *const ::std::os::raw::c_char; | ||
extern "C" { | ||
#[doc = " envoy_dynamic_module_on_program_init is called by the main thread exactly when the module is\n loaded. The function returns the ABI version of the dynamic module. If null is returned, the\n module will be unloaded immediately.\n\n For Envoy, the return value will be used to check the compatibility of the dynamic module.\n\n For dynamic modules, this is useful when they need to perform some process-wide\n initialization or check if the module is compatible with the platform, such as CPU features.\n Note that initialization routines of a dynamic module can also be performed without this function\n through constructor functions in an object file. However, normal constructors cannot be used\n to check compatibility and gracefully fail the initialization because there is no way to\n report an error to Envoy.\n\n @return envoy_dynamic_module_type_abi_version is the ABI version of the dynamic module. Null\n means the error and the module will be unloaded immediately."] | ||
pub fn envoy_dynamic_module_on_program_init() -> envoy_dynamic_module_type_abi_version; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#![allow(non_upper_case_globals)] | ||
#![allow(non_camel_case_types)] | ||
#![allow(non_snake_case)] | ||
#![allow(dead_code)] | ||
|
||
mod abi; | ||
|
||
/// Declare the init function for the dynamic module. This function is called when the dynamic module is loaded. | ||
/// The function must return true on success, and false on failure. When it returns false, | ||
/// the dynamic module will not be loaded. | ||
/// | ||
/// This is useful to perform any process-wide initialization that the dynamic module needs. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use envoy_proxy_dynamic_modules_rust_sdk::declare_program_init; | ||
/// | ||
/// declare_program_init!(my_program_init); | ||
/// | ||
/// fn my_program_init() -> bool { | ||
/// true | ||
/// } | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! declare_program_init { | ||
($f:ident) => { | ||
#[no_mangle] | ||
pub extern "C" fn envoy_dynamic_module_on_program_init() -> *const ::std::os::raw::c_char { | ||
if ($f()) { | ||
// This magic number is sha256 of the ABI headers which must match the | ||
// value in abi_version.h | ||
b"749b1e6bf97309b7d171009700a80e651ac61e35f9770c24a63460d765895a51\0".as_ptr() | ||
as *const ::std::os::raw::c_char | ||
} else { | ||
::std::ptr::null() | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
load("@rules_rust//rust:defs.bzl", "rust_shared_library") | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
package(default_visibility = ["//test/extensions/dynamic_modules:__pkg__"]) | ||
|
||
rust_shared_library( | ||
name = "no_op", | ||
srcs = ["no_op.rs"], | ||
edition = "2021", | ||
deps = [ | ||
"//source/extensions/dynamic_modules/sdk/rust:envoy_proxy_dynamic_modules_rust_sdk", | ||
], | ||
) | ||
|
||
rust_shared_library( | ||
name = "no_program_init", | ||
srcs = ["no_program_init.rs"], | ||
edition = "2021", | ||
) | ||
|
||
rust_shared_library( | ||
name = "program_init_fail", | ||
srcs = ["program_init_fail.rs"], | ||
edition = "2021", | ||
) | ||
|
||
rust_shared_library( | ||
name = "abi_version_mismatch", | ||
srcs = ["abi_version_mismatch.rs"], | ||
edition = "2021", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[package] | ||
name = "test-programs" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
repository = "https://github.com/envoyproxy/envoy" | ||
|
||
[dependencies] | ||
envoy-proxy-dynamic-modules-rust-sdk = { path = "../../../../../source/extensions/dynamic_modules/sdk/rust" } | ||
|
||
[[example]] | ||
name = "no_op" | ||
path = "no_op.rs" | ||
crate-type = ["cdylib"] | ||
|
||
[[example]] | ||
name = "no_program_init" | ||
path = "no_program_init.rs" | ||
crate-type = ["cdylib"] | ||
|
||
[[example]] | ||
name = "program_init_fail" | ||
path = "program_init_fail.rs" | ||
crate-type = ["cdylib"] | ||
|
||
[[example]] | ||
name = "abi_version_mismatch" | ||
path = "abi_version_mismatch.rs" | ||
crate-type = ["cdylib"] |
4 changes: 4 additions & 0 deletions
4
test/extensions/dynamic_modules/test_data/rust/abi_version_mismatch.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#[no_mangle] | ||
pub extern "C" fn envoy_dynamic_module_on_program_init() -> *const ::std::os::raw::c_char { | ||
b"invalid-version-hash\0".as_ptr() as *const ::std::os::raw::c_char | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use envoy_proxy_dynamic_modules_rust_sdk::declare_program_init; | ||
use std::sync::atomic::{AtomicI32, Ordering}; | ||
|
||
declare_program_init!(init); | ||
|
||
fn init() -> bool { | ||
true | ||
} | ||
|
||
static SOME_VARIABLE: AtomicI32 = AtomicI32::new(1); | ||
|
||
#[no_mangle] | ||
pub extern "C" fn getSomeVariable() -> i32 { | ||
SOME_VARIABLE.fetch_add(1, Ordering::SeqCst) | ||
} |
3 changes: 3 additions & 0 deletions
3
test/extensions/dynamic_modules/test_data/rust/no_program_init.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub extern "C" fn foo() -> i32 { | ||
0 | ||
} |
4 changes: 4 additions & 0 deletions
4
test/extensions/dynamic_modules/test_data/rust/program_init_fail.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#[no_mangle] | ||
pub extern "C" fn envoy_dynamic_module_on_program_init() -> *const ::std::os::raw::c_char { | ||
::std::ptr::null() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: i tried so hard (like spent the entire two days) to get the crate_universe working in our envoy build settings but no luck. basically it is officially recommended way to deal with cargo dependencies but it seems not that compatible with our settings. So I had to choose to do the manual bindgen for now - which of course not ideal so I left this TODO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use crate universe here with bazel: https://github.com/bitdriftlabs/capture-sdk. Not sure if there is anything to be learned from that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I identified the root cause: rules_rust repo declared in Envoy is a bit old and patched version (used by the comptime option Wasmtime dependency and tests for Wasm extensions. They will never be able to use the vanilla rules_rust), hence
crate_universe_dependencies
requiresbootstrap = True
argument due to the reason descrbied in the crate_universe doc:This results in rebuilding cargo-bazel which in turns tries to use the system gcc linker. Since we cannot assume (at least for the current build container and CI settings) gcc exists in the system and set in the PATH, that results in the build failure.
I think we have a few options here:
rules_rust_2
or whatever different repo name.what do you think? I think 2. is the easiest one but to be honest, that could be the beginning of a mess. If it's ok, I would go for 1., but not sure either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #35323 for discussion of the deps clusterfu
i think the solution is a fix in rules_rust (bazelbuild/rules_rust#2665)
cc @martijneken
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok cool, more complex than I expected - I think for now we should go with as-is while waiting for the @phlax's PR got merged. I will migrate here to use crate_universe once it's done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well i got an idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok no luck - i will revert the commits trying to use crate_universe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
heads up - rules_rust is about to be updated