diff --git a/Makefile b/Makefile index f913b23..b19e241 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ rust-test: rust-dependencies wasm: cd $(PROJECT_DIR)/lib && cargo build --target wasm32-unknown-unknown --release - mkdir -p $(PROJECT_DIR)/pkg/cedar/static + mkdir -p $(PROJECT_DIR)/static cp $(PROJECT_DIR)/lib/target/wasm32-unknown-unknown/release/cedarwasm.wasm $(PROJECT_DIR)/static/cedar.wasm build: dependencies wasm diff --git a/api_test.go b/api_test.go index bb2ff7a..f77b810 100644 --- a/api_test.go +++ b/api_test.go @@ -39,7 +39,7 @@ func aHugeAllocationMustReturnAPtr(t *testing.T, module api.Module) { func twoConcurrentAllocationMustReturnDifferentPtr(t *testing.T, module api.Module) { exportedFuncs := exportFuncs(module) - entitiesSize := uint64(100) + entitiesSize := uint64(10) entitiesPtr1, err := exportedFuncs[string(allocate)].Call(context.Background(), entitiesSize) if err != nil { t.Fatal(err) diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 58426a0..3f9e501 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" version = "1.0.0" [dependencies] -cedar-policy = { version = "2.0" } +cedar-policy = { version = "2.0.0" } wee_alloc = "0.4.5" once_cell = "1.17.1" diff --git a/lib/src/interface.rs b/lib/src/interface.rs index 1d917b4..80d3570 100644 --- a/lib/src/interface.rs +++ b/lib/src/interface.rs @@ -2,7 +2,6 @@ extern crate alloc; extern crate core; extern crate wee_alloc; -use std::mem::MaybeUninit; use cedar_policy::{PolicySet, Entities, Authorizer, EntityUid, Context, Request, Decision}; use std::{slice}; @@ -19,7 +18,7 @@ static mut ENGINE: Lazy= Lazy::new(|| { } }); -static mut HEAP: Lazy]>>> = Lazy::new(|| { +static mut HEAP: Lazy> = Lazy::new(|| { HashMap::new() }); @@ -137,18 +136,15 @@ pub unsafe extern "C" fn _allocate(size: u32) -> * mut u8 { /// Allocates size bytes and leaks the pointer where they start. unsafe fn allocate(size: usize) -> *mut u8 { // Allocate the amount of bytes needed. - let vec: Vec> = Vec::with_capacity(size); - - let boxed_vec = vec.into_boxed_slice(); + let vec: Vec = Vec::with_capacity(size); // into_raw leaks the memory to the caller. - let ptr = Box::into_raw(Box::from(&boxed_vec)) as *mut u8; + let ptr = vec.as_ptr() as *mut u8; // Store the boxed_vec to prevent it from being deallocated. - HEAP.insert(ptr, boxed_vec); + HEAP.insert(ptr, vec.leak()); // Return the pointer to the caller. ptr - } @@ -255,9 +251,6 @@ mod test { #[test] fn allocate_deallocate() { unsafe { - let zero:u8 = 0; - HEAP.insert(zero as *mut u8, Box::new([MaybeUninit::new(0); 10])); // Insert a dummy value to make sure the map is initialized. - HEAP.remove(&(zero as *mut u8)).unwrap(); // Remove the dummy value. let ptr = allocate(10); assert_eq!(HEAP.contains_key(&ptr), true); deallocate(ptr, 10); diff --git a/static/cedar.wasm b/static/cedar.wasm index a9dc852..6e7986e 100755 Binary files a/static/cedar.wasm and b/static/cedar.wasm differ