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