Skip to content

Commit c2511cd

Browse files
authored
Merge pull request #4 from katanemo/more-compatibility
more compatibility fixes
2 parents a0af0bd + 7587a75 commit c2511cd

File tree

2 files changed

+60
-51
lines changed

2 files changed

+60
-51
lines changed

src/expectations.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ impl ExpectHandle {
4444
pub fn assert_stage(&self) {
4545
if self.staged.expect_count > 0 {
4646
panic!(
47-
"Error: failed to consume all expectations - total remaining: {}",
48-
self.staged.expect_count
47+
"Error: failed to consume all expectations - total remaining: {}\n{:?}",
48+
self.staged.expect_count, self.staged
4949
);
5050
} else if self.staged.expect_count < 0 {
5151
panic!(

src/hostcalls.rs

+58-49
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ fn get_hostfunc(
808808
-> i32 {
809809
// Default Function: respond with a default header map value corresponding to map_type (if exists)
810810
// Expectation: respond with set expected header map value for the given key and map_type
811-
// Panics if there is no header map value in expectation or host simulator for the provided map_type and key
811+
// Panics if there is no header map value in expectation or host simulator for the provided map_type and key and one was expected
812812
let mem = match caller.get_export("memory") {
813813
Some(Extern::Memory(mem)) => mem,
814814
_ => {
@@ -832,7 +832,7 @@ fn get_hostfunc(
832832
};
833833

834834
unsafe {
835-
let (string_key, string_value) = {
835+
let (string_key, maybe_string_value) = {
836836
let key_data_ptr = mem
837837
.data(&caller)
838838
.get(key_data as u32 as usize..)
@@ -841,59 +841,64 @@ fn get_hostfunc(
841841
.map(|string_msg| std::str::from_utf8(string_msg).unwrap())
842842
.unwrap();
843843

844-
let string_value = match EXPECT
844+
let maybe_string_value = EXPECT
845845
.lock()
846846
.unwrap()
847847
.staged
848848
.get_expect_get_header_map_value(map_type, string_key)
849-
{
850-
Some(expect_string_value) => expect_string_value,
851-
None => {
852-
match HOST.lock().unwrap().staged.get_header_map_value(map_type, &string_key) {
853-
Some(host_string_value) => host_string_value,
854-
None => panic!("Error: proxy_get_header_map_value | no header map value for key {}", string_key)}
855-
}
856-
};
857-
(string_key.to_string(), string_value)
858-
};
859-
860-
let value_data_add = {
861-
let mut result = [Val::I32(0)];
862-
malloc
863-
.call(
864-
&mut caller,
865-
&[Val::I32(string_value.len() as i32)],
866-
&mut result,
867-
)
868-
.unwrap();
869-
result[0].i32().unwrap() as u32 as usize
849+
.or_else(|| {
850+
HOST.lock()
851+
.unwrap()
852+
.staged
853+
.get_header_map_value(map_type, &string_key)
854+
});
855+
(string_key.to_string(), maybe_string_value)
870856
};
871857

872-
let value_data_ptr = mem
873-
.data_mut(&mut caller)
874-
.get_unchecked_mut(value_data_add..value_data_add + string_value.len());
875-
value_data_ptr.copy_from_slice((&string_value).as_bytes());
876-
877-
let return_value_data_ptr = mem.data_mut(&mut caller).get_unchecked_mut(
878-
return_value_data as u32 as usize
879-
..return_value_data as u32 as usize + 4,
880-
);
881-
return_value_data_ptr
882-
.copy_from_slice(&(value_data_add as u32).to_le_bytes());
883-
884-
let return_value_size_ptr = mem.data_mut(&mut caller).get_unchecked_mut(
885-
return_value_size as u32 as usize
886-
..return_value_size as u32 as usize + 4,
887-
);
888-
return_value_size_ptr
889-
.copy_from_slice(&(string_value.len() as u32).to_le_bytes());
890-
891-
println!("[vm->host] proxy_get_header_map_value(map_type={}, key_data={}, key_size={}) -> (...) status: {:?}",
892-
map_type, string_key, key_size, get_status()
893-
);
894-
println!("[vm<-host] proxy_get_header_map_value(...) -> (return_value_data={}, return_value_size={}) return: {:?}",
895-
string_value, string_value.len(), Status::Ok
896-
);
858+
match maybe_string_value {
859+
Some(string_value) => {
860+
let value_data_add = {
861+
let mut result = [Val::I32(0)];
862+
malloc
863+
.call(
864+
&mut caller,
865+
&[Val::I32(string_value.len() as i32)],
866+
&mut result,
867+
)
868+
.unwrap();
869+
result[0].i32().unwrap() as u32 as usize
870+
};
871+
872+
let value_data_ptr = mem.data_mut(&mut caller).get_unchecked_mut(
873+
value_data_add..value_data_add + string_value.len(),
874+
);
875+
value_data_ptr.copy_from_slice((&string_value).as_bytes());
876+
877+
let return_value_data_ptr =
878+
mem.data_mut(&mut caller).get_unchecked_mut(
879+
return_value_data as u32 as usize
880+
..return_value_data as u32 as usize + 4,
881+
);
882+
return_value_data_ptr
883+
.copy_from_slice(&(value_data_add as u32).to_le_bytes());
884+
885+
let return_value_size_ptr =
886+
mem.data_mut(&mut caller).get_unchecked_mut(
887+
return_value_size as u32 as usize
888+
..return_value_size as u32 as usize + 4,
889+
);
890+
return_value_size_ptr
891+
.copy_from_slice(&(string_value.len() as u32).to_le_bytes());
892+
893+
println!("[vm->host] proxy_get_header_map_value(map_type={}, key_data={}, key_size={}) -> (...) status: {:?}", map_type, string_key, key_size, get_status());
894+
println!("[vm<-host] proxy_get_header_map_value(...) -> (return_value_data={}, return_value_size={}) return: {:?}", string_value, string_value.len(), Status::Ok);
895+
}
896+
None => {
897+
let mut data_ptr =
898+
mem.data_ptr(&mut caller).offset(return_value_data as isize);
899+
data_ptr = std::ptr::null_mut();
900+
}
901+
}
897902
}
898903
assert_ne!(get_status(), ExpectStatus::Failed);
899904
set_status(ExpectStatus::Unexpected);
@@ -1729,6 +1734,10 @@ fn get_hostfunc(
17291734
|mut _caller: Caller<'_, ()>, _param1: i32| -> () { () },
17301735
)),
17311736

1737+
"sched_yield" => Some(Func::wrap(store, |mut _caller: Caller<'_, ()>| -> i32 {
1738+
Status::Ok as i32
1739+
})),
1740+
17321741
"proxy_set_effective_context" => {
17331742
Some(Func::wrap(
17341743
store,

0 commit comments

Comments
 (0)