Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 1 | /* |
| 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 Nguyen | 97ba12b | 2020-08-24 22:31:15 -0400 | [diff] [blame] | 35 | #include <assert.h> |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 36 | #include <dlfcn.h> |
Vy Nguyen | 19f8486 | 2020-09-23 21:43:31 -0400 | [diff] [blame^] | 37 | #include <elf.h> |
| 38 | #include <err.h> |
| 39 | #include <errno.h> |
| 40 | #include <fcntl.h> |
| 41 | #include <sched.h> |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 42 | #include <stdio.h> |
Vy Nguyen | 19f8486 | 2020-09-23 21:43:31 -0400 | [diff] [blame^] | 43 | #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 Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 50 | |
| 51 | // Helper binary to use TLS-related functions in thread_properties |
| 52 | |
| 53 | // Tests __get_static_tls_bound. |
Vy Nguyen | 97ba12b | 2020-08-24 22:31:15 -0400 | [diff] [blame] | 54 | thread_local int local_var; |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 55 | void test_static_tls_bounds() { |
Vy Nguyen | 97ba12b | 2020-08-24 22:31:15 -0400 | [diff] [blame] | 56 | local_var = 123; |
| 57 | void* start_addr = nullptr; |
| 58 | void* end_addr = nullptr; |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 59 | |
| 60 | __libc_get_static_tls_bounds(reinterpret_cast<void**>(&start_addr), |
| 61 | reinterpret_cast<void**>(&end_addr)); |
Vy Nguyen | 97ba12b | 2020-08-24 22:31:15 -0400 | [diff] [blame] | 62 | assert(start_addr != nullptr); |
| 63 | assert(end_addr != nullptr); |
| 64 | |
| 65 | assert(&local_var >= start_addr && &local_var < end_addr); |
| 66 | |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 67 | 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 Nguyen | 19f8486 | 2020-09-23 21:43:31 -0400 | [diff] [blame^] | 73 | // found_count has to be Global variable so that the non-capturing lambda |
| 74 | // can access it. |
| 75 | int found_count = 0; |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 76 | void test_iter_tls() { |
| 77 | void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW); |
Vy Nguyen | 19f8486 | 2020-09-23 21:43:31 -0400 | [diff] [blame^] | 78 | 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 Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 81 | }; |
| 82 | __libc_iterate_dynamic_tls(gettid(), cb, nullptr); |
Vy Nguyen | 19f8486 | 2020-09-23 21:43:31 -0400 | [diff] [blame^] | 83 | |
| 84 | // It should be found exactly once. |
| 85 | assert(found_count == 1); |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 86 | printf("done_iterate_dynamic_tls\n"); |
| 87 | } |
| 88 | |
Vy Nguyen | 19f8486 | 2020-09-23 21:43:31 -0400 | [diff] [blame^] | 89 | void* parent_addr = nullptr; |
| 90 | void 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 Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 117 | int main() { |
| 118 | test_static_tls_bounds(); |
| 119 | test_iter_tls(); |
Vy Nguyen | 19f8486 | 2020-09-23 21:43:31 -0400 | [diff] [blame^] | 120 | test_iterate_another_thread_tls(); |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | #else |
| 125 | int main() { |
| 126 | return 0; |
| 127 | } |
| 128 | #endif // __BIONIC__ |