Skip to content

Commit f1e1f57

Browse files
committed
cli: Generate driver structs automatically using bindgen
Added a build script that automatically generates rust bindings for the C structures used by the NE driver. Currently, rust does not allow to implement a destructor and to derive the `Copy` trait for a structure. By default, bindgen derives `Copy` for the generated structures. Although the behavior can be altered by using the Builder's `no_copy()` method, this will also prevent deriving `Clone` (which in turn makes the binding unusable). According to these sources (https://users.rust-lang.org/t/how-to-derive-custom-traits-for-struct-definitions-generated-by-bindgen/39710, rust-lang/rust-bindgen#1089), adding custom derives for the generated bindings is not currently supported. Update: as a workaround (for `ne_user_memory_region`), the bindgen generated struct has been kept and the CLI's MemoryRegion now contains a method for converting between its own format and the driver struct format. This is used for converting to the driver struct representation, when issuing a `NE_SET_USER_MEMORY_REGION` ioctl. The conversion between the two mentioned types is done by implementing the `From` trait for UserMemoryRegion ( which also automatically provides an implementation for `.into()`, for constructing a UserMemoryRegion instance from a MemoryRegion one). Signed-off-by: Gabriel Bercaru <[email protected]>
1 parent b74f562 commit f1e1f57

File tree

5 files changed

+198
-30
lines changed

5 files changed

+198
-30
lines changed

Cargo.lock

+117
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ procfs = "0.7"
2222
eif_loader = { path = "./eif_loader" }
2323
enclave_build = { path = "./enclave_build"}
2424

25+
[build-dependencies]
26+
bindgen = { version=">=0.54" }
27+
2528
[dev-dependencies]
2629
log = "0.4"
2730
num-derive = "0.2"

build.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
extern crate bindgen;
2+
3+
use std::path::PathBuf;
14
use std::process::Command;
25

6+
const HEADER_PATH: &str = "include/uapi/linux/nitro_enclaves.h";
7+
const OUT_FILE: &str = "/driver_structs.rs";
8+
39
fn main() {
410
// Get latest commit SHA.
511
let output = Command::new("git")
@@ -21,4 +27,24 @@ fn main() {
2127
};
2228

2329
println!("cargo:rustc-env=COMMIT_ID={}", output_str.trim());
30+
println!("cargo:rerun-if-changed={}", HEADER_PATH);
31+
32+
let bindings = bindgen::Builder::default()
33+
.header(HEADER_PATH)
34+
.whitelist_type("ne_image_load_info")
35+
.whitelist_type("ne_user_memory_region")
36+
.whitelist_type("ne_enclave_start_info")
37+
.clang_arg(r"-fretain-comments-from-system-headers")
38+
.clang_arg(r"-fparse-all-comments")
39+
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
40+
.generate_comments(true)
41+
.generate()
42+
.expect("Unable to generate bindings");
43+
44+
let mut path_str = std::env::var("OUT_DIR").unwrap();
45+
path_str.push_str(&OUT_FILE);
46+
let out_path = PathBuf::from(path_str);
47+
bindings
48+
.write_to_file(out_path)
49+
.expect("Could not write bindings");
2450
}

0 commit comments

Comments
 (0)