From 08dc2442882665d6e96262eaec03099a9a8f535d Mon Sep 17 00:00:00 2001 From: asraa Date: Wed, 10 Jul 2019 17:19:15 -0400 Subject: [PATCH] fuzz: fix filesystem bug, use std::__fs::filesystem (#7507) Since OSS-Fuzz minijail prohibits mkdir calls, we use std::__fs::filesystem namespace which implements filesystem. The OSS-Fuzz image was taking the mkdir branch, since neither filesystem nor experimental/filesystem was found. However, we are able to use std::__fs::filesystem branch in the OSS-Fuzz image and the std::experimental::filesystem branch locally. Risk Level: Low Testing: Add RELEASE_ASSERT(1==0, "") under the #if defined(_LIBCPP_VERSION) && TEST_STD_VER < 17 branch to verify that branch is taken in oss-fuzz docker. run_fuzzer with oss-fuzz docker is successful. Fixes OSS-Fuzz issue: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=15414 Signed-off-by: Asra Ali --- test/test_common/environment.cc | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/test/test_common/environment.cc b/test/test_common/environment.cc index 8afd806cd331..2291fb09a094 100644 --- a/test/test_common/environment.cc +++ b/test/test_common/environment.cc @@ -3,11 +3,12 @@ #include #include -#ifdef __has_include -#if __has_include() +// TODO(asraa): Remove and rely only on when Envoy requires +// Clang >= 9. +#if defined(_LIBCPP_VERSION) && !defined(__APPLE__) #include -// TODO(asraa): Remove this when Envoy requires Clang >= 9. -#elif __has_include() +#elif defined __has_include +#if __has_include() #include #endif #endif @@ -41,8 +42,8 @@ std::string makeTempDir(char* name_template) { char* dirname = ::_mktemp(name_template); RELEASE_ASSERT(dirname != nullptr, fmt::format("failed to create tempdir from template: {} {}", name_template, strerror(errno))); -#ifdef __cpp_lib_filesystem - std::filesystem::create_directories(dirname); +#if defined(_LIBCPP_VERSION) && !defined(__APPLE__) + std::__fs::filesystem::create_directories(dirname); #elif defined __cpp_lib_experimental_filesystem std::experimental::filesystem::create_directories(dirname); #endif @@ -84,10 +85,10 @@ char** argv_; } // namespace void TestEnvironment::createPath(const std::string& path) { -#ifdef __cpp_lib_filesystem +#if defined(_LIBCPP_VERSION) && !defined(__APPLE__) // We don't want to rely on mkdir etc. if we can avoid it, since it might not // exist in some environments such as ClusterFuzz. - std::filesystem::create_directories(std::filesystem::path(path)); + std::__fs::filesystem::create_directories(std::__fs::filesystem::path(path)); #elif defined __cpp_lib_experimental_filesystem std::experimental::filesystem::create_directories(std::experimental::filesystem::path(path)); #else @@ -97,10 +98,10 @@ void TestEnvironment::createPath(const std::string& path) { } void TestEnvironment::createParentPath(const std::string& path) { -#ifdef __cpp_lib_filesystem +#if defined(_LIBCPP_VERSION) && !defined(__APPLE__) // We don't want to rely on mkdir etc. if we can avoid it, since it might not // exist in some environments such as ClusterFuzz. - std::filesystem::create_directories(std::filesystem::path(path).parent_path()); + std::__fs::filesystem::create_directories(std::__fs::filesystem::path(path).parent_path()); #elif defined __cpp_lib_experimental_filesystem std::experimental::filesystem::create_directories( std::experimental::filesystem::path(path).parent_path()); @@ -112,13 +113,13 @@ void TestEnvironment::createParentPath(const std::string& path) { void TestEnvironment::removePath(const std::string& path) { RELEASE_ASSERT(absl::StartsWith(path, TestEnvironment::temporaryDirectory()), ""); -#ifdef __cpp_lib_filesystem +#if defined(_LIBCPP_VERSION) && !defined(__APPLE__) // We don't want to rely on mkdir etc. if we can avoid it, since it might not // exist in some environments such as ClusterFuzz. - if (!std::filesystem::exists(path)) { + if (!std::__fs::filesystem::exists(path)) { return; } - std::filesystem::remove_all(std::filesystem::path(path)); + std::__fs::filesystem::remove_all(std::__fs::filesystem::path(path)); #elif defined __cpp_lib_experimental_filesystem if (!std::experimental::filesystem::exists(path)) { return;