Coverage Report

Created: 2025-07-18 06:10

/src/tinysparql/subprojects/glib-2.80.3/glib/glib-init.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2011 Canonical Limited
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, but
12
 * 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
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * Author: Ryan Lortie <desrt@desrt.ca>
20
 */
21
22
#include "config.h"
23
24
#include "glib-init.h"
25
#include "gmacros.h"
26
#include "gtypes.h"
27
#include "gutils.h"     /* for GDebugKey */
28
#include "gconstructor.h"
29
#include "gconstructorprivate.h"
30
#include "gmem.h"       /* for g_mem_gc_friendly */
31
32
#include <string.h>
33
#include <stdlib.h>
34
#include <stdio.h>
35
#include <ctype.h>
36
37
/* Deliberately not checking HAVE_STDINT_H here: we officially require a
38
 * C99 toolchain, which implies <stdint.h>, int8_t and so on. If your
39
 * toolchain does not have this, now would be a good time to upgrade. */
40
#include <stdint.h>
41
42
/* This seems as good a place as any to make static assertions about platform
43
 * assumptions we make throughout GLib. */
44
45
/* We do not support 36-bit bytes or other historical curiosities. */
46
G_STATIC_ASSERT (CHAR_BIT == 8);
47
48
/* We assume that data pointers are the same size as function pointers... */
49
G_STATIC_ASSERT (sizeof (gpointer) == sizeof (GFunc));
50
G_STATIC_ASSERT (G_ALIGNOF (gpointer) == G_ALIGNOF (GFunc));
51
/* ... and that all function pointers are the same size. */
52
G_STATIC_ASSERT (sizeof (GFunc) == sizeof (GCompareDataFunc));
53
G_STATIC_ASSERT (G_ALIGNOF (GFunc) == G_ALIGNOF (GCompareDataFunc));
54
55
/* We assume that "small" enums (those where all values fit in INT32_MIN
56
 * to INT32_MAX) are exactly int-sized. In particular, we assume that if
57
 * an enum has no members that exceed the range of char/short, the
58
 * compiler will make it int-sized anyway, so adding a member later that
59
 * *does* exceed the range of char/short is not an ABI break. */
60
typedef enum {
61
    TEST_CHAR_0 = 0
62
} TestChar;
63
typedef enum {
64
    TEST_SHORT_0 = 0,
65
    TEST_SHORT_256 = 256
66
} TestShort;
67
typedef enum {
68
    TEST_INT32_MIN = G_MININT32,
69
    TEST_INT32_MAX = G_MAXINT32
70
} TestInt;
71
G_STATIC_ASSERT (sizeof (TestChar) == sizeof (int));
72
G_STATIC_ASSERT (sizeof (TestShort) == sizeof (int));
73
G_STATIC_ASSERT (sizeof (TestInt) == sizeof (int));
74
G_STATIC_ASSERT (G_ALIGNOF (TestChar) == G_ALIGNOF (int));
75
G_STATIC_ASSERT (G_ALIGNOF (TestShort) == G_ALIGNOF (int));
76
G_STATIC_ASSERT (G_ALIGNOF (TestInt) == G_ALIGNOF (int));
77
78
G_STATIC_ASSERT (sizeof (gchar) == 1);
79
G_STATIC_ASSERT (sizeof (guchar) == 1);
80
G_STATIC_ASSERT (sizeof (gint8) * CHAR_BIT == 8);
81
G_STATIC_ASSERT (sizeof (guint8) * CHAR_BIT == 8);
82
G_STATIC_ASSERT (sizeof (gint16) * CHAR_BIT == 16);
83
G_STATIC_ASSERT (sizeof (guint16) * CHAR_BIT == 16);
84
G_STATIC_ASSERT (sizeof (gint32) * CHAR_BIT == 32);
85
G_STATIC_ASSERT (sizeof (guint32) * CHAR_BIT == 32);
86
G_STATIC_ASSERT (sizeof (gint64) * CHAR_BIT == 64);
87
G_STATIC_ASSERT (sizeof (guint64) * CHAR_BIT == 64);
88
89
G_STATIC_ASSERT (sizeof (void *) == GLIB_SIZEOF_VOID_P);
90
G_STATIC_ASSERT (sizeof (gintptr) == sizeof (void *));
91
G_STATIC_ASSERT (sizeof (guintptr) == sizeof (void *));
92
93
G_STATIC_ASSERT (sizeof (short) == sizeof (gshort));
94
G_STATIC_ASSERT (G_MINSHORT == SHRT_MIN);
95
G_STATIC_ASSERT (G_MAXSHORT == SHRT_MAX);
96
G_STATIC_ASSERT (sizeof (unsigned short) == sizeof (gushort));
97
G_STATIC_ASSERT (G_MAXUSHORT == USHRT_MAX);
98
99
G_STATIC_ASSERT (sizeof (int) == sizeof (gint));
100
G_STATIC_ASSERT (G_MININT == INT_MIN);
101
G_STATIC_ASSERT (G_MAXINT == INT_MAX);
102
G_STATIC_ASSERT (sizeof (unsigned int) == sizeof (guint));
103
G_STATIC_ASSERT (G_MAXUINT == UINT_MAX);
104
105
G_STATIC_ASSERT (sizeof (long) == GLIB_SIZEOF_LONG);
106
G_STATIC_ASSERT (sizeof (long) == sizeof (glong));
107
G_STATIC_ASSERT (G_MINLONG == LONG_MIN);
108
G_STATIC_ASSERT (G_MAXLONG == LONG_MAX);
109
G_STATIC_ASSERT (sizeof (unsigned long) == sizeof (gulong));
110
G_STATIC_ASSERT (G_MAXULONG == ULONG_MAX);
111
112
G_STATIC_ASSERT (G_HAVE_GINT64 == 1);
113
114
G_STATIC_ASSERT (sizeof (size_t) == GLIB_SIZEOF_SIZE_T);
115
/* Not a typo: ssize_t is POSIX, not Standard C, but if it exists then
116
 * it's the same size as size_t. */
117
G_STATIC_ASSERT (sizeof (size_t) == GLIB_SIZEOF_SSIZE_T);
118
G_STATIC_ASSERT (sizeof (gsize) == GLIB_SIZEOF_SSIZE_T);
119
G_STATIC_ASSERT (sizeof (gsize) == sizeof (size_t));
120
G_STATIC_ASSERT (G_MAXSIZE == SIZE_MAX);
121
/* Again this is size_t not ssize_t, because ssize_t is POSIX, not C99 */
122
G_STATIC_ASSERT (sizeof (gssize) == sizeof (size_t));
123
G_STATIC_ASSERT (G_ALIGNOF (gsize) == G_ALIGNOF (size_t));
124
G_STATIC_ASSERT (G_ALIGNOF (gssize) == G_ALIGNOF (size_t));
125
/* We assume that GSIZE_TO_POINTER is reversible by GPOINTER_TO_SIZE
126
 * without losing information.
127
 * However, we do not assume that GPOINTER_TO_SIZE can store an arbitrary
128
 * pointer in a gsize (known to be false on CHERI). */
129
G_STATIC_ASSERT (sizeof (size_t) <= sizeof (void *));
130
/* Standard C does not guarantee that size_t is the same as uintptr_t,
131
 * but GLib currently assumes they are the same: see
132
 * <https://gitlab.gnome.org/GNOME/glib/-/issues/2842>.
133
 *
134
 * To enable working on bringup for new architectures these assertions
135
 * can be disabled with -DG_ENABLE_EXPERIMENTAL_ABI_COMPILATION.
136
 *
137
 * FIXME: remove these assertions once the API/ABI has stabilized. */
138
#ifndef G_ENABLE_EXPERIMENTAL_ABI_COMPILATION
139
G_STATIC_ASSERT (sizeof (size_t) == sizeof (uintptr_t));
140
G_STATIC_ASSERT (G_ALIGNOF (size_t) == G_ALIGNOF (uintptr_t));
141
#endif
142
/* goffset is always 64-bit, even if off_t is only 32-bit
143
 * (compiling without large-file-support on 32-bit) */
144
G_STATIC_ASSERT (sizeof (goffset) == sizeof (gint64));
145
G_STATIC_ASSERT (G_ALIGNOF (goffset) == G_ALIGNOF (gint64));
146
147
G_STATIC_ASSERT (sizeof (gfloat) == sizeof (float));
148
G_STATIC_ASSERT (G_ALIGNOF (gfloat) == G_ALIGNOF (float));
149
G_STATIC_ASSERT (sizeof (gdouble) == sizeof (double));
150
G_STATIC_ASSERT (G_ALIGNOF (gdouble) == G_ALIGNOF (double));
151
152
G_STATIC_ASSERT (sizeof (gintptr) == sizeof (intptr_t));
153
G_STATIC_ASSERT (sizeof (guintptr) == sizeof (uintptr_t));
154
G_STATIC_ASSERT (G_ALIGNOF (gintptr) == G_ALIGNOF (intptr_t));
155
G_STATIC_ASSERT (G_ALIGNOF (guintptr) == G_ALIGNOF (uintptr_t));
156
157
G_STATIC_ASSERT (sizeof (gint8) == sizeof (int8_t));
158
G_STATIC_ASSERT (sizeof (guint8) == sizeof (uint8_t));
159
G_STATIC_ASSERT (G_ALIGNOF (gint8) == G_ALIGNOF (int8_t));
160
G_STATIC_ASSERT (G_ALIGNOF (guint8) == G_ALIGNOF (uint8_t));
161
162
G_STATIC_ASSERT (sizeof (gint16) == sizeof (int16_t));
163
G_STATIC_ASSERT (sizeof (guint16) == sizeof (uint16_t));
164
G_STATIC_ASSERT (G_ALIGNOF (gint16) == G_ALIGNOF (int16_t));
165
G_STATIC_ASSERT (G_ALIGNOF (guint16) == G_ALIGNOF (uint16_t));
166
167
G_STATIC_ASSERT (sizeof (gint32) == sizeof (int32_t));
168
G_STATIC_ASSERT (sizeof (guint32) == sizeof (uint32_t));
169
G_STATIC_ASSERT (G_ALIGNOF (gint32) == G_ALIGNOF (int32_t));
170
G_STATIC_ASSERT (G_ALIGNOF (guint32) == G_ALIGNOF (uint32_t));
171
172
G_STATIC_ASSERT (sizeof (gint64) == sizeof (int64_t));
173
G_STATIC_ASSERT (sizeof (guint64) == sizeof (uint64_t));
174
G_STATIC_ASSERT (G_ALIGNOF (gint64) == G_ALIGNOF (int64_t));
175
G_STATIC_ASSERT (G_ALIGNOF (guint64) == G_ALIGNOF (uint64_t));
176
177
/**
178
 * g_mem_gc_friendly:
179
 *
180
 * This variable is %TRUE if the `G_DEBUG` environment variable
181
 * includes the key `gc-friendly`.
182
 */
183
gboolean g_mem_gc_friendly = FALSE;
184
185
GLogLevelFlags g_log_msg_prefix = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING |
186
                                  G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG;
187
GLogLevelFlags g_log_always_fatal = G_LOG_FATAL_MASK;
188
189
static gboolean
190
debug_key_matches (const gchar *key,
191
                   const gchar *token,
192
                   guint        length)
193
0
{
194
  /* may not call GLib functions: see note in g_parse_debug_string() */
195
0
  for (; length; length--, key++, token++)
196
0
    {
197
0
      char k = (*key   == '_') ? '-' : tolower (*key  );
198
0
      char t = (*token == '_') ? '-' : tolower (*token);
199
200
0
      if (k != t)
201
0
        return FALSE;
202
0
    }
203
204
0
  return *key == '\0';
205
0
}
206
207
/* The GVariant documentation indirectly says that int is at least 32 bits
208
 * (by saying that b, y, n, q, i, u, h are promoted to int). On any
209
 * reasonable platform, int is in fact *exactly* 32 bits long, because
210
 * otherwise, {signed char, short, int} wouldn't be sufficient to provide
211
 * {int8_t, int16_t, int32_t}. */
212
G_STATIC_ASSERT (sizeof (int) == sizeof (gint32));
213
214
/**
215
 * g_parse_debug_string:
216
 * @string: (nullable): a list of debug options separated by colons, spaces, or
217
 * commas, or %NULL.
218
 * @keys: (array length=nkeys): pointer to an array of #GDebugKey which associate
219
 *     strings with bit flags.
220
 * @nkeys: the number of #GDebugKeys in the array.
221
 *
222
 * Parses a string containing debugging options
223
 * into a %guint containing bit flags. This is used
224
 * within GDK and GTK to parse the debug options passed on the
225
 * command line or through environment variables.
226
 *
227
 * If @string is equal to "all", all flags are set. Any flags
228
 * specified along with "all" in @string are inverted; thus,
229
 * "all,foo,bar" or "foo,bar,all" sets all flags except those
230
 * corresponding to "foo" and "bar".
231
 *
232
 * If @string is equal to "help", all the available keys in @keys
233
 * are printed out to standard error.
234
 *
235
 * Returns: the combined set of bit flags.
236
 */
237
guint
238
g_parse_debug_string  (const gchar     *string,
239
                       const GDebugKey *keys,
240
                       guint            nkeys)
241
0
{
242
0
  guint i;
243
0
  guint result = 0;
244
245
0
  if (string == NULL)
246
0
    return 0;
247
248
  /* this function is used during the initialisation of gmessages, gmem
249
   * and gslice, so it may not do anything that causes memory to be
250
   * allocated or risks messages being emitted.
251
   *
252
   * this means, more or less, that this code may not call anything
253
   * inside GLib.
254
   */
255
256
0
  if (!strcasecmp (string, "help"))
257
0
    {
258
      /* using stdio directly for the reason stated above */
259
0
      fprintf (stderr, "Supported debug values:");
260
0
      for (i = 0; i < nkeys; i++)
261
0
        fprintf (stderr, " %s", keys[i].key);
262
0
      fprintf (stderr, " all help\n");
263
0
    }
264
0
  else
265
0
    {
266
0
      const gchar *p = string;
267
0
      const gchar *q;
268
0
      gboolean invert = FALSE;
269
270
0
      while (*p)
271
0
       {
272
0
         q = strpbrk (p, ":;, \t");
273
0
         if (!q)
274
0
           q = p + strlen (p);
275
276
0
         if (debug_key_matches ("all", p, q - p))
277
0
           {
278
0
             invert = TRUE;
279
0
           }
280
0
         else
281
0
           {
282
0
             for (i = 0; i < nkeys; i++)
283
0
               if (debug_key_matches (keys[i].key, p, q - p))
284
0
                 result |= keys[i].value;
285
0
           }
286
287
0
         p = q;
288
0
         if (*p)
289
0
           p++;
290
0
       }
291
292
0
      if (invert)
293
0
        {
294
0
          guint all_flags = 0;
295
296
0
          for (i = 0; i < nkeys; i++)
297
0
            all_flags |= keys[i].value;
298
299
0
          result = all_flags & (~result);
300
0
        }
301
0
    }
302
303
0
  return result;
304
0
}
305
306
static guint
307
g_parse_debug_envvar (const gchar     *envvar,
308
                      const GDebugKey *keys,
309
                      gint             n_keys,
310
                      guint            default_value)
311
16
{
312
16
  const gchar *value;
313
314
#ifdef OS_WIN32
315
  /* "fatal-warnings,fatal-criticals,all,help" is pretty short */
316
  gchar buffer[100];
317
318
  if (GetEnvironmentVariable (envvar, buffer, 100) < 100)
319
    value = buffer;
320
  else
321
    return 0;
322
#else
323
16
  value = getenv (envvar);
324
16
#endif
325
326
16
  if (value == NULL)
327
16
    return default_value;
328
329
0
  return g_parse_debug_string (value, keys, n_keys);
330
16
}
331
332
static void
333
g_messages_prefixed_init (void)
334
8
{
335
8
  const GDebugKey keys[] = {
336
8
    { "error", G_LOG_LEVEL_ERROR },
337
8
    { "critical", G_LOG_LEVEL_CRITICAL },
338
8
    { "warning", G_LOG_LEVEL_WARNING },
339
8
    { "message", G_LOG_LEVEL_MESSAGE },
340
8
    { "info", G_LOG_LEVEL_INFO },
341
8
    { "debug", G_LOG_LEVEL_DEBUG }
342
8
  };
343
344
8
  g_log_msg_prefix = g_parse_debug_envvar ("G_MESSAGES_PREFIXED", keys, G_N_ELEMENTS (keys), g_log_msg_prefix);
345
8
}
346
347
static void
348
g_debug_init (void)
349
8
{
350
8
  const GDebugKey keys[] = {
351
8
    { "gc-friendly", 1 },
352
8
    {"fatal-warnings",  G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL },
353
8
    {"fatal-criticals", G_LOG_LEVEL_CRITICAL }
354
8
  };
355
8
  GLogLevelFlags flags;
356
357
8
  flags = g_parse_debug_envvar ("G_DEBUG", keys, G_N_ELEMENTS (keys), 0);
358
359
8
  g_log_always_fatal |= flags & G_LOG_LEVEL_MASK;
360
361
8
  g_mem_gc_friendly = flags & 1;
362
8
}
363
364
void
365
glib_init (void)
366
16
{
367
16
  static gboolean glib_inited;
368
369
16
  if (glib_inited)
370
8
    return;
371
372
8
  glib_inited = TRUE;
373
374
8
  g_messages_prefixed_init ();
375
8
  g_debug_init ();
376
8
  g_quark_init ();
377
8
  g_error_init ();
378
8
}
379
380
#ifdef G_PLATFORM_WIN32
381
382
HMODULE glib_dll = NULL;
383
void glib_win32_init (void);
384
385
void
386
glib_win32_init (void)
387
{
388
  /* May be called more than once in static compilation mode */
389
  static gboolean win32_already_init = FALSE;
390
  if (!win32_already_init)
391
    {
392
      win32_already_init = TRUE;
393
394
      g_crash_handler_win32_init ();
395
#ifdef THREADS_WIN32
396
      g_thread_win32_init ();
397
#endif
398
399
      g_clock_win32_init ();
400
      glib_init ();
401
      /* must go after glib_init */
402
      g_console_win32_init ();
403
    }
404
}
405
406
static void
407
glib_win32_deinit (gboolean detach_thread)
408
{
409
#ifdef THREADS_WIN32
410
  if (detach_thread)
411
    g_thread_win32_process_detach ();
412
#endif
413
  g_crash_handler_win32_deinit ();
414
}
415
416
#ifndef GLIB_STATIC_COMPILATION
417
418
BOOL WINAPI DllMain (HINSTANCE hinstDLL,
419
                     DWORD     fdwReason,
420
                     LPVOID    lpvReserved);
421
422
BOOL WINAPI
423
DllMain (HINSTANCE hinstDLL,
424
         DWORD     fdwReason,
425
         LPVOID    lpvReserved)
426
{
427
  switch (fdwReason)
428
    {
429
    case DLL_PROCESS_ATTACH:
430
      glib_dll = hinstDLL;
431
      glib_win32_init ();
432
      break;
433
434
    case DLL_THREAD_DETACH:
435
#ifdef THREADS_WIN32
436
      g_thread_win32_thread_detach ();
437
#endif
438
      break;
439
440
    case DLL_PROCESS_DETACH:
441
      glib_win32_deinit (lpvReserved == NULL);
442
      break;
443
444
    default:
445
      /* do nothing */
446
      ;
447
    }
448
449
  return TRUE;
450
}
451
452
#else
453
454
#ifndef G_HAS_CONSTRUCTORS
455
#error static compilation on Windows requires constructor support
456
#endif
457
458
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
459
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(glib_priv_constructor)
460
#endif
461
462
static gboolean tls_callback_invoked;
463
464
G_DEFINE_CONSTRUCTOR (glib_priv_constructor)
465
466
static void
467
glib_priv_constructor (void)
468
{
469
  glib_win32_init ();
470
471
  if (!tls_callback_invoked)
472
    g_critical ("TLS callback not invoked");
473
}
474
475
#ifndef G_HAS_TLS_CALLBACKS
476
#error static compilation on Windows requires TLS callbacks support
477
#endif
478
479
G_DEFINE_TLS_CALLBACK (glib_priv_tls_callback)
480
481
static void NTAPI
482
glib_priv_tls_callback (LPVOID hinstance,
483
                        DWORD  reason,
484
                        LPVOID reserved)
485
{
486
  switch (reason)
487
    {
488
    case DLL_PROCESS_ATTACH:
489
      glib_dll = hinstance;
490
      tls_callback_invoked = TRUE;
491
      break;
492
    case DLL_THREAD_DETACH:
493
#ifdef THREADS_WIN32
494
      g_thread_win32_thread_detach ();
495
#endif
496
      break;
497
    case DLL_PROCESS_DETACH:
498
      glib_win32_deinit (reserved == NULL);
499
      break;
500
501
    default:
502
      break;
503
    }
504
}
505
506
#endif /* GLIB_STATIC_COMPILATION */
507
508
#elif defined(G_HAS_CONSTRUCTORS) /* && !G_PLATFORM_WIN32 */
509
510
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
511
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(glib_init_ctor)
512
#endif
513
G_DEFINE_CONSTRUCTOR(glib_init_ctor)
514
515
static void
516
glib_init_ctor (void)
517
8
{
518
8
  glib_init ();
519
8
}
520
521
#else /* !G_PLATFORM_WIN32 && !G_HAS_CONSTRUCTORS */
522
# error Your platform/compiler is missing constructor support
523
#endif /* G_PLATFORM_WIN32 */