blob: 2d83d7080428f10358b187ecb1ec3d9a39d39db0 [file] [log] [blame]
Ryan Pricharde4ee12f2019-01-15 20:35:00 -08001/*
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#include <gtest/gtest.h>
30
31#include <thread>
32
33#include "private/__get_tls.h"
34
35// Specify the LE access model explicitly. This file is compiled into the
36// bionic-unit-tests executable, but the compiler sees an -fpic object file
37// output into a static library, so it defaults to dynamic TLS accesses.
38
39// This variable will be zero-initialized (.tbss)
40__attribute__((tls_model("local-exec"))) static __thread int tlsvar_le_zero;
41
42// This variable will have an initializer (.tdata)
43__attribute__((tls_model("local-exec"))) static __thread int tlsvar_le_init = 10;
44
45// Access libtest_elftls_shared_var's TLS variable using an IE access.
46__attribute__((tls_model("initial-exec"))) extern "C" __thread int elftls_shared_var;
47
48TEST(elftls, basic_le) {
49 // Check the variables on the main thread.
50 ASSERT_EQ(11, ++tlsvar_le_init);
51 ASSERT_EQ(1, ++tlsvar_le_zero);
52
53 // Check variables on a new thread.
54 std::thread([] {
55 ASSERT_EQ(11, ++tlsvar_le_init);
56 ASSERT_EQ(1, ++tlsvar_le_zero);
57 }).join();
58}
59
60TEST(elftls, shared_ie) {
61 ASSERT_EQ(21, ++elftls_shared_var);
62 std::thread([] {
63 ASSERT_EQ(21, ++elftls_shared_var);
64 }).join();
65}
66
67extern "C" int* missing_weak_tls_addr();
68extern "C" int bump_static_tls_var_1();
69extern "C" int bump_static_tls_var_2();
70
71TEST(elftls, tprel_missing_weak) {
72 ASSERT_EQ(static_cast<void*>(__get_tls()), missing_weak_tls_addr());
73 std::thread([] {
74 ASSERT_EQ(static_cast<void*>(__get_tls()), missing_weak_tls_addr());
75 }).join();
76}
77
78TEST(elftls, tprel_addend) {
79 ASSERT_EQ(4, bump_static_tls_var_1());
80 ASSERT_EQ(8, bump_static_tls_var_2());
81 std::thread([] {
82 ASSERT_EQ(4, bump_static_tls_var_1());
83 ASSERT_EQ(8, bump_static_tls_var_2());
84 }).join();
85}
Ryan Prichard06d2d792019-01-23 23:19:19 -080086
87// Because this C++ source file is built with -fpic, the compiler will access
88// this variable using a GD model. Typically, the static linker will relax the
89// GD to LE, but the arm32 linker doesn't do TLS relaxations, so we can test
90// calling __tls_get_addr in a static executable. The static linker knows that
91// the main executable's TlsIndex::module_id is 1 and writes that into the GOT.
92__thread int tlsvar_general = 30;
93
94TEST(elftls, general) {
95 ASSERT_EQ(31, ++tlsvar_general);
96 std::thread([] {
97 ASSERT_EQ(31, ++tlsvar_general);
98 }).join();
99}