From b4200b866aa017dc0f1dd33bd7fccbbf27fb52a7 Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Fri, 1 Jan 2021 19:36:57 -0500 Subject: [PATCH] [rt] Fix some FFI safety issues Need #[repr(C)] on a union to make it FFI safe, and need extern "C" on FFI function pointers. --- Cargo.lock | 2 +- rt/src/lib.rs | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1c11f1f..4a9c04c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -99,7 +99,7 @@ dependencies = [ [[package]] name = "ivy" -version = "0.1.0" +version = "0.2.0" dependencies = [ "ess", "pretty", diff --git a/rt/src/lib.rs b/rt/src/lib.rs index 72bf27a..aea68b9 100644 --- a/rt/src/lib.rs +++ b/rt/src/lib.rs @@ -1,5 +1,6 @@ use std::sync::atomic::{Ordering, AtomicU32}; +const _STDIN: i32 = 0; const _STDOUT: i32 = 1; const STDERR: i32 = 2; @@ -36,12 +37,13 @@ pub struct ObjLam { tag: ObjTag, upvars: u16, rc: AtomicU32, - func: fn(&ObjLam) -> Obj, + func: extern "C" fn(&ObjLam) -> Obj, params: u16, filled: u16, } #[derive(Clone, Copy)] +#[repr(C)] pub union Obj { int: i64, header: *mut ObjHeader, @@ -97,7 +99,7 @@ pub unsafe extern "C" fn ivy_make_int(value: i64) -> Obj { } #[no_mangle] -pub unsafe extern "C" fn ivy_make_lam(func: fn(&ObjLam) -> Obj, params: u16, upvars: u16) -> Obj { +pub unsafe extern "C" fn ivy_make_lam(func: extern "C" fn(&ObjLam) -> Obj, params: u16, upvars: u16) -> Obj { let size = ObjLam::size_of(params, upvars); let box_lam = sys::malloc(size) as *mut ObjLam; box_lam.write(ObjLam { -- 2.47.0