From 20834e8510d33c4c8199338ca46600a278e88caa Mon Sep 17 00:00:00 2001 From: Hadi Ravanbakhsh Date: Tue, 25 Apr 2023 14:52:53 -0400 Subject: [PATCH 1/3] Sanitizes launcher tests using https://github.com/google/sanitizers/wiki/AddressSanitizer --- launcher/fuzzed_data_provider_test.cpp | 3 ++- launcher/jvm_tooling.cpp | 13 +++++++++---- launcher/jvm_tooling_test.cpp | 3 ++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/launcher/fuzzed_data_provider_test.cpp b/launcher/fuzzed_data_provider_test.cpp index 77ff9a3e1..9907b75e2 100644 --- a/launcher/fuzzed_data_provider_test.cpp +++ b/launcher/fuzzed_data_provider_test.cpp @@ -36,7 +36,8 @@ class FuzzedDataProviderTest : public ::testing::Test { // destroyed after all tests in this test suite have finished. static void SetUpTestCase() { using ::bazel::tools::cpp::runfiles::Runfiles; - FLAGS_cp = Runfiles::CreateForTest()->Rlocation( + std::unique_ptr runfiles(Runfiles::CreateForTest()); + FLAGS_cp = runfiles->Rlocation( "jazzer/launcher/testdata/fuzz_target_mocks_deploy.jar"); jvm_ = std::make_unique(); diff --git a/launcher/jvm_tooling.cpp b/launcher/jvm_tooling.cpp index 1b0e50009..0e8575903 100644 --- a/launcher/jvm_tooling.cpp +++ b/launcher/jvm_tooling.cpp @@ -64,16 +64,21 @@ std::string getExecutablePath() { char buf[655536]; #if defined(__APPLE__) uint32_t buf_size = sizeof(buf); - if (_NSGetExecutablePath(buf, &buf_size) != 0) { + ssize_t read_bytes = buf_size - 1; + bool failed _NSGetExecutablePath(buf, &buf_size) != 0; #elif defined(_WIN32) - if (GetModuleFileNameA(NULL, buf, sizeof(buf)) == 0) { + ssize_t read_bytes = GetModuleFileNameA(NULL, buf, sizeof(buf)); + bool failed = (read_bytes == 0); #elif defined(_ANDROID) - if (true) { + bool failed = true; #else // Assume Linux - if (readlink("/proc/self/exe", buf, sizeof(buf)) == -1) { + ssize_t read_bytes = readlink("/proc/self/exe", buf, sizeof(buf)); + bool failed = (read_bytes == -1); #endif + if (failed) { return ""; } + buf[read_bytes] = '\0'; return {buf}; } diff --git a/launcher/jvm_tooling_test.cpp b/launcher/jvm_tooling_test.cpp index 8cfb6bc6e..2a70dcb99 100644 --- a/launcher/jvm_tooling_test.cpp +++ b/launcher/jvm_tooling_test.cpp @@ -36,7 +36,8 @@ class JvmToolingTest : public ::testing::Test { FLAGS_jvm_args = "-Denv1=va\\" ARG_SEPARATOR "l1\\\\" ARG_SEPARATOR "-Denv2=val2"; using ::bazel::tools::cpp::runfiles::Runfiles; - FLAGS_cp = Runfiles::CreateForTest()->Rlocation( + std::unique_ptr runfiles(Runfiles::CreateForTest()); + FLAGS_cp = runfiles->Rlocation( "jazzer/launcher/testdata/fuzz_target_mocks_deploy.jar"); jvm_ = std::unique_ptr(new JVM()); From 342e41b8896e0001e3dc3ee2ec864ce060ad9052 Mon Sep 17 00:00:00 2001 From: hadi88 Date: Tue, 25 Apr 2023 15:49:18 -0400 Subject: [PATCH 2/3] Update launcher/jvm_tooling.cpp Co-authored-by: Fabian Meumertzheim --- launcher/jvm_tooling.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/launcher/jvm_tooling.cpp b/launcher/jvm_tooling.cpp index 0e8575903..ba4ceefa2 100644 --- a/launcher/jvm_tooling.cpp +++ b/launcher/jvm_tooling.cpp @@ -65,7 +65,7 @@ std::string getExecutablePath() { #if defined(__APPLE__) uint32_t buf_size = sizeof(buf); ssize_t read_bytes = buf_size - 1; - bool failed _NSGetExecutablePath(buf, &buf_size) != 0; + bool failed = (_NSGetExecutablePath(buf, &buf_size) != 0); #elif defined(_WIN32) ssize_t read_bytes = GetModuleFileNameA(NULL, buf, sizeof(buf)); bool failed = (read_bytes == 0); From 16cd14c9e081f6ae20987b5390150f5e0f150f94 Mon Sep 17 00:00:00 2001 From: Hadi Ravanbakhsh Date: Tue, 25 Apr 2023 14:52:53 -0400 Subject: [PATCH 3/3] Sanitizes launcher tests using https://github.com/google/sanitizers/wiki/AddressSanitizer --- launcher/fuzzed_data_provider_test.cpp | 3 ++- launcher/jvm_tooling.cpp | 13 +++++++++---- launcher/jvm_tooling_test.cpp | 3 ++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/launcher/fuzzed_data_provider_test.cpp b/launcher/fuzzed_data_provider_test.cpp index 77ff9a3e1..9907b75e2 100644 --- a/launcher/fuzzed_data_provider_test.cpp +++ b/launcher/fuzzed_data_provider_test.cpp @@ -36,7 +36,8 @@ class FuzzedDataProviderTest : public ::testing::Test { // destroyed after all tests in this test suite have finished. static void SetUpTestCase() { using ::bazel::tools::cpp::runfiles::Runfiles; - FLAGS_cp = Runfiles::CreateForTest()->Rlocation( + std::unique_ptr runfiles(Runfiles::CreateForTest()); + FLAGS_cp = runfiles->Rlocation( "jazzer/launcher/testdata/fuzz_target_mocks_deploy.jar"); jvm_ = std::make_unique(); diff --git a/launcher/jvm_tooling.cpp b/launcher/jvm_tooling.cpp index 1b0e50009..8dfbed94d 100644 --- a/launcher/jvm_tooling.cpp +++ b/launcher/jvm_tooling.cpp @@ -64,16 +64,21 @@ std::string getExecutablePath() { char buf[655536]; #if defined(__APPLE__) uint32_t buf_size = sizeof(buf); - if (_NSGetExecutablePath(buf, &buf_size) != 0) { + uint32_t read_bytes = buf_size - 1; + bool failed = (_NSGetExecutablePath(buf, &buf_size) != 0); #elif defined(_WIN32) - if (GetModuleFileNameA(NULL, buf, sizeof(buf)) == 0) { + DWORD read_bytes = GetModuleFileNameA(NULL, buf, sizeof(buf)); + bool failed = (read_bytes == 0); #elif defined(_ANDROID) - if (true) { + bool failed = true; #else // Assume Linux - if (readlink("/proc/self/exe", buf, sizeof(buf)) == -1) { + ssize_t read_bytes = readlink("/proc/self/exe", buf, sizeof(buf)); + bool failed = (read_bytes == -1); #endif + if (failed) { return ""; } + buf[read_bytes] = '\0'; return {buf}; } diff --git a/launcher/jvm_tooling_test.cpp b/launcher/jvm_tooling_test.cpp index 8cfb6bc6e..2a70dcb99 100644 --- a/launcher/jvm_tooling_test.cpp +++ b/launcher/jvm_tooling_test.cpp @@ -36,7 +36,8 @@ class JvmToolingTest : public ::testing::Test { FLAGS_jvm_args = "-Denv1=va\\" ARG_SEPARATOR "l1\\\\" ARG_SEPARATOR "-Denv2=val2"; using ::bazel::tools::cpp::runfiles::Runfiles; - FLAGS_cp = Runfiles::CreateForTest()->Rlocation( + std::unique_ptr runfiles(Runfiles::CreateForTest()); + FLAGS_cp = runfiles->Rlocation( "jazzer/launcher/testdata/fuzz_target_mocks_deploy.jar"); jvm_ = std::unique_ptr(new JVM());