blob: 0a97c281d41e3be92bdb882046c633a93deab2e8 [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 <dlfcn.h>
30#include <gtest/gtest.h>
31
32#include <thread>
33
34#include "gtest_globals.h"
35#include "utils.h"
36
37// Access libtest_elftls_shared_var.so's TLS variable using an IE access.
38__attribute__((tls_model("initial-exec"))) extern "C" __thread int elftls_shared_var;
39
40TEST(elftls_dl, dlopen_shared_var_ie) {
41 // libtest_elftls_shared_var_ie.so can be dlopen'ed, even though it contains a
42 // TLS IE access, because its IE access references a TLS variable from
43 // libtest_elftls_shared_var.so, which is DT_NEEDED by the executable. This
44 // pattern appears in sanitizers, which use TLS IE instrumentation in shared
45 // objects to access special variables exported from the executable or from a
46 // preloaded solib.
47 void* lib = dlopen("libtest_elftls_shared_var_ie.so", RTLD_LOCAL | RTLD_NOW);
48 ASSERT_NE(nullptr, lib);
49
50 auto bump_shared_var = reinterpret_cast<int(*)()>(dlsym(lib, "bump_shared_var"));
51 ASSERT_NE(nullptr, bump_shared_var);
52
53 ASSERT_EQ(21, ++elftls_shared_var);
54 ASSERT_EQ(22, bump_shared_var());
55
56 std::thread([bump_shared_var] {
57 ASSERT_EQ(21, ++elftls_shared_var);
58 ASSERT_EQ(22, bump_shared_var());
59 }).join();
60}
61
62TEST(elftls_dl, dlopen_ie_error) {
63 std::string helper = GetTestlibRoot() +
64 "/elftls_dlopen_ie_error_helper/elftls_dlopen_ie_error_helper";
65 std::string src_path = GetTestlibRoot() + "/libtest_elftls_shared_var_ie.so";
66 std::string dst_path = GetTestlibRoot() + "/libtest_elftls_shared_var.so";
67#if defined(__BIONIC__)
68 std::string error =
69 "dlerror: dlopen failed: TLS symbol \"elftls_shared_var\" in dlopened \"" + dst_path + "\" " +
70 "referenced from \"" + src_path + "\" using IE access model\n";
71#else
72 // glibc will reserve some surplus static TLS memory, allowing this test to pass.
73 std::string error = "success\n";
74#endif
75
76 chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
77 ExecTestHelper eth;
78 eth.SetArgs({ helper.c_str(), nullptr });
79 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, error.c_str());
80}