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> |
| 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 Nguyen | 97ba12b | 2020-08-24 22:31:15 -0400 | [diff] [blame] | 43 | thread_local int local_var; |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 44 | void test_static_tls_bounds() { |
Vy Nguyen | 97ba12b | 2020-08-24 22:31:15 -0400 | [diff] [blame] | 45 | local_var = 123; |
| 46 | void* start_addr = nullptr; |
| 47 | void* end_addr = nullptr; |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 48 | |
| 49 | __libc_get_static_tls_bounds(reinterpret_cast<void**>(&start_addr), |
| 50 | reinterpret_cast<void**>(&end_addr)); |
Vy Nguyen | 97ba12b | 2020-08-24 22:31:15 -0400 | [diff] [blame] | 51 | assert(start_addr != nullptr); |
| 52 | assert(end_addr != nullptr); |
| 53 | |
| 54 | assert(&local_var >= start_addr && &local_var < end_addr); |
| 55 | |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 56 | 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]; |
| 62 | void 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 | |
| 73 | int main() { |
| 74 | test_static_tls_bounds(); |
| 75 | test_iter_tls(); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | #else |
| 80 | int main() { |
| 81 | return 0; |
| 82 | } |
| 83 | #endif // __BIONIC__ |