vmbase: Initialize heap in rust_entry
Initialize the heap as we enter Rust so that any code that follows can
make use of alloc, preventing bugs such as aosp/2567870 where client
code used the heap before initializing it.
Test: TH
Change-Id: Ibe6629a22cd828d8b9c6418b91b5971dbbeb4c3d
diff --git a/vmbase/src/entry.rs b/vmbase/src/entry.rs
index 8cdfe77..df0bb7c 100644
--- a/vmbase/src/entry.rs
+++ b/vmbase/src/entry.rs
@@ -14,11 +14,13 @@
//! Rust entry point.
-use crate::{console, power::shutdown};
+use crate::{console, heap, power::shutdown};
/// This is the entry point to the Rust code, called from the binary entry point in `entry.S`.
#[no_mangle]
extern "C" fn rust_entry(x0: u64, x1: u64, x2: u64, x3: u64) -> ! {
+ // SAFETY - Only called once, from here, and inaccessible to client code.
+ unsafe { heap::init() };
console::init();
unsafe {
main(x0, x1, x2, x3);
diff --git a/vmbase/src/heap.rs b/vmbase/src/heap.rs
index a28a02c..08240b9 100644
--- a/vmbase/src/heap.rs
+++ b/vmbase/src/heap.rs
@@ -51,7 +51,7 @@
/// # Safety
///
/// Must be called no more than once.
-pub unsafe fn init() {
+pub(crate) unsafe fn init() {
// SAFETY: Nothing else accesses this memory, and we hand it over to the heap to manage and
// never touch it again. The heap is locked, so there cannot be any races.
let (start, size) = unsafe { (HEAP.as_mut_ptr() as usize, HEAP.len()) };