2012-02-03 19:42:56 +01:00
|
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
|
|
|
|
* GObject introspection: Helper functions for ffi integration
|
2008-11-11 01:04:45 +01:00
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2008 Red Hat, Inc
|
|
|
|
|
* Copyright (C) 2005 Matthias Clasen
|
|
|
|
|
*
|
2023-10-25 18:10:10 +02:00
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
|
*
|
2008-11-11 01:04:45 +01:00
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
|
*/
|
2009-12-02 13:38:58 +01:00
|
|
|
|
|
2011-09-03 18:02:54 +02:00
|
|
|
|
#include "config.h"
|
|
|
|
|
|
2009-12-02 13:38:58 +01:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
2009-02-05 01:40:14 +01:00
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
2012-07-24 09:56:59 +02:00
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2009-02-05 01:40:14 +01:00
|
|
|
|
#include <unistd.h>
|
2012-07-24 09:56:59 +02:00
|
|
|
|
#endif
|
2008-11-11 01:04:45 +01:00
|
|
|
|
#include "girffi.h"
|
2009-02-05 01:40:14 +01:00
|
|
|
|
#include "girepository.h"
|
2010-06-07 04:22:57 +02:00
|
|
|
|
#include "girepository-private.h"
|
2008-11-11 01:04:45 +01:00
|
|
|
|
|
2011-12-21 21:55:18 +01:00
|
|
|
|
static ffi_type *
|
|
|
|
|
gi_type_tag_get_ffi_type_internal (GITypeTag tag,
|
|
|
|
|
gboolean is_pointer,
|
2024-01-16 17:30:37 +01:00
|
|
|
|
gboolean is_enum)
|
2008-11-11 01:04:45 +01:00
|
|
|
|
{
|
|
|
|
|
switch (tag)
|
|
|
|
|
{
|
|
|
|
|
case GI_TYPE_TAG_BOOLEAN:
|
|
|
|
|
return &ffi_type_uint;
|
|
|
|
|
case GI_TYPE_TAG_INT8:
|
|
|
|
|
return &ffi_type_sint8;
|
|
|
|
|
case GI_TYPE_TAG_UINT8:
|
|
|
|
|
return &ffi_type_uint8;
|
|
|
|
|
case GI_TYPE_TAG_INT16:
|
|
|
|
|
return &ffi_type_sint16;
|
|
|
|
|
case GI_TYPE_TAG_UINT16:
|
|
|
|
|
return &ffi_type_uint16;
|
|
|
|
|
case GI_TYPE_TAG_INT32:
|
|
|
|
|
return &ffi_type_sint32;
|
|
|
|
|
case GI_TYPE_TAG_UINT32:
|
2010-10-26 17:12:26 +02:00
|
|
|
|
case GI_TYPE_TAG_UNICHAR:
|
2008-11-11 01:04:45 +01:00
|
|
|
|
return &ffi_type_uint32;
|
|
|
|
|
case GI_TYPE_TAG_INT64:
|
|
|
|
|
return &ffi_type_sint64;
|
|
|
|
|
case GI_TYPE_TAG_UINT64:
|
|
|
|
|
return &ffi_type_uint64;
|
|
|
|
|
case GI_TYPE_TAG_GTYPE:
|
|
|
|
|
#if GLIB_SIZEOF_SIZE_T == 4
|
|
|
|
|
return &ffi_type_uint32;
|
|
|
|
|
#elif GLIB_SIZEOF_SIZE_T == 8
|
|
|
|
|
return &ffi_type_uint64;
|
|
|
|
|
#else
|
|
|
|
|
# error "Unexpected size for size_t: not 4 or 8"
|
|
|
|
|
#endif
|
|
|
|
|
case GI_TYPE_TAG_FLOAT:
|
|
|
|
|
return &ffi_type_float;
|
|
|
|
|
case GI_TYPE_TAG_DOUBLE:
|
|
|
|
|
return &ffi_type_double;
|
|
|
|
|
case GI_TYPE_TAG_UTF8:
|
|
|
|
|
case GI_TYPE_TAG_FILENAME:
|
|
|
|
|
case GI_TYPE_TAG_ARRAY:
|
|
|
|
|
case GI_TYPE_TAG_GLIST:
|
|
|
|
|
case GI_TYPE_TAG_GSLIST:
|
|
|
|
|
case GI_TYPE_TAG_GHASH:
|
|
|
|
|
case GI_TYPE_TAG_ERROR:
|
|
|
|
|
return &ffi_type_pointer;
|
2011-12-21 21:55:18 +01:00
|
|
|
|
case GI_TYPE_TAG_INTERFACE:
|
|
|
|
|
{
|
2024-01-16 17:30:37 +01:00
|
|
|
|
/* We need to handle enums specially:
|
|
|
|
|
* https://bugzilla.gnome.org/show_bug.cgi?id=665150
|
|
|
|
|
*/
|
2011-12-21 21:55:18 +01:00
|
|
|
|
if (!is_enum)
|
|
|
|
|
return &ffi_type_pointer;
|
2024-01-16 17:30:37 +01:00
|
|
|
|
else
|
|
|
|
|
return &ffi_type_sint32;
|
2011-12-21 21:55:18 +01:00
|
|
|
|
}
|
2009-12-07 17:54:18 +01:00
|
|
|
|
case GI_TYPE_TAG_VOID:
|
|
|
|
|
if (is_pointer)
|
|
|
|
|
return &ffi_type_pointer;
|
|
|
|
|
else
|
|
|
|
|
return &ffi_type_void;
|
2018-07-29 17:13:16 +02:00
|
|
|
|
default:
|
|
|
|
|
break;
|
2008-11-11 01:04:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
|
|
|
|
|
return NULL;
|
2010-03-24 19:00:06 +01:00
|
|
|
|
}
|
2009-02-05 01:40:14 +01:00
|
|
|
|
|
2011-12-21 21:55:18 +01:00
|
|
|
|
/**
|
|
|
|
|
* gi_type_tag_get_ffi_type:
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* @type_tag: a #GITypeTag
|
|
|
|
|
* @is_pointer: whether this is a pointer type
|
2011-12-21 21:55:18 +01:00
|
|
|
|
*
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* Get the `ffi_type` corresponding to @type_tag.
|
2013-10-10 22:21:18 +02:00
|
|
|
|
*
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* Returns: (transfer none): an `ffi_type` corresponding to the platform default
|
|
|
|
|
* C ABI for @tag and @is_pointer.
|
|
|
|
|
* Since: 2.80
|
2011-12-21 21:55:18 +01:00
|
|
|
|
*/
|
|
|
|
|
ffi_type *
|
2013-10-10 22:21:18 +02:00
|
|
|
|
gi_type_tag_get_ffi_type (GITypeTag type_tag,
|
2024-01-16 17:30:37 +01:00
|
|
|
|
gboolean is_pointer)
|
2011-12-21 21:55:18 +01:00
|
|
|
|
{
|
2013-10-10 22:21:18 +02:00
|
|
|
|
return gi_type_tag_get_ffi_type_internal (type_tag, is_pointer, FALSE);
|
2011-12-21 21:55:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2009-12-07 17:54:18 +01:00
|
|
|
|
/**
|
2023-11-08 15:17:52 +01:00
|
|
|
|
* gi_type_info_get_ffi_type:
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* @info: a #GITypeInfo
|
2009-12-07 17:54:18 +01:00
|
|
|
|
*
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* Get the `ffi_type` corresponding to @info.
|
2013-10-10 22:21:18 +02:00
|
|
|
|
*
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* Returns: (transfer none): a `ffi_type` corresponding to the platform default
|
|
|
|
|
* C ABI for @info.
|
|
|
|
|
* Since: 2.80
|
2009-12-07 17:54:18 +01:00
|
|
|
|
*/
|
|
|
|
|
ffi_type *
|
2023-11-08 15:17:52 +01:00
|
|
|
|
gi_type_info_get_ffi_type (GITypeInfo *info)
|
2009-10-20 21:33:33 +02:00
|
|
|
|
{
|
2012-01-06 20:55:45 +01:00
|
|
|
|
gboolean is_enum = FALSE;
|
2011-12-21 21:55:18 +01:00
|
|
|
|
GIBaseInfo *iinfo;
|
|
|
|
|
|
2023-11-08 15:17:52 +01:00
|
|
|
|
if (gi_type_info_get_tag (info) == GI_TYPE_TAG_INTERFACE)
|
2011-12-21 21:55:18 +01:00
|
|
|
|
{
|
2023-11-08 15:17:52 +01:00
|
|
|
|
iinfo = gi_type_info_get_interface (info);
|
2023-12-12 17:16:30 +01:00
|
|
|
|
switch (gi_base_info_get_info_type (iinfo))
|
2012-01-06 20:55:45 +01:00
|
|
|
|
{
|
|
|
|
|
case GI_INFO_TYPE_ENUM:
|
|
|
|
|
case GI_INFO_TYPE_FLAGS:
|
|
|
|
|
is_enum = TRUE;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-11-08 15:17:52 +01:00
|
|
|
|
gi_base_info_unref (iinfo);
|
2011-12-21 21:55:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 15:17:52 +01:00
|
|
|
|
return gi_type_tag_get_ffi_type_internal (gi_type_info_get_tag (info), gi_type_info_is_pointer (info), is_enum);
|
2009-10-20 21:33:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-02-05 01:40:14 +01:00
|
|
|
|
/**
|
2023-11-08 15:17:52 +01:00
|
|
|
|
* gi_callable_info_get_ffi_arg_types:
|
2009-02-05 01:40:14 +01:00
|
|
|
|
* @callable_info: a callable info from a typelib
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* @n_args_p: (out) (optional): the number of arguments returned
|
2009-02-05 01:40:14 +01:00
|
|
|
|
*
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* Get the `ffi_type`s for the arguments of @callable_info.
|
2013-10-10 22:21:18 +02:00
|
|
|
|
*
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* Returns: (transfer container) (array length=n_args_p): an array of
|
|
|
|
|
* `ffi_type*`. The array itself should be freed using [func@GLib.free] after
|
|
|
|
|
* use.
|
|
|
|
|
* Since: 2.80
|
2009-02-05 01:40:14 +01:00
|
|
|
|
*/
|
2009-12-07 17:54:18 +01:00
|
|
|
|
static ffi_type **
|
2023-11-08 15:17:52 +01:00
|
|
|
|
gi_callable_info_get_ffi_arg_types (GICallableInfo *callable_info,
|
2024-01-05 14:08:24 +01:00
|
|
|
|
size_t *n_args_p)
|
2009-02-05 01:40:14 +01:00
|
|
|
|
{
|
|
|
|
|
ffi_type **arg_types;
|
2012-04-09 22:51:08 +02:00
|
|
|
|
gboolean is_method, throws;
|
2024-01-16 01:57:09 +01:00
|
|
|
|
size_t n_args, n_invoke_args, i, offset;
|
2010-03-24 19:00:06 +01:00
|
|
|
|
|
2009-02-05 01:40:14 +01:00
|
|
|
|
g_return_val_if_fail (callable_info != NULL, NULL);
|
|
|
|
|
|
2023-11-08 15:17:52 +01:00
|
|
|
|
n_args = gi_callable_info_get_n_args (callable_info);
|
|
|
|
|
is_method = gi_callable_info_is_method (callable_info);
|
|
|
|
|
throws = gi_callable_info_can_throw_gerror (callable_info);
|
2012-04-09 22:51:08 +02:00
|
|
|
|
offset = is_method ? 1 : 0;
|
2010-03-24 19:00:06 +01:00
|
|
|
|
|
2012-04-09 22:51:08 +02:00
|
|
|
|
n_invoke_args = n_args;
|
|
|
|
|
|
|
|
|
|
if (is_method)
|
|
|
|
|
n_invoke_args++;
|
|
|
|
|
if (throws)
|
|
|
|
|
n_invoke_args++;
|
|
|
|
|
|
|
|
|
|
if (n_args_p)
|
|
|
|
|
*n_args_p = n_invoke_args;
|
|
|
|
|
|
|
|
|
|
arg_types = (ffi_type **) g_new0 (ffi_type *, n_invoke_args + 1);
|
|
|
|
|
|
|
|
|
|
if (is_method)
|
|
|
|
|
arg_types[0] = &ffi_type_pointer;
|
|
|
|
|
if (throws)
|
|
|
|
|
arg_types[n_invoke_args - 1] = &ffi_type_pointer;
|
2010-03-24 19:00:06 +01:00
|
|
|
|
|
2009-02-05 01:40:14 +01:00
|
|
|
|
for (i = 0; i < n_args; ++i)
|
|
|
|
|
{
|
2012-04-09 22:51:08 +02:00
|
|
|
|
GIArgInfo arg_info;
|
|
|
|
|
GITypeInfo arg_type;
|
|
|
|
|
|
2023-11-08 15:17:52 +01:00
|
|
|
|
gi_callable_info_load_arg (callable_info, i, &arg_info);
|
|
|
|
|
gi_arg_info_load_type (&arg_info, &arg_type);
|
|
|
|
|
switch (gi_arg_info_get_direction (&arg_info))
|
2011-02-06 05:48:58 +01:00
|
|
|
|
{
|
|
|
|
|
case GI_DIRECTION_IN:
|
2023-11-08 15:17:52 +01:00
|
|
|
|
arg_types[i + offset] = gi_type_info_get_ffi_type (&arg_type);
|
2011-02-06 05:48:58 +01:00
|
|
|
|
break;
|
|
|
|
|
case GI_DIRECTION_OUT:
|
|
|
|
|
case GI_DIRECTION_INOUT:
|
2012-04-09 22:51:08 +02:00
|
|
|
|
arg_types[i + offset] = &ffi_type_pointer;
|
2011-02-06 05:48:58 +01:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
}
|
2024-01-23 19:07:17 +01:00
|
|
|
|
|
|
|
|
|
gi_base_info_clear (&arg_type);
|
|
|
|
|
gi_base_info_clear (&arg_info);
|
2009-02-05 01:40:14 +01:00
|
|
|
|
}
|
2012-04-09 22:51:08 +02:00
|
|
|
|
|
|
|
|
|
arg_types[n_invoke_args] = NULL;
|
2009-02-05 01:40:14 +01:00
|
|
|
|
|
|
|
|
|
return arg_types;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-11-08 15:17:52 +01:00
|
|
|
|
* gi_callable_info_get_ffi_return_type:
|
2009-02-05 01:40:14 +01:00
|
|
|
|
* @callable_info: a callable info from a typelib
|
|
|
|
|
*
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* Fetches the `ffi_type` for a corresponding return value of
|
|
|
|
|
* a [class@GIRepository.CallableInfo].
|
2013-10-10 22:21:18 +02:00
|
|
|
|
*
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* Returns: (transfer none): the `ffi_type` for the return value
|
|
|
|
|
* Since: 2.80
|
2009-02-05 01:40:14 +01:00
|
|
|
|
*/
|
2009-12-07 17:54:18 +01:00
|
|
|
|
static ffi_type *
|
2023-11-08 15:17:52 +01:00
|
|
|
|
gi_callable_info_get_ffi_return_type (GICallableInfo *callable_info)
|
2009-02-05 01:40:14 +01:00
|
|
|
|
{
|
|
|
|
|
GITypeInfo *return_type;
|
2009-12-07 17:54:18 +01:00
|
|
|
|
ffi_type *return_ffi_type;
|
2009-02-05 01:40:14 +01:00
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (callable_info != NULL, NULL);
|
|
|
|
|
|
2023-11-08 15:17:52 +01:00
|
|
|
|
return_type = gi_callable_info_get_return_type (callable_info);
|
|
|
|
|
return_ffi_type = gi_type_info_get_ffi_type (return_type);
|
|
|
|
|
gi_base_info_unref((GIBaseInfo*)return_type);
|
2010-03-24 19:00:06 +01:00
|
|
|
|
|
2009-12-07 17:54:18 +01:00
|
|
|
|
return return_ffi_type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-11-08 15:17:52 +01:00
|
|
|
|
* gi_function_info_prep_invoker:
|
2009-12-07 17:54:18 +01:00
|
|
|
|
* @info: A #GIFunctionInfo
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* @invoker: (out caller-allocates): Output invoker structure
|
2009-12-07 17:54:18 +01:00
|
|
|
|
* @error: A #GError
|
|
|
|
|
*
|
|
|
|
|
* Initialize the caller-allocated @invoker structure with a cache
|
|
|
|
|
* of information needed to invoke the C function corresponding to
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* @info with the platform’s default ABI.
|
2009-12-07 17:54:18 +01:00
|
|
|
|
*
|
|
|
|
|
* A primary intent of this function is that a dynamic structure allocated
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* by a language binding could contain a [type@GIRepository.FunctionInvoker]
|
|
|
|
|
* structure inside the binding’s function mapping.
|
2010-05-18 23:10:51 +02:00
|
|
|
|
*
|
2024-01-23 19:12:48 +01:00
|
|
|
|
* @invoker must be freed using [method@GIRepository.FunctionInvoker.clear]
|
2024-01-23 19:09:28 +01:00
|
|
|
|
* when it’s finished with.
|
|
|
|
|
*
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* Returns: `TRUE` on success, `FALSE` otherwise with @error set.
|
|
|
|
|
* Since: 2.80
|
2009-12-07 17:54:18 +01:00
|
|
|
|
*/
|
|
|
|
|
gboolean
|
2023-11-08 15:17:52 +01:00
|
|
|
|
gi_function_info_prep_invoker (GIFunctionInfo *info,
|
|
|
|
|
GIFunctionInvoker *invoker,
|
|
|
|
|
GError **error)
|
2009-12-07 17:54:18 +01:00
|
|
|
|
{
|
|
|
|
|
const char *symbol;
|
2024-01-15 20:16:00 +01:00
|
|
|
|
void *addr;
|
2010-03-24 19:00:06 +01:00
|
|
|
|
|
2009-12-07 17:54:18 +01:00
|
|
|
|
g_return_val_if_fail (info != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (invoker != NULL, FALSE);
|
2010-03-24 19:00:06 +01:00
|
|
|
|
|
2023-11-08 15:17:52 +01:00
|
|
|
|
symbol = gi_function_info_get_symbol ((GIFunctionInfo*) info);
|
2010-03-24 19:00:06 +01:00
|
|
|
|
|
2023-11-08 15:17:52 +01:00
|
|
|
|
if (!gi_typelib_symbol (gi_base_info_get_typelib ((GIBaseInfo *) info),
|
|
|
|
|
symbol, &addr))
|
2009-12-07 17:54:18 +01:00
|
|
|
|
{
|
|
|
|
|
g_set_error (error,
|
2023-11-08 15:17:52 +01:00
|
|
|
|
GI_INVOKE_ERROR,
|
|
|
|
|
GI_INVOKE_ERROR_SYMBOL_NOT_FOUND,
|
2009-12-07 17:54:18 +01:00
|
|
|
|
"Could not locate %s: %s", symbol, g_module_error ());
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-28 18:14:30 +01:00
|
|
|
|
return gi_function_invoker_new_for_address (addr, (GICallableInfo *) info, invoker, error);
|
2011-11-28 20:51:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-11-08 15:17:52 +01:00
|
|
|
|
* gi_function_invoker_new_for_address:
|
2011-11-28 20:51:04 +01:00
|
|
|
|
* @addr: The address
|
|
|
|
|
* @info: A #GICallableInfo
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* @invoker: (out caller-allocates): Output invoker structure
|
2011-11-28 20:51:04 +01:00
|
|
|
|
* @error: A #GError
|
|
|
|
|
*
|
|
|
|
|
* Initialize the caller-allocated @invoker structure with a cache
|
|
|
|
|
* of information needed to invoke the C function corresponding to
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* @info with the platform’s default ABI.
|
2011-11-28 20:51:04 +01:00
|
|
|
|
*
|
|
|
|
|
* A primary intent of this function is that a dynamic structure allocated
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* by a language binding could contain a [type@GIRepository.FunctionInvoker]
|
|
|
|
|
* structure inside the binding’s function mapping.
|
2011-11-28 20:51:04 +01:00
|
|
|
|
*
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* Returns: `TRUE` on success, `FALSE` otherwise with @error set.
|
|
|
|
|
* Since: 2.80
|
2011-11-28 20:51:04 +01:00
|
|
|
|
*/
|
|
|
|
|
gboolean
|
2024-01-15 20:16:00 +01:00
|
|
|
|
gi_function_invoker_new_for_address (void *addr,
|
2023-11-08 15:17:52 +01:00
|
|
|
|
GICallableInfo *info,
|
|
|
|
|
GIFunctionInvoker *invoker,
|
|
|
|
|
GError **error)
|
2011-11-28 20:51:04 +01:00
|
|
|
|
{
|
|
|
|
|
ffi_type **atypes;
|
2024-01-05 14:08:24 +01:00
|
|
|
|
size_t n_args;
|
2011-11-28 20:51:04 +01:00
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (info != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (invoker != NULL, FALSE);
|
|
|
|
|
|
|
|
|
|
invoker->native_address = addr;
|
|
|
|
|
|
2023-11-08 15:17:52 +01:00
|
|
|
|
atypes = gi_callable_info_get_ffi_arg_types (info, &n_args);
|
2010-03-24 19:00:06 +01:00
|
|
|
|
|
2012-04-09 22:51:08 +02:00
|
|
|
|
return ffi_prep_cif (&(invoker->cif), FFI_DEFAULT_ABI, n_args,
|
2023-11-08 15:17:52 +01:00
|
|
|
|
gi_callable_info_get_ffi_return_type (info),
|
2012-04-09 22:51:08 +02:00
|
|
|
|
atypes) == FFI_OK;
|
2009-12-07 17:54:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-01-23 19:12:48 +01:00
|
|
|
|
* gi_function_invoker_clear:
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* @invoker: (transfer none): A #GIFunctionInvoker
|
|
|
|
|
*
|
|
|
|
|
* Release all resources allocated for the internals of @invoker.
|
2010-03-24 19:00:06 +01:00
|
|
|
|
*
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* Callers are responsible for freeing any resources allocated for the structure
|
2009-12-07 17:54:18 +01:00
|
|
|
|
* itself however.
|
2023-12-14 02:03:37 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.80
|
2009-12-07 17:54:18 +01:00
|
|
|
|
*/
|
|
|
|
|
void
|
2024-01-23 19:12:48 +01:00
|
|
|
|
gi_function_invoker_clear (GIFunctionInvoker *invoker)
|
2009-12-07 17:54:18 +01:00
|
|
|
|
{
|
|
|
|
|
g_free (invoker->cif.arg_types);
|
2009-02-05 01:40:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-04-07 23:48:52 +02:00
|
|
|
|
typedef struct {
|
|
|
|
|
ffi_closure ffi_closure;
|
2024-01-15 20:16:00 +01:00
|
|
|
|
void *writable_self;
|
|
|
|
|
void *native_address;
|
2010-04-07 23:48:52 +02:00
|
|
|
|
} GIClosureWrapper;
|
|
|
|
|
|
2009-02-05 01:40:14 +01:00
|
|
|
|
/**
|
2023-11-08 15:17:52 +01:00
|
|
|
|
* gi_callable_info_create_closure:
|
2009-02-05 01:40:14 +01:00
|
|
|
|
* @callable_info: a callable info from a typelib
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* @cif: a `ffi_cif` structure
|
2009-02-05 01:40:14 +01:00
|
|
|
|
* @callback: the ffi callback
|
|
|
|
|
* @user_data: data to be passed into the callback
|
|
|
|
|
*
|
|
|
|
|
* Prepares a callback for ffi invocation.
|
|
|
|
|
*
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* Returns: (transfer full) (nullable): the `ffi_closure`, or `NULL` on error.
|
|
|
|
|
* The return value should be freed by calling
|
|
|
|
|
* [method@GIRepository.CallableInfo.destroy_closure].
|
|
|
|
|
* Since: 2.80
|
2009-02-05 01:40:14 +01:00
|
|
|
|
*/
|
|
|
|
|
ffi_closure *
|
2023-11-08 15:17:52 +01:00
|
|
|
|
gi_callable_info_create_closure (GICallableInfo *callable_info,
|
|
|
|
|
ffi_cif *cif,
|
|
|
|
|
GIFFIClosureCallback callback,
|
2024-01-15 20:16:00 +01:00
|
|
|
|
void *user_data)
|
2009-02-05 01:40:14 +01:00
|
|
|
|
{
|
2024-01-15 20:16:00 +01:00
|
|
|
|
void *exec_ptr;
|
2024-01-05 14:08:24 +01:00
|
|
|
|
size_t n_args;
|
2012-04-09 22:51:08 +02:00
|
|
|
|
ffi_type **atypes;
|
2010-04-07 23:48:52 +02:00
|
|
|
|
GIClosureWrapper *closure;
|
2009-02-05 01:40:14 +01:00
|
|
|
|
ffi_status status;
|
2010-03-24 19:00:06 +01:00
|
|
|
|
|
2009-02-05 01:40:14 +01:00
|
|
|
|
g_return_val_if_fail (callable_info != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (cif != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (callback != NULL, FALSE);
|
2010-03-24 19:00:06 +01:00
|
|
|
|
|
2010-04-07 23:48:52 +02:00
|
|
|
|
closure = ffi_closure_alloc (sizeof (GIClosureWrapper), &exec_ptr);
|
2009-02-05 01:40:14 +01:00
|
|
|
|
if (!closure)
|
|
|
|
|
{
|
2023-12-20 23:41:10 +01:00
|
|
|
|
g_warning ("could not allocate closure");
|
2009-02-05 01:40:14 +01:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2010-04-07 23:48:52 +02:00
|
|
|
|
closure->writable_self = closure;
|
2021-07-17 19:19:17 +02:00
|
|
|
|
closure->native_address = exec_ptr;
|
2009-02-05 01:40:14 +01:00
|
|
|
|
|
2021-11-01 17:22:50 +01:00
|
|
|
|
|
2023-11-08 15:17:52 +01:00
|
|
|
|
atypes = gi_callable_info_get_ffi_arg_types (callable_info, &n_args);
|
2012-04-09 22:51:08 +02:00
|
|
|
|
status = ffi_prep_cif (cif, FFI_DEFAULT_ABI, n_args,
|
2023-11-08 15:17:52 +01:00
|
|
|
|
gi_callable_info_get_ffi_return_type (callable_info),
|
2012-04-09 22:51:08 +02:00
|
|
|
|
atypes);
|
2009-02-05 01:40:14 +01:00
|
|
|
|
if (status != FFI_OK)
|
|
|
|
|
{
|
2023-12-20 23:41:10 +01:00
|
|
|
|
g_warning ("ffi_prep_cif failed: %d", status);
|
2010-04-07 23:48:52 +02:00
|
|
|
|
ffi_closure_free (closure);
|
2009-02-05 01:40:14 +01:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-07 23:48:52 +02:00
|
|
|
|
status = ffi_prep_closure_loc (&closure->ffi_closure, cif, callback, user_data, exec_ptr);
|
2009-02-05 01:40:14 +01:00
|
|
|
|
if (status != FFI_OK)
|
|
|
|
|
{
|
2023-12-20 23:41:10 +01:00
|
|
|
|
g_warning ("ffi_prep_closure failed: %d", status);
|
2010-04-07 23:48:52 +02:00
|
|
|
|
ffi_closure_free (closure);
|
2009-02-05 01:40:14 +01:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
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:
https://github.com/libffi/libffi/commit/9ba559217bea0803263a9a9a0bafcf9203606f5b
Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
2021-06-27 21:57:13 +02:00
|
|
|
|
return &closure->ffi_closure;
|
2009-02-05 01:40:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-17 19:19:17 +02:00
|
|
|
|
/**
|
2023-11-08 15:17:52 +01:00
|
|
|
|
* gi_callable_info_get_closure_native_address:
|
2021-07-17 19:19:17 +02:00
|
|
|
|
* @callable_info: a callable info from a typelib
|
|
|
|
|
* @closure: ffi closure
|
|
|
|
|
*
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* Gets callable code from `ffi_closure` prepared by
|
|
|
|
|
* [method@GIRepository.CallableInfo.create_closure].
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer none): native address
|
|
|
|
|
* Since: 2.80
|
2021-07-17 19:19:17 +02:00
|
|
|
|
*/
|
2024-01-15 20:16:00 +01:00
|
|
|
|
void **
|
2023-11-08 15:17:52 +01:00
|
|
|
|
gi_callable_info_get_closure_native_address (GICallableInfo *callable_info,
|
|
|
|
|
ffi_closure *closure)
|
2021-07-17 19:19:17 +02:00
|
|
|
|
{
|
|
|
|
|
GIClosureWrapper *wrapper = (GIClosureWrapper *)closure;
|
|
|
|
|
return wrapper->native_address;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-05 01:40:14 +01:00
|
|
|
|
/**
|
2023-11-08 15:17:52 +01:00
|
|
|
|
* gi_callable_info_destroy_closure:
|
2009-02-05 01:40:14 +01:00
|
|
|
|
* @callable_info: a callable info from a typelib
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* @closure: (transfer full): ffi closure
|
|
|
|
|
*
|
|
|
|
|
* Frees a `ffi_closure` returned from
|
|
|
|
|
* [method@GIRepository.CallableInfo.create_closure].
|
2009-02-05 01:40:14 +01:00
|
|
|
|
*
|
2023-12-14 02:03:37 +01:00
|
|
|
|
* Since: 2.80
|
2009-02-05 01:40:14 +01:00
|
|
|
|
*/
|
|
|
|
|
void
|
2023-11-08 15:17:52 +01:00
|
|
|
|
gi_callable_info_destroy_closure (GICallableInfo *callable_info,
|
|
|
|
|
ffi_closure *closure)
|
2009-02-05 01:40:14 +01:00
|
|
|
|
{
|
2010-04-07 23:48:52 +02:00
|
|
|
|
GIClosureWrapper *wrapper = (GIClosureWrapper *)closure;
|
|
|
|
|
|
2011-06-20 21:17:55 +02:00
|
|
|
|
g_free (wrapper->ffi_closure.cif->arg_types);
|
2010-04-07 23:48:52 +02:00
|
|
|
|
ffi_closure_free (wrapper->writable_self);
|
2009-02-05 01:40:14 +01:00
|
|
|
|
}
|