LCOV - code coverage report
Current view: top level - glib/glib - glib-private.h (source / functions) Hit Total Coverage
Test: unnamed Lines: 14 21 66.7 %
Date: 2024-04-23 05:16:05 Functions: 5 5 100.0 %
Branches: 7 18 38.9 %

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

Generated by: LCOV version 1.14