Skip to content

Commit e9be418

Browse files
authored
SDK: Only define global alloc in code output by entrypoint macro (#4733)
* Permit removal of the global alloc declaration in the SDK We shouldn't declare a global alloc in unit tests. (Ours is currently interopable with linux, which is why nothing breaks right now) * Revert "Permit removal of the global alloc declaration in the SDK" This reverts commit d4fdf80. * Move alloc and logger delcarations into entrypoint macro * make change non breaking * re-export the alloc struct from the sdk, so the entrypoint macro can use it * fix typo specifying dep * Add workspace wide setting to disable default features for oak_enclave_runtime_support, as otherwise it is ignored * Make oak_enclave_runtime_support/src/heap.rs clippy compliant since it's now public and parsed * Remove empty allocator init function
1 parent 918e83d commit e9be418

File tree

9 files changed

+28
-24
lines changed

9 files changed

+28
-24
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ oak_containers_sdk = { path = "./oak_containers_sdk" }
103103
oak_core = { path = "./oak_core" }
104104
oak_crypto = { path = "./oak_crypto" }
105105
oak_dice = { path = "./oak_dice" }
106-
oak_enclave_runtime_support = { path = "./oak_enclave_runtime_support" }
106+
oak_enclave_runtime_support = { path = "./oak_enclave_runtime_support", default-features = false }
107107
oak_functions_abi = { path = "./oak_functions_abi" }
108108
oak_functions_client = { path = "./oak_functions_client" }
109109
oak_functions_launcher = { path = "./oak_functions_launcher" }

oak_enclave_runtime_support/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ authors = ["Andri Saar <andrisaar@google.com>"]
55
edition = "2021"
66
license = "Apache-2.0"
77

8+
[features]
9+
default = ["global_allocator"]
10+
global_allocator = []
11+
812
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
913
[dependencies]
1014
libm = "*"

oak_enclave_runtime_support/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
Runtime support library for applications built for Oak Restricted Kernel.
88

9-
For now, the runtime support library only sets up the global heap allocator.
9+
For now, the runtime support library provides a global heap allocator.

oak_enclave_runtime_support/src/heap.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,18 @@ impl GrowableHeap {
7979
}
8080
}
8181

82-
pub unsafe fn init(&mut self) {}
83-
82+
#[allow(clippy::result_unit_err)]
8483
pub fn allocate(&mut self, layout: Layout) -> Result<NonNull<u8>, ()> {
8584
self.heap
8685
.allocate(layout)
8786
.ok_or_else(|| log::error!("failed to allocate memory with layout: {:?}", layout))
8887
}
8988

89+
/// # Safety
90+
///
91+
/// - `ptr` must denote a memory block previously allocated via `self`.
92+
/// - The memory block must have been allocated with the same alignment ([`Layout::align`]) as
93+
/// `align`.
9094
pub unsafe fn deallocate(&mut self, ptr: NonNull<u8>, align: usize) {
9195
self.heap.deallocate(ptr, align)
9296
}

oak_enclave_runtime_support/src/lib.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818

1919
use heap::LockedGrowableHeap;
2020

21-
mod heap;
21+
pub mod heap;
2222
mod libm;
2323

24-
#[cfg_attr(not(test), global_allocator)]
24+
#[cfg(feature = "global_allocator")]
25+
#[global_allocator]
2526
static ALLOCATOR: LockedGrowableHeap = LockedGrowableHeap::empty();
2627

27-
pub fn init() {
28-
unsafe {
29-
ALLOCATOR.lock().init();
30-
}
31-
}
28+
#[deprecated(note = "please make use of `oak_restricted_kernel_sdk::entrypoint` instead.")]
29+
pub fn init() {}

oak_restricted_kernel_sdk/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ oak_crypto = { workspace = true }
1717
oak_core = { workspace = true }
1818
oak_dice = { workspace = true }
1919
oak_restricted_kernel_interface = { workspace = true }
20-
oak_enclave_runtime_support = { workspace = true }
20+
oak_enclave_runtime_support = { default-features = false, workspace = true }
2121
oak_restricted_kernel_sdk_proc_macro = { workspace = true }
2222
oak_restricted_kernel_dice = { workspace = true, optional = true }
2323
oak_stage0_dice = { workspace = true, optional = true }

oak_restricted_kernel_sdk/src/lib.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,16 @@ mod channel;
2222
mod dice;
2323
mod logging;
2424

25+
pub mod utils {
26+
pub use oak_core::*;
27+
pub use oak_enclave_runtime_support::heap;
28+
}
29+
2530
pub use channel::*;
2631
pub use dice::*;
2732
pub use logging::StderrLogger;
28-
use logging::STDERR_LOGGER;
29-
pub use oak_core as utils;
3033
pub use oak_restricted_kernel_sdk_proc_macro::entrypoint;
3134

32-
/// Initialization function that sets up the allocator and logger.
33-
pub fn init(log_level: log::LevelFilter) {
34-
log::set_logger(&STDERR_LOGGER).expect("failed to set logger");
35-
log::set_max_level(log_level);
36-
oak_enclave_runtime_support::init();
37-
}
38-
3935
pub fn alloc_error_handler(layout: ::core::alloc::Layout) -> ! {
4036
panic!("error allocating memory: {:#?}", layout);
4137
}

oak_restricted_kernel_sdk/src/logging.rs

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ use core::fmt::Write;
1818

1919
use oak_restricted_kernel_interface::syscall::{fsync, write};
2020

21-
pub static STDERR_LOGGER: StderrLogger = StderrLogger {};
22-
2321
struct Stderr {}
2422

2523
impl Stderr {

oak_restricted_kernel_sdk_proc_macro/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,15 @@ fn process_entry_fn(entry_fn: ItemFn) -> TokenStream {
6262
let generated = quote! {
6363
#entry_fn
6464

65+
#[global_allocator]
66+
static ALLOCATOR: oak_restricted_kernel_sdk::utils::heap::LockedGrowableHeap = oak_restricted_kernel_sdk::utils::heap::LockedGrowableHeap::empty();
67+
6568
static LOGGER: oak_restricted_kernel_sdk::StderrLogger = oak_restricted_kernel_sdk::StderrLogger {};
6669

6770
#[no_mangle]
6871
fn _start() -> ! {
69-
oak_restricted_kernel_sdk::init(log::LevelFilter::Debug);
72+
log::set_logger(&LOGGER).expect("failed to set logger");
73+
log::set_max_level(log::LevelFilter::Debug);
7074
log::info!("In main!");
7175
#entry_fn_name();
7276
}

0 commit comments

Comments
 (0)