Coverage Report

Created: 2024-07-23 07:36

/src/gnutls/lib/dlwrap/zlib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copying and distribution of this file, with or without modification,
3
 * are permitted in any medium without royalty provided the copyright
4
 * notice and this notice are preserved.  This file is offered as-is,
5
 * without any warranty.
6
 */
7
8
#ifdef HAVE_CONFIG_H
9
#include "config.h"
10
#endif
11
12
#include "zlib.h"
13
14
#if defined(GNUTLS_ZLIB_ENABLE_DLOPEN) && GNUTLS_ZLIB_ENABLE_DLOPEN
15
16
#include <assert.h>
17
#include <dlfcn.h>
18
#include <errno.h>
19
#include <stdlib.h>
20
21
/* If Z_LIBRARY_SONAME is defined, dlopen handle can be automatically
22
 * set; otherwise, the caller needs to call
23
 * gnutls_zlib_ensure_library with soname determined at run time.
24
 */
25
#ifdef Z_LIBRARY_SONAME
26
27
static void
28
ensure_library (void)
29
0
{
30
0
  if (gnutls_zlib_ensure_library (Z_LIBRARY_SONAME, RTLD_LAZY | RTLD_LOCAL) < 0)
31
0
    abort ();
32
0
}
33
34
#if defined(GNUTLS_ZLIB_ENABLE_PTHREAD) && GNUTLS_ZLIB_ENABLE_PTHREAD
35
#include <pthread.h>
36
37
static pthread_once_t dlopen_once = PTHREAD_ONCE_INIT;
38
39
#define ENSURE_LIBRARY pthread_once(&dlopen_once, ensure_library)
40
41
#else /* GNUTLS_ZLIB_ENABLE_PTHREAD */
42
43
0
#define ENSURE_LIBRARY do {     \
44
0
    if (!gnutls_zlib_dlhandle) \
45
0
      ensure_library();       \
46
0
  } while (0)
47
48
#endif /* !GNUTLS_ZLIB_ENABLE_PTHREAD */
49
50
#else /* Z_LIBRARY_SONAME */
51
52
#define ENSURE_LIBRARY do {} while (0)
53
54
#endif /* !Z_LIBRARY_SONAME */
55
56
static void *gnutls_zlib_dlhandle;
57
58
/* Define redirection symbols */
59
#pragma GCC diagnostic push
60
#pragma GCC diagnostic ignored "-Wunused-macros"
61
62
#if (2 <= __GNUC__ || (4 <= __clang_major__))
63
#define FUNC(ret, name, args, cargs)      \
64
  static __typeof__(name)(*gnutls_zlib_sym_##name);
65
#else
66
#define FUNC(ret, name, args, cargs)    \
67
  static ret(*gnutls_zlib_sym_##name)args;
68
#endif
69
#define VOID_FUNC FUNC
70
#include "zlibfuncs.h"
71
#undef VOID_FUNC
72
#undef FUNC
73
74
#pragma GCC diagnostic pop
75
76
/* Define redirection wrapper functions */
77
#pragma GCC diagnostic push
78
#pragma GCC diagnostic ignored "-Wunused-macros"
79
80
#define FUNC(ret, name, args, cargs)        \
81
0
ret gnutls_zlib_func_##name args           \
82
0
{             \
83
0
  ENSURE_LIBRARY;         \
84
0
  assert (gnutls_zlib_sym_##name);      \
85
0
  return gnutls_zlib_sym_##name cargs;     \
86
0
}
Unexecuted instantiation: gnutls_zlib_func_compress
Unexecuted instantiation: gnutls_zlib_func_compressBound
Unexecuted instantiation: gnutls_zlib_func_uncompress
87
#define VOID_FUNC(ret, name, args, cargs)   \
88
ret gnutls_zlib_func_##name args           \
89
{             \
90
  ENSURE_LIBRARY;         \
91
  assert (gnutls_zlib_sym_##name);      \
92
  gnutls_zlib_sym_##name cargs;       \
93
}
94
#include "zlibfuncs.h"
95
#undef VOID_FUNC
96
#undef FUNC
97
98
#pragma GCC diagnostic pop
99
100
static int
101
ensure_symbol (const char *name, void **symp)
102
0
{
103
0
  if (!*symp)
104
0
    {
105
0
      void *sym = dlsym (gnutls_zlib_dlhandle, name);
106
0
      if (!sym)
107
0
  return -errno;
108
0
      *symp = sym;
109
0
    }
110
0
  return 0;
111
0
}
112
113
int
114
gnutls_zlib_ensure_library (const char *soname, int flags)
115
0
{
116
0
  int err;
117
118
0
  if (!gnutls_zlib_dlhandle)
119
0
    {
120
0
      gnutls_zlib_dlhandle = dlopen (soname, flags);
121
0
      if (!gnutls_zlib_dlhandle)
122
0
  return -errno;
123
0
    }
124
125
0
#define ENSURE_SYMBOL(name)         \
126
0
  ensure_symbol(#name, (void **)&gnutls_zlib_sym_##name)
127
128
0
#pragma GCC diagnostic push
129
0
#pragma GCC diagnostic ignored "-Wunused-macros"
130
131
0
#define FUNC(ret, name, args, cargs)  \
132
0
  err = ENSURE_SYMBOL(name);   \
133
0
  if (err < 0)       \
134
0
    return err;
135
0
#define VOID_FUNC FUNC
136
0
#include "zlibfuncs.h"
137
0
#undef VOID_FUNC
138
0
#undef FUNC
139
140
0
#pragma GCC diagnostic pop
141
142
0
#undef ENSURE_SYMBOL
143
0
  return 0;
144
0
}
145
146
void
147
gnutls_zlib_unload_library (void)
148
0
{
149
0
  if (gnutls_zlib_dlhandle)
150
0
    dlclose (gnutls_zlib_dlhandle);
151
152
0
#pragma GCC diagnostic push
153
0
#pragma GCC diagnostic ignored "-Wunused-macros"
154
155
0
#define FUNC(ret, name, args, cargs)    \
156
0
  gnutls_zlib_sym_##name = NULL;
157
0
#define VOID_FUNC FUNC
158
0
#include "zlibfuncs.h"
159
0
#undef VOID_FUNC
160
0
#undef FUNC
161
162
0
#pragma GCC diagnostic pop
163
164
0
#undef RESET_SYMBOL
165
0
}
166
167
#else /* GNUTLS_ZLIB_ENABLE_DLOPEN */
168
169
int
170
gnutls_zlib_ensure_library (const char *soname, int flags)
171
{
172
  (void) soname;
173
  (void) flags;
174
  return 0;
175
}
176
177
void
178
gnutls_zlib_unload_library (void)
179
{
180
}
181
182
#endif /* !GNUTLS_ZLIB_ENABLE_DLOPEN */