Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions cpp/src/arrow/util/fuzz_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@

#include "arrow/util/fuzz_internal.h"

#include <cstdint>
#include <utility>

#include "arrow/memory_pool.h"
#include "arrow/status.h"
#include "arrow/util/io_util.h"
#include "arrow/util/logging_internal.h"
#include "arrow/util/value_parsing.h"

namespace arrow::internal {

Expand All @@ -30,8 +35,24 @@ MemoryPool* fuzzing_memory_pool() {
}

void LogFuzzStatus(const Status& st, const uint8_t* data, int64_t size) {
// Most fuzz inputs will be invalid and generate errors, only log potential OOMs
if (st.IsOutOfMemory()) {
static const int kVerbosity = []() {
auto maybe_env_value = GetEnvVar("ARROW_FUZZING_VERBOSITY");
if (maybe_env_value.status().IsKeyError()) {
return 0;
}
auto env_value = std::move(maybe_env_value).ValueOrDie();
int32_t value;
if (!ParseValue<Int32Type>(env_value.data(), env_value.length(), &value)) {
Status::Invalid("Invalid value for ARROW_FUZZING_VERBOSITY: '", env_value, "'")
.Abort();
}
return value;
}();

if (kVerbosity >= 1) {
ARROW_LOG(WARNING) << "Fuzzing input with size=" << size
<< " failed: " << st.ToString();
} else if (st.IsOutOfMemory()) {
ARROW_LOG(WARNING) << "Fuzzing input with size=" << size
<< " hit allocation failure: " << st.ToString();
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/util/fuzz_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ constexpr int64_t kFuzzingMemoryLimit = 2200LL * 1000 * 1000;
/// Return a memory pool that will not allocate more than kFuzzingMemoryLimit bytes.
ARROW_EXPORT MemoryPool* fuzzing_memory_pool();

// Optionally log the outcome of fuzzing an input
/// Optionally log the outcome of fuzzing an input
ARROW_EXPORT void LogFuzzStatus(const Status&, const uint8_t* data, int64_t size);

} // namespace arrow::internal
41 changes: 14 additions & 27 deletions cpp/src/arrow/util/io_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1758,32 +1758,30 @@ Status FileTruncate(int fd, const int64_t size) {
// Environment variables
//

Result<std::string> GetEnvVar(const char* name) {
Result<std::string> GetEnvVar(std::string_view name) {
#ifdef _WIN32
// On Windows, getenv() reads an early copy of the process' environment
// which doesn't get updated when SetEnvironmentVariable() is called.
constexpr int32_t bufsize = 2000;
char c_str[bufsize];
auto res = GetEnvironmentVariableA(name, c_str, bufsize);
auto res = GetEnvironmentVariableA(name.data(), c_str, bufsize);
if (res >= bufsize) {
return Status::CapacityError("environment variable value too long");
} else if (res == 0) {
return Status::KeyError("environment variable undefined");
return Status::KeyError("environment variable '", name, "'undefined");
}
return std::string(c_str);
#else
char* c_str = getenv(name);
char* c_str = getenv(name.data());
if (c_str == nullptr) {
return Status::KeyError("environment variable undefined");
return Status::KeyError("environment variable '", name, "'undefined");
}
return std::string(c_str);
#endif
}

Result<std::string> GetEnvVar(const std::string& name) { return GetEnvVar(name.c_str()); }

#ifdef _WIN32
Result<NativePathString> GetEnvVarNative(const std::string& name) {
Result<NativePathString> GetEnvVarNative(std::string_view name) {
NativePathString w_name;
constexpr int32_t bufsize = 2000;
wchar_t w_str[bufsize];
Expand All @@ -1793,62 +1791,51 @@ Result<NativePathString> GetEnvVarNative(const std::string& name) {
if (res >= bufsize) {
return Status::CapacityError("environment variable value too long");
} else if (res == 0) {
return Status::KeyError("environment variable undefined");
return Status::KeyError("environment variable '", name, "'undefined");
}
return NativePathString(w_str);
}

Result<NativePathString> GetEnvVarNative(const char* name) {
return GetEnvVarNative(std::string(name));
}

#else

Result<NativePathString> GetEnvVarNative(const std::string& name) {
Result<NativePathString> GetEnvVarNative(std::string_view name) {
return GetEnvVar(name);
}

Result<NativePathString> GetEnvVarNative(const char* name) { return GetEnvVar(name); }
#endif

Status SetEnvVar(const char* name, const char* value) {
Status SetEnvVar(std::string_view name, std::string_view value) {
#ifdef _WIN32
if (SetEnvironmentVariableA(name, value)) {
if (SetEnvironmentVariableA(name.data(), value.data())) {
return Status::OK();
} else {
return Status::Invalid("failed setting environment variable");
}
#else
if (setenv(name, value, 1) == 0) {
if (setenv(name.data(), value.data(), 1) == 0) {
return Status::OK();
} else {
return Status::Invalid("failed setting environment variable");
}
#endif
}

Status SetEnvVar(const std::string& name, const std::string& value) {
return SetEnvVar(name.c_str(), value.c_str());
}

Status DelEnvVar(const char* name) {
Status DelEnvVar(std::string_view name) {
#ifdef _WIN32
if (SetEnvironmentVariableA(name, nullptr)) {
if (SetEnvironmentVariableA(name.data(), nullptr)) {
return Status::OK();
} else {
return Status::Invalid("failed deleting environment variable");
}
#else
if (unsetenv(name) == 0) {
if (unsetenv(name.data()) == 0) {
return Status::OK();
} else {
return Status::Invalid("failed deleting environment variable");
}
#endif
}

Status DelEnvVar(const std::string& name) { return DelEnvVar(name.c_str()); }

//
// Temporary directories
//
Expand Down
18 changes: 6 additions & 12 deletions cpp/src/arrow/util/io_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -238,23 +239,16 @@ Status MemoryMapRemap(void* addr, size_t old_size, size_t new_size, int fildes,
ARROW_EXPORT
Status MemoryAdviseWillNeed(const std::vector<MemoryRegion>& regions);

// Returns KeyError if the environment variable doesn't exist
ARROW_EXPORT
Result<std::string> GetEnvVar(const char* name);
Result<std::string> GetEnvVar(std::string_view name);
ARROW_EXPORT
Result<std::string> GetEnvVar(const std::string& name);
ARROW_EXPORT
Result<NativePathString> GetEnvVarNative(const char* name);
ARROW_EXPORT
Result<NativePathString> GetEnvVarNative(const std::string& name);
Result<NativePathString> GetEnvVarNative(std::string_view name);

ARROW_EXPORT
Status SetEnvVar(const char* name, const char* value);
ARROW_EXPORT
Status SetEnvVar(const std::string& name, const std::string& value);
ARROW_EXPORT
Status DelEnvVar(const char* name);
Status SetEnvVar(std::string_view name, std::string_view value);
ARROW_EXPORT
Status DelEnvVar(const std::string& name);
Status DelEnvVar(std::string_view name);

ARROW_EXPORT
std::string ErrnoMessage(int errnum);
Expand Down
Loading