File: constructor.c

package info (click to toggle)
glib2.0 2.84.3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie, trixie-proposed-updates, trixie-updates
  • size: 66,100 kB
  • sloc: ansic: 538,905; python: 9,624; sh: 1,572; xml: 1,482; perl: 1,222; cpp: 535; makefile: 316; javascript: 11
file content (260 lines) | stat: -rw-r--r-- 5,930 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* constructor.c - Test for constructors
 *
 * Copyright © 2023 Luca Bacci
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include "../gconstructorprivate.h"

#ifndef _WIN32
#include <dlfcn.h>
#else
#include <windows.h>
#endif

#if defined(_WIN32)
  #define MODULE_IMPORT \
    __declspec (dllimport)
#else
  #define MODULE_IMPORT
#endif

MODULE_IMPORT
void string_add_exclusive (const char *string);

MODULE_IMPORT
void string_check (const char *string);

MODULE_IMPORT
int string_find (const char *string);

#if G_HAS_CONSTRUCTORS

#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS (ctor)
#endif

G_DEFINE_CONSTRUCTOR (ctor)

static void
ctor (void)
{
  string_add_exclusive (G_STRINGIFY (PREFIX) "_" "ctor");
}

#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS (dtor)
#endif

G_DEFINE_DESTRUCTOR (dtor)

static void
dtor (void)
{
  string_add_exclusive (G_STRINGIFY (PREFIX) "_" "dtor");

  if (string_find ("app_dtor") && string_find ("lib_dtor"))
    {
      /* All destructors were invoked, this is the last.
       * Call _Exit (EXIT_SUCCESS) to exit immediately
       * with a success code */
      _Exit (EXIT_SUCCESS);
    }
}

#endif /* G_HAS_CONSTRUCTORS */


#if defined (_WIN32) && defined (G_HAS_TLS_CALLBACKS)

extern IMAGE_DOS_HEADER __ImageBase;

static inline HMODULE
this_module (void)
{
  return (HMODULE) &__ImageBase;
}

G_DEFINE_TLS_CALLBACK (tls_callback)

static void NTAPI
tls_callback (PVOID  hInstance,
              DWORD  dwReason,
              LPVOID lpvReserved)
{
  /* The HINSTANCE we get must match the address of __ImageBase */
  g_assert_true (hInstance == this_module ());

#ifdef BUILD_TEST_EXECUTABLE
  /* Yes, we can call GetModuleHandle (NULL) with the loader lock */
  g_assert_true (hInstance == GetModuleHandle (NULL));
#endif

  switch (dwReason)
    {
    case DLL_PROCESS_ATTACH:
      {
#ifndef BUILD_TEST_EXECUTABLE
        /* the library is explicitly loaded */
        g_assert_null (lpvReserved);
#endif
        string_add_exclusive (G_STRINGIFY (PREFIX) "_" "tlscb_process_attach");
      }
      break;

    case DLL_THREAD_ATTACH:
      break;

    case DLL_THREAD_DETACH:
      break;

    case DLL_PROCESS_DETACH:
      {
#ifndef BUILD_TEST_EXECUTABLE
        /* the library is explicitly unloaded */
        g_assert_null (lpvReserved);
#endif
        string_add_exclusive (G_STRINGIFY (PREFIX) "_" "tlscb_process_detach");
      }
      break;

    default:
      g_assert_not_reached ();
      break;
    }
}

#endif /* _WIN32 && G_HAS_TLS_CALLBACKS */

#ifdef BUILD_TEST_EXECUTABLE

void *library;

static void
load_library (const char *path)
{
#ifndef _WIN32
  library = dlopen (path, RTLD_NOW);
  if (!library)
    {
      g_error ("%s (%s) failed: %s", "dlopen", path, dlerror ());
    }
#else
  wchar_t *path_utf16 = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
  g_assert_nonnull (path_utf16);

  library = LoadLibraryW (path_utf16);
  if (!library)
    {
      g_error ("%s (%s) failed with error code %u",
               "FreeLibrary", path, (unsigned int) GetLastError ());
    }

  g_free (path_utf16);
#endif
}

static void
unload_library (void)
{
#ifndef _WIN32
  if (dlclose (library) != 0)
    {
      g_error ("%s failed: %s", "dlclose", dlerror ());
    }
#else
  if (!FreeLibrary (library))
    {
      g_error ("%s failed with error code %u",
               "FreeLibrary", (unsigned int) GetLastError ());
    }
#endif
}

static void
test_app (void)
{
#if G_HAS_CONSTRUCTORS
  string_check ("app_" "ctor");
#endif
#if defined (_WIN32) && defined (G_HAS_TLS_CALLBACKS)
  string_check ("app_" "tlscb_process_attach");
#endif
}

static void
test_lib (gconstpointer data)
{
  const char *library_path = (const char*) data;

  /* Constructors */
  load_library (library_path);

#if G_HAS_CONSTRUCTORS
  string_check ("lib_" "ctor");
#endif
#if defined (_WIN32) && defined (G_HAS_TLS_CALLBACKS)
  string_check ("lib_" "tlscb_process_attach");
#endif

  /* Destructors */
  unload_library ();

#if G_HAS_DESTRUCTORS
  /* Destructors in dynamically-loaded libraries do not
   * necessarily run on dlclose. On some systems dlclose
   * is effectively a no-op (e.g with the Musl LibC) and
   * destructors run at program exit */
  g_test_message ("Destructors run on module unload: %s\n",
                  string_find ("lib_" "dtor") ? "yes" : "no");
#endif
#if defined (_WIN32) && defined (G_HAS_TLS_CALLBACKS)
  string_check ("lib_" "tlscb_process_detach");
#endif
}

int
main (int argc, char *argv[])
{

  const char *libname = G_STRINGIFY (LIB_NAME);
  const char *builddir;
  char *path;
  int ret;

  g_assert_nonnull ((builddir = g_getenv ("G_TEST_BUILDDIR")));

  path = g_build_filename (builddir, libname, NULL);

  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/constructor/application", test_app);
  g_test_add_data_func ("/constructor/library", path, test_lib);

  ret = g_test_run ();
  g_assert_cmpint (ret, ==, 0);

  g_free (path);

  /* Return EXIT_FAILURE from main. The last destructor will
   * call _Exit (EXIT_SUCCESS) if everything went well. This
   * is a way to test that destructors get invoked */
  return EXIT_FAILURE;
}

#endif /* BUILD_TEST_EXECUTABLE */