You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
FFIForeign function interfacequestionNot a task, but rather a question or discussion topicwontfixFor issues that we've reviewed and decided do not require changes.
I ran into some surprising behavior in the Cryptol FFI yesterday. Let's say you have a foreign function that takes an array whose length is defined as an expression involving a type parameter:
The array gets passed to the FFI function as a length followed by a pointer.
#[no_mangle]#[export_name = "myfunc"]pubextern"C"fnmyfunc(n:usize,v:*constu32) -> u32{eprintln!("In FFI: n = {n}");
n asu32}
It looks like Cryptol is passing in the value of the type parameter as the array length, instead of using the value of the expression. Example output:
Cryptol> :l /home/cryptol/ffi_test.cry
Loading module Cryptol
Loading module Main
Loading dynamic library ffi_test.so
Main> let v = [1, 2] : [2][32]
Main> myfunc v
In FFI: n = 1
0x00000001
This is actually the intended behavior. Sequences are not passed as a pair of length and pointer, but rather the pointer only. Additionally, numeric type arguments are passed as size_t's at the beginning of the argument list. Therefore, myfunc receiving n as the first argument rather than n+1 is correct. The size of any array can always be determined since it is some arithmetic expression built from some type arguments which are always passed explicitly. You can read more here and here.
The text was updated successfully, but these errors were encountered:
qsctr
added
question
Not a task, but rather a question or discussion topic
wontfix
For issues that we've reviewed and decided do not require changes.
FFI
Foreign function interface
labels
Jun 30, 2023
FFIForeign function interfacequestionNot a task, but rather a question or discussion topicwontfixFor issues that we've reviewed and decided do not require changes.
A Cryptol user reports:
This is actually the intended behavior. Sequences are not passed as a pair of length and pointer, but rather the pointer only. Additionally, numeric type arguments are passed as
size_t
's at the beginning of the argument list. Therefore,myfunc
receivingn
as the first argument rather thann+1
is correct. The size of any array can always be determined since it is some arithmetic expression built from some type arguments which are always passed explicitly. You can read more here and here.cc @eddywestbrook @weaversa
The text was updated successfully, but these errors were encountered: