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