blob: 3f8d11885ea90821b0e25cf3f4e7a3a6fa00e639 [file] [log] [blame]
Vy Nguyend5007512020-07-14 17:37:04 -04001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29// Prevent tests from being compiled with glibc because thread_properties.h
30// only exists in Bionic.
31#if defined(__BIONIC__)
32
33#include <sys/thread_properties.h>
34
Vy Nguyen97ba12b2020-08-24 22:31:15 -040035#include <assert.h>
Vy Nguyend5007512020-07-14 17:37:04 -040036#include <dlfcn.h>
37#include <stdio.h>
38#include <unistd.h> // for gettid
39
40// Helper binary to use TLS-related functions in thread_properties
41
42// Tests __get_static_tls_bound.
Vy Nguyen97ba12b2020-08-24 22:31:15 -040043thread_local int local_var;
Vy Nguyend5007512020-07-14 17:37:04 -040044void test_static_tls_bounds() {
Vy Nguyen97ba12b2020-08-24 22:31:15 -040045 local_var = 123;
46 void* start_addr = nullptr;
47 void* end_addr = nullptr;
Vy Nguyend5007512020-07-14 17:37:04 -040048
49 __libc_get_static_tls_bounds(reinterpret_cast<void**>(&start_addr),
50 reinterpret_cast<void**>(&end_addr));
Vy Nguyen97ba12b2020-08-24 22:31:15 -040051 assert(start_addr != nullptr);
52 assert(end_addr != nullptr);
53
54 assert(&local_var >= start_addr && &local_var < end_addr);
55
Vy Nguyend5007512020-07-14 17:37:04 -040056 printf("done_get_static_tls_bounds\n");
57}
58
59// Tests iterate_dynamic tls chunks.
60// Export a var from the shared so.
61__thread char large_tls_var[4 * 1024 * 1024];
62void test_iter_tls() {
63 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
64
65 int i = 0;
66 auto cb = [&](void* dtls_begin, void* dtls_end, size_t dso_id, void* arg) {
67 printf("iterate_cb i = %d\n", i++);
68 };
69 __libc_iterate_dynamic_tls(gettid(), cb, nullptr);
70 printf("done_iterate_dynamic_tls\n");
71}
72
73int main() {
74 test_static_tls_bounds();
75 test_iter_tls();
76 return 0;
77}
78
79#else
80int main() {
81 return 0;
82}
83#endif // __BIONIC__