// Access libtest_elftls_shared_var.so's TLS variable using an IE access.
__attribute__((tls_model("initial-exec"))) extern"C" __thread int elftls_shared_var;
TEST(elftls_dl, dlopen_shared_var_ie) { // libtest_elftls_shared_var_ie.so can be dlopen'ed, even though it contains a // TLS IE access, because its IE access references a TLS variable from // libtest_elftls_shared_var.so, which is DT_NEEDED by the executable. This // pattern appears in sanitizers, which use TLS IE instrumentation in shared // objects to access special variables exported from the executable or from a // preloaded solib. void* lib = dlopen("libtest_elftls_shared_var_ie.so", RTLD_LOCAL | RTLD_NOW);
ASSERT_NE(nullptr, lib);
auto bump_shared_var = reinterpret_cast<int(*)()>(dlsym(lib, "bump_shared_var"));
ASSERT_NE(nullptr, bump_shared_var);
// Use a GD access (__tls_get_addr or TLSDESC) to modify a variable in static // TLS memory.
TEST(elftls_dl, access_static_tls) { void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
ASSERT_NE(nullptr, lib);
auto bump_shared_var = reinterpret_cast<int(*)()>(dlsym(lib, "bump_shared_var"));
ASSERT_NE(nullptr, bump_shared_var);
// The Bionic linker resolves a TPREL relocation to an unresolved weak TLS // symbol to 0, which is added to the thread pointer. N.B.: A TPREL relocation // in a static executable is resolved by the static linker instead, and static // linker behavior varies (especially with bfd and gold). See // https://bugs.llvm.org/show_bug.cgi?id=40570.
TEST(elftls_dl, tprel_missing_weak) {
ASSERT_EQ(static_cast<void*>(__get_tls()), missing_weak_tls_addr());
std::thread([] {
ASSERT_EQ(static_cast<void*>(__get_tls()), missing_weak_tls_addr());
}).join();
}
// The behavior of accessing an unresolved weak TLS symbol using a dynamic TLS // relocation depends on which kind of implementation the target uses. With // TLSDESC, the result is NULL. With __tls_get_addr, the result is the // generation count (or maybe undefined behavior)? This test only tests TLSDESC.
TEST(elftls_dl, tlsdesc_missing_weak) { #ifdefined(__aarch64__) || defined(__riscv) void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
ASSERT_NE(nullptr, lib);
auto missing_weak_dyn_tls_addr = reinterpret_cast<int*(*)()>(dlsym(lib, "missing_weak_dyn_tls_addr"));
ASSERT_NE(nullptr, missing_weak_dyn_tls_addr);
ASSERT_EQ(nullptr, missing_weak_dyn_tls_addr());
std::thread([missing_weak_dyn_tls_addr] {
ASSERT_EQ(nullptr, missing_weak_dyn_tls_addr());
}).join(); #else
GTEST_SKIP() << "This test is only run on TLSDESC-based targets"; #endif
}
// Verify that variables are reset to their initial values after the library // containing them is closed.
TEST(elftls_dl, dlclose_resets_values) { for (int round = 0; round < 2; ++round) { void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
ASSERT_NE(nullptr, lib);
auto bump_local_vars = reinterpret_cast<int(*)()>(dlsym(lib, "bump_local_vars"));
ASSERT_NE(nullptr, bump_local_vars);
// Calling dlclose should remove the entry for the solib from the global list of // ELF TLS modules. Test that repeatedly loading and unloading a library doesn't // increase the DTV size.
TEST(elftls_dl, dlclose_removes_entry) { #ifdefined(__BIONIC__) auto dtv = []() -> TlsDtv* { return __get_tcb_dtv(__get_bionic_tcb()); };
bool first = true;
size_t count = 0;
// Use a large number of rounds in case the DTV is initially larger than // expected. for (int round = 0; round < 32; ++round) { void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
ASSERT_NE(nullptr, lib);
auto bump_local_vars = reinterpret_cast<int(*)()>(dlsym(lib, "bump_local_vars"));
ASSERT_NE(nullptr, bump_local_vars);
ASSERT_EQ(42, bump_local_vars()); if (first) {
first = false;
count = dtv()->count;
} else {
ASSERT_EQ(count, dtv()->count);
}
// Use dlsym to get the address of a TLS variable in static TLS and compare it // against the ordinary address of the variable.
TEST(elftls_dl, dlsym_static_tls) { void* lib = dlopen("libtest_elftls_shared_var.so", RTLD_LOCAL | RTLD_NOW);
ASSERT_NE(nullptr, lib);
// Use dlsym to get the address of a TLS variable in dynamic TLS and compare it // against the ordinary address of the variable.
TEST(elftls_dl, dlsym_dynamic_tls) { void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
ASSERT_NE(nullptr, lib); auto get_var_addr = reinterpret_cast<int*(*)()>(dlsym(lib, "get_large_tls_var_addr"));
ASSERT_NE(nullptr, get_var_addr);
// Verify that dladdr does not misinterpret a TLS symbol's value as a virtual // address.
TEST(elftls_dl, dladdr_skip_tls_symbol) { void* lib = dlopen("libtest_elftls_dynamic.so", RTLD_LOCAL | RTLD_NOW);
auto get_tls_info = []() { auto callback = [](dl_phdr_info* info, size_t, void* data) {
TlsInfo& tls_info = *static_cast<TlsInfo*>(data);
// This test is also run with glibc, where dlpi_name may have relative path components, so // examine just the basename when searching for the library. if (strcmp(android::base::Basename(info->dlpi_name).c_str(), "libtest_elftls_dynamic.so") != 0) return0;
tls_info.found = true;
tls_info.modid = info->dlpi_tls_modid;
tls_info.data = info->dlpi_tls_data; for (ElfW(Half) i = 0; i < info->dlpi_phnum; ++i) { if (info->dlpi_phdr[i].p_type == PT_TLS) {
tls_info.memsz = info->dlpi_phdr[i].p_memsz;
}
}
EXPECT_NE(static_cast<size_t>(0), tls_info.memsz); return1;
};
TlsInfo result {};
dl_iterate_phdr(callback, &result); return result;
};
// The executable has a TLS segment, so it will use module ID #1, and the DSO's ID will be larger // than 1. Initially, the data field is nullptr, because this thread's instance hasn't been // allocated yet.
TlsInfo tls_info = get_tls_info();
ASSERT_TRUE(tls_info.found);
ASSERT_GT(tls_info.modid, static_cast<size_t>(1));
ASSERT_EQ(nullptr, tls_info.data);
void* var_addr = get_var_addr();
// Verify that dl_iterate_phdr returns a range of memory covering the allocated TLS variable.
tls_info = get_tls_info();
ASSERT_TRUE(tls_info.found);
ASSERT_GE(var_addr, tls_info.data);
ASSERT_LT(var_addr, static_cast<char*>(tls_info.data) + tls_info.memsz);
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.19 Sekunden
(vorverarbeitet am 2026-06-28)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.