wip: need more work on ret pointer

This commit is contained in:
Matthew LeVan 2024-01-23 15:31:36 -05:00
parent a8ed832f6b
commit 4ff53a1845
3 changed files with 62 additions and 34 deletions

View File

@ -393,11 +393,20 @@ pub struct CCallback<'closure> {
}
impl<'closure> CCallback<'closure> {
pub fn new<F>(closure: &'closure mut F) -> Self where F: FnMut() -> Result {
pub fn new<F>(closure: &'closure mut F) -> Self
where
F: FnMut() -> Result,
{
let function: unsafe extern "C" fn(*mut c_void) -> *mut c_void = Self::call_closure::<F>;
debug_assert_eq!(std::mem::size_of::<&'closure mut F>(), std::mem::size_of::<*const c_void>());
debug_assert_eq!(std::mem::size_of_val(&function), std::mem::size_of::<*const c_void>());
debug_assert_eq!(
std::mem::size_of::<&'closure mut F>(),
std::mem::size_of::<*const c_void>()
);
debug_assert_eq!(
std::mem::size_of_val(&function),
std::mem::size_of::<*const c_void>()
);
Self {
function,
@ -406,10 +415,15 @@ impl<'closure> CCallback<'closure> {
}
}
unsafe extern "C" fn call_closure<F>(user_data: *mut c_void) -> *mut c_void where F: FnMut() -> Result {
unsafe extern "C" fn call_closure<F>(user_data: *mut c_void) -> *mut c_void
where
F: FnMut() -> Result,
{
let cb: &mut F = user_data.cast::<F>().as_mut().unwrap();
let mut v = (*cb)();
eprint!("ares: v: {:?}\r\n", v);
let v_ptr = &mut v as *mut _ as *mut c_void;
eprint!("ares: v_ptr: {:p}\r\n", v_ptr);
v_ptr
}
}
@ -422,7 +436,7 @@ impl<'closure> CCallback<'closure> {
// let c = CCallback::new(closure);
// unsafe { (c.function)(c.user_data, 123) };
// assert_eq!(v, [123]);
// }
@ -434,20 +448,25 @@ pub fn call_with_guard<F: FnMut() -> Result>(
let c = CCallback::new(f);
let mut result: Result = Ok(D(0));
let result_ptr = &mut result as *mut _ as *mut c_void;
let result_ptr_ptr = result_ptr as *mut *mut c_void;
eprint!("ares: result_ptr_ptr: {:p}\r\n", result_ptr_ptr);
unsafe {
let err = guard(
Some(c.function as unsafe extern "C" fn(*mut c_void) -> *mut c_void),
c.user_data as *mut c_void,
stack,
alloc,
result_ptr,
result_ptr_ptr,
);
eprint!("ares: result_ptr: {:p}\r\n", result_ptr);
if let Ok(err) = GuardError::try_from(err) {
match err {
GuardError::GuardSound => {
eprint!("ares: result: {:?}\n", result);
return result;
}
GuardError::GuardArmor => {
@ -932,9 +951,9 @@ pub fn interpret(context: &mut Context, mut subject: Noun, formula: Noun) -> Res
},
NockWork::Work11S(mut sint) => match sint.todo {
Todo11S::ComputeResult => {
if let Some(ret) =
hint::match_pre_nock(context, subject, sint.tag, None, sint.body)
{
if let Some(ret) = hint::match_pre_nock(
context, subject, sint.tag, None, sint.body,
) {
match ret {
Ok(found) => {
res = found;
@ -1008,25 +1027,32 @@ pub fn interpret(context: &mut Context, mut subject: Noun, formula: Noun) -> Res
break Err(Error::ScryCrashed(D(0)));
}
}
Right(cell) => match cell.tail().as_either_atom_cell() {
Left(_) => {
let stack = &mut context.stack;
let hunk = T(
stack,
&[D(tas!(b"hunk")), scry.reff, scry.path],
);
mean_push(stack, hunk);
break Err(Error::ScryCrashed(D(0)));
Right(cell) => {
match cell.tail().as_either_atom_cell() {
Left(_) => {
let stack = &mut context.stack;
let hunk = T(
stack,
&[
D(tas!(b"hunk")),
scry.reff,
scry.path,
],
);
mean_push(stack, hunk);
break Err(Error::ScryCrashed(D(0)));
}
Right(cell) => {
res = cell.tail();
context.scry_stack = scry_stack;
context.stack.pop::<NockWork>();
}
}
Right(cell) => {
res = cell.tail();
context.scry_stack = scry_stack;
context.stack.pop::<NockWork>();
}
},
}
},
Err(error) => match error {
Error::Deterministic(trace) | Error::ScryCrashed(trace) => {
Error::Deterministic(trace)
| Error::ScryCrashed(trace) => {
break Err(Error::ScryCrashed(trace));
}
Error::NonDeterministic(_) => {
@ -1050,6 +1076,7 @@ pub fn interpret(context: &mut Context, mut subject: Noun, formula: Noun) -> Res
})
});
eprint!("nock: {:?}", nock);
match nock {
Ok(res) => Ok(res),
Err(err) => Err(exit(context, &snapshot, virtual_frame, err)),

View File

@ -112,7 +112,7 @@ guard_err guard(
void *user_data,
void *const *stack_pp,
void *const *alloc_pp,
void *ret
void **ret
)
{
stack = (uint64_t**) stack_pp;
@ -124,6 +124,7 @@ guard_err guard(
fprintf(stderr, "guard: stack pointer at %p\r\n", (void *) *stack);
fprintf(stderr, "guard: alloc pointer at %p\r\n", (void *) *alloc);
fprintf(stderr, "guard: ret pointer at %p\r\n", (void *) ret);
fprintf(stderr, "guard: res pointer at %p\r\n", (void *) *ret);
if (guard_p == 0) {
fprintf(stderr, "guard: installing guard page\r\n");
@ -147,21 +148,20 @@ guard_err guard(
result = f(user_data);
}
else {
fprintf(stderr, "guard: jump buffer already set\r\n");
fprintf(stderr, "guard: longjmp\r\n");
if (err != guard_sound) {
fprintf(stderr, "guard: not sound\r\n");
goto fail;
}
else {
fprintf(stderr, "guard: assigning ret\r\n");
*(void **)ret = result;
}
}
fprintf(stderr, "guard: assigning ret to %p\r\n", result);
*ret = result;
if (mprotect(guard_p, GD_PAGESIZE, PROT_READ | PROT_WRITE) == -1) {
err = guard_armor;
goto fail;
}
fprintf(stderr, "guard: uninstalled guard page\r\n");
return guard_sound;
@ -169,6 +169,7 @@ fail:
if (mprotect(guard_p, GD_PAGESIZE, PROT_READ | PROT_WRITE) == -1) {
fprintf(stderr, "guard: failed to uninstall guard page\r\n");
}
fprintf(stderr, "guard: uninstalled guard page\r\n");
switch (err) {
case guard_armor:
fprintf(stderr, "guard: armor error\r\n");

View File

@ -45,7 +45,7 @@ guard_err guard(
void *user_data,
void *const *stack_pp,
void *const *alloc_pp,
void *ret
void **ret
);