1 /* glib-private.h - GLib-internal private API, shared between glib, gobject, gio
2 * Copyright (C) 2011 Red Hat, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #ifndef __GLIB_PRIVATE_H__
21 #define __GLIB_PRIVATE_H__
25 #include "gvariant-vectors.h"
26 #include "gstdioprivate.h"
27 #include "gdatasetprivate.h"
29 /* gcc defines __SANITIZE_ADDRESS__, clang sets the address_sanitizer
32 * MSVC defines __SANITIZE_ADDRESS__ as well when AddressSanitizer
33 * is enabled but __lsan_ignore_object() equivalent method is not supported
35 * https://docs.microsoft.com/en-us/cpp/sanitizers/asan-building?view=msvc-160
37 #if !defined(_MSC_VER) && (defined(__SANITIZE_ADDRESS__) || g_macro__has_feature(address_sanitizer))
40 * %_GLIB_ADDRESS_SANITIZER:
42 * Private macro defined if the AddressSanitizer is in use by GLib itself.
44 #define _GLIB_ADDRESS_SANITIZER
46 #include <sanitizer/lsan_interface.h>
48 /* If GLib itself is not compiled with ASAN sanitizer we may still want to
49 * control it in case it's linked by the loading application, so we need to
50 * do this check dynamically.
51 * However MinGW/Cygwin doesn't support weak attribute properly (even if it advertises
52 * it), so we ignore it in such case since it's not convenient to go through
54 * Under MSVC we could use alternatename, but it doesn't seem to be as reliable
55 * as we'd like: https://stackoverflow.com/a/11529277/210151 and
56 * https://devblogs.microsoft.com/oldnewthing/20200731-00/?p=104024
58 #elif defined (G_OS_UNIX) && !defined (__APPLE__) && !defined(__CYGWIN__) && g_macro__has_attribute (weak)
60 #define HAS_DYNAMIC_ASAN_LOADING
62 void __lsan_enable (void) __attribute__ ((weak));
63 void __lsan_disable (void) __attribute__ ((weak));
64 void __lsan_ignore_object (const void *p) __attribute__ ((weak));
70 * @ptr: a pointer to a member @field of type @type.
71 * @type: the type of the container in which @field is embedded.
72 * @field: the name of the field in @type.
74 * Casts away constness of @ptr.
76 * Returns: a pointer to the container, so that "&(@container)->field == (@ptr)" holds.
78 #define G_CONTAINER_OF(ptr, type, field) ((type *) G_STRUCT_MEMBER_P (ptr, -G_STRUCT_OFFSET (type, field)))
81 * g_leak_sanitizer_is_supported:
83 * Checks at runtime if LeakSanitizer is currently supported by the running
84 * binary. This may imply that GLib itself is not compiled with sanitizer
85 * but that the loading program is.
87 static inline gboolean
88 g_leak_sanitizer_is_supported (void)
90 #if defined (_GLIB_ADDRESS_SANITIZER)
92 #elif defined (HAS_DYNAMIC_ASAN_LOADING)
93 return __lsan_enable != NULL && __lsan_ignore_object != NULL;
103 * Tell AddressSanitizer and similar tools that if the object pointed to
104 * by @p is leaked, it is not a problem. Use this to suppress memory leak
105 * reports when a potentially unreachable pointer is deliberately not
106 * going to be deallocated.
109 g_ignore_leak (gconstpointer p)
111 #if defined (_GLIB_ADDRESS_SANITIZER)
113 __lsan_ignore_object (p);
114 #elif defined (HAS_DYNAMIC_ASAN_LOADING)
115 if (p != NULL && __lsan_ignore_object != NULL)
116 __lsan_ignore_object (p);
121 * g_ignore_strv_leak:
122 * @strv: (nullable) (array zero-terminated=1): an array of strings
124 * The same as g_ignore_leak(), but for the memory pointed to by @strv,
125 * and for each element of @strv.
128 g_ignore_strv_leak (GStrv strv)
132 if (!g_leak_sanitizer_is_supported ())
137 g_ignore_leak (strv);
139 for (item = strv; *item != NULL; item++)
140 g_ignore_leak (*item);
145 * g_begin_ignore_leaks:
147 * Tell AddressSanitizer and similar tools to ignore all leaks from this point
148 * onwards, until g_end_ignore_leaks() is called.
150 * Try to use g_ignore_leak() where possible to target deliberate leaks more
154 g_begin_ignore_leaks (void)
156 #if defined (_GLIB_ADDRESS_SANITIZER)
158 #elif defined (HAS_DYNAMIC_ASAN_LOADING)
159 if (__lsan_disable != NULL)
165 * g_end_ignore_leaks:
167 * Start ignoring leaks again; this must be paired with a previous call to
168 * g_begin_ignore_leaks().
171 g_end_ignore_leaks (void)
173 #if defined (_GLIB_ADDRESS_SANITIZER)
175 #elif defined (HAS_DYNAMIC_ASAN_LOADING)
176 if (__lsan_enable != NULL)
181 #undef HAS_DYNAMIC_ASAN_LOADING
183 GMainContext * g_get_worker_context (void);
184 gboolean g_check_setuid (void);
185 GMainContext * g_main_context_new_with_next_id (guint next_id);
186 void g_variant_to_vectors (GVariant *value,
187 GVariantVectors *vectors);
188 GVariant * g_variant_from_vectors (const GVariantType *type,
189 GVariantVector *vectors,
194 #if (defined (HAVE__SET_THREAD_LOCAL_INVALID_PARAMETER_HANDLER) || \
195 defined (HAVE__SET_INVALID_PARAMETER_HANDLER)) && \
196 defined (HAVE__CRT_SET_REPORT_MODE)
197 # define USE_INVALID_PARAMETER_HANDLER
200 #ifdef USE_INVALID_PARAMETER_HANDLER
201 struct _GWin32InvalidParameterHandler
203 _invalid_parameter_handler old_handler;
204 _invalid_parameter_handler pushed_handler;
205 int prev_report_mode;
206 int pushed_report_mode;
209 struct _GWin32InvalidParameterHandler
216 GLIB_AVAILABLE_IN_ALL
217 gchar *_glib_get_locale_dir (void);
220 GDir * g_dir_open_with_errno (const gchar *path, guint flags);
221 GDir * g_dir_new_from_dirp (gpointer dirp);
223 typedef struct _GWin32InvalidParameterHandler GWin32InvalidParameterHandler;
224 void g_win32_push_empty_invalid_parameter_handler (GWin32InvalidParameterHandler *items);
225 void g_win32_pop_invalid_parameter_handler (GWin32InvalidParameterHandler *items);
227 char *g_find_program_for_path (const char *program,
229 const char *working_dir);
231 int g_uri_get_default_scheme_port (const char *scheme);
233 #define GLIB_PRIVATE_CALL(symbol) (glib__private__()->symbol)
238 GWakeup * (* g_wakeup_new) (void);
239 void (* g_wakeup_free) (GWakeup *wakeup);
240 void (* g_wakeup_get_pollfd) (GWakeup *wakeup,
242 void (* g_wakeup_signal) (GWakeup *wakeup);
243 void (* g_wakeup_acknowledge) (GWakeup *wakeup);
246 GMainContext * (* g_get_worker_context) (void);
248 gboolean (* g_check_setuid) (void);
249 GMainContext * (* g_main_context_new_with_next_id) (guint next_id);
251 GDir * (* g_dir_open_with_errno) (const gchar *path,
253 GDir * (* g_dir_new_from_dirp) (gpointer dirp);
255 void (* g_variant_to_vectors) (GVariant *value,
256 GVariantVectors *vectors);
257 GVariant * (* g_variant_from_vectors) (const GVariantType *type,
258 GVariantVector *vectors,
262 void (* g_variant_vectors_deinit) (GVariantVectors *vectors);
264 /* See glib-init.c */
265 void (* glib_init) (void);
269 int (* g_win32_stat_utf8) (const gchar *filename,
270 GWin32PrivateStat *buf);
272 int (* g_win32_lstat_utf8) (const gchar *filename,
273 GWin32PrivateStat *buf);
275 int (* g_win32_readlink_utf8) (const gchar *filename,
281 int (* g_win32_fstat) (int fd,
282 GWin32PrivateStat *buf);
285 gchar *(*g_win32_find_helper_executable_path) (const gchar *process_name,
288 int (* g_win32_reopen_noninherited) (int fd,
292 gboolean (* g_win32_handle_is_socket) (void *handle);
296 /* See glib-private.c */
297 void (* g_win32_push_empty_invalid_parameter_handler) (GWin32InvalidParameterHandler *items);
299 void (* g_win32_pop_invalid_parameter_handler) (GWin32InvalidParameterHandler *items);
302 char *(* g_find_program_for_path) (const char *program,
304 const char *working_dir);
307 int (* g_uri_get_default_scheme_port) (const char *scheme);
310 gboolean (* g_set_prgname_once) (const gchar *prgname);
312 gpointer (*g_datalist_id_update_atomic) (GData **datalist,
314 GDataListUpdateAtomicFunc callback,
317 /* Add other private functions here, initialize them in glib-private.c */
320 GLIB_AVAILABLE_IN_ALL
321 const GLibPrivateVTable *glib__private__ (void);
323 /* Please see following for the use of ".ACP" over ""
324 * on Windows, although both are accepted at compile-time
325 * but "" renders translated console messages unreadable if
326 * built with Visual Studio 2012 and later (this is, unfortunately,
329 * https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale
330 * https://gitlab.gnome.org/GNOME/glib/merge_requests/895#note_525881
331 * https://gitlab.gnome.org/GNOME/glib/merge_requests/895#note_525900
333 * Additional related items:
334 * https://stackoverflow.com/questions/22604329/php-5-5-setlocale-not-working-in-cli-on-windows
335 * https://bugs.php.net/bug.php?id=66265
339 # define GLIB_DEFAULT_LOCALE ".ACP"
341 # define GLIB_DEFAULT_LOCALE ""
344 gboolean g_uint_equal (gconstpointer v1, gconstpointer v2);
345 guint g_uint_hash (gconstpointer v);
347 #if defined(__GNUC__)
348 #define G_THREAD_LOCAL __thread
350 #undef G_THREAD_LOCAL
353 /* Convenience wrapper to call private g_datalist_id_update_atomic() function. */
354 #define _g_datalist_id_update_atomic(datalist, key_id, callback, user_data) \
355 (GLIB_PRIVATE_CALL (g_datalist_id_update_atomic) ((datalist), (key_id), (callback), (user_data)))
357 #endif /* __GLIB_PRIVATE_H__ */