Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*	$NetBSD: t_tls_extern.c,v 1.12 2023/06/04 01:24:58 joerg Exp $	*/

/*-
 * Copyright (c) 2023 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>

#include <atf-c.h>
#include <dlfcn.h>

#define	ATF_REQUIRE_DL(x)	ATF_REQUIRE_MSG(x, "%s: %s", #x, dlerror())

enum order {
	DEF_USE_EAGER,
	DEF_USE_LAZY,
	USE_DEF,
	USE_DEF_NOLOAD,
};

static void
tls_extern(const char *libdef, const char *libuse, enum order order)
{
	void *def, *use;
	int *(*fdef)(void), *(*fuse)(void);
	int *pdef, *puse;

	(void)dlerror();

	switch (order) {
	case DEF_USE_EAGER:
		ATF_REQUIRE_DL(def = dlopen(libdef, 0));
		ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
		pdef = (*fdef)();
		ATF_REQUIRE_DL(use = dlopen(libuse, 0));
		ATF_REQUIRE_DL(fuse = dlsym(use, "fuse"));
		puse = (*fuse)();
		break;
	case DEF_USE_LAZY:
		ATF_REQUIRE_DL(def = dlopen(libdef, 0));
		ATF_REQUIRE_DL(use = dlopen(libuse, 0));
		goto lazy;
	case USE_DEF:
		ATF_REQUIRE_DL(use = dlopen(libuse, 0));
		ATF_REQUIRE_DL(def = dlopen(libdef, 0));
		goto lazy;
	case USE_DEF_NOLOAD:
		ATF_REQUIRE_DL(use = dlopen(libuse, 0));
		ATF_REQUIRE_DL(def = dlopen(libdef, RTLD_NOLOAD));
lazy:		ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
		ATF_REQUIRE_DL(fuse = dlsym(use, "fuse"));
		pdef = (*fdef)();
		puse = (*fuse)();
		break;
	}

	ATF_CHECK_EQ_MSG(pdef, puse,
	    "%p in defining library != %p in using library",
	    pdef, puse);
}

ATF_TC(dynamic_abusedef);
ATF_TC_HEAD(dynamic_abusedef, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
	    " loading static use than dynamic def");
}
ATF_TC_BODY(dynamic_abusedef, tc)
{
	tls_extern("libh_def_dynamic.so", "libh_abuse_dynamic.so", USE_DEF);
}

ATF_TC(dynamic_abusedefnoload);
ATF_TC_HEAD(dynamic_abusedefnoload, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
	    " loading static use then dynamic def with RTLD_NOLOAD");
}
ATF_TC_BODY(dynamic_abusedefnoload, tc)
{
	tls_extern("libh_def_dynamic.so", "libh_abuse_dynamic.so",
	    USE_DEF_NOLOAD);
}

ATF_TC(dynamic_defabuse_eager);
ATF_TC_HEAD(dynamic_defabuse_eager, tc)
{
	atf_tc_set_md_var(tc, "descr", "dlopen refuses extern __thread for TLS,"
	    " loading dynamic def then static use eagerly");
}
ATF_TC_BODY(dynamic_defabuse_eager, tc)
{
	void *def;
	int *(*fdef)(void);

	ATF_REQUIRE_DL(def = dlopen("libh_def_dynamic.so", 0));
	ATF_REQUIRE_DL(fdef = dlsym(def, "fdef"));
	(void)(*fdef)();
	ATF_CHECK_EQ_MSG(NULL, dlopen("libh_abuse_dynamic.so", 0),
	    "dlopen failed to detect static-then-dynamic abuse");
}

ATF_TC(dynamic_defabuse_lazy);
ATF_TC_HEAD(dynamic_defabuse_lazy, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
	    " loading dynamic def then static use lazily");
}
ATF_TC_BODY(dynamic_defabuse_lazy, tc)
{
	tls_extern("libh_def_dynamic.so", "libh_abuse_dynamic.so",
	    DEF_USE_LAZY);
}

ATF_TC(dynamic_defuse_eager);
ATF_TC_HEAD(dynamic_defuse_eager, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
	    " loading def then use eagerly");
}
ATF_TC_BODY(dynamic_defuse_eager, tc)
{
	tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
	    DEF_USE_EAGER);
}

ATF_TC(dynamic_defuse_lazy);
ATF_TC_HEAD(dynamic_defuse_lazy, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
	    " loading def then use lazyly");
}
ATF_TC_BODY(dynamic_defuse_lazy, tc)
{
	tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
	    DEF_USE_LAZY);
}

ATF_TC(dynamic_usedef);
ATF_TC_HEAD(dynamic_usedef, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
	    " loading use then def");
}
ATF_TC_BODY(dynamic_usedef, tc)
{
	tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
	    USE_DEF);
}

ATF_TC(dynamic_usedefnoload);
ATF_TC_HEAD(dynamic_usedefnoload, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for dynamic TLS works,"
	    " loading use then def with RTLD_NOLOAD");
}
ATF_TC_BODY(dynamic_usedefnoload, tc)
{
	tls_extern("libh_def_dynamic.so", "libh_use_dynamic.so",
	    USE_DEF_NOLOAD);
}

ATF_TC(static_abusedef);
ATF_TC_HEAD(static_abusedef, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
	    " loading dynamic use then static def");
}
ATF_TC_BODY(static_abusedef, tc)
{
	tls_extern("libh_def_static.so", "libh_abuse_static.so", USE_DEF);
}

ATF_TC(static_abusedefnoload);
ATF_TC_HEAD(static_abusedefnoload, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
	    " loading dynamic use then static def with RTLD_NOLOAD");
}
ATF_TC_BODY(static_abusedefnoload, tc)
{
	tls_extern("libh_def_static.so", "libh_abuse_static.so",
	    USE_DEF_NOLOAD);
}

ATF_TC(static_defabuse_eager);
ATF_TC_HEAD(static_defabuse_eager, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
	    " loading static def then dynamic use eagerly");
}
ATF_TC_BODY(static_defabuse_eager, tc)
{
	tls_extern("libh_def_static.so", "libh_abuse_static.so",
	    DEF_USE_EAGER);
}

ATF_TC(static_defabuse_lazy);
ATF_TC_HEAD(static_defabuse_lazy, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
	    " loading static def then dynamic use lazyly");
}
ATF_TC_BODY(static_defabuse_lazy, tc)
{
	tls_extern("libh_def_static.so", "libh_abuse_static.so",
	    DEF_USE_LAZY);
}

ATF_TC(static_defuse_eager);
ATF_TC_HEAD(static_defuse_eager, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
	    " loading def then use eagerly");
}
ATF_TC_BODY(static_defuse_eager, tc)
{
	tls_extern("libh_def_static.so", "libh_use_static.so",
	    DEF_USE_EAGER);
}

ATF_TC(static_defuse_lazy);
ATF_TC_HEAD(static_defuse_lazy, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
	    " loading def then use lazyly");
}
ATF_TC_BODY(static_defuse_lazy, tc)
{
	tls_extern("libh_def_static.so", "libh_use_static.so",
	    DEF_USE_LAZY);
}

ATF_TC(static_usedef);
ATF_TC_HEAD(static_usedef, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
	    " loading use then def");
}
ATF_TC_BODY(static_usedef, tc)
{
	tls_extern("libh_def_static.so", "libh_use_static.so",
	    USE_DEF);
}

ATF_TC(static_usedefnoload);
ATF_TC_HEAD(static_usedefnoload, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for static TLS works,"
	    " loading use then def with RTLD_NOLOAD");
}
ATF_TC_BODY(static_usedefnoload, tc)
{
	tls_extern("libh_def_static.so", "libh_use_static.so",
	    USE_DEF_NOLOAD);
}

ATF_TC(onlydef_dynamic_static_ctor);
ATF_TC_HEAD(onlydef_dynamic_static_ctor, tc)
{
	atf_tc_set_md_var(tc, "descr", "definition-only library,"
	    " dynamic load and use in ctor, then static load fails");
}
ATF_TC_BODY(onlydef_dynamic_static_ctor, tc)
{

	ATF_REQUIRE_DL(dlopen("libh_onlydef.so", 0));
	ATF_REQUIRE_DL(dlopen("libh_onlyctor_dynamic.so", 0));
	ATF_CHECK_EQ_MSG(NULL, dlopen("libh_onlyuse_static.so", 0),
	    "dlopen failed to detect dynamic-then-static abuse");
}

ATF_TC(onlydef_dynamic_static_eager);
ATF_TC_HEAD(onlydef_dynamic_static_eager, tc)
{
	atf_tc_set_md_var(tc, "descr", "definition-only library,"
	    " dynamic load and use, then static load fails");
}
ATF_TC_BODY(onlydef_dynamic_static_eager, tc)
{
	void *use_dynamic;
	int *(*fdynamic)(void);

	ATF_REQUIRE_DL(use_dynamic = dlopen("libh_onlyuse_dynamic.so", 0));
	ATF_REQUIRE_DL(fdynamic = dlsym(use_dynamic, "fdynamic"));
	(void)(*fdynamic)();
	ATF_CHECK_EQ_MSG(NULL, dlopen("libh_onlyuse_static.so", 0),
	    "dlopen failed to detect dynamic-then-static abuse");
}

ATF_TC(onlydef_dynamic_static_lazy);
ATF_TC_HEAD(onlydef_dynamic_static_lazy, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
	    " with definition-only library, dynamic and static load and use");
}
ATF_TC_BODY(onlydef_dynamic_static_lazy, tc)
{
	void *use_dynamic, *use_static;
	int *(*fdynamic)(void), *(*fstatic)(void);
	int *pdynamic, *pstatic;

	ATF_REQUIRE_DL(use_dynamic = dlopen("libh_onlyuse_dynamic.so", 0));
	ATF_REQUIRE_DL(use_static = dlopen("libh_onlyuse_static.so", 0));
	ATF_REQUIRE_DL(fdynamic = dlsym(use_dynamic, "fdynamic"));
	ATF_REQUIRE_DL(fstatic = dlsym(use_static, "fstatic"));
	pdynamic = (*fdynamic)();
	pstatic = (*fstatic)();
	ATF_CHECK_EQ_MSG(pdynamic, pstatic,
	    "%p in dynamic tls user != %p in static tls user",
	    pdynamic, pstatic);
}

ATF_TC(onlydef_static_dynamic_eager);
ATF_TC_HEAD(onlydef_static_dynamic_eager, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
	    " with definition-only library,"
	    " static load and use, then dynamic load and use");
}
ATF_TC_BODY(onlydef_static_dynamic_eager, tc)
{
	void *use_static, *use_dynamic;
	int *(*fstatic)(void), *(*fdynamic)(void);
	int *pstatic, *pdynamic;

	ATF_REQUIRE_DL(dlopen("libh_onlydef.so", 0));
	ATF_REQUIRE_DL(use_static = dlopen("libh_onlyuse_static.so", 0));
	ATF_REQUIRE_DL(fstatic = dlsym(use_static, "fstatic"));
	pstatic = (*fstatic)();
	ATF_REQUIRE_DL(use_dynamic = dlopen("libh_onlyuse_dynamic.so", 0));
	ATF_REQUIRE_DL(fdynamic = dlsym(use_dynamic, "fdynamic"));
	pdynamic = (*fdynamic)();
	ATF_CHECK_EQ_MSG(pstatic, pdynamic,
	    "%p in static tls user != %p in dynamic tls user",
	    pstatic, pdynamic);
}

ATF_TC(onlydef_static_dynamic_lazy);
ATF_TC_HEAD(onlydef_static_dynamic_lazy, tc)
{
	atf_tc_set_md_var(tc, "descr", "extern __thread for TLS works,"
	    " with definition-only library, static and dynamic load and use");
}
ATF_TC_BODY(onlydef_static_dynamic_lazy, tc)
{
	void *use_static, *use_dynamic;
	int *(*fstatic)(void), *(*fdynamic)(void);
	int *pstatic, *pdynamic;

	ATF_REQUIRE_DL(dlopen("libh_onlydef.so", 0));
	ATF_REQUIRE_DL(use_static = dlopen("libh_onlyuse_static.so", 0));
	ATF_REQUIRE_DL(use_dynamic = dlopen("libh_onlyuse_dynamic.so", 0));
	ATF_REQUIRE_DL(fstatic = dlsym(use_static, "fstatic"));
	ATF_REQUIRE_DL(fdynamic = dlsym(use_dynamic, "fdynamic"));
	pstatic = (*fstatic)();
	pdynamic = (*fdynamic)();
	ATF_CHECK_EQ_MSG(pstatic, pdynamic,
	    "%p in static tls user != %p in dynamic tls user",
	    pstatic, pdynamic);
}

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, dynamic_abusedef);
	ATF_TP_ADD_TC(tp, dynamic_abusedefnoload);
	ATF_TP_ADD_TC(tp, dynamic_defabuse_eager);
	ATF_TP_ADD_TC(tp, dynamic_defabuse_lazy);
	ATF_TP_ADD_TC(tp, dynamic_defuse_eager);
	ATF_TP_ADD_TC(tp, dynamic_defuse_lazy);
	ATF_TP_ADD_TC(tp, dynamic_usedef);
	ATF_TP_ADD_TC(tp, dynamic_usedefnoload);
	ATF_TP_ADD_TC(tp, onlydef_dynamic_static_ctor);
	ATF_TP_ADD_TC(tp, onlydef_dynamic_static_eager);
	ATF_TP_ADD_TC(tp, onlydef_dynamic_static_lazy);
	ATF_TP_ADD_TC(tp, onlydef_static_dynamic_eager);
	ATF_TP_ADD_TC(tp, onlydef_static_dynamic_lazy);
	ATF_TP_ADD_TC(tp, static_abusedef);
	ATF_TP_ADD_TC(tp, static_abusedefnoload);
	ATF_TP_ADD_TC(tp, static_defabuse_eager);
	ATF_TP_ADD_TC(tp, static_defabuse_lazy);
	ATF_TP_ADD_TC(tp, static_defuse_eager);
	ATF_TP_ADD_TC(tp, static_defuse_lazy);
	ATF_TP_ADD_TC(tp, static_usedef);
	ATF_TP_ADD_TC(tp, static_usedefnoload);
	return atf_no_error();
}