forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarshal.rs
More file actions
618 lines (556 loc) · 21 KB
/
Copy pathmarshal.rs
File metadata and controls
618 lines (556 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! Marshalling between Nulang `Value`s and C ABI types.
use std::ffi::{c_char, c_void, CStr, CString};
use crate::bytecode::FfiType;
use crate::vm::Value;
use super::native::NativeFunction;
/// C ABI types supported by the FFI layer.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CType {
I64,
F64,
Bool,
CStr,
VoidPtr,
Unit,
}
/// A C function signature for marshalling.
#[derive(Debug, Clone, PartialEq)]
pub struct Signature {
pub params: Vec<CType>,
pub ret: CType,
}
impl Signature {
pub fn new(params: Vec<CType>, ret: CType) -> Self {
Self { params, ret }
}
}
/// Convert a bytecode FFI type to the runtime C type used for marshalling.
pub fn ffi_type_to_ctype(t: &FfiType) -> Option<CType> {
match t {
FfiType::Int => Some(CType::I64),
FfiType::Float => Some(CType::F64),
FfiType::Bool => Some(CType::Bool),
FfiType::String => Some(CType::CStr),
FfiType::Unit => Some(CType::Unit),
FfiType::Pointer => Some(CType::VoidPtr),
}
}
// ---------------------------------------------------------------------------
// Conversion helpers: Value -> C argument
// ---------------------------------------------------------------------------
/// Extract an `i64` from a Nulang value.
pub fn value_to_i64(v: &Value) -> Result<i64, String> {
v.as_int().ok_or_else(|| "expected int".to_string())
}
/// Extract an `f64` from a Nulang value.
pub fn value_to_f64(v: &Value) -> Result<f64, String> {
v.as_float().ok_or_else(|| "expected float".to_string())
}
/// Extract a `bool` from a Nulang value.
pub fn value_to_bool(v: &Value) -> Result<bool, String> {
v.as_bool().ok_or_else(|| "expected bool".to_string())
}
/// Extract a C string pointer from a Nulang pointer value.
///
/// # Safety
/// The returned pointer is borrowed from the value and must remain valid for
/// the duration of the native call.
pub unsafe fn value_to_cstr(v: &Value) -> Result<*const c_char, String> {
v.as_ptr()
.ok_or_else(|| "expected pointer string".to_string())
.map(|p| p as *const c_char)
}
/// Extract a void pointer from a Nulang pointer value.
///
/// # Safety
/// The returned pointer is borrowed from the value and must remain valid for
/// the duration of the native call.
pub unsafe fn value_to_voidptr(v: &Value) -> Result<*mut c_void, String> {
v.as_ptr()
.ok_or_else(|| "expected pointer".to_string())
.map(|p| p as *mut c_void)
}
// ---------------------------------------------------------------------------
// Conversion helpers: C return value -> Value
// ---------------------------------------------------------------------------
/// Marshal a C `i64` return value into a Nulang value.
pub fn i64_to_value(n: i64) -> Value {
Value::int(n)
}
/// Marshal a C `f64` return value into a Nulang value.
pub fn f64_to_value(f: f64) -> Value {
Value::float(f)
}
/// Marshal a C `bool` return value into a Nulang value.
pub fn bool_to_value(b: bool) -> Value {
Value::bool(b)
}
/// Marshal a C string return value into a Nulang pointer value.
///
/// The string is copied into a `CString` and the pointer is leaked to the VM
/// heap model. The caller is responsible for freeing the returned pointer with
/// `free_cstr_value` once it is copied into the actor heap.
///
/// # Safety
/// `s` must be a valid, null-terminated C string.
pub unsafe fn cstr_to_value(s: *const c_char) -> Value {
if s.is_null() {
return Value::nil();
}
let cstr = CStr::from_ptr(s);
let cstring = CString::new(cstr.to_bytes()).unwrap_or_else(|_| CString::default());
Value::ptr(cstring.into_raw() as *mut u8)
}
/// Free a pointer value previously created by `cstr_to_value`.
///
/// # Safety
/// `v` must be a pointer value whose payload was returned by `CString::into_raw`.
pub unsafe fn free_cstr_value(v: Value) {
if let Some(ptr) = v.as_ptr() {
// SAFETY: ptr came from CString::into_raw in cstr_to_value.
let _ = CString::from_raw(ptr as *mut c_char);
}
}
/// Marshal a C void pointer return value into a Nulang pointer value.
pub fn voidptr_to_value(p: *mut c_void) -> Value {
if p.is_null() {
Value::nil()
} else {
Value::ptr(p as *mut u8)
}
}
/// Marshal a C unit return value into a Nulang unit value.
pub fn unit_to_value() -> Value {
Value::unit()
}
// ---------------------------------------------------------------------------
// Type-driven dispatch trait
// ---------------------------------------------------------------------------
/// Maps a supported C type to its Rust FFI representation and provides
/// conversions to/from Nulang `Value`.
pub trait CTypeArg: Copy {
/// The Rust type used in an `extern "C" fn` signature.
type Abi: Copy;
/// The corresponding `CType` variant.
const CTYPE: CType;
/// Convert from a Nulang `Value` to this argument type.
fn from_value(v: Value) -> Result<Self, String>;
/// Convert this argument type to a Nulang `Value`.
fn to_value(self) -> Value;
}
impl CTypeArg for i64 {
type Abi = i64;
const CTYPE: CType = CType::I64;
fn from_value(v: Value) -> Result<Self, String> {
value_to_i64(&v)
}
fn to_value(self) -> Value {
i64_to_value(self)
}
}
impl CTypeArg for f64 {
type Abi = f64;
const CTYPE: CType = CType::F64;
fn from_value(v: Value) -> Result<Self, String> {
value_to_f64(&v)
}
fn to_value(self) -> Value {
f64_to_value(self)
}
}
impl CTypeArg for bool {
type Abi = bool;
const CTYPE: CType = CType::Bool;
fn from_value(v: Value) -> Result<Self, String> {
value_to_bool(&v)
}
fn to_value(self) -> Value {
bool_to_value(self)
}
}
impl CTypeArg for *const c_char {
type Abi = *const c_char;
const CTYPE: CType = CType::CStr;
fn from_value(v: Value) -> Result<Self, String> {
// SAFETY: we only borrow the pointer for the duration of the call.
unsafe { value_to_cstr(&v) }
}
// SAFETY: trait-impl signature is fixed; the pointer is a C string
// produced by the FFI call whose signature declared it as CType::CStr.
#[allow(clippy::not_unsafe_ptr_arg_deref)]
fn to_value(self) -> Value {
// SAFETY: `self` is a C string pointer.
unsafe { cstr_to_value(self) }
}
}
impl CTypeArg for *mut c_void {
type Abi = *mut c_void;
const CTYPE: CType = CType::VoidPtr;
fn from_value(v: Value) -> Result<Self, String> {
// SAFETY: we only borrow the pointer for the duration of the call.
unsafe { value_to_voidptr(&v) }
}
fn to_value(self) -> Value {
voidptr_to_value(self)
}
}
impl CTypeArg for () {
type Abi = ();
const CTYPE: CType = CType::Unit;
fn from_value(_v: Value) -> Result<Self, String> {
Ok(())
}
fn to_value(self) -> Value {
unit_to_value()
}
}
// ---------------------------------------------------------------------------
// Compile-time mapping from CType tokens to Rust ABI types.
// ---------------------------------------------------------------------------
/// Inject the return-type list into another macro invocation.
macro_rules! with_returns {
($macro:ident!($($args:tt)*)) => {
$macro!($($args)*, [(I64, i64); (F64, f64); (Bool, bool); (CStr, *const std::ffi::c_char); (VoidPtr, *mut std::ffi::c_void); (Unit, ())])
};
}
/// Generate match arms for arity 0 (no parameters).
macro_rules! arity_0_arms {
($ptr:expr, $ret:expr, [$(($r:ident, $rty:ty));*]) => {
match $ret {
$(CType::$r => {
// SAFETY: caller guarantees the function ABI matches.
let f: extern "C" fn() -> $rty = unsafe { std::mem::transmute($ptr) };
Ok(<$rty as CTypeArg>::to_value(f()))
},)*
}
};
}
/// Generate match arms for a single parameter of a fixed Rust type.
macro_rules! arity_1_arms {
($ptr:expr, $args:expr, $ret:expr, $pty:ty, [$(($r:ident, $rty:ty));*]) => {
match $ret {
$(CType::$r => {{
// SAFETY: caller guarantees the function ABI matches.
let f: extern "C" fn($pty) -> $rty = unsafe { std::mem::transmute($ptr) };
let mut __iter = $args.iter();
let __a0 = <$pty as CTypeArg>::from_value(__iter.next().copied().unwrap_or(Value::nil()))?;
Ok(<$rty as CTypeArg>::to_value(f(__a0)))
}},)*
}
};
}
/// Generate match arms for two parameters of fixed Rust types.
macro_rules! arity_2_arms {
($ptr:expr, $args:expr, $ret:expr, $pty0:ty, $pty1:ty, [$(($r:ident, $rty:ty));*]) => {
match $ret {
$(CType::$r => {{
// SAFETY: caller guarantees the function ABI matches.
let f: extern "C" fn($pty0, $pty1) -> $rty = unsafe { std::mem::transmute($ptr) };
let mut __iter = $args.iter();
let __a0 = <$pty0 as CTypeArg>::from_value(__iter.next().copied().unwrap_or(Value::nil()))?;
let __a1 = <$pty1 as CTypeArg>::from_value(__iter.next().copied().unwrap_or(Value::nil()))?;
Ok(<$rty as CTypeArg>::to_value(f(__a0, __a1)))
}},)*
}
};
}
/// Generate match arms for three parameters of fixed Rust types.
macro_rules! arity_3_arms {
($ptr:expr, $args:expr, $ret:expr, $pty0:ty, $pty1:ty, $pty2:ty, [$(($r:ident, $rty:ty));*]) => {
match $ret {
$(CType::$r => {{
// SAFETY: caller guarantees the function ABI matches.
let f: extern "C" fn($pty0, $pty1, $pty2) -> $rty = unsafe { std::mem::transmute($ptr) };
let mut __iter = $args.iter();
let __a0 = <$pty0 as CTypeArg>::from_value(__iter.next().copied().unwrap_or(Value::nil()))?;
let __a1 = <$pty1 as CTypeArg>::from_value(__iter.next().copied().unwrap_or(Value::nil()))?;
let __a2 = <$pty2 as CTypeArg>::from_value(__iter.next().copied().unwrap_or(Value::nil()))?;
Ok(<$rty as CTypeArg>::to_value(f(__a0, __a1, __a2)))
}},)*
}
};
}
/// Generate match arms for four parameters of fixed Rust types.
macro_rules! arity_4_arms {
($ptr:expr, $args:expr, $ret:expr, $pty0:ty, $pty1:ty, $pty2:ty, $pty3:ty, [$(($r:ident, $rty:ty));*]) => {
match $ret {
$(CType::$r => {{
// SAFETY: caller guarantees the function ABI matches.
let f: extern "C" fn($pty0, $pty1, $pty2, $pty3) -> $rty = unsafe { std::mem::transmute($ptr) };
let mut __iter = $args.iter();
let __a0 = <$pty0 as CTypeArg>::from_value(__iter.next().copied().unwrap_or(Value::nil()))?;
let __a1 = <$pty1 as CTypeArg>::from_value(__iter.next().copied().unwrap_or(Value::nil()))?;
let __a2 = <$pty2 as CTypeArg>::from_value(__iter.next().copied().unwrap_or(Value::nil()))?;
let __a3 = <$pty3 as CTypeArg>::from_value(__iter.next().copied().unwrap_or(Value::nil()))?;
Ok(<$rty as CTypeArg>::to_value(f(__a0, __a1, __a2, __a3)))
}},)*
}
};
}
/// Marshal arguments, call a native function, and marshal the return value.
///
/// Supports signatures with up to four parameters.
///
/// # Safety
/// `func.ptr` must point to a valid function whose ABI matches `func.signature`.
pub unsafe fn call_native(func: &NativeFunction, args: &[Value]) -> Result<Value, String> {
if args.len() != func.signature.params.len() {
return Err(format!(
"argument count mismatch: expected {}, got {}",
func.signature.params.len(),
args.len()
));
}
let p = &func.signature.params;
let ret = func.signature.ret;
match p.as_slice() {
[] => with_returns!(arity_0_arms!(func.ptr, ret)),
[p0] => match p0 {
CType::I64 => with_returns!(arity_1_arms!(func.ptr, args, ret, i64)),
CType::F64 => with_returns!(arity_1_arms!(func.ptr, args, ret, f64)),
CType::Bool => with_returns!(arity_1_arms!(func.ptr, args, ret, bool)),
CType::CStr => {
with_returns!(arity_1_arms!(func.ptr, args, ret, *const std::ffi::c_char))
}
CType::VoidPtr => {
with_returns!(arity_1_arms!(func.ptr, args, ret, *mut std::ffi::c_void))
}
CType::Unit => with_returns!(arity_1_arms!(func.ptr, args, ret, ())),
},
[p0, p1] => match (p0, p1) {
// I64
(CType::I64, CType::I64) => with_returns!(arity_2_arms!(func.ptr, args, ret, i64, i64)),
(CType::I64, CType::F64) => with_returns!(arity_2_arms!(func.ptr, args, ret, i64, f64)),
(CType::I64, CType::Bool) => {
with_returns!(arity_2_arms!(func.ptr, args, ret, i64, bool))
}
// F64
(CType::F64, CType::I64) => with_returns!(arity_2_arms!(func.ptr, args, ret, f64, i64)),
(CType::F64, CType::F64) => with_returns!(arity_2_arms!(func.ptr, args, ret, f64, f64)),
(CType::F64, CType::Bool) => {
with_returns!(arity_2_arms!(func.ptr, args, ret, f64, bool))
}
// Bool
(CType::Bool, CType::I64) => {
with_returns!(arity_2_arms!(func.ptr, args, ret, bool, i64))
}
(CType::Bool, CType::F64) => {
with_returns!(arity_2_arms!(func.ptr, args, ret, bool, f64))
}
(CType::Bool, CType::Bool) => {
with_returns!(arity_2_arms!(func.ptr, args, ret, bool, bool))
}
_ => Err("unsupported arity-2 parameter types".to_string()),
},
[p0, p1, p2] => match (p0, p1, p2) {
(CType::I64, CType::I64, CType::I64) => {
with_returns!(arity_3_arms!(func.ptr, args, ret, i64, i64, i64))
}
_ => {
Err("unsupported arity-3 parameter types (only I64,I64,I64 supported)".to_string())
}
},
[p0, p1, p2, p3] => match (p0, p1, p2, p3) {
(CType::I64, CType::I64, CType::I64, CType::I64) => {
with_returns!(arity_4_arms!(func.ptr, args, ret, i64, i64, i64, i64))
}
_ => Err("unsupported arity-4 parameter types (only I64x4 supported)".to_string()),
},
_ => Err(format!(
"unsupported parameter count: {} (max 4 supported)",
p.len()
)),
}
}
#[cfg(test)]
mod tests {
use super::super::native::NativeLibrary;
use super::*;
use std::ffi::CString;
extern "C" fn add_two(a: i64, b: i64) -> i64 {
a + b
}
extern "C" fn negate_f(x: f64) -> f64 {
-x
}
extern "C" fn echo_bool(b: bool) -> bool {
!b
}
extern "C" fn strlen_c(s: *const c_char) -> i64 {
if s.is_null() {
return 0;
}
// SAFETY: test strings are valid null-terminated C strings.
unsafe { CStr::from_ptr(s).to_bytes().len() as i64 }
}
extern "C" fn return_unit() {}
extern "C" fn sum_three(a: i64, b: i64, c: i64) -> i64 {
a + b + c
}
fn make_func(ptr: *const c_void, signature: Signature) -> NativeFunction {
NativeFunction {
ptr,
signature,
library: None,
symbol: "test".to_string(),
}
}
#[test]
fn test_call_native_i64_add() {
let func = make_func(
add_two as *const c_void,
Signature::new(vec![CType::I64, CType::I64], CType::I64),
);
let args = [Value::int(3), Value::int(5)];
// SAFETY: pointer matches signature.
let result = unsafe { call_native(&func, &args).unwrap() };
assert_eq!(result.as_int(), Some(8));
}
#[test]
fn test_marshal_i64_roundtrip() {
let v = Value::int(-42);
assert_eq!(value_to_i64(&v), Ok(-42));
assert_eq!(i64_to_value(-42).as_int(), Some(-42));
}
#[test]
fn test_marshal_f64_roundtrip() {
let v = Value::float(2.5);
assert_eq!(value_to_f64(&v), Ok(2.5));
assert_eq!(f64_to_value(2.5).as_float(), Some(2.5));
}
#[test]
fn test_marshal_bool_roundtrip() {
let v = Value::bool(true);
assert_eq!(value_to_bool(&v), Ok(true));
assert!(!bool_to_value(false).as_bool().unwrap());
}
#[test]
fn test_marshal_cstr_roundtrip() {
let original = CString::new("hello ffi").unwrap();
let ptr = original.as_ptr() as *mut u8;
let v = Value::ptr(ptr);
// SAFETY: pointer is a valid C string for the borrow.
let borrowed = unsafe { value_to_cstr(&v).unwrap() };
// SAFETY: borrowed pointer is valid.
let round = unsafe { cstr_to_value(borrowed) };
let round_ptr = round.as_ptr().unwrap() as *const c_char;
// SAFETY: round pointer is a valid C string.
assert_eq!(
unsafe { CStr::from_ptr(round_ptr).to_str().unwrap() },
"hello ffi"
);
}
#[test]
fn test_marshal_voidptr_roundtrip() {
let mut n: i64 = 123;
let p = &mut n as *mut i64 as *mut c_void;
let v = voidptr_to_value(p);
// SAFETY: pointer is valid.
let p2 = unsafe { value_to_voidptr(&v).unwrap() } as *mut i64;
// SAFETY: p2 points to valid i64.
assert_eq!(unsafe { *p2 }, 123);
}
#[test]
fn test_marshal_unit() {
let v = unit_to_value();
assert!(v.is_unit());
let u = <() as CTypeArg>::from_value(v).unwrap();
assert_eq!(u, ());
}
#[test]
fn test_call_native_float() {
let func = make_func(
negate_f as *const c_void,
Signature::new(vec![CType::F64], CType::F64),
);
// SAFETY: pointer matches signature.
let result = unsafe { call_native(&func, &[Value::float(2.5)]).unwrap() };
assert_eq!(result.as_float(), Some(-2.5));
}
#[test]
fn test_call_native_bool() {
let func = make_func(
echo_bool as *const c_void,
Signature::new(vec![CType::Bool], CType::Bool),
);
// SAFETY: pointer matches signature.
let result = unsafe { call_native(&func, &[Value::bool(true)]).unwrap() };
assert_eq!(result.as_bool(), Some(false));
}
#[test]
fn test_call_native_cstr() {
let func = make_func(
strlen_c as *const c_void,
Signature::new(vec![CType::CStr], CType::I64),
);
let s = CString::new("nulang").unwrap();
let v = Value::ptr(s.as_ptr() as *mut u8);
// SAFETY: pointer matches signature and is a valid C string.
let result = unsafe { call_native(&func, &[v]).unwrap() };
assert_eq!(result.as_int(), Some(6));
}
#[test]
fn test_call_native_unit_ret() {
let func = make_func(
return_unit as *const c_void,
Signature::new(vec![], CType::Unit),
);
// SAFETY: pointer matches signature.
let result = unsafe { call_native(&func, &[]).unwrap() };
assert!(result.is_unit());
}
#[test]
fn test_call_native_three_args() {
let func = make_func(
sum_three as *const c_void,
Signature::new(vec![CType::I64, CType::I64, CType::I64], CType::I64),
);
// SAFETY: pointer matches signature.
let result =
unsafe { call_native(&func, &[Value::int(1), Value::int(2), Value::int(3)]).unwrap() };
assert_eq!(result.as_int(), Some(6));
}
#[test]
fn test_call_native_argument_count_mismatch() {
let func = make_func(
add_two as *const c_void,
Signature::new(vec![CType::I64, CType::I64], CType::I64),
);
// SAFETY: call itself is safe; we only check the error it returns.
let result = unsafe { call_native(&func, &[Value::int(1)]) };
assert!(result.is_err());
}
#[test]
#[cfg(target_os = "linux")]
fn test_load_libm_sqrt() {
// SAFETY: libm.so.6 is a trusted system library.
let lib = unsafe { NativeLibrary::open("libm.so.6") };
if let Err(e) = &lib {
eprintln!("warning: could not open libm.so.6: {}", e);
return;
}
let lib = lib.unwrap();
// SAFETY: sqrt has the expected signature.
let sqrt: libloading::Symbol<extern "C" fn(f64) -> f64> =
unsafe { lib.resolve(b"sqrt\0").unwrap() };
assert!((sqrt(4.0) - 2.0).abs() < 1e-12);
}
}
/// Map a Nulang type to its FFI representation.
/// Moved from compiler.rs; shared with the MIR codegen.
pub(crate) fn nulang_type_to_ffi_type(ty: &crate::types::Type) -> Option<crate::bytecode::FfiType> {
use crate::bytecode::FfiType;
use crate::types::Type;
match ty {
Type::Primitive(p) => match p {
crate::types::PrimitiveType::Int => Some(FfiType::Int),
crate::types::PrimitiveType::Float => Some(FfiType::Float),
crate::types::PrimitiveType::Bool => Some(FfiType::Bool),
crate::types::PrimitiveType::String => Some(FfiType::String),
crate::types::PrimitiveType::Unit => Some(FfiType::Unit),
_ => None,
},
_ => None,
}
}