Skip to content

Commit

Permalink
Merge pull request #10 from Joffref/joffref/fix-memory-leak
Browse files Browse the repository at this point in the history
Fix OOM issues due to wrong pointer allocation
  • Loading branch information
Joffref authored May 28, 2023
2 parents 3cbc678 + c700f00 commit 9d3aeef
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
15 changes: 4 additions & 11 deletions lib/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -19,7 +18,7 @@ static mut ENGINE: Lazy<CedarEngine>= Lazy::new(|| {
}
});

static mut HEAP: Lazy<HashMap<* mut u8, Box<[MaybeUninit<u8>]>>> = Lazy::new(|| {
static mut HEAP: Lazy<HashMap<* mut u8, &mut [u8]>> = Lazy::new(|| {
HashMap::new()
});

Expand Down Expand Up @@ -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<MaybeUninit<u8>> = Vec::with_capacity(size);

let boxed_vec = vec.into_boxed_slice();
let vec: Vec<u8> = 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

}


Expand Down Expand Up @@ -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);
Expand Down
Binary file modified static/cedar.wasm
Binary file not shown.

0 comments on commit 9d3aeef

Please sign in to comment.