summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristopher Ferris2018-05-24 20:35:07 -0500
committerGerrit Code Review2018-05-24 20:35:07 -0500
commit04b83d8ec24083c75c456a6fe7d08eb66555fe40 (patch)
treea41816e8edbffed4302c60518e77fbfb1fbddb85
parent54be246a1f916a9be409b59ceaebf7a564170d2c (diff)
parent3acf57775940aa6e6326c39e0e88339156b641e7 (diff)
downloadplatform-system-core-04b83d8ec24083c75c456a6fe7d08eb66555fe40.tar.gz
platform-system-core-04b83d8ec24083c75c456a6fe7d08eb66555fe40.tar.xz
platform-system-core-04b83d8ec24083c75c456a6fe7d08eb66555fe40.zip
Merge "Change tests to use a temporary dir for files."
-rw-r--r--libbacktrace/backtrace_test.cpp87
1 files changed, 45 insertions, 42 deletions
diff --git a/libbacktrace/backtrace_test.cpp b/libbacktrace/backtrace_test.cpp
index 1e3d37981..f78a31f4f 100644
--- a/libbacktrace/backtrace_test.cpp
+++ b/libbacktrace/backtrace_test.cpp
@@ -46,6 +46,7 @@
46 46
47#include <android-base/macros.h> 47#include <android-base/macros.h>
48#include <android-base/stringprintf.h> 48#include <android-base/stringprintf.h>
49#include <android-base/test_utils.h>
49#include <android-base/unique_fd.h> 50#include <android-base/unique_fd.h>
50#include <cutils/atomic.h> 51#include <cutils/atomic.h>
51#include <cutils/threads.h> 52#include <cutils/threads.h>
@@ -1186,49 +1187,45 @@ static void VerifyFunctionsFound(const std::vector<std::string>& found_functions
1186 ASSERT_TRUE(expected_functions.empty()) << "Not all functions found in shared library."; 1187 ASSERT_TRUE(expected_functions.empty()) << "Not all functions found in shared library.";
1187} 1188}
1188 1189
1189static const char* CopySharedLibrary() { 1190static void CopySharedLibrary(const char* tmp_dir, std::string* tmp_so_name) {
1190#if defined(__LP64__) 1191 std::string system_dir;
1191 const char* lib_name = "lib64";
1192#else
1193 const char* lib_name = "lib";
1194#endif
1195 1192
1196#if defined(__BIONIC__) 1193#if defined(__BIONIC__)
1197 const char* tmp_so_name = "/data/local/tmp/libbacktrace_test.so"; 1194 system_dir = "/system/lib";
1198 std::string cp_cmd = android::base::StringPrintf("cp /system/%s/libbacktrace_test.so %s",
1199 lib_name, tmp_so_name);
1200#else 1195#else
1201 const char* tmp_so_name = "/tmp/libbacktrace_test.so"; 1196 const char* host_out_env = getenv("ANDROID_HOST_OUT");
1202 if (getenv("ANDROID_HOST_OUT") == NULL) { 1197 ASSERT_TRUE(host_out_env != nullptr);
1203 fprintf(stderr, "ANDROID_HOST_OUT not set, make sure you run lunch."); 1198 system_dir = std::string(host_out_env) + "/lib";
1204 return nullptr;
1205 }
1206 std::string cp_cmd = android::base::StringPrintf("cp %s/%s/libbacktrace_test.so %s",
1207 getenv("ANDROID_HOST_OUT"), lib_name,
1208 tmp_so_name);
1209#endif 1199#endif
1210 1200
1211 // Copy the shared so to a tempory directory. 1201#if defined(__LP64__)
1212 system(cp_cmd.c_str()); 1202 system_dir += "64";
1203#endif
1204
1205 *tmp_so_name = std::string(tmp_dir) + "/libbacktrace_test.so";
1206 std::string cp_cmd =
1207 android::base::StringPrintf("cp %s/libbacktrace_test.so %s", system_dir.c_str(), tmp_dir);
1213 1208
1214 return tmp_so_name; 1209 // Copy the shared so to a tempory directory.
1210 ASSERT_EQ(0, system(cp_cmd.c_str()));
1215} 1211}
1216 1212
1217TEST(libbacktrace, check_unreadable_elf_local) { 1213TEST(libbacktrace, check_unreadable_elf_local) {
1218 const char* tmp_so_name = CopySharedLibrary(); 1214 TemporaryDir td;
1219 ASSERT_TRUE(tmp_so_name != nullptr); 1215 std::string tmp_so_name;
1216 ASSERT_NO_FATAL_FAILURE(CopySharedLibrary(td.path, &tmp_so_name));
1220 1217
1221 struct stat buf; 1218 struct stat buf;
1222 ASSERT_TRUE(stat(tmp_so_name, &buf) != -1); 1219 ASSERT_TRUE(stat(tmp_so_name.c_str(), &buf) != -1);
1223 uint64_t map_size = buf.st_size; 1220 uint64_t map_size = buf.st_size;
1224 1221
1225 int fd = open(tmp_so_name, O_RDONLY); 1222 int fd = open(tmp_so_name.c_str(), O_RDONLY);
1226 ASSERT_TRUE(fd != -1); 1223 ASSERT_TRUE(fd != -1);
1227 1224
1228 void* map = mmap(nullptr, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0); 1225 void* map = mmap(nullptr, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
1229 ASSERT_TRUE(map != MAP_FAILED); 1226 ASSERT_TRUE(map != MAP_FAILED);
1230 close(fd); 1227 close(fd);
1231 ASSERT_TRUE(unlink(tmp_so_name) != -1); 1228 ASSERT_TRUE(unlink(tmp_so_name.c_str()) != -1);
1232 1229
1233 std::vector<std::string> found_functions; 1230 std::vector<std::string> found_functions;
1234 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, 1231 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS,
@@ -1256,32 +1253,33 @@ TEST(libbacktrace, check_unreadable_elf_local) {
1256} 1253}
1257 1254
1258TEST(libbacktrace, check_unreadable_elf_remote) { 1255TEST(libbacktrace, check_unreadable_elf_remote) {
1259 const char* tmp_so_name = CopySharedLibrary(); 1256 TemporaryDir td;
1260 ASSERT_TRUE(tmp_so_name != nullptr); 1257 std::string tmp_so_name;
1258 ASSERT_NO_FATAL_FAILURE(CopySharedLibrary(td.path, &tmp_so_name));
1261 1259
1262 g_ready = 0; 1260 g_ready = 0;
1263 1261
1264 struct stat buf; 1262 struct stat buf;
1265 ASSERT_TRUE(stat(tmp_so_name, &buf) != -1); 1263 ASSERT_TRUE(stat(tmp_so_name.c_str(), &buf) != -1);
1266 uint64_t map_size = buf.st_size; 1264 uint64_t map_size = buf.st_size;
1267 1265
1268 pid_t pid; 1266 pid_t pid;
1269 if ((pid = fork()) == 0) { 1267 if ((pid = fork()) == 0) {
1270 int fd = open(tmp_so_name, O_RDONLY); 1268 int fd = open(tmp_so_name.c_str(), O_RDONLY);
1271 if (fd == -1) { 1269 if (fd == -1) {
1272 fprintf(stderr, "Failed to open file %s: %s\n", tmp_so_name, strerror(errno)); 1270 fprintf(stderr, "Failed to open file %s: %s\n", tmp_so_name.c_str(), strerror(errno));
1273 unlink(tmp_so_name); 1271 unlink(tmp_so_name.c_str());
1274 exit(0); 1272 exit(0);
1275 } 1273 }
1276 1274
1277 void* map = mmap(nullptr, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0); 1275 void* map = mmap(nullptr, map_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
1278 if (map == MAP_FAILED) { 1276 if (map == MAP_FAILED) {
1279 fprintf(stderr, "Failed to map in memory: %s\n", strerror(errno)); 1277 fprintf(stderr, "Failed to map in memory: %s\n", strerror(errno));
1280 unlink(tmp_so_name); 1278 unlink(tmp_so_name.c_str());
1281 exit(0); 1279 exit(0);
1282 } 1280 }
1283 close(fd); 1281 close(fd);
1284 if (unlink(tmp_so_name) == -1) { 1282 if (unlink(tmp_so_name.c_str()) == -1) {
1285 fprintf(stderr, "Failed to unlink: %s\n", strerror(errno)); 1283 fprintf(stderr, "Failed to unlink: %s\n", strerror(errno));
1286 exit(0); 1284 exit(0);
1287 } 1285 }
@@ -1394,11 +1392,13 @@ static void VerifyUnreadableElfBacktrace(void* func) {
1394typedef int (*test_func_t)(int, int, int, int, void (*)(void*), void*); 1392typedef int (*test_func_t)(int, int, int, int, void (*)(void*), void*);
1395 1393
1396TEST(libbacktrace, unwind_through_unreadable_elf_local) { 1394TEST(libbacktrace, unwind_through_unreadable_elf_local) {
1397 const char* tmp_so_name = CopySharedLibrary(); 1395 TemporaryDir td;
1398 ASSERT_TRUE(tmp_so_name != nullptr); 1396 std::string tmp_so_name;
1399 void* lib_handle = dlopen(tmp_so_name, RTLD_NOW); 1397 ASSERT_NO_FATAL_FAILURE(CopySharedLibrary(td.path, &tmp_so_name));
1398
1399 void* lib_handle = dlopen(tmp_so_name.c_str(), RTLD_NOW);
1400 ASSERT_TRUE(lib_handle != nullptr); 1400 ASSERT_TRUE(lib_handle != nullptr);
1401 ASSERT_TRUE(unlink(tmp_so_name) != -1); 1401 ASSERT_TRUE(unlink(tmp_so_name.c_str()) != -1);
1402 1402
1403 test_func_t test_func; 1403 test_func_t test_func;
1404 test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one")); 1404 test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
@@ -1411,11 +1411,13 @@ TEST(libbacktrace, unwind_through_unreadable_elf_local) {
1411} 1411}
1412 1412
1413TEST(libbacktrace, unwind_through_unreadable_elf_remote) { 1413TEST(libbacktrace, unwind_through_unreadable_elf_remote) {
1414 const char* tmp_so_name = CopySharedLibrary(); 1414 TemporaryDir td;
1415 ASSERT_TRUE(tmp_so_name != nullptr); 1415 std::string tmp_so_name;
1416 void* lib_handle = dlopen(tmp_so_name, RTLD_NOW); 1416 ASSERT_NO_FATAL_FAILURE(CopySharedLibrary(td.path, &tmp_so_name));
1417
1418 void* lib_handle = dlopen(tmp_so_name.c_str(), RTLD_NOW);
1417 ASSERT_TRUE(lib_handle != nullptr); 1419 ASSERT_TRUE(lib_handle != nullptr);
1418 ASSERT_TRUE(unlink(tmp_so_name) != -1); 1420 ASSERT_TRUE(unlink(tmp_so_name.c_str()) != -1);
1419 1421
1420 test_func_t test_func; 1422 test_func_t test_func;
1421 test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one")); 1423 test_func = reinterpret_cast<test_func_t>(dlsym(lib_handle, "test_level_one"));
@@ -1444,7 +1446,8 @@ TEST(libbacktrace, unwind_through_unreadable_elf_remote) {
1444 1446
1445 size_t frame_num; 1447 size_t frame_num;
1446 if (FindFuncFrameInBacktrace(backtrace.get(), reinterpret_cast<uint64_t>(test_func), 1448 if (FindFuncFrameInBacktrace(backtrace.get(), reinterpret_cast<uint64_t>(test_func),
1447 &frame_num)) { 1449 &frame_num) &&
1450 frame_num != 0) {
1448 VerifyUnreadableElfFrame(backtrace.get(), reinterpret_cast<uint64_t>(test_func), frame_num); 1451 VerifyUnreadableElfFrame(backtrace.get(), reinterpret_cast<uint64_t>(test_func), frame_num);
1449 done = true; 1452 done = true;
1450 } 1453 }