This is the mail archive of the libffi-discuss@sourceware.org mailing list for the libffi project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Passing function pointer to fcall


I did figure this out already.
Anyway thanks alot.
Â
Just a few days away newLisp 10.3.8 will be released - and then bring support
for
libffi on Windows, Linux and Mac OS.Â


Anthony Green <green@moxielogic.com> hat am 7. Dezember 2011 um 13:01
geschrieben:

> Stefan,
>
>  The problem below is "avalues[3] = &printf". You need to add one
> more level of indirection.. so...
>
> void *p = printf;
>Â Âand
> avalues[3] = &p;
>
> AG
>
>
> On 11/27/2011 9:12 AM, Stefan Sonnenberg-Carstens wrote:
> > So, I extended the program some more:
> >
> > #include<stdio.h>
> > #include<ffi.h>
> > #include<stdlib.h>
> > #include<windows.h>
> >
> > int add(int a,int b, char *fmt,void *fn) {
> >Â Â Â // int (*func)(char *,int) = (int (*)(char
> > *,int))GetProcAddress(GetModuleHandle("msvcrt.dll"),fn);
> >Â Â Â int (*func)(char *,int) = (int (*)(char *,int))fn;
> >Â Â Â return func(fmt,a+b);
> > }
> >
> > int main(void)
> >Â Â Â {
> >
> >Â Â Â ffi_cif cif;
> >Â Â Â ffi_abi abi;
> >Â Â Â ffi_status status;
> >Â Â Â int nargs = 4;
> >Â Â Â ffi_type *rtype =&ffi_type_sint32;
> >Â Â Â ffi_type *atypes[4];
> >Â Â Â void *avalues[4];
> >Â Â Â int result;
> >
> >Â Â Â int a,b;
> >Â Â Â a = 3;
> >Â Â Â b = 4;
> >
> >Â Â Â char *fmt = "The answer is %i\n";
> >   char *fn = "printf";
> >
> >Â Â Â atypes[0] =&ffi_type_sint32;
> >Â Â Â atypes[1] =&ffi_type_sint32;
> >Â Â Â atypes[2] =&ffi_type_pointer;
> >Â Â Â atypes[3] =&ffi_type_pointer;
> >
> >Â Â Â avalues[0] = malloc(atypes[0]->size);
> >Â Â Â avalues[1] = malloc(atypes[1]->size);
> >
> >Â Â Â *(int *) avalues[0] = a;
> >Â Â Â *(int *) avalues[1] = b;
> >Â Â Â avalues[2] = (char *)&fmt;
> >Â Â Â avalues[3] =&printf;
> >
> >Â Â Â printf("direct call: %i",add(3,4,"erg: %i\n",&printf));
> >
> >Â Â Â status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, nargs, rtype, atypes);
> >
> >Â Â Â if(status != FFI_OK)
> >Â Â Â Â Â printf("ffi_prep_cif failed (%i)\n",status);
> >
> >Â Â Â // crash occurs somewhere near here ...
> >Â Â Â ffi_call(&cif,FFI_FN(add),&result,avalues);
> >
> >Â Â Â printf("%i\n",result);
> >
> >Â Â Â return 0 ;
> >
> >Â Â Â }
> >
> >
> > As you can see, I call my add function twice, once directly
> > and once via ffi_call.
> > Both times I pass a reference to printf.
> > As the direct method works, I guess the principal code is OK.
> >
> > Now, when I run the program it shows:
> >
> > $ dyn_callback.exe
> > erg: 7
> > direct call: 7
> >
> > Then it crashes and creates this callstack:
> >
> > dyn_callback.exe caused an Access Violation at location 614c25ff Reading
> > from location 614c25ff.
> >
> > Registers:
> > eax=614c25ff ebx=0040138c ecx=00000000 edx=00000003 esi=45206472
> > edi=6085db86
> > eip=614c25ff esp=0022fe34 ebp=0022fe60 iopl=0Â Â Â Â Ânv up ei pl nz na pe
> > nc
> > cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000     Â
> > Âefl=00000202
> >
> > Call stack:
> > AddrPC  ÂAddrReturn AddrFrame AddrStack Params
> > 614C25FFÂ Â004013AFÂ Â0022FE30Â Â0022FE34Â Â00403064Â Â00000007Â Â77C35C94
> >Â Â 77C12580
> > 614C25FF
> > 004013AFÂ Â6B746957Â Â0022FE60Â Â0022FE34Â Â00000003Â Â00000004Â Â00403064
> >Â Â 614C25FF
> > 004013AF dyn_callback.exe:004013AF add dyn_callback.c:10
> >
> >Â Â Â Â Â...
> >Â Â Â Â Â Â Âint (*func)(char *,int) = (int (*)(char *,int))fn;
> >Â Â Â Â Â Â Âreturn func(fmt,a+b);
> >>Â Â Â Â Â}
> >Â Â Â Â Âint main(void)
> >Â Â Â Â Â...
> >
> > 6B746957Â Â6B7465AFÂ Â0022FE78Â Â0022FE34Â Â6B746298Â Â0022FEB4Â Â00000010
> >Â Â 00000001
> > 6B746957Â libffi-6.dll:6B746957Â ffi_call_win32
> > 6B7465AFÂ Â004014FBÂ Â0022FEC8Â Â0022FE34Â Â0022FF20Â Â0040138CÂ Â0022FEFC
> >Â Â 0022FF00
> > 6B7465AFÂ libffi-6.dll:6B7465AFC:\MinGW\msys\1.0\local\bin\libffi-6.dll:
> > No symbol found
> >Â Â ffi_call
> > 004014FBÂ Â004010B9Â Â0022FF58Â Â0022FE34Â Â00000001Â Â005B2C98Â Â005B2ED0
> >Â Â 00405004
> > 004014FB dyn_callback.exe:004014FB main dyn_callback.c:53
> >
> >Â Â Â Â Â...
> >Â Â Â Â Â Â Âffi_call(&cif,FFI_FN(add),&result,avalues);
> >
> >>Â Â Â Â Âprintf("%i\n",result);
> >Â Â Â Â Âreturn 0 ;
> >Â Â Â Â Â...
> >
> > 004010B9Â Â00401284Â Â0022FFA0Â Â0022FE34Â Â00000001Â ÂA47CBD08Â Â7C90DCBA
> >Â Â 7C817064
> > 004010B9 dyn_callback.exe:004010B9 __mingw_CRTStartup crt1.c:244
> >
> > 00401284Â Â7C817067Â Â0022FFC0Â Â0022FE34Â Â6085DB86Â Â45206472Â Â7FFDA000
> >Â Â C0000005
> > 00401284 dyn_callback.exe:00401284 WinMainCRTStartup crt1.c:274
> >
> > 7C817067Â Â00000000Â Â0022FFF0Â Â0022FE34Â Â0040126CÂ Â00000000Â Â00000000
> >Â Â 00000000
> > 7C817067Â kernel32.dll:7C817067
> > C:\WINDOWS\system32\kernel32.dll: No symbols
> >Â Â RegisterWaitForInputIdle
> > DEBUG_EVENT:
> >Â Â Â Â ÂdwDebugEventCode = EXIT_PROCESS_DEBUG_EVENT
> >Â Â Â Â ÂdwProcessId = CCC
> >Â Â Â Â ÂdwThreadId = A30
> >Â Â Â Â ÂdwExitCode = C0000005
> >
> > I installed libffi 3.0.11 beforhand, but sadly it produces no debugging
> > symbols.
> >
>
>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]