bind to abstract types for c struct with idris - ffi

I can not find how to treat this typedef struct TF_Status TF_Status; as abstract types and bind to that
the c function is TF_Status* TF_NewStatus();
data TF_Status
tfNewStatus : IO TF_Status
tfNewStatus = foreign FFI_C "TF_NewStatus" (IO TF_Status)
http://docs.idris-lang.org/en/latest/reference/ffi.html
it complains that When checking argument fty to function foreign: Can't find a value of type FTy FFI_C [] (IO TF_Status)

TF_Status* TF_NewStatus(); returns a pointer to a TF_Status when called. So you only need
tfNewStatus : IO Ptr
tfNewStatus = foreign FFI_C "TF_NewStatus" (IO Ptr)

Related

Compilation error "Incomplete definition of type 'struct proc'"

We are trying to monitor the processes using the kauth process listener (KAUTH_SCOPE_PROCESS). One of the arguments for the kauth process listener is a pointer to proc_t (struct proc)
We want to access some of the members of proc_t, for example, p_name(process name), p_textvp (vnode of process executable) etc. We wrote a code however while compiling, we were getting compilation error "Incomplete definition of type 'struct proc'“
Would be appreciable if someone guides me to fix this.
static int ProcessScopeListener(
kauth_cred_t credential,
void* idata,
kauth_action_t action,
uintptr_t arg0,
uintptr_t arg1,
uintptr_t arg2,
uintptr_t arg3
)
{
proc_t process = (proc_t) arg0;
…
//Compilation error in the following two lines
char* proc_name = &process->p_name[0];
struct vnode* p_textvp = process-> p_textvp;

.
.
.
return KERN_SUCESS;
}
Let me know if you need more information.
That struct is opaque, dereferencing pointers to it directly is not supported, as its layout may change from OS version to OS version.
Use the accessor functions such as proc_name() instead. Note that p_textvp is always NULL so there is no accessor.

ocaml c interop passing struct

I hit weird case when trying to call c from ocaml.
This is the c side of things:
typedef struct {
TSNode node;
} AstNode;
CAMLprim value caml_ts_document_root_node(value document) {
CAMLparam1(document);
TSNode root_node = ts_document_root_node(document);
AstNode elNode;
elNode.node = root_node;
CAMLreturn(&elNode);
}
CAMLprim value caml_ts_node_string(value node) {
CAMLparam1(node)
CAMLlocal1(mls);
AstNode* n = (AstNode*) node;
char *s = ts_node_string(n->node);
mls = caml_copy_string(s);
CAMLreturn(mls);
}
On the ocaml side
type ts_point
type ts_document
external ts_node_string : ts_node -> string = "caml_ts_node_string"
external ts_document_root_node : ts_document -> ts_node = "caml_ts_document_root_node"
If you see the code, I'm wrapping in caml_ts_document_root_node the TSNode root_node = ts_document_root_node(document); in an extra defined struct AstNode.
When I write the following implementation however:
CAMLprim value caml_ts_document_root_node(value document) {
CAMLparam1(document);
TSNode root_node = ts_document_root_node(document);
CAMLreturn(&root_node);
}
My code segfaults when calling caml_ts_node_string on the returned node by caml_ts_document_root_node.
Does anyone have any hints on why the segfault appears when I don't wrap a TSNode in an extra struct when interoping from ocaml?
That's definitely not the right usage of the foreign interface! You can't just take a value and cast it to OCaml value. OCaml values are specially encoded, even integers, and have a different representation than C values.
If you want to encode a C value as an OCaml value, you shall use custom values.
First of all, you need to implement the interface of a custom value, fortunately, you can rely on defaults for that:
static struct custom_operations ast_ops = {
"ast_node",
custom_finalize_default
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default,
custom_compare_ext_default
};
Next, you need to learn how to allocate custom blocks. For example, the following call will allocate the new AstNode in the OCaml heap:
res = caml_alloc_custom(&ast_ops, sizeof(AstNode), 0, 1);
To access the value itself, you need to use the Data_custom_val macro, e.g.,
if (res) {
AstNode *node = Data_custom_val(res);
TsNode *tsnode = res->node;
}
The complete example of a correct (I hope) implementation of your first function is below:
CAMLprim value caml_ts_document_root_node(value document) {
CAMLparam1(document);
CAMLlocal1(res);
res = caml_alloc_custom(&ast_ops, sizeof(AstNodes), 0, 1);
if (res) {
AstNode *ast = (AstNode *)Data_custom_val(res);
ast->node = ts_document_root_node(document);
}
CAMLreturn(res);
}
As you may see, this is not trivial and rather low-level. Though nothing really magical, especially after you've read the corresponding parts of the OCaml documentation. However, it is much easier to use the CTypes library, that hides most of those complexities and allows you to call C function directly from OCaml
This seems to be unrelated to the ocaml interop part; you are returning the address of a local variable in this function:
CAMLprim value caml_ts_document_root_node(value document) {
// ...
AstNode elNode;
// ...
CAMLreturn(&elNode);
}
When it returns, the (stack) memory it refers to is invalid (in the sense that it will be reused at the next function call).

Swift: Unable to cast function pointer to (void*) for use in C-style third party library

How do I cast a function pointer in Swift to a (void*) for use in a third party C-style library?
I am programming an acquisition card that requires setting up an interrupt callback using a C-style function provided by a third party library. That particular function, which must be called to set up the callback, takes a (void*) argument for the callback. In C, this works:
// somefile.c
int PHX_StreamRead( tHandle, etAcq, void*);
PHX_StreamRead(handle, PHX_START, (void*)&phxvptrs_callback);
static void phxvptrs_callback(tHandle h, ui32 mask, void *p) {
//... stuff
}
But in Swift, I am unable to call PHX_StreamRead() using the function pointer phxvptrs_callback, because I get a compilation error (wrong pointer type):
// file.swift
PHX_StreamRead(handle, PHX_START, &phxvptrs_callback) // Does not compile
// Error: "Cannot pass immutable value as inout argument: phxvptrs_callback is a function"
However, (surprisingly to me at least), all my attempts to cast that pointer to anything that looks like an UnsafeRawPointer failed:
// attempts.swift
typealias callbackFctType = ((tHandle, ui32, UnsafeMutableRawPointer) -> Void)
let p1 = UnsafeRawPointer(&phxvptrs_callback) // does not compile
// Error: "Cannot pass immutable value as inout argument: phxvptrs_callback is a function"
let p2 = UnsafePointer<callbackFctType>(&phxvptrs_callback) // Similar error
I figured out a workaround (below) but I would like to understand why these casts are refusing to compile. I assumed pretty much anything could be cast to UnsafeRawPointer.
My workaround has been to create a small C file with the callback definition and a wrapper function MyPHX_StreamRead_START_WithCallBack() that just calls PHX_StreamRead() with the proper accepted C syntax:
// file.c
static void phxvptrs_callback(tHandle h, ui32 mask, void *p) {
//... stuff
}
int MyPHX_StreamRead_START_WithCallBack(tHandle handle) {
return PHX_StreamRead(handle, PHX_START, &phxvptrs_callback);
}
Of course, calling that wrapper from Swift is not an issue and solves my problem but I find this solution "Unswift".
As far as I know, the only way to convert function pointer types in Swift is using unsafeBitCast.
Try something like this:
PHX_StreamRead(handle, PHX_START, unsafeBitCast(phxvptrs_callback, to: UnsafeMutableRawPointer.self))
EDIT
If you get "fatal error: can't unsafeBitCast between types of different sizes", Swift may not be treating phxvptrs_callback as a C-function (#convention(c) closure). In such cases, a little more code needed:
typealias callbackFctType = #convention(c) (tHandle, ui32, UnsafeMutableRawPointer?)->Void
let callbackWrapper: callbackFctType = phxvptrs_callback
PHX_StreamRead(handle, PHX_START, unsafeBitCast(callbackWrapper, to: UnsafeMutableRawPointer.self))
or this would compile and work as expected:
typealias callbackFctType = #convention(c) (tHandle, ui32, UnsafeMutableRawPointer?)->Void
PHX_StreamRead(handle, PHX_START, unsafeBitCast(phxvptrs_callback as callbackFctType, to: UnsafeMutableRawPointer.self))
EDIT2
To write a callback in Swift, you can write something like this:
typealias callbackFctType = #convention(c) (tHandle, ui32, UnsafeMutableRawPointer?)->Void
let myCallback: callbackFctType = {handle, mask, p in
//This closure cannot capture the context of its surrounding scope.
//(Which means this closure cannot use `self` even when in a class.)
//...
}
PHX_StreamRead(handle, PHX_START, unsafeBitCast(myCallback, to: UnsafeMutableRawPointer.self))

Calling a C library method from swift using pointers

Given the following ODBC C API information and associated type definitions, how does one call the SQLSetEnvAttr function from swift?
From swift, my code successfully invokes the prerequisite SQLAllocHandle function which provides the handle to the environment (henv) utilized in the subsequent SQLSetEnvAttr function call.
I have tried a variety of approaches including UnsafeMutablePointer, attempting to follow instructions referenced on the following sites, but I couldn't figure out how to get the compiler to allow me to convert from a Void * to a SQLPOINTER (even though it is defined to be the same thing). Additionally, I was stymied on how to make the UnsafeMutablePointer point to the value of the CUnsignedLong variable (SQL_OV_ODBC3 typedef) I used (set to 3)
http://www.sitepoint.com/using-legacy-c-apis-swift/
http://chris.eidhof.nl/posts/swift-c-interop.html
ODBC API
typedef signed short int SQLSMALLINT;
typedef SQLSMALLINT SQLRETURN;
typedef void * SQLPOINTER;
#define SQL_ATTR_ODBC_VERSION 200
#define SQL_OV_ODBC3 3UL
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER) SQL_OV_ODBC3, 0);
The working swift code that gets me the handle to the environment is:
var sqlHenvPtr : UnsafeMutablePointer<SQLHENV> = UnsafeMutablePointer<SQLHENV>.alloc(1)
var retcode : CShort = SQLAllocHandle(Int16(SQL_HANDLE_ENV), nil, sqlHenvPtr)
Looking for help with how to define and pass the third parameter:
let SQL_ATTR_ODBC_VERSION : Int32 = 200
retcode = SQLSetEnvAttr(sqlHenvPtr.memory, SQL_ATTR_ODBC_VERSION, ???, 0)
Any assistance would be much appreciated.
Updated per Chris's feedback
let value = UnsafeMutablePointer<Void>(bitPattern: 3)
SQLSetEnvAttr(0, 0, value, 0)
The expected value by SQLSetEnvAttr() is 0x3 (in your case it was 0x10025a478). I verified that my own prototype C function which accepts SQLPOINTER receives 0x3 in case of preparing 3rd parameter in proposed way. Hope it helps now.

Opaque C structs: various ways to declare them

I've seen both of the following two styles of declaring opaque types in C APIs. What are the various ways to declare opaque structs/pointers in C? Is there any clear advantage to using one style over the other?
Option 1
// foo.h
typedef struct foo * fooRef;
void doStuff(fooRef f);
// foo.c
struct foo {
int x;
int y;
};
Option 2
// foo.h
typedef struct _foo foo;
void doStuff(foo *f);
// foo.c
struct _foo {
int x;
int y;
};
My vote is for the third option that mouviciel posted then deleted:
I have seen a third way:
// foo.h
struct foo;
void doStuff(struct foo *f);
// foo.c
struct foo {
int x;
int y;
};
If you really can't stand typing the struct keyword, typedef struct foo foo; (note: get rid of the useless and problematic underscore) is acceptable. But whatever you do, never use typedef to define names for pointer types. It hides the extremely important piece of information that variables of this type reference an object which could be modified whenever you pass them to functions, and it makes dealing with differently-qualified (for instance, const-qualified) versions of the pointer a major pain.
Option 1.5 ("Object-based" C Architecture):
I am accustomed to using Option 1, except where you name your reference with _h to signify it is a "handle" to a C-style "object" of this given C "class". Then, you ensure your function prototypes use const wherever the content of this object "handle" is an input only, and cannot be changed, and don't use const wherever the content can be changed. So, do this style:
// -------------
// my_module.h
// -------------
// An opaque pointer (handle) to a C-style "object" of "class" type
// "my_module" (struct my_module_s *, or my_module_h):
typedef struct my_module_s *my_module_h;
void doStuff1(my_module_h my_module);
void doStuff2(const my_module_h my_module);
// -------------
// my_module.c
// -------------
// Definition of the opaque struct "object" of C-style "class" "my_module".
struct my_module_s
{
int int1;
int int2;
float f1;
// etc. etc--add more "private" member variables as you see fit
};
Here's a full example using opaque pointers in C to create objects. The following architecture might be called "object-based C":
//==============================================================================================
// my_module.h
//==============================================================================================
// An opaque pointer (handle) to a C-style "object" of "class" type "my_module" (struct
// my_module_s *, or my_module_h):
typedef struct my_module_s *my_module_h;
// Create a new "object" of "class" "my_module": A function that takes a *pointer to* an
// "object" handle, `malloc`s memory for a new copy of the opaque `struct my_module_s`, then
// points the user's input handle (via its passed-in pointer) to this newly-created "object" of
// "class" "my_module".
void my_module_open(my_module_h * my_module_h_p);
// A function that takes this "object" (via its handle) as an input only and cannot modify it
void my_module_do_stuff1(const my_module_h my_module);
// A function that can modify the private content of this "object" (via its handle) (but still
// cannot modify the handle itself)
void my_module_do_stuff2(my_module_h my_module);
// Destroy the passed-in "object" of "class" type "my_module": A function that can close this
// object by stopping all operations, as required, and `free`ing its memory.
void my_module_close(my_module_h my_module);
//==============================================================================================
// my_module.c
//==============================================================================================
// Definition of the opaque struct "object" of C-style "class" "my_module".
// - NB: Since this is an opaque struct (declared in the header but not defined until the source
// file), it has the following 2 important properties:
// 1) It permits data hiding, wherein you end up with the equivalent of a C++ "class" with only
// *private* member variables.
// 2) Objects of this "class" can only be dynamically allocated. No static allocation is
// possible since any module including the header file does not know the contents of *nor the
// size of* (this is the critical part) this "class" (ie: C struct).
struct my_module_s
{
int my_private_int1;
int my_private_int2;
float my_private_float;
// etc. etc--add more "private" member variables as you see fit
};
void my_module_open(my_module_h * my_module_h_p)
{
// Ensure the passed-in pointer is not NULL (since it is a core dump/segmentation fault to
// try to dereference a NULL pointer)
if (!my_module_h_p)
{
// Print some error or store some error code here, and return it at the end of the
// function instead of returning void.
goto done;
}
// Now allocate the actual memory for a new my_module C object from the heap, thereby
// dynamically creating this C-style "object".
my_module_h my_module; // Create a local object handle (pointer to a struct)
// Dynamically allocate memory for the full contents of the struct "object"
my_module = malloc(sizeof(*my_module));
if (!my_module)
{
// Malloc failed due to out-of-memory. Print some error or store some error code here,
// and return it at the end of the function instead of returning void.
goto done;
}
// Initialize all memory to zero (OR just use `calloc()` instead of `malloc()` above!)
memset(my_module, 0, sizeof(*my_module));
// Now pass out this object to the user, and exit.
*my_module_h_p = my_module;
done:
}
void my_module_do_stuff1(const my_module_h my_module)
{
// Ensure my_module is not a NULL pointer.
if (!my_module)
{
goto done;
}
// Do stuff where you use my_module private "member" variables.
// Ex: use `my_module->my_private_int1` here, or `my_module->my_private_float`, etc.
done:
}
void my_module_do_stuff2(my_module_h my_module)
{
// Ensure my_module is not a NULL pointer.
if (!my_module)
{
goto done;
}
// Do stuff where you use AND UPDATE my_module private "member" variables.
// Ex:
my_module->my_private_int1 = 7;
my_module->my_private_float = 3.14159;
// Etc.
done:
}
void my_module_close(my_module_h my_module)
{
// Ensure my_module is not a NULL pointer.
if (!my_module)
{
goto done;
}
free(my_module);
done:
}
Simplified example usage:
#include "my_module.h"
#include <stdbool.h>
#include <stdio.h>
int main()
{
printf("Hello World\n");
bool exit_now = false;
// setup/initialization
my_module_h my_module = NULL;
// For safety-critical and real-time embedded systems, it is **critical** that you ONLY call
// the `_open()` functions during **initialization**, but NOT during normal run-time,
// so that once the system is initialized and up-and-running, you can safely know that
// no more dynamic-memory allocation, which is non-deterministic and can lead to crashes,
// will occur.
my_module_open(&my_module);
// Ensure initialization was successful and `my_module` is no longer NULL.
if (!my_module)
{
// await connection of debugger, or automatic system power reset by watchdog
log_errors_and_enter_infinite_loop();
}
// run the program in this infinite main loop
while (exit_now == false)
{
my_module_do_stuff1(my_module);
my_module_do_stuff2(my_module);
}
// program clean-up; will only be reached in this case in the event of a major system
// problem, which triggers the infinite main loop above to `break` or exit via the
// `exit_now` variable
my_module_close(my_module);
// for microcontrollers or other low-level embedded systems, we can never return,
// so enter infinite loop instead
while (true) {}; // await reset by watchdog
return 0;
}
The only improvements beyond this would be to:
Implement full error handling and return the error instead of void. Ex:
/// #brief my_module error codes
typedef enum my_module_error_e
{
/// No error
MY_MODULE_ERROR_OK = 0,
/// Invalid Arguments (ex: NULL pointer passed in where a valid pointer is required)
MY_MODULE_ERROR_INVARG,
/// Out of memory
MY_MODULE_ERROR_NOMEM,
/// etc. etc.
MY_MODULE_ERROR_PROBLEM1,
} my_module_error_t;
Now, instead of returning a void type in all of the functions above and below, return a my_module_error_t error type instead!
Add a configuration struct called my_module_config_t to the .h file, and pass it in to the open function to update internal variables when you create a new object. This helps encapsulate all configuration variables in a single struct for cleanliness when calling _open().
Example:
//--------------------
// my_module.h
//--------------------
// my_module configuration struct
typedef struct my_module_config_s
{
int my_config_param_int;
float my_config_param_float;
} my_module_config_t;
my_module_error_t my_module_open(my_module_h * my_module_h_p,
const my_module_config_t *config);
//--------------------
// my_module.c
//--------------------
my_module_error_t my_module_open(my_module_h * my_module_h_p,
const my_module_config_t *config)
{
my_module_error_t err = MY_MODULE_ERROR_OK;
// Ensure the passed-in pointer is not NULL (since it is a core dump/segmentation fault
// to try to dereference a NULL pointer)
if (!my_module_h_p)
{
// Print some error or store some error code here, and return it at the end of the
// function instead of returning void. Ex:
err = MY_MODULE_ERROR_INVARG;
goto done;
}
// Now allocate the actual memory for a new my_module C object from the heap, thereby
// dynamically creating this C-style "object".
my_module_h my_module; // Create a local object handle (pointer to a struct)
// Dynamically allocate memory for the full contents of the struct "object"
my_module = malloc(sizeof(*my_module));
if (!my_module)
{
// Malloc failed due to out-of-memory. Print some error or store some error code
// here, and return it at the end of the function instead of returning void. Ex:
err = MY_MODULE_ERROR_NOMEM;
goto done;
}
// Initialize all memory to zero (OR just use `calloc()` instead of `malloc()` above!)
memset(my_module, 0, sizeof(*my_module));
// Now initialize the object with values per the config struct passed in. Set these
// private variables inside `my_module` to whatever they need to be. You get the idea...
my_module->my_private_int1 = config->my_config_param_int;
my_module->my_private_int2 = config->my_config_param_int*3/2;
my_module->my_private_float = config->my_config_param_float;
// etc etc
// Now pass out this object handle to the user, and exit.
*my_module_h_p = my_module;
done:
return err;
}
And usage:
my_module_error_t err = MY_MODULE_ERROR_OK;
my_module_h my_module = NULL;
my_module_config_t my_module_config =
{
.my_config_param_int = 7,
.my_config_param_float = 13.1278,
};
err = my_module_open(&my_module, &my_module_config);
if (err != MY_MODULE_ERROR_OK)
{
switch (err)
{
case MY_MODULE_ERROR_INVARG:
printf("MY_MODULE_ERROR_INVARG\n");
break;
case MY_MODULE_ERROR_NOMEM:
printf("MY_MODULE_ERROR_NOMEM\n");
break;
case MY_MODULE_ERROR_PROBLEM1:
printf("MY_MODULE_ERROR_PROBLEM1\n");
break;
case MY_MODULE_ERROR_OK:
// not reachable, but included so that when you compile with
// `-Wall -Wextra -Werror`, the compiler will fail to build if you forget to handle
// any of the error codes in this switch statement.
break;
}
// Do whatever else you need to in the event of an error, here. Ex:
// await connection of debugger, or automatic system power reset by watchdog
while (true) {};
}
// ...continue other module initialization, and enter main loop
See also:
[another answer of mine which references my answer above] Architectural considerations and approaches to opaque structs and data hiding in C
Additional reading on object-based C architecture:
Providing helper functions when rolling out own structures
Additional reading and justification for valid usage of goto in error handling for professional code:
An argument in favor of the use of goto in C for error handling: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/Research_General/goto_for_error_handling_in_C/readme.md
*****EXCELLENT ARTICLE showing the virtues of using goto in error handling in C: "Using goto for error handling in C" - https://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c
Valid use of goto for error management in C?
Error handling in C code
Search terms to make more googlable: opaque pointer in C, opaque struct in C, typedef enum in C, error handling in C, c architecture, object-based c architecture, dynamic memory allocation at initialization architecture in c
bar(const fooRef) declares an immutable address as argument. bar(const foo *) declares an address of an immutable foo as argument.
For this reason, I tend to prefer option 2. I.e., the presented interface type is one where cv-ness can be specified at each level of indirection. Of course one can sidestep the option 1 library writer and just use foo, opening yourself to all sorts of horror when the library writer changes the implementation. (I.e., the option 1 library writer only perceives that fooRef is part of the invariant interface and that foo can come, go, be altered, whatever. The option 2 library writer perceives that foo is part of the invariant interface.)
I'm more surprised that no one's suggested combined typedef/struct constructions.
typedef struct { ... } foo;
Option 3: Give people choice
/* foo.h */
typedef struct PersonInstance PersonInstance;
typedef struct PersonInstance * PersonHandle;
typedef const struct PersonInstance * ConstPersonHandle;
void saveStuff (PersonHandle person);
int readStuff (ConstPersonHandle person);
...
/* foo.c */
struct PersonInstance {
int a;
int b;
...
};
...

Resources