public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
* Have to allocate extra space when calling a function with two pointer arguments (Aarch64/M1) or ffi_prep_cif segfaults
@ 2024-06-16 18:07 Vanessa McHale
  0 siblings, 0 replies; only message in thread
From: Vanessa McHale @ 2024-06-16 18:07 UTC (permalink / raw)
  To: libffi-discuss

Hello!

I have code for a JIT which allocates a cif on the heap (for reuse).

#define DO(i,n,a) {int i;for(i=0;i<n;i++){a;}}

#define F(r,t) {switch(t){\\
   case I_t: r=&ffi_type_sint64;break;\\
   case F_t: r=&ffi_type_double;break;\\
   case FA: r=&ffi_type_pointer;break;\\
   case IA: r=&ffi_type_pointer;break\\
}}

ffi_cif* apple_ffi(FnTy* ty) {
   ffi_cif* cif=malloc(sizeof(cif));
   int argc=ty->argc;
   ffi_type** args=malloc(sizeof(ffi_type*)*argc+1);
   enum apple_t* argv=ty->args;
   DO(i,argc,F(args[i],argv[i]))
   ffi_type* ret;F(ret,ty->res);
   ffi_prep_cif(cif,FFI_DEFAULT_ABI,(unsigned int)argc,ret,args);
   return cif;
}

Now, the curious part is that I have to write

ffi_type** args=malloc(sizeof(ffi_type*)*argc+1);

over the more obvious

ffi_type** args=malloc(sizeof(ffi_type*)*argc);

for the array of arguments. Otherwise, ffi_prep_cif will segfault when called for a function with two array arguments (I figured this out via println-debugging, it is the call to ffi_prep_cif which segfaults, not something thereafter). 

Any idea what I am doing wrong? Is this a bug in aarch64 support? 

Cheers,
Vanessa McHale

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-06-16 18:07 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-16 18:07 Have to allocate extra space when calling a function with two pointer arguments (Aarch64/M1) or ffi_prep_cif segfaults Vanessa McHale

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).