Commit Graph

482 Commits

Author SHA1 Message Date
Andoni Morales Alastruey
62fb5eb6d4 girepository: add support for relocations
Relocation is a common scenario in macOS applications and frameworks.
This commit provides a mechanism to support relocations by inferring
the typelib dir from the path of the loaded image libgirepository-1.0.dylib.
2024-06-04 13:00:42 +02:00
Emmanuele Bassi
d242bad64e Dump pointer types in the introspection blob
Instead of skipping types that inherit from G_TYPE_POINTER, add a new
ancillary element into the raw dump that gets processed by g-ir-scanner.

See: https://gitlab.gnome.org/GNOME/gobject-introspection/-/merge_requests/470
2024-05-31 18:40:21 +01:00
Emmanuele Bassi
e7ef715883 build: Add docs.c to the GLib GIR
For historical reasons, some doc blocks are included in an ancillary
file. Add it to the list of sources parsed by g-ir-scanner, to increase
the coverage of various macros.

Fixes: #3372
2024-05-20 13:34:19 +01:00
Philip Withnall
04a6ee3e9b Merge branch 'wip/3v1n0/gi-repository-leak-fixes' into 'main'
girepository/build: Actually use our compiler to generate GLib typelibs and fix parser leaks

See merge request GNOME/glib!4064
2024-05-16 23:15:16 +00:00
Marco Trevisan (Treviño)
b27fbf503b girepository/girparser,girmodule: Use a GPtrArray to hold the dependencies
It's just better than a list of strings for various reasons, but mostly
here it allows also to avoid copying the lists but making the
ownership clearer through references
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
2e2e606208 girparser: Simplify the ownership of context modules and parsed ones
There's no need to copy the list of modules and free it again, we can
just handle this by moving the modules to the parser promptly.
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
23d3a5e859 girepository/girparser: Free the temporary context modules list when done
When we're about to return the found module we can just drop the list
of modules that we had previously copied in the temporary context
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
b59c3f73ee girepository/girparser: Free the parsed modules list elements
We were removing the modules themselves, but not the list items
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
6e38293fec girepository/girparser: Move dependencies ownership to each module
In this way the module can survive without that the parser is fully
alive.

At the moment a module has not a ref-count system, but this makes things
clearer when we return the module from a parser.
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
9d65076eba girepository/girparser: Cleanup the node stack if anything destroyed it
If we've leftover nodes in the stack we should cleanup it
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
997d32e9cd girepository/girparser: Free the struct or union node if was not tracked
In case the node was pushed to a non-empty node stack, then we were not
tracking it in the entries list, and so nothing was freeing it on
destruction.

As per this, once parsing is done, we can free it or we'd leak.
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
98dce8a1c6 girepository/girparser: Do not allocate the same node name twice
We were setting the node name two times, and the latter was overwriting
the first one, making impossible to free the value when destroying the
node
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
a359333dd0 girepository/girnode: Fully free the members and discriminators lists
We were removing the contents but not the lists themselves
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
4cdf53a15c girepository/girmodule: Cleanup the temporary dependencies string
It's used during build phase, as a temporary variable but never freed
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
6a4f8e5bb7 girepository/girmodule: Use clearer scope for the build struct
Allocate it only when needed avoiding manual memset
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
05f606f3c5 girepository/girmodule: Cleanup the build stack once we've done
The stack is allocated with a list that we need to free once done
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
6f69fd53df girepository/girnode: Simplify the ownership fo the serialized type string 2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
9f4b97bb92 girepository/girmodule: Cleanup the builder types hash keys
We were adding new keys but never removing them, so use the proper
destroy notify for them and clarify the ownership on the caller.
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
1b8ef05321 girepository/girmodule: Free the version, shared library and c prefix
Those strings are explicitly duplicated but never free'd
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
47cf6387ad girepository/compiler: Free the parser when not needed
The compiler code was full of leaks and nothing really checked on them.

While it's not a big deal per se, per the nature of it, it's still
better to ensure that memory management is well done so that there are
no problems when using it with sanitizers.

So, the source of the problems was not freeing the parser, but that
wasn't enough, more to come...
2024-05-16 22:51:12 +01:00
Marco Trevisan (Treviño)
311367620a girepository/build: Actually use our compiler to generate GLib typelibs
We are overriding the default g-ir-compiler for local usage, but this
is not actually happen since that's computed while parsing introspection

So generate the compiler as first thing, then in case handle the
introspection data
2024-05-16 22:51:12 +01:00
Philip Withnall
03248e7b18
girepository: Make gi_repository_get_loaded_namespaces() deterministic
As with the previous two commits, the results of calling
`gi_repository_get_loaded_namespaces()` were previously
non-deterministic due to being generated by iterating over a hash table,
which has a non-deterministic iteration order.

Fix that by using the new `ordered_typelibs` and `ordered_lazy_typelibs`
arrays to provide deterministic ordering.

At the same time, significantly reduce the number of allocations needed
to build the return value — previously the results would be built as a
linked list before being turned into an array. The linked list is now
banished to history.

Add some more unit tests to maximise test coverage of this method.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Fixes: #3303
2024-05-16 22:47:49 +01:00
Philip Withnall
0d199d6199
girepository: Make gi_repository_find_by_error_domain() deterministic
As with the previous commit, finding a `GIBaseInfo` matching the given
error domain was non-deterministic because it iterated through a hash
table of typelibs. Hash table iteration is non-deterministic.

Change the method to instead use the `ordered_typelibs` and
`ordered_lazy_typelibs` arrays to give deterministic iteration order.

Add a unit test, although it can’t test the interesting case of an error
domain which is present in both `GioUnix`/`GioWin32` and `Gio`, because
no such error domain exists.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3303
2024-05-16 22:47:42 +01:00
Philip Withnall
81b16a88d1
girepository: Make gi_repository_find_by_gtype() deterministic
When faced with a `GType` which is present in multiple typelibs, the old
implementation was not deterministic, as it iterated over a hash table
of typelibs. The iteration order of a hash table is not deterministic.

Use the new `ordered_typelibs` and `ordered_lazy_typelibs` arrays to
iterate instead, making the order deterministic.

Add a unit test to check this. In particular, to check that symbols
which are present in both `Gio` and `GioUnix` are correctly resolved as
being from `GioUnix`.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3303
2024-05-16 22:47:35 +01:00
Philip Withnall
14d196a6c1
gitypelib: Fix iterating through typelib prefixes
The iteration code used `g_string_overwrite_len()` to try and simplify
buffer allocation and growth, but seemingly forgot to handle the fact
that it doesn’t nul-terminate what it overwrites: the method is intended
to be used to splice bits into longer strings, not to overwrite an
entire nul-terminated string.

This meant that when iterating over a comma-separated `c_prefix` like
`GUnix,G`, on the second iteration `g_string_overwrite_len()` would be
used to write `G` into index 0 of the already-set `GUnix` string in the
buffer, leading to the first iteration happening all over again and the
`G` prefix being ignored.

This led to symbols failing to be matched to the `GioUnix` typelib, even
though they should have been.

This will be checked by a test in the following commit.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3303
2024-05-16 22:47:29 +01:00
Philip Withnall
e0533125d3
girepository: Fix a typo in a code comment
Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2024-05-16 22:47:23 +01:00
Philip Withnall
1ed2cf63ed
girepository: Keep an ordered list of the loaded typelibs
There are various places where the set of typelibs is iterated over or
returned in an ordered way. In order to keep results deterministic and
reproducible, we need to keep this set ordered.

Keep a `GPtrArray` of the typelibs (one for fully-loaded ones and one
for lazy ones) alongside the existing hash tables. This will be used for
iteration in the next few commits.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3303
2024-05-16 22:47:16 +01:00
Adam Sampson
ae33e87117 girepository: Don't assume a bitfield has a fixed size
The type used when declaring a bitfield member of a struct doesn't
affect the amount of space allocated for it - only whether it's signed
or unsigned. In standard C99 (6.2.7.1), only _Bool, signed int and
unsigned int or typedefs to them are allowed as bitfield types, but GCC
allows other integer types as an extension.

In this case, the GIBaseInfo and GIBaseInfoStack structs are meant to
have identical layout. However, type_is_embedded was declared as an
unsigned bitfield in the former and a uint32_t in the latter. This was
harmless on most platforms because the following member is an aligned
pointer, but (for example) on m68k-linux-gnu pointers only need to be
16-bit aligned, so GCC only allocates 16 bits for the bitfield.

Change the type in the declaration to unsigned int, and add an padding
bitfield following it to ensure there's space for 32 bits on all
platforms in the future.

Signed-off-by: Adam Sampson <ats@offog.org>
2024-05-15 12:09:21 +01:00
Marco Trevisan (Treviño)
caf2f2fbda girepository/introspection: Set asan option only on address sanitizer
It's the only sanitizer failing when generating the introspection data
2024-05-10 04:16:17 +02:00
Marco Trevisan (Treviño)
0eb6c85606 build: Define glib_sanitizers variable to easily get the sanitizers in use
It's an array containing the list of sanitizers in use, normally it
contains a value, but in some cases may have more than one (e.g.
'address' and 'undefined').

And so use it to avoid repeated checks
2024-05-10 02:15:52 +02:00
Marco Trevisan (Treviño)
c78d0ad514 girepository/introspection: Properly check for sanitizer value
The sanitizer option is set to 'none' when not used
2024-05-09 17:51:10 +02:00
Marco Trevisan (Treviño)
3de5264f24 girrepository/tests: Add full gir dependencies on tests
Tests may have runtime dependencies that are related to the typelib
dependencies, so we need to satify them or the tests will fail at
runtime if we're not building their prerequisite for other reasons.

That's saying that the tests are currently failing when explicitly
running as standalone in meson.

Co-Authored-By: Philip Withnall <philip@tecnocode.co.uk>
2024-05-09 15:48:58 +02:00
Philip Withnall
f1d5a71bbc
girparser: Silence a scan-build NULL pointer dereference warning
It’s very obviously a false positive, as `str` has been added to on the
previous line, so can’t be `(void *) 0`. Not sure what scan-build is
thinking.

I’d rather not have this assertion (it doesn’t help the programmer’s
understanding of the code), but I would also rather have scan-build
running with no warnings so that it can helpfully catch newly-introduced
errors in future.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #1767
2024-04-25 23:57:59 +01:00
Philip Withnall
ec36370dcb
girepository: Fix various implicit conversions from size_t to smaller types
Basically various trivial instances of the following MSVC compiler
warning:
```
../gio/gio-tool-set.c(50): warning C4267: '=': conversion from 'size_t' to 'int', possible loss of data
```

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2024-04-25 00:41:34 +01:00
Alexander Kanavin
22ec5a96e3 girepository/introspection: correctly install .gir files into custom locations
There is a meson option (gir_dir_prefix), but without being passed in here
the files would always get installed into the default location (datadir).

Signed-off-by: Alexander Kanavin <alex@linutronix.de>
2024-04-23 14:21:21 +02:00
Philip Withnall
3c6c60611f
girnode: Simplify NULL node handling
All of the indications in the surrounding code are that `node` should
never be `NULL`, but the error handling for it did actually allow it to
be `NULL` iff its `parent` was also `NULL`.

That made scan-build (kind of legitimately) warn about `NULL` pointer
dereferences of `node`.

Avoid that by unambiguously using an assertion to prevent `NULL` nodes.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #1767
2024-04-12 18:45:59 +01:00
Philip Withnall
6162bccf1f
girffi: Fix ffi_cif leaks on error return paths
Spotted by scan-build.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #1767
2024-04-12 18:45:18 +01:00
Philip Withnall
e45c93da79
girffi: Add hints to indicate ownership transfer into ffi_cif
scan-build thinks that the `atypes` array is leaked, but it’s not.
Ownership is transferred into the `ffi_cif` structure, and it’s
eventually freed in `gi_callable_info_destroy_closure()`.

Try and help the static analysis by adding an explicit ownership
transfer annotation. It probably won’t help.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #1767
2024-04-12 18:45:09 +01:00
Emmanuele Bassi
52cc4d7b52 Remove unused cmph files
The cmph utility tool is not built, and the licensing of wingetopt.[ch]
is dubious at best.
2024-04-12 15:26:22 +01:00
Ville Skyttä
b20647c2e2 docs: spelling and grammar fixes
Signed-off-by: Ville Skyttä <ville.skytta@iki.fi>
2024-04-01 11:01:06 +00:00
Simon McVittie
d9ee53d0eb girparser: Add a pseudo-doc-comment for signedness()
We could consider adding this to gmacros if it's useful anywhere else.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-03-18 10:48:31 +00:00
Simon McVittie
c7e0f3e6b8 girparser: Adjust signedness() to avoid compiler warnings
No functional change intended.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-03-18 10:45:28 +00:00
Philip Withnall
8c9b9bca35 Merge branch 'wip/smcv/inline-sizeof' into 'main'
girparser: Make sizes in integer_aliases more obviously correct

See merge request GNOME/glib!3970
2024-03-18 10:34:01 +00:00
Simon McVittie
31b9c2f589 girparser: Make sizes in integer_aliases more obviously correct
We don't actually need to use the Meson-detected size macros here,
because the result of `sizeof()` is an integer constant expression.

No functional change.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-03-17 12:34:09 +00:00
Simon McVittie
fa45ea2ac9 girparser: Allow time_t, off_t, etc. to appear in GIR XML
g-ir-scanner currently maps these to lower-level types at scan time by
assuming that time_t is an alias for long, off_t is an alias for size_t
and so on. This is not always accurate: some ILP32 architectures have
64-bit time_t (for Y2038 compatibility) and 64-bit off_t (for large file
support), and that mismatch is tracked as GNOME/gobject-introspection#494.

One option for resolving this g-ir-scanner bug is to have it pass these
types through to the GIR XML, and teach g-ir-compiler and its replacement
gi-compile-repository to convert them to the corresponding concrete
type tag, as they already do for abstract types such as `long long` and
`size_t`.

Loosely based on GNOME/gobject-introspection!451 by Shuyu Liu.

Co-authored-by: Shuyu Liu <liushuyu011@gmail.com>
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-03-16 10:25:30 +00:00
Simon McVittie
d2a6c379e8 girparser: Don't assume sizeof(size_t) == sizeof(void *)
We don't actually need to use the results of configure-time checks here:
sizeof is a perfectly reasonable integer constant expression, so we can
use that directly.

Helps: https://gitlab.gnome.org/GNOME/glib/-/issues/2842
Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-03-16 10:24:58 +00:00
Simon McVittie
1a8e2228f6 girepository: Provide placeholders for positional parameters
Otherwise, correct invocation isn't clear from the --help output.

This does not introduce new translated strings: FILE was already
translated.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-28 11:27:15 +00:00
Simon McVittie
9541b43eef girepository: Provide placeholders for --includedir
If we don't do this, the --help text is formatted as though the option
did not expect an argument.

This does not introduce new translated strings: DIRECTORY was already
translated.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2024-02-28 11:26:59 +00:00
Philip Withnall
bd17a09862 girepository: Combine input file validation code paths in utilities
The code and strings are the same, so let’s simplify things and reduce
LoC for no functional change.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2024-02-26 18:10:56 +00:00
Philip Withnall
08b3f14a32 compiler: Correctly use g_strerror() instead of strerror()
This ensures the return value is always UTF-8.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2024-02-26 18:10:56 +00:00