Commit Graph

42 Commits

Author SHA1 Message Date
Cimbali
d991e197de Remove runtime warnings, add doxygen deprecation notices 2021-11-23 23:04:26 +00:00
Emmanuele Bassi
b03c3d0a16 Fix styling issues from suggestions 2021-11-23 23:04:26 +00:00
Cimbali
f77269c480 Avoid leaking memory from FFI closure if no segfault risk 2021-11-23 23:04:26 +00:00
Cimbali
8e96029a32 Create new API for libffi closures
Deprecate the previous API as per discussion in !283.
2021-11-23 23:04:26 +00:00
Sergei Trofimovich
5e053279b5 girffi.h: add g_callable_info_get_closure_native_address() API
Commit 6bab939bf ("girffi.c: fix return value for g_callable_info_prepare_closure()")
effectively changes semantics of return value from code pointer to data pinter (closure).

`gjs` (and probably other software) relies on old (incorrect) semantics of
g_callable_info_prepare_closure(): https://gitlab.gnome.org/GNOME/gjs/-/issues/428

This change exposes the API that allows extracting directly
callacble code pointer. `gjs` will have to adapt to the new API.

Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
2021-11-23 23:04:26 +00:00
Sergei Trofimovich
6a8dce273a girffi.c: fix return value for g_callable_info_prepare_closure()
The initial failure was observed on `meld` against recently released
`libffi-3.4-rc1`. There `meld` crashes as:

```
$ meld
Segmentation fault (core dumped)

$ gdb --args /usr/bin/python3.9 /usr/bin/meld
(gdb) run
...
Thread 1 "python3.9" received signal SIGSEGV, Segmentation fault.
0x00007fffe9ac1ae8 in g_callable_info_free_closure (
  callable_info=0x555555d45990, closure=0x7fffe9e70c20)
    at ../gobject-introspection-1.68.0/girepository/girffi.c:428
428       g_free (wrapper->ffi_closure.cif->arg_types);
(gdb) bt
  callable_info=0x555555d45990, closure=0x7fffe9e70c20)
    at ../gobject-introspection-1.68.0/girepository/girffi.c:428
  data=0x555555d252d0)
    at ../pygobject-3.40.1/gi/pygi-closure.c:635
...
```

The bug here is in type mismatch between expected return value of
`g_callable_info_prepare_closure()` and actual value (executable
code pointer):

```c
ffi_closure * g_callable_info_prepare_closure(...) {
    gpointer exec_ptr;
    ...
    status = ffi_prep_closure_loc (&closure->ffi_closure, cif, callback, user_data, exec_ptr);

    return exec_ptr;
}
```

Note: `exec_ptr` is a code pointer that could be directly executed by
caller, like `((rt (*)(a1,a2))exec_ptr)(1,2);` It should never be wrapped
into an `ffi_closure*`, which is normally called via `ffi_call(closure, ...)`.

We see the problem when we try to free direct code pointer instead of
`ffi_closure()` as starting from libffi-3.4 executable trampoline and
`ffi_closure()` don't necessarily live in the same block:

    9ba559217b

Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
2021-11-23 23:04:26 +00:00
Christoph Reiter
a718ebac86 build: enable -Wswitch-default
In case the surrounding code handles missing cases break, otherwise add
a g_assert_not_reached().

The generated parser code triggers this as well, so disable it there only.
2018-07-29 18:57:03 +02:00
Dieter Verfaillie
bb7f8d57c2 docs: fix up reference docs a bit
- require GTK-Doc 1.19
      - remove sgml mode
      - automatically generate gi.types (needs GTK-Doc 1.19)
      - fix https://bugzilla.gnome.org/show_bug.cgi?id=700025
[WIP] - rearange sections a bit
[WIP] - add gi-building, gi-programming sections
[WIP] - mark missing docs with TODO, which is only marginaly
        better than nothing but at least can be grepped :)

https://bugzilla.gnome.org/show_bug.cgi?id=571648
2013-10-10 16:48:51 -04:00
Chun-wei Fan
c449db7704 girffi.c: Don't include unistd.h unconditionally
It does not exist on all platforms

https://bugzilla.gnome.org/show_bug.cgi?id=681820
2012-10-27 12:06:09 -04:00
Jasper St. Pierre
3bcc0e43c6 girffi: Fix g_callable_info_prepare_closure for certain callables
Namely, those that are methods and those that throw GErrors.
We have very similar code in two places that calculate arg lengths and
argument types to stick into libffi. Merge, clean up, and correct both.

https://bugzilla.gnome.org/show_bug.cgi?id=673805
2012-08-20 15:01:59 -04:00
Jasper St. Pierre
d65cec7f15 gicallableinfo: Add two new convenience methods: is_method and can_throw_gerror
https://bugzilla.gnome.org/show_bug.cgi?id=673805
2012-08-20 15:01:59 -04:00
Jasper St. Pierre
a714bef965 girffi: Virtual functions are methods 2012-02-10 10:19:17 -05:00
Jasper St. Pierre
04cf2f480d girffi: Fix g_function_info_new_for_address to respect G_VFUNC_THROWS 2012-02-03 13:48:39 -05:00
Colin Walters
e865dcb7b4 Add Emacs mode lines to C sources 2012-02-03 13:42:56 -05:00
Jasper St. Pierre
a0d19ca066 girffi: Add new g_function_invoker_new_for_address
This is a new method designed to make a GIFunctionInvoker for
any GICallableInfo*, for bindings to use.
2012-02-03 11:52:33 -05:00
Jasper St. Pierre
3e1486f924 girffi: Fix compiler warnings 2012-01-06 15:00:46 -05:00
Ray Strode
c99df8f34c ffi: Treat enums as 32 bit signed values to fix PPC64
To call a function dynamically using ffi, the caller
first has to tell ffi the size of all the input arguments
of the function. On little endian architectures (like x86_64)
specifying a size that's too large will happen to work because
of how the bits are laid out in memory.  On big endian architectures,
however, specifying the wrong size can lead to reading the wrong
bits.

The function g_type_info_get_ffi_type maps input giargument types to
specific sizes. It was assuming enums were word (pointer) sized; in
fact they can be in theory any size (1,2,4,8 bytes), but in practice
in introspection (via GIArgument) as well as GValue we're limited to 4
byte enums.

This commit fixes PPC64 (big endian, 64 bit).

Signed-off-by: Colin Walters <walters@verbum.org>

https://bugzilla.gnome.org/show_bug.cgi?id=665150
2011-12-21 15:55:18 -05:00
Colin Walters
6f0cf9a0aa girffi: Remove unnecessary sys/mman.h include
While we're here move config.h to the top for consistency.
2011-09-03 12:03:34 -04:00
Giovanni Campagna
98067194d6 Free allocated ffi_types in g_callable_info_free_closure()
g_callable_info_prepare_closure() allocates memory for the argument
types in the ffi_cif, so we need to free it.

https://bugzilla.gnome.org/show_bug.cgi?id=652954
2011-06-20 21:19:05 +02:00
Jonathan Matthew
5debbc28a9 Account for arg direction in g_callable_info_get_ffi_arg_types
https://bugzilla.gnome.org/show_bug.cgi?id=641647
2011-02-09 07:52:53 +10:00
Colin Walters
f9a3bb7300 Add support for gunichar in typelib
Some API such as gtk_text_iter_get_char returns an individual
"gunichar"; we should support this.

https://bugzilla.gnome.org/show_bug.cgi?id=633197
2010-11-12 16:00:10 -05:00
Colin Walters
e6bb30500c Export gi_type_tag_get_ffi_type
This is needed by the offsets code, and is generally useful.  We
need to export it for a future patch which won't export symbols
with a leading _.
2010-11-09 17:07:26 -05:00
Colin Walters
3a310fd242 Don't include machine-dependent integral types in the typelib
Previously we had both e.g. GI_TYPE_TAG_LONG and GI_TYPE_TAG_INT64,
but in fact the typelib is already machine-specific, so it makes sense
to just encode this as a fixed type.  The .gir remains abstract.

We also remove size_t from the typelib; one would never want to treat
it differently than an integer.

time_t is removed as well; while bindings like gjs had special handling
to turn it into e.g. a JS Date object, I don't think we should encourage
people to use these POSIX types in their API.  Use GTimeVal or the like
instead.

Because the typelib is now really machine-specific, we need to remove
the -expected.tgirs from git.  (We could potentially add a check
which wasn't just a literal diff later)

https://bugzilla.gnome.org/show_bug.cgi?id=623774
2010-07-09 14:15:52 -04:00
Johan Dahlin
6b4647c088 [girepository] Kill girffi-private.h 2010-06-06 23:22:57 -03:00
Johan Dahlin
51118b3338 [girepository] Document most of the structs 2010-05-18 18:11:58 -03:00
Owen W. Taylor
9d2fd90f0a Correctly use ffi_closure_alloc(), fixing mmap permissions error
This restores the reverted-commit ed8634d:

    Author: Dan Winship <danw@gnome.org>
    Date:   Mon Apr 5 14:05:52 2010 -0400

    g_callable_info_prepare_closure: handle mmap permissions error

    When SELinux or similar is active, a process may not be able to mmap()
    a segment that is both writable and executable, which would causing
    g_callable_info_prepare_closure() to fail. Libffi has a workaround for
    this problem though (it maps the same non-anonymous region twice, once
    writable and once executable, and returns two separate pointers to
    it), so use that.

But with an added fix, when using ffi_closure_alloc(), we need to use
ffi_prep_closure_loc() so we can pass in the executable address of the
trampoline separately from the writable address, rather than the
deprecated ffi_prep_closure().

https://bugzilla.gnome.org/show_bug.cgi?id=615105
2010-04-08 10:50:04 -04:00
Colin Walters
6545640515 Revert "g_callable_info_prepare_closure: handle mmap permissions error"
This reverts commit ed8634ddf73a56cb1935fd87254b3c6c04352893.

This commit caused crashes in gjs/gnome-shell, which we're still trying
to track down.  See: http://bugzilla.gnome.org/615078
2010-04-07 15:22:00 -04:00
Dan Winship
24363834ff g_callable_info_prepare_closure: handle mmap permissions error
When SELinux or similar is active, a process may not be able to mmap()
a segment that is both writable and executable, which would causing
g_callable_info_prepare_closure() to fail. Libffi has a workaround for
this problem though (it maps the same non-anonymous region twice, once
writable and once executable, and returns two separate pointers to
it), so use that.

https://bugzilla.gnome.org/show_bug.cgi?id=614903
2010-04-06 08:33:54 -04:00
Johan Dahlin
6b0463dd16 [girepository] Remove trailing whitespace 2010-03-24 15:02:05 -03:00
Johan Dahlin
109159ee05 The private header should be included in girffi.c
Not in the public girffi.h.
2009-12-31 17:37:21 +01:00
Colin Walters
f2f0625622 [girffi] Clean up API, add g_function_info_prep_invoker
Rather than having bindings use g_function_info_invoke, which is basically
a toy/demo API, export a convenience utility function which takes the introspection
information and sets up things we need to pass to libffi.

Then invocation can be done directly to libffi by a binding.

As part of this work, remove some (unused by gjs) public functions from the
girffi API, and instead export a function to map to libffi which can work
semi-correctly.

https://bugzilla.gnome.org/show_bug.cgi?id=604074
2009-12-16 18:17:12 -05:00
Johan Dahlin
954312db60 Revert "GI_TYPE_TAG_VOID != ffi_type_void"
This reverts commit 28cccba737ec2214da66b0d74059278162cf5fd0.
2009-12-15 11:00:52 -02:00
Iain Nicol
c22f11f1b8 Remove some unportable integral type size assumptions
https://bugzilla.gnome.org/show_bug.cgi?id=602762
2009-12-02 10:45:20 -02:00
Jasper Lievisse Adriaanse
a7c04b69de Fix build on OpenBSD
Due to a missing header, gobject-introspection fails to compile on OpenBSD.
And only due to headers-including-headers practice this doesn't blow up on
many other platforms.

https://bugzilla.gnome.org/show_bug.cgi?id=596226
2009-12-02 10:39:31 -02:00
Maxim Ermilov
4b796371ae GI_TYPE_TAG_VOID != ffi_type_void
https://bugzilla.gnome.org/show_bug.cgi?id=603157
2009-12-02 10:37:19 -02:00
Maxim Ermilov
a668ac5303 Plug a leak in g_callable_info_get_ffi_return_type
https://bugzilla.gnome.org/show_bug.cgi?id=603526
2009-12-02 10:19:45 -02:00
Tomeu Vizoso
43f1c2db17 Add g_ir_ffi_convert_arguments 2009-11-12 10:53:45 +01:00
Mark Lee
378350f3a9 Bug 584423 – Add short/ushort support
Add type tags for short and ushort, plus all of the requisite code needed
to utilize them in libgirepository.
Add support in the scanner's AST files.
Add test functions to the everything library and the expected gir file.

gtypelib.c constant validation fixed by Colin Walters <walters@verbum.org>
2009-06-22 14:01:56 -04:00
Johan Dahlin
fe8c6d34c4 Add gtk-doc support 2009-02-12 01:32:25 -02:00
Johan Dahlin
314ad462dc Add utility functions for invocing closures given a GICallableInfo using
2009-02-04  Johan Dahlin  <jdahlin@async.com.br>

    * girepository/Makefile.am:
    * girepository/girffi.c (g_callable_info_get_ffi_arg_types),
    (g_callable_info_get_ffi_return_type),
    (g_callable_info_prepare_closure), (g_callable_info_free_closure):
    * girepository/girffi.h:
    Add utility functions for invocing closures given a GICallableInfo
    using libffi.


svn path=/trunk/; revision=1084
2009-02-05 00:40:14 +00:00
Owen Taylor
3b0bf76d21 girepository/girffi.c: Fix "Unexpected time for time_t" message.
svn path=/trunk/; revision=919
2008-11-13 21:38:42 +00:00
Owen Taylor
d041deae59 Split g_ir_ffi_get_ffi_type() out from ginvoke.c
Extract a function to convert GITypeTag to ffi_type from the internals
of ginvoke.c. This will be useful in figure out structure alignment.

Also fix handling of gsize and time_t to be portable. (Add a check
to configure.ac to figure out the width of time_t.)

svn path=/trunk/; revision=873
2008-11-11 00:04:45 +00:00