blob: b3f511e03aafa95969412fd36a1eab1bef29962d [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
Ryan Prichard43963922024-03-14 16:51:27 -070033#include "gtest_globals.h"
34#include "utils.h"
35
Ryan Pricharde4ee12f2019-01-15 20:35:00 -080036// Specify the LE access model explicitly. This file is compiled into the
37// bionic-unit-tests executable, but the compiler sees an -fpic object file
38// output into a static library, so it defaults to dynamic TLS accesses.
39
40// This variable will be zero-initialized (.tbss)
41__attribute__((tls_model("local-exec"))) static __thread int tlsvar_le_zero;
42
43// This variable will have an initializer (.tdata)
44__attribute__((tls_model("local-exec"))) static __thread int tlsvar_le_init = 10;
45
46// Access libtest_elftls_shared_var's TLS variable using an IE access.
47__attribute__((tls_model("initial-exec"))) extern "C" __thread int elftls_shared_var;
48
49TEST(elftls, basic_le) {
50 // Check the variables on the main thread.
51 ASSERT_EQ(11, ++tlsvar_le_init);
52 ASSERT_EQ(1, ++tlsvar_le_zero);
53
54 // Check variables on a new thread.
55 std::thread([] {
56 ASSERT_EQ(11, ++tlsvar_le_init);
57 ASSERT_EQ(1, ++tlsvar_le_zero);
58 }).join();
59}
60
61TEST(elftls, shared_ie) {
62 ASSERT_EQ(21, ++elftls_shared_var);
63 std::thread([] {
64 ASSERT_EQ(21, ++elftls_shared_var);
65 }).join();
66}
67
Ryan Pricharde4ee12f2019-01-15 20:35:00 -080068extern "C" int bump_static_tls_var_1();
69extern "C" int bump_static_tls_var_2();
70
Ryan Pricharde4ee12f2019-01-15 20:35:00 -080071TEST(elftls, tprel_addend) {
72 ASSERT_EQ(4, bump_static_tls_var_1());
73 ASSERT_EQ(8, bump_static_tls_var_2());
74 std::thread([] {
75 ASSERT_EQ(4, bump_static_tls_var_1());
76 ASSERT_EQ(8, bump_static_tls_var_2());
77 }).join();
78}
Ryan Prichard06d2d792019-01-23 23:19:19 -080079
80// Because this C++ source file is built with -fpic, the compiler will access
81// this variable using a GD model. Typically, the static linker will relax the
82// GD to LE, but the arm32 linker doesn't do TLS relaxations, so we can test
83// calling __tls_get_addr in a static executable. The static linker knows that
84// the main executable's TlsIndex::module_id is 1 and writes that into the GOT.
85__thread int tlsvar_general = 30;
86
87TEST(elftls, general) {
88 ASSERT_EQ(31, ++tlsvar_general);
89 std::thread([] {
90 ASSERT_EQ(31, ++tlsvar_general);
91 }).join();
92}
Ryan Prichard43963922024-03-14 16:51:27 -070093
94TEST(elftls, align_test) {
95 std::string helper = GetTestLibRoot() + "/elftls_align_test_helper";
96 ExecTestHelper eth;
97 eth.SetArgs({helper.c_str(), nullptr});
98 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
99}
100
101TEST(elftls, skew_align_test) {
102 std::string helper = GetTestLibRoot() + "/elftls_skew_align_test_helper";
103 ExecTestHelper eth;
104 eth.SetArgs({helper.c_str(), nullptr});
105 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
106}