Fix g_autoptr_cleanup_gstring_free( ) argument to avoid of confliting arument name
[platform/upstream/glib.git] / glib / glib-private.h
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 "gvariant-vectors.h"
26 #include "gstdioprivate.h"
27 #include "gdatasetprivate.h"
28
29 /* gcc defines __SANITIZE_ADDRESS__, clang sets the address_sanitizer
30  * feature flag.
31  *
32  * MSVC defines __SANITIZE_ADDRESS__ as well when AddressSanitizer
33  * is enabled but __lsan_ignore_object() equivalent method is not supported
34  * See also
35  * https://docs.microsoft.com/en-us/cpp/sanitizers/asan-building?view=msvc-160
36  */
37 #if !defined(_MSC_VER) && (defined(__SANITIZE_ADDRESS__) || g_macro__has_feature(address_sanitizer))
38
39 /*
40  * %_GLIB_ADDRESS_SANITIZER:
41  *
42  * Private macro defined if the AddressSanitizer is in use by GLib itself.
43  */
44 #define _GLIB_ADDRESS_SANITIZER
45
46 #include <sanitizer/lsan_interface.h>
47
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
53  * dlsym().
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
57  */
58 #elif defined (G_OS_UNIX) && !defined (__APPLE__) && !defined(__CYGWIN__) && g_macro__has_attribute (weak)
59
60 #define HAS_DYNAMIC_ASAN_LOADING
61
62 void __lsan_enable (void) __attribute__ ((weak));
63 void __lsan_disable (void) __attribute__ ((weak));
64 void __lsan_ignore_object (const void *p) __attribute__ ((weak));
65
66 #endif
67
68 /**
69  * G_CONTAINER_OF:
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.
73  *
74  * Casts away constness of @ptr.
75  *
76  * Returns: a pointer to the container, so that "&(@container)->field == (@ptr)" holds.
77  */
78 #define G_CONTAINER_OF(ptr, type, field) ((type *) G_STRUCT_MEMBER_P (ptr, -G_STRUCT_OFFSET (type, field)))
79
80 /*
81  * g_leak_sanitizer_is_supported:
82  *
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.
86  */
87 static inline gboolean
88 g_leak_sanitizer_is_supported (void)
89 {
90 #if defined (_GLIB_ADDRESS_SANITIZER)
91   return TRUE;
92 #elif defined (HAS_DYNAMIC_ASAN_LOADING)
93   return __lsan_enable != NULL && __lsan_ignore_object != NULL;
94 #else
95   return FALSE;
96 #endif
97 }
98
99 /*
100  * g_ignore_leak:
101  * @p: any pointer
102  *
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.
107  */
108 static inline void
109 g_ignore_leak (gconstpointer p)
110 {
111 #if defined (_GLIB_ADDRESS_SANITIZER)
112   if (p != NULL)
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);
117 #endif
118 }
119
120 /*
121  * g_ignore_strv_leak:
122  * @strv: (nullable) (array zero-terminated=1): an array of strings
123  *
124  * The same as g_ignore_leak(), but for the memory pointed to by @strv,
125  * and for each element of @strv.
126  */
127 static inline void
128 g_ignore_strv_leak (GStrv strv)
129 {
130   gchar **item;
131
132   if (!g_leak_sanitizer_is_supported ())
133     return;
134
135   if (strv)
136     {
137       g_ignore_leak (strv);
138
139       for (item = strv; *item != NULL; item++)
140         g_ignore_leak (*item);
141     }
142 }
143
144 /*
145  * g_begin_ignore_leaks:
146  *
147  * Tell AddressSanitizer and similar tools to ignore all leaks from this point
148  * onwards, until g_end_ignore_leaks() is called.
149  *
150  * Try to use g_ignore_leak() where possible to target deliberate leaks more
151  * specifically.
152  */
153 static inline void
154 g_begin_ignore_leaks (void)
155 {
156 #if defined (_GLIB_ADDRESS_SANITIZER)
157   __lsan_disable ();
158 #elif defined (HAS_DYNAMIC_ASAN_LOADING)
159   if (__lsan_disable != NULL)
160     __lsan_disable ();
161 #endif
162 }
163
164 /*
165  * g_end_ignore_leaks:
166  *
167  * Start ignoring leaks again; this must be paired with a previous call to
168  * g_begin_ignore_leaks().
169  */
170 static inline void
171 g_end_ignore_leaks (void)
172 {
173 #if defined (_GLIB_ADDRESS_SANITIZER)
174   __lsan_enable ();
175 #elif defined (HAS_DYNAMIC_ASAN_LOADING)
176   if (__lsan_enable != NULL)
177     __lsan_enable ();
178 #endif
179 }
180
181 #undef HAS_DYNAMIC_ASAN_LOADING
182
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,
190                                                          gsize               n_vectors,
191                                                          gsize               size,
192                                                          gboolean            trusted);
193
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
198 #endif
199
200 #ifdef USE_INVALID_PARAMETER_HANDLER
201 struct _GWin32InvalidParameterHandler
202 {
203   _invalid_parameter_handler old_handler;
204   _invalid_parameter_handler pushed_handler;
205   int prev_report_mode;
206   int pushed_report_mode;
207 };
208 #else
209 struct _GWin32InvalidParameterHandler
210 {
211   int unused_really;
212 };
213 #endif
214
215 #ifdef G_OS_WIN32
216 GLIB_AVAILABLE_IN_ALL
217 gchar *_glib_get_locale_dir    (void);
218 #endif
219
220 GDir * g_dir_open_with_errno (const gchar *path, guint flags);
221 GDir * g_dir_new_from_dirp (gpointer dirp);
222
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);
226
227 char *g_find_program_for_path (const char *program,
228                                const char *path,
229                                const char *working_dir);
230
231 int g_uri_get_default_scheme_port (const char *scheme);
232
233 #define GLIB_PRIVATE_CALL(symbol) (glib__private__()->symbol)
234
235
236 typedef struct {
237   /* See gwakeup.c */
238   GWakeup *             (* g_wakeup_new)                (void);
239   void                  (* g_wakeup_free)               (GWakeup *wakeup);
240   void                  (* g_wakeup_get_pollfd)         (GWakeup *wakeup,
241                                                         GPollFD *poll_fd);
242   void                  (* g_wakeup_signal)             (GWakeup *wakeup);
243   void                  (* g_wakeup_acknowledge)        (GWakeup *wakeup);
244
245   /* See gmain.c */
246   GMainContext *        (* g_get_worker_context)        (void);
247
248   gboolean              (* g_check_setuid)              (void);
249   GMainContext *        (* g_main_context_new_with_next_id) (guint next_id);
250
251   GDir *                (* g_dir_open_with_errno)       (const gchar *path,
252                                                          guint        flags);
253   GDir *                (* g_dir_new_from_dirp)         (gpointer dirp);
254
255   void                  (* g_variant_to_vectors)        (GVariant    *value,
256                                                          GVariantVectors *vectors);
257   GVariant *            (* g_variant_from_vectors)      (const GVariantType *type,
258                                                          GVariantVector  *vectors,
259                                                          gsize            n_vectors,
260                                                          gsize size,
261                                                          gboolean trusted);
262   void                  (* g_variant_vectors_deinit)    (GVariantVectors *vectors);
263
264   /* See glib-init.c */
265   void                  (* glib_init)                   (void);
266
267   /* See gstdio.c */
268 #ifdef G_OS_WIN32
269   int                   (* g_win32_stat_utf8)           (const gchar        *filename,
270                                                          GWin32PrivateStat  *buf);
271
272   int                   (* g_win32_lstat_utf8)          (const gchar        *filename,
273                                                          GWin32PrivateStat  *buf);
274
275   int                   (* g_win32_readlink_utf8)       (const gchar        *filename,
276                                                          gchar              *buf,
277                                                          gsize               buf_size,
278                                                          gchar             **alloc_buf,
279                                                          gboolean            terminate);
280
281   int                   (* g_win32_fstat)               (int                 fd,
282                                                          GWin32PrivateStat  *buf);
283
284   /* See gwin32.c */
285   gchar *(*g_win32_find_helper_executable_path) (const gchar *process_name,
286                                                  void *dll_handle);
287
288   int                   (* g_win32_reopen_noninherited) (int      fd,
289                                                          int      mode,
290                                                          GError **err);
291
292   gboolean              (* g_win32_handle_is_socket)    (void *handle);
293
294 #endif
295
296   /* See glib-private.c */
297   void (* g_win32_push_empty_invalid_parameter_handler) (GWin32InvalidParameterHandler *items);
298
299   void (* g_win32_pop_invalid_parameter_handler)        (GWin32InvalidParameterHandler *items);
300
301   /* See gutils.c */
302   char *(* g_find_program_for_path) (const char *program,
303                                      const char *path,
304                                      const char *working_dir);
305
306   /* See guri.c */
307   int (* g_uri_get_default_scheme_port) (const char *scheme);
308
309   /* See gutils.c */
310   gboolean (* g_set_prgname_once) (const gchar *prgname);
311
312   gpointer (*g_datalist_id_update_atomic) (GData **datalist,
313                                            GQuark key_id,
314                                            GDataListUpdateAtomicFunc callback,
315                                            gpointer user_data);
316
317   /* Add other private functions here, initialize them in glib-private.c */
318 } GLibPrivateVTable;
319
320 GLIB_AVAILABLE_IN_ALL
321 const GLibPrivateVTable *glib__private__ (void);
322
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,
327  * undocumented):
328  *
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
332  *
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
336  */
337
338 #ifdef G_OS_WIN32
339 # define GLIB_DEFAULT_LOCALE ".ACP"
340 #else
341 # define GLIB_DEFAULT_LOCALE ""
342 #endif
343
344 gboolean g_uint_equal (gconstpointer v1, gconstpointer v2);
345 guint g_uint_hash (gconstpointer v);
346
347 #if defined(__GNUC__)
348 #define G_THREAD_LOCAL __thread
349 #else
350 #undef G_THREAD_LOCAL
351 #endif
352
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)))
356
357 #endif /* __GLIB_PRIVATE_H__ */