Skip to content

Commit

Permalink
cli: Generate driver structs automatically using bindgen
Browse files Browse the repository at this point in the history
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 <bercarug@amazon.com>
  • Loading branch information
bercarug committed Jul 14, 2020
1 parent b74f562 commit f1e1f57
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 30 deletions.
117 changes: 117 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Expand Up @@ -22,6 +22,9 @@ procfs = "0.7"
eif_loader = { path = "./eif_loader" }
enclave_build = { path = "./enclave_build"}

[build-dependencies]
bindgen = { version=">=0.54" }

[dev-dependencies]
log = "0.4"
num-derive = "0.2"
Expand Down
26 changes: 26 additions & 0 deletions build.rs
@@ -1,5 +1,11 @@
extern crate bindgen;

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

const HEADER_PATH: &str = "include/uapi/linux/nitro_enclaves.h";
const OUT_FILE: &str = "/driver_structs.rs";

fn main() {
// Get latest commit SHA.
let output = Command::new("git")
Expand All @@ -21,4 +27,24 @@ fn main() {
};

println!("cargo:rustc-env=COMMIT_ID={}", output_str.trim());
println!("cargo:rerun-if-changed={}", HEADER_PATH);

let bindings = bindgen::Builder::default()
.header(HEADER_PATH)
.whitelist_type("ne_image_load_info")
.whitelist_type("ne_user_memory_region")
.whitelist_type("ne_enclave_start_info")
.clang_arg(r"-fretain-comments-from-system-headers")
.clang_arg(r"-fparse-all-comments")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate_comments(true)
.generate()
.expect("Unable to generate bindings");

let mut path_str = std::env::var("OUT_DIR").unwrap();
path_str.push_str(&OUT_FILE);
let out_path = PathBuf::from(path_str);
bindings
.write_to_file(out_path)
.expect("Could not write bindings");
}

0 comments on commit f1e1f57

Please sign in to comment.