Skip to content

Commit

Permalink
fuzz: fix filesystem bug, use std::__fs::filesystem (#7507)
Browse files Browse the repository at this point in the history
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 <asraa@google.com>
  • Loading branch information
asraa authored and htuch committed Jul 10, 2019
1 parent 165290e commit 08dc244
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions test/test_common/environment.cc
Expand Up @@ -3,11 +3,12 @@
#include <sys/un.h>
#include <unistd.h>

#ifdef __has_include
#if __has_include(<filesystem>)
// TODO(asraa): Remove <experimental/filesystem> and rely only on <filesystem> when Envoy requires
// Clang >= 9.
#if defined(_LIBCPP_VERSION) && !defined(__APPLE__)
#include <filesystem>
// TODO(asraa): Remove this when Envoy requires Clang >= 9.
#elif __has_include(<experimental/filesystem>)
#elif defined __has_include
#if __has_include(<experimental/filesystem>)
#include <experimental/filesystem>
#endif
#endif
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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());
Expand All @@ -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;
Expand Down

0 comments on commit 08dc244

Please sign in to comment.