blob: 5982de4632ff6d9b272066bdbf0ff8ce5669e6e9 [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>
Vy Nguyen19f84862020-09-23 21:43:31 -040037#include <elf.h>
38#include <err.h>
39#include <errno.h>
40#include <fcntl.h>
41#include <sched.h>
Vy Nguyend5007512020-07-14 17:37:04 -040042#include <stdio.h>
Vy Nguyen19f84862020-09-23 21:43:31 -040043#include <string.h>
44#include <sys/prctl.h>
45#include <sys/ptrace.h>
46#include <sys/uio.h>
47#include <sys/user.h>
48#include <sys/wait.h>
49#include <unistd.h>
Vy Nguyend5007512020-07-14 17:37:04 -040050
51// Helper binary to use TLS-related functions in thread_properties
52
53// Tests __get_static_tls_bound.
Vy Nguyen97ba12b2020-08-24 22:31:15 -040054thread_local int local_var;
Vy Nguyend5007512020-07-14 17:37:04 -040055void test_static_tls_bounds() {
Vy Nguyen97ba12b2020-08-24 22:31:15 -040056 local_var = 123;
57 void* start_addr = nullptr;
58 void* end_addr = nullptr;
Vy Nguyend5007512020-07-14 17:37:04 -040059
60 __libc_get_static_tls_bounds(reinterpret_cast<void**>(&start_addr),
61 reinterpret_cast<void**>(&end_addr));
Vy Nguyen97ba12b2020-08-24 22:31:15 -040062 assert(start_addr != nullptr);
63 assert(end_addr != nullptr);
64
65 assert(&local_var >= start_addr && &local_var < end_addr);
66
Vy Nguyend5007512020-07-14 17:37:04 -040067 printf("done_get_static_tls_bounds\n");
68}
69
70// Tests iterate_dynamic tls chunks.
71// Export a var from the shared so.
72__thread char large_tls_var[4 * 1024 * 1024];
Vy Nguyen19f84862020-09-23 21:43:31 -040073// found_count has to be Global variable so that the non-capturing lambda
74// can access it.
75int found_count = 0;
Vy Nguyend5007512020-07-14 17:37:04 -040076void test_iter_tls() {
77 void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
Vy Nguyen19f84862020-09-23 21:43:31 -040078 large_tls_var[1025] = 'a';
79 auto cb = +[](void* dtls_begin, void* dtls_end, size_t dso_id, void* arg) {
80 if (&large_tls_var >= dtls_begin && &large_tls_var < dtls_end) ++found_count;
Vy Nguyend5007512020-07-14 17:37:04 -040081 };
82 __libc_iterate_dynamic_tls(gettid(), cb, nullptr);
Vy Nguyen19f84862020-09-23 21:43:31 -040083
84 // It should be found exactly once.
85 assert(found_count == 1);
Vy Nguyend5007512020-07-14 17:37:04 -040086 printf("done_iterate_dynamic_tls\n");
87}
88
Vy Nguyen19f84862020-09-23 21:43:31 -040089void* parent_addr = nullptr;
90void test_iterate_another_thread_tls() {
91 large_tls_var[1025] = 'b';
92 parent_addr = &large_tls_var;
93 found_count = 0;
94
95 pid_t pid = fork();
96 assert(pid != -1);
97 int status;
98 if (pid) {
99 // Parent.
100 assert(pid == wait(&status));
101 assert(0 == status);
102 } else {
103 // Child.
104 pid_t parent_pid = getppid();
105 assert(0 == ptrace(PTRACE_ATTACH, parent_pid));
106 assert(parent_pid == waitpid(parent_pid, &status, 0));
107
108 auto cb = +[](void* dtls_begin, void* dtls_end, size_t dso_id, void* arg) {
109 if (parent_addr >= dtls_begin && parent_addr < dtls_end) ++found_count;
110 };
111 __libc_iterate_dynamic_tls(parent_pid, cb, nullptr);
112 // It should be found exactly once.
113 assert(found_count == 1);
114 printf("done_iterate_another_thread_tls\n");
115 }
116}
Vy Nguyend5007512020-07-14 17:37:04 -0400117int main() {
118 test_static_tls_bounds();
119 test_iter_tls();
Vy Nguyen19f84862020-09-23 21:43:31 -0400120 test_iterate_another_thread_tls();
Vy Nguyend5007512020-07-14 17:37:04 -0400121 return 0;
122}
123
124#else
125int main() {
126 return 0;
127}
128#endif // __BIONIC__