Branch data Line data Source code
1 : : /* glib-private.h - GLib-internal private API, shared between glib, gobject, gio
2 : : * Copyright (C) 2011 Red Hat, Inc.
3 : : *
4 : : * SPDX-License-Identifier: LGPL-2.1-or-later
5 : : *
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.
10 : : *
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.
15 : : *
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/>.
18 : : */
19 : :
20 : : #ifndef __GLIB_PRIVATE_H__
21 : : #define __GLIB_PRIVATE_H__
22 : :
23 : : #include <glib.h>
24 : : #include "gwakeup.h"
25 : : #include "gstdioprivate.h"
26 : : #include "gdatasetprivate.h"
27 : :
28 : : /*
29 : : * G_SIGNEDNESS_OF:
30 : : * @T: a numeric type such as `unsigned int`
31 : : *
32 : : * An integer constant expression indicating whether @T is a signed type.
33 : : *
34 : : * Returns: 1 if @T is signed, 0 if it is unsigned
35 : : */
36 : : #define G_SIGNEDNESS_OF(T) (((T) -1) <= 0)
37 : :
38 : : /* gcc defines __SANITIZE_ADDRESS__, clang sets the address_sanitizer
39 : : * feature flag.
40 : : *
41 : : * MSVC defines __SANITIZE_ADDRESS__ as well when AddressSanitizer
42 : : * is enabled but __lsan_ignore_object() equivalent method is not supported
43 : : * See also
44 : : * https://docs.microsoft.com/en-us/cpp/sanitizers/asan-building?view=msvc-160
45 : : */
46 : : #if !defined(_MSC_VER) && (defined(__SANITIZE_ADDRESS__) || g_macro__has_feature(address_sanitizer))
47 : :
48 : : /*
49 : : * %_GLIB_ADDRESS_SANITIZER:
50 : : *
51 : : * Private macro defined if the AddressSanitizer is in use by GLib itself.
52 : : */
53 : : #define _GLIB_ADDRESS_SANITIZER
54 : :
55 : : #include <sanitizer/lsan_interface.h>
56 : :
57 : : /* If GLib itself is not compiled with ASAN sanitizer we may still want to
58 : : * control it in case it's linked by the loading application, so we need to
59 : : * do this check dynamically.
60 : : * However MinGW/Cygwin doesn't support weak attribute properly (even if it advertises
61 : : * it), so we ignore it in such case since it's not convenient to go through
62 : : * dlsym().
63 : : * Under MSVC we could use alternatename, but it doesn't seem to be as reliable
64 : : * as we'd like: https://stackoverflow.com/a/11529277/210151 and
65 : : * https://devblogs.microsoft.com/oldnewthing/20200731-00/?p=104024
66 : : */
67 : : #elif defined (G_OS_UNIX) && !defined (__APPLE__) && !defined(__CYGWIN__) && !defined(_AIX) && \
68 : : g_macro__has_attribute (weak)
69 : :
70 : : #define HAS_DYNAMIC_ASAN_LOADING
71 : :
72 : : void __lsan_enable (void) __attribute__ ((weak));
73 : : void __lsan_disable (void) __attribute__ ((weak));
74 : : void __lsan_ignore_object (const void *p) __attribute__ ((weak));
75 : :
76 : : #endif
77 : :
78 : : /**
79 : : * G_CONTAINER_OF:
80 : : * @ptr: a pointer to a member @field of type @type.
81 : : * @type: the type of the container in which @field is embedded.
82 : : * @field: the name of the field in @type.
83 : : *
84 : : * Casts away constness of @ptr.
85 : : *
86 : : * Returns: a pointer to the container, so that "&(@container)->field == (@ptr)" holds.
87 : : */
88 : : #define G_CONTAINER_OF(ptr, type, field) ((type *) G_STRUCT_MEMBER_P (ptr, -G_STRUCT_OFFSET (type, field)))
89 : :
90 : : /*
91 : : * g_leak_sanitizer_is_supported:
92 : : *
93 : : * Checks at runtime if LeakSanitizer is currently supported by the running
94 : : * binary. This may imply that GLib itself is not compiled with sanitizer
95 : : * but that the loading program is.
96 : : */
97 : : static inline gboolean
98 : 1140 : g_leak_sanitizer_is_supported (void)
99 : : {
100 : : #if defined (_GLIB_ADDRESS_SANITIZER)
101 : : return TRUE;
102 : : #elif defined (HAS_DYNAMIC_ASAN_LOADING)
103 : 1140 : return __lsan_enable != NULL && __lsan_ignore_object != NULL;
104 : : #else
105 : : return FALSE;
106 : : #endif
107 : : }
108 : :
109 : : /*
110 : : * g_ignore_leak:
111 : : * @p: any pointer
112 : : *
113 : : * Tell AddressSanitizer and similar tools that if the object pointed to
114 : : * by @p is leaked, it is not a problem. Use this to suppress memory leak
115 : : * reports when a potentially unreachable pointer is deliberately not
116 : : * going to be deallocated.
117 : : */
118 : : static inline void
119 : 3908 : g_ignore_leak (gconstpointer p)
120 : : {
121 : : #if defined (_GLIB_ADDRESS_SANITIZER)
122 : : if (p != NULL)
123 : : __lsan_ignore_object (p);
124 : : #elif defined (HAS_DYNAMIC_ASAN_LOADING)
125 : 3908 : if (p != NULL && __lsan_ignore_object != NULL)
126 : 0 : __lsan_ignore_object (p);
127 : : #endif
128 : 3908 : }
129 : :
130 : : /*
131 : : * g_ignore_strv_leak:
132 : : * @strv: (nullable) (array zero-terminated=1): an array of strings
133 : : *
134 : : * The same as g_ignore_leak(), but for the memory pointed to by @strv,
135 : : * and for each element of @strv.
136 : : */
137 : : static inline void
138 : 1140 : g_ignore_strv_leak (GStrv strv)
139 : : {
140 : : gchar **item;
141 : :
142 : 1140 : if (!g_leak_sanitizer_is_supported ())
143 : 1140 : return;
144 : :
145 : 0 : if (strv)
146 : : {
147 : 0 : g_ignore_leak (strv);
148 : :
149 : 0 : for (item = strv; *item != NULL; item++)
150 : 0 : g_ignore_leak (*item);
151 : : }
152 : : }
153 : :
154 : : /*
155 : : * g_begin_ignore_leaks:
156 : : *
157 : : * Tell AddressSanitizer and similar tools to ignore all leaks from this point
158 : : * onwards, until g_end_ignore_leaks() is called.
159 : : *
160 : : * Try to use g_ignore_leak() where possible to target deliberate leaks more
161 : : * specifically.
162 : : */
163 : : static inline void
164 : 10761 : g_begin_ignore_leaks (void)
165 : : {
166 : : #if defined (_GLIB_ADDRESS_SANITIZER)
167 : : __lsan_disable ();
168 : : #elif defined (HAS_DYNAMIC_ASAN_LOADING)
169 : 10761 : if (__lsan_disable != NULL)
170 : 0 : __lsan_disable ();
171 : : #endif
172 : 10761 : }
173 : :
174 : : /*
175 : : * g_end_ignore_leaks:
176 : : *
177 : : * Start ignoring leaks again; this must be paired with a previous call to
178 : : * g_begin_ignore_leaks().
179 : : */
180 : : static inline void
181 : 10761 : g_end_ignore_leaks (void)
182 : : {
183 : : #if defined (_GLIB_ADDRESS_SANITIZER)
184 : : __lsan_enable ();
185 : : #elif defined (HAS_DYNAMIC_ASAN_LOADING)
186 : 10761 : if (__lsan_enable != NULL)
187 : 0 : __lsan_enable ();
188 : : #endif
189 : 10761 : }
190 : :
191 : : #undef HAS_DYNAMIC_ASAN_LOADING
192 : :
193 : : GMainContext * g_get_worker_context (void);
194 : : gboolean g_check_setuid (void);
195 : : GMainContext * g_main_context_new_with_next_id (guint next_id);
196 : :
197 : : #if (defined (HAVE__SET_THREAD_LOCAL_INVALID_PARAMETER_HANDLER) || \
198 : : defined (HAVE__SET_INVALID_PARAMETER_HANDLER)) && \
199 : : defined (HAVE__CRT_SET_REPORT_MODE)
200 : : # define USE_INVALID_PARAMETER_HANDLER
201 : : #endif
202 : :
203 : : #ifdef USE_INVALID_PARAMETER_HANDLER
204 : : struct _GWin32InvalidParameterHandler
205 : : {
206 : : _invalid_parameter_handler old_handler;
207 : : _invalid_parameter_handler pushed_handler;
208 : : int prev_report_mode;
209 : : int pushed_report_mode;
210 : : };
211 : : #else
212 : : struct _GWin32InvalidParameterHandler
213 : : {
214 : : int unused_really;
215 : : };
216 : : #endif
217 : :
218 : : #ifdef G_OS_WIN32
219 : : GLIB_AVAILABLE_IN_ALL
220 : : gchar *_glib_get_locale_dir (void);
221 : : #endif
222 : :
223 : : GDir * g_dir_open_with_errno (const gchar *path, guint flags);
224 : : GDir * g_dir_new_from_dirp (gpointer dirp);
225 : :
226 : : typedef struct _GWin32InvalidParameterHandler GWin32InvalidParameterHandler;
227 : : void g_win32_push_empty_invalid_parameter_handler (GWin32InvalidParameterHandler *items);
228 : : void g_win32_pop_invalid_parameter_handler (GWin32InvalidParameterHandler *items);
229 : :
230 : : char *g_find_program_for_path (const char *program,
231 : : const char *path,
232 : : const char *working_dir);
233 : :
234 : : int g_uri_get_default_scheme_port (const char *scheme);
235 : :
236 : : #define GLIB_PRIVATE_CALL(symbol) (glib__private__()->symbol)
237 : :
238 : :
239 : : typedef struct {
240 : : /* See gwakeup.c */
241 : : GWakeup * (* g_wakeup_new) (void);
242 : : void (* g_wakeup_free) (GWakeup *wakeup);
243 : : void (* g_wakeup_get_pollfd) (GWakeup *wakeup,
244 : : GPollFD *poll_fd);
245 : : void (* g_wakeup_signal) (GWakeup *wakeup);
246 : : void (* g_wakeup_acknowledge) (GWakeup *wakeup);
247 : :
248 : : /* See gmain.c */
249 : : GMainContext * (* g_get_worker_context) (void);
250 : :
251 : : gboolean (* g_check_setuid) (void);
252 : : GMainContext * (* g_main_context_new_with_next_id) (guint next_id);
253 : :
254 : : GDir * (* g_dir_open_with_errno) (const gchar *path,
255 : : guint flags);
256 : : GDir * (* g_dir_new_from_dirp) (gpointer dirp);
257 : :
258 : : /* See glib-init.c */
259 : : void (* glib_init) (void);
260 : :
261 : : /* See gstdio.c */
262 : : #ifdef G_OS_WIN32
263 : : int (* g_win32_stat_utf8) (const gchar *filename,
264 : : GWin32PrivateStat *buf);
265 : :
266 : : int (* g_win32_lstat_utf8) (const gchar *filename,
267 : : GWin32PrivateStat *buf);
268 : :
269 : : int (* g_win32_readlink_utf8) (const gchar *filename,
270 : : gchar *buf,
271 : : gsize buf_size,
272 : : gchar **alloc_buf,
273 : : gboolean terminate);
274 : :
275 : : int (* g_win32_fstat) (int fd,
276 : : GWin32PrivateStat *buf);
277 : :
278 : : /* See gwin32.c */
279 : : gchar *(*g_win32_find_helper_executable_path) (const gchar *process_name,
280 : : void *dll_handle);
281 : :
282 : : int (* g_win32_reopen_noninherited) (int fd,
283 : : int mode,
284 : : GError **err);
285 : :
286 : : gboolean (* g_win32_handle_is_socket) (void *handle);
287 : :
288 : : #endif
289 : :
290 : : /* See glib-private.c */
291 : : void (* g_win32_push_empty_invalid_parameter_handler) (GWin32InvalidParameterHandler *items);
292 : :
293 : : void (* g_win32_pop_invalid_parameter_handler) (GWin32InvalidParameterHandler *items);
294 : :
295 : : /* See gutils.c */
296 : : char *(* g_find_program_for_path) (const char *program,
297 : : const char *path,
298 : : const char *working_dir);
299 : :
300 : : /* See guri.c */
301 : : int (* g_uri_get_default_scheme_port) (const char *scheme);
302 : :
303 : : /* See gutils.c */
304 : : gboolean (* g_set_prgname_once) (const gchar *prgname);
305 : :
306 : : gpointer (*g_datalist_id_update_atomic) (GData **datalist,
307 : : GQuark key_id,
308 : : GDataListUpdateAtomicFunc callback,
309 : : gpointer user_data);
310 : :
311 : : /* Add other private functions here, initialize them in glib-private.c */
312 : : } GLibPrivateVTable;
313 : :
314 : : GLIB_AVAILABLE_IN_ALL
315 : : const GLibPrivateVTable *glib__private__ (void);
316 : :
317 : : /* Please see following for the use of ".ACP" over ""
318 : : * on Windows, although both are accepted at compile-time
319 : : * but "" renders translated console messages unreadable if
320 : : * built with Visual Studio 2012 and later (this is, unfortunately,
321 : : * undocumented):
322 : : *
323 : : * https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale
324 : : * https://gitlab.gnome.org/GNOME/glib/merge_requests/895#note_525881
325 : : * https://gitlab.gnome.org/GNOME/glib/merge_requests/895#note_525900
326 : : *
327 : : * Additional related items:
328 : : * https://stackoverflow.com/questions/22604329/php-5-5-setlocale-not-working-in-cli-on-windows
329 : : * https://bugs.php.net/bug.php?id=66265
330 : : */
331 : :
332 : : #ifdef G_OS_WIN32
333 : : # define GLIB_DEFAULT_LOCALE ".ACP"
334 : : #else
335 : : # define GLIB_DEFAULT_LOCALE ""
336 : : #endif
337 : :
338 : : gboolean g_uint_equal (gconstpointer v1, gconstpointer v2);
339 : : guint g_uint_hash (gconstpointer v);
340 : :
341 : : #if defined(__GNUC__)
342 : : #define G_THREAD_LOCAL __thread
343 : : #else
344 : : #undef G_THREAD_LOCAL
345 : : #endif
346 : :
347 : : /* Convenience wrapper to call private g_datalist_id_update_atomic() function. */
348 : : #define _g_datalist_id_update_atomic(datalist, key_id, callback, user_data) \
349 : : (GLIB_PRIVATE_CALL (g_datalist_id_update_atomic) ((datalist), (key_id), (callback), (user_data)))
350 : :
351 : : #endif /* __GLIB_PRIVATE_H__ */
|