Fix race in ld.config.txt tests
The tests can run in parallel, and if they do, one test can truncate
ld.config.txt while another test is reading it (via the loader). Fix the
issue by using a TemporaryFile as the LD_CONFIG_FILE.
Test: adb shell /data/nativetest64/bionic-unit-tests/bionic-unit-tests --gtest_filter=dl.exec_with_ld_config_file:dl.exec_with_ld_config_file_with_ld_preload:dl.disable_ld_config_file --gtest_repeat=1000
Bug: b/77555708
Change-Id: I9763caa076ece30d1a0eb9c8892a310ac51543b6
(cherry picked from commit 6c3f97d2360ba1a2b2be160003fa3b4dd7b33ae1)
diff --git a/tests/dl_test.cpp b/tests/dl_test.cpp
index 7444e3a..38d7783 100644
--- a/tests/dl_test.cpp
+++ b/tests/dl_test.cpp
@@ -31,6 +31,7 @@
#include <fstream>
#include "gtest_globals.h"
+#include "TemporaryFile.h"
#include "utils.h"
extern "C" int main_global_default_serial() {
@@ -167,8 +168,8 @@
}
#if defined(__BIONIC__)
-static void create_ld_config_file(std::string& config_file) {
- std::ofstream fout(config_file.c_str(), std::ios::out);
+static void create_ld_config_file(const char* config_file) {
+ std::ofstream fout(config_file, std::ios::out);
fout << "dir.test = " << get_testlib_root() << "/ld_config_test_helper/" << std::endl
<< "[test]" << std::endl
<< "additional.namespaces = ns2" << std::endl
@@ -196,9 +197,9 @@
}
std::string helper = get_testlib_root() +
"/ld_config_test_helper/ld_config_test_helper";
- std::string config_file = get_testlib_root() + "/ld.config.txt";
- create_ld_config_file(config_file);
- std::string env = std::string("LD_CONFIG_FILE=") + config_file;
+ TemporaryFile config_file;
+ create_ld_config_file(config_file.filename);
+ std::string env = std::string("LD_CONFIG_FILE=") + config_file.filename;
chmod(helper.c_str(), 0755);
ExecTestHelper eth;
eth.SetArgs({ helper.c_str(), nullptr });
@@ -218,9 +219,9 @@
}
std::string helper = get_testlib_root() +
"/ld_config_test_helper/ld_config_test_helper";
- std::string config_file = get_testlib_root() + "/ld.config.txt";
- create_ld_config_file(config_file);
- std::string env = std::string("LD_CONFIG_FILE=") + config_file;
+ TemporaryFile config_file;
+ create_ld_config_file(config_file.filename);
+ std::string env = std::string("LD_CONFIG_FILE=") + config_file.filename;
std::string env2 = std::string("LD_PRELOAD=") + get_testlib_root() + "/ld_config_test_helper_lib3.so";
chmod(helper.c_str(), 0755);
ExecTestHelper eth;
@@ -248,9 +249,9 @@
std::string error_message = "CANNOT LINK EXECUTABLE \"" + get_testlib_root() + "/ld_config_test_helper/ld_config_test_helper\": library \"ld_config_test_helper_lib1.so\" not found\n";
std::string helper = get_testlib_root() +
"/ld_config_test_helper/ld_config_test_helper";
- std::string config_file = get_testlib_root() + "/ld.config.txt";
- create_ld_config_file(config_file);
- std::string env = std::string("LD_CONFIG_FILE=") + config_file;
+ TemporaryFile config_file;
+ create_ld_config_file(config_file.filename);
+ std::string env = std::string("LD_CONFIG_FILE=") + config_file.filename;
chmod(helper.c_str(), 0755);
ExecTestHelper eth;
eth.SetArgs({ helper.c_str(), nullptr });