Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow proxy_get_header_map_value to return nullptr #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 55 additions & 51 deletions src/hostcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ fn get_hostfunc(
.staged
.get_expect_set_header_map_pairs(map_type, header_map_ptr);
}
println!("[vm->host] proxy_set_header_map_pairs(map_type={}, map_data, map_size) status: {:?}",
println!("[vm->host] proxy_set_header_map_pairs(map_type={}, map_data, map_size) status: {:?}",
map_type, get_status()
);
println!(
Expand All @@ -797,9 +797,8 @@ fn get_hostfunc(
return_value_data: i32,
return_value_size: i32|
-> i32 {
// Default Function: respond with a default header map value corresponding to map_type (if exists)
// Default Function: respond with a default header map value corresponding to map_type (if exists, nullptr otherwise)
// Expectation: respond with set expected header map value for the given key and map_type
// Panics if there is no header map value in expectation or host simulator for the provided map_type and key
let mem = match caller.get_export("memory") {
Some(Extern::Memory(mem)) => mem,
_ => {
Expand All @@ -823,7 +822,7 @@ fn get_hostfunc(
};

unsafe {
let (string_key, string_value) = {
let (string_key, maybe_string_value) = {
let key_data_ptr = mem
.data(&caller)
.get(key_data as u32 as usize..)
Expand All @@ -832,59 +831,64 @@ fn get_hostfunc(
.map(|string_msg| std::str::from_utf8(string_msg).unwrap())
.unwrap();

let string_value = match EXPECT
let maybe_string_value = EXPECT
.lock()
.unwrap()
.staged
.get_expect_get_header_map_value(map_type, string_key)
{
Some(expect_string_value) => expect_string_value,
None => {
match HOST.lock().unwrap().staged.get_header_map_value(map_type, &string_key) {
Some(host_string_value) => host_string_value,
None => panic!("Error: proxy_get_header_map_value | no header map value for key {}", string_key)}
}
};
(string_key.to_string(), string_value)
};

let value_data_add = {
let mut result = [Val::I32(0)];
malloc
.call(
&mut caller,
&[Val::I32(string_value.len() as i32)],
&mut result,
)
.unwrap();
result[0].i32().unwrap() as u32 as usize
.or_else(|| {
HOST.lock()
.unwrap()
.staged
.get_header_map_value(map_type, &string_key)
});
(string_key.to_string(), maybe_string_value)
};

let value_data_ptr = mem
.data_mut(&mut caller)
.get_unchecked_mut(value_data_add..value_data_add + string_value.len());
value_data_ptr.copy_from_slice((&string_value).as_bytes());

let return_value_data_ptr = mem.data_mut(&mut caller).get_unchecked_mut(
return_value_data as u32 as usize
..return_value_data as u32 as usize + 4,
);
return_value_data_ptr
.copy_from_slice(&(value_data_add as u32).to_le_bytes());

let return_value_size_ptr = mem.data_mut(&mut caller).get_unchecked_mut(
return_value_size as u32 as usize
..return_value_size as u32 as usize + 4,
);
return_value_size_ptr
.copy_from_slice(&(string_value.len() as u32).to_le_bytes());

println!("[vm->host] proxy_get_header_map_value(map_type={}, key_data={}, key_size={}) -> (...) status: {:?}",
map_type, string_key, key_size, get_status()
);
println!("[vm<-host] proxy_get_header_map_value(...) -> (return_value_data={}, return_value_size={}) return: {:?}",
string_value, string_value.len(), Status::Ok
);
match maybe_string_value {
Some(string_value) => {
let value_data_add = {
let mut result = [Val::I32(0)];
malloc
.call(
&mut caller,
&[Val::I32(string_value.len() as i32)],
&mut result,
)
.unwrap();
result[0].i32().unwrap() as u32 as usize
};

let value_data_ptr = mem.data_mut(&mut caller).get_unchecked_mut(
value_data_add..value_data_add + string_value.len(),
);
value_data_ptr.copy_from_slice((&string_value).as_bytes());

let return_value_data_ptr =
mem.data_mut(&mut caller).get_unchecked_mut(
return_value_data as u32 as usize
..return_value_data as u32 as usize + 4,
);
return_value_data_ptr
.copy_from_slice(&(value_data_add as u32).to_le_bytes());

let return_value_size_ptr =
mem.data_mut(&mut caller).get_unchecked_mut(
return_value_size as u32 as usize
..return_value_size as u32 as usize + 4,
);
return_value_size_ptr
.copy_from_slice(&(string_value.len() as u32).to_le_bytes());

println!("[vm->host] proxy_get_header_map_value(map_type={}, key_data={}, key_size={}) -> (...) status: {:?}", map_type, string_key, key_size, get_status());
println!("[vm<-host] proxy_get_header_map_value(...) -> (return_value_data={}, return_value_size={}) return: {:?}", string_value, string_value.len(), Status::Ok);
}
None => {
let mut data_ptr =
mem.data_ptr(&mut caller).offset(return_value_data as isize);
data_ptr = std::ptr::null_mut();
}
}
}
assert_ne!(get_status(), ExpectStatus::Failed);
set_status(ExpectStatus::Unexpected);
Expand Down
Loading