diff --git a/Cargo.toml b/Cargo.toml index 23f9e8ed99f..def284f3f44 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -103,7 +103,7 @@ oak_containers_sdk = { path = "./oak_containers_sdk" } oak_core = { path = "./oak_core" } oak_crypto = { path = "./oak_crypto" } oak_dice = { path = "./oak_dice" } -oak_enclave_runtime_support = { path = "./oak_enclave_runtime_support" } +oak_enclave_runtime_support = { path = "./oak_enclave_runtime_support", default-features = false } oak_functions_abi = { path = "./oak_functions_abi" } oak_functions_client = { path = "./oak_functions_client" } oak_functions_launcher = { path = "./oak_functions_launcher" } diff --git a/oak_enclave_runtime_support/Cargo.toml b/oak_enclave_runtime_support/Cargo.toml index a53ae1eb70a..beb2c28c850 100644 --- a/oak_enclave_runtime_support/Cargo.toml +++ b/oak_enclave_runtime_support/Cargo.toml @@ -5,6 +5,10 @@ authors = ["Andri Saar "] edition = "2021" license = "Apache-2.0" +[features] +default = ["global_allocator"] +global_allocator = [] + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] libm = "*" diff --git a/oak_enclave_runtime_support/README.md b/oak_enclave_runtime_support/README.md index d11d87e6955..1d5b98749f5 100644 --- a/oak_enclave_runtime_support/README.md +++ b/oak_enclave_runtime_support/README.md @@ -6,4 +6,4 @@ Runtime support library for applications built for Oak Restricted Kernel. -For now, the runtime support library only sets up the global heap allocator. +For now, the runtime support library provides a global heap allocator. diff --git a/oak_enclave_runtime_support/src/heap.rs b/oak_enclave_runtime_support/src/heap.rs index de6d785d740..795af6b3cf8 100644 --- a/oak_enclave_runtime_support/src/heap.rs +++ b/oak_enclave_runtime_support/src/heap.rs @@ -79,14 +79,18 @@ impl GrowableHeap { } } - pub unsafe fn init(&mut self) {} - + #[allow(clippy::result_unit_err)] pub fn allocate(&mut self, layout: Layout) -> Result, ()> { self.heap .allocate(layout) .ok_or_else(|| log::error!("failed to allocate memory with layout: {:?}", layout)) } + /// # Safety + /// + /// - `ptr` must denote a memory block previously allocated via `self`. + /// - The memory block must have been allocated with the same alignment ([`Layout::align`]) as + /// `align`. pub unsafe fn deallocate(&mut self, ptr: NonNull, align: usize) { self.heap.deallocate(ptr, align) } diff --git a/oak_enclave_runtime_support/src/lib.rs b/oak_enclave_runtime_support/src/lib.rs index 34598563298..3215354e041 100644 --- a/oak_enclave_runtime_support/src/lib.rs +++ b/oak_enclave_runtime_support/src/lib.rs @@ -18,14 +18,12 @@ use heap::LockedGrowableHeap; -mod heap; +pub mod heap; mod libm; -#[cfg_attr(not(test), global_allocator)] +#[cfg(feature = "global_allocator")] +#[global_allocator] static ALLOCATOR: LockedGrowableHeap = LockedGrowableHeap::empty(); -pub fn init() { - unsafe { - ALLOCATOR.lock().init(); - } -} +#[deprecated(note = "please make use of `oak_restricted_kernel_sdk::entrypoint` instead.")] +pub fn init() {} diff --git a/oak_restricted_kernel_sdk/Cargo.toml b/oak_restricted_kernel_sdk/Cargo.toml index d3e183d61dd..c9c3cc12e4a 100644 --- a/oak_restricted_kernel_sdk/Cargo.toml +++ b/oak_restricted_kernel_sdk/Cargo.toml @@ -17,7 +17,7 @@ oak_crypto = { workspace = true } oak_core = { workspace = true } oak_dice = { workspace = true } oak_restricted_kernel_interface = { workspace = true } -oak_enclave_runtime_support = { workspace = true } +oak_enclave_runtime_support = { default-features = false, workspace = true } oak_restricted_kernel_sdk_proc_macro = { workspace = true } oak_restricted_kernel_dice = { workspace = true, optional = true } oak_stage0_dice = { workspace = true, optional = true } diff --git a/oak_restricted_kernel_sdk/src/lib.rs b/oak_restricted_kernel_sdk/src/lib.rs index 4fa696ba611..12688b1e60e 100644 --- a/oak_restricted_kernel_sdk/src/lib.rs +++ b/oak_restricted_kernel_sdk/src/lib.rs @@ -22,20 +22,16 @@ mod channel; mod dice; mod logging; +pub mod utils { + pub use oak_core::*; + pub use oak_enclave_runtime_support::heap; +} + pub use channel::*; pub use dice::*; pub use logging::StderrLogger; -use logging::STDERR_LOGGER; -pub use oak_core as utils; pub use oak_restricted_kernel_sdk_proc_macro::entrypoint; -/// Initialization function that sets up the allocator and logger. -pub fn init(log_level: log::LevelFilter) { - log::set_logger(&STDERR_LOGGER).expect("failed to set logger"); - log::set_max_level(log_level); - oak_enclave_runtime_support::init(); -} - pub fn alloc_error_handler(layout: ::core::alloc::Layout) -> ! { panic!("error allocating memory: {:#?}", layout); } diff --git a/oak_restricted_kernel_sdk/src/logging.rs b/oak_restricted_kernel_sdk/src/logging.rs index b539cd49eb3..ff8aa35aca9 100644 --- a/oak_restricted_kernel_sdk/src/logging.rs +++ b/oak_restricted_kernel_sdk/src/logging.rs @@ -18,8 +18,6 @@ use core::fmt::Write; use oak_restricted_kernel_interface::syscall::{fsync, write}; -pub static STDERR_LOGGER: StderrLogger = StderrLogger {}; - struct Stderr {} impl Stderr { diff --git a/oak_restricted_kernel_sdk_proc_macro/src/lib.rs b/oak_restricted_kernel_sdk_proc_macro/src/lib.rs index eb27c95e58a..dad7aa3e0b7 100644 --- a/oak_restricted_kernel_sdk_proc_macro/src/lib.rs +++ b/oak_restricted_kernel_sdk_proc_macro/src/lib.rs @@ -62,11 +62,15 @@ fn process_entry_fn(entry_fn: ItemFn) -> TokenStream { let generated = quote! { #entry_fn + #[global_allocator] + static ALLOCATOR: oak_restricted_kernel_sdk::utils::heap::LockedGrowableHeap = oak_restricted_kernel_sdk::utils::heap::LockedGrowableHeap::empty(); + static LOGGER: oak_restricted_kernel_sdk::StderrLogger = oak_restricted_kernel_sdk::StderrLogger {}; #[no_mangle] fn _start() -> ! { - oak_restricted_kernel_sdk::init(log::LevelFilter::Debug); + log::set_logger(&LOGGER).expect("failed to set logger"); + log::set_max_level(log::LevelFilter::Debug); log::info!("In main!"); #entry_fn_name(); }