-
-
Notifications
You must be signed in to change notification settings - Fork 32k
Adds a fuzzer for LoadEnvironment #34844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1570339
build: adds a new fuzzer that targets node::LoadEnvironment()
DavidKorczynski 4df0816
build: rename cc file to header file.
DavidKorczynski 2a6cda5
build: Resolves the comments from bnoordhuis.
DavidKorczynski 9735bfb
build: Fix remaining curly-bracket notation.
DavidKorczynski 3db21a0
build: Fix a typecast so the compiler wont complain.
DavidKorczynski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* A fuzzer focused on the node::LoadEnvironment() function. | ||
* | ||
* Code here has been inspired by the cctest test case. | ||
*/ | ||
|
||
#include <stdlib.h> | ||
#include "node.h" | ||
#include "node_platform.h" | ||
#include "node_internals.h" | ||
#include "env-inl.h" | ||
#include "util-inl.h" | ||
#include "v8.h" | ||
#include "libplatform/libplatform.h" | ||
#include "aliased_buffer.h" | ||
#include "fuzz_helper.h" | ||
|
||
using node::AliasedBufferBase; | ||
|
||
/* General set up */ | ||
using ArrayBufferUniquePtr = std::unique_ptr<node::ArrayBufferAllocator, | ||
decltype(&node::FreeArrayBufferAllocator)>; | ||
using TracingAgentUniquePtr = std::unique_ptr<node::tracing::Agent>; | ||
using NodePlatformUniquePtr = std::unique_ptr<node::NodePlatform>; | ||
|
||
static TracingAgentUniquePtr tracing_agent; | ||
static NodePlatformUniquePtr platform; | ||
static uv_loop_t current_loop; | ||
|
||
extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) { | ||
uv_os_unsetenv("NODE_OPTIONS"); | ||
std::vector<std::string> node_argv{ "fuzz_env" }; | ||
std::vector<std::string> exec_argv; | ||
std::vector<std::string> errors; | ||
|
||
node::InitializeNodeWithArgs(&node_argv, &exec_argv, &errors); | ||
|
||
tracing_agent = std::make_unique<node::tracing::Agent>(); | ||
node::tracing::TraceEventHelper::SetAgent(tracing_agent.get()); | ||
node::tracing::TracingController* tracing_controller = | ||
tracing_agent->GetTracingController(); | ||
CHECK_EQ(0, uv_loop_init(¤t_loop)); | ||
static constexpr int kV8ThreadPoolSize = 4; | ||
platform.reset( | ||
new node::NodePlatform(kV8ThreadPoolSize, tracing_controller)); | ||
v8::V8::InitializePlatform(platform.get()); | ||
v8::V8::Initialize(); | ||
return 0; | ||
} | ||
|
||
class FuzzerFixtureHelper { | ||
public: | ||
v8::Isolate* isolate_; | ||
ArrayBufferUniquePtr allocator; | ||
|
||
FuzzerFixtureHelper() | ||
: allocator(ArrayBufferUniquePtr(node::CreateArrayBufferAllocator(), | ||
&node::FreeArrayBufferAllocator)) { | ||
isolate_ = NewIsolate(allocator.get(), ¤t_loop, platform.get()); | ||
CHECK_NOT_NULL(isolate_); | ||
isolate_->Enter(); | ||
}; | ||
|
||
void Teardown() { | ||
platform->DrainTasks(isolate_); | ||
isolate_->Exit(); | ||
platform->UnregisterIsolate(isolate_); | ||
isolate_->Dispose(); | ||
isolate_ = nullptr; | ||
} | ||
}; | ||
|
||
void EnvTest(v8::Isolate* isolate_, char* env_string) { | ||
const v8::HandleScope handle_scope(isolate_); | ||
Argv argv; | ||
|
||
node::EnvironmentFlags::Flags flags = node::EnvironmentFlags::kDefaultFlags; | ||
auto isolate = handle_scope.GetIsolate(); | ||
v8::Local<v8::Context> context_ = node::NewContext(isolate); | ||
context_->Enter(); | ||
|
||
node::IsolateData* isolate_data_ = node::CreateIsolateData(isolate, ¤t_loop, | ||
platform.get()); | ||
std::vector<std::string> args(*argv, *argv + 1); | ||
std::vector<std::string> exec_args(*argv, *argv + 1); | ||
node::Environment* environment_ = node::CreateEnvironment(isolate_data_, | ||
context_, args, exec_args, flags); | ||
node::Environment* envi = environment_; | ||
SetProcessExitHandler(envi, [&](node::Environment* env_, int exit_code) { | ||
node::Stop(envi); | ||
}); | ||
node::LoadEnvironment(envi, env_string); | ||
|
||
// Cleanup! | ||
node::FreeEnvironment(environment_); | ||
node::FreeIsolateData(isolate_data_); | ||
context_->Exit(); | ||
} | ||
bnoordhuis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data2, size_t size) { | ||
FuzzerFixtureHelper ffh; | ||
std::string s(reinterpret_cast<const char*>(data2), size); | ||
EnvTest(ffh.isolate_, (char*)s.c_str()); | ||
ffh.Teardown(); | ||
return 0; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
struct Argv { | ||
public: | ||
Argv() : Argv({"node", "-p", "process.version"}) {} | ||
|
||
Argv(const std::initializer_list<const char*> &args) { | ||
nr_args_ = args.size(); | ||
int total_len = 0; | ||
for (auto it = args.begin(); it != args.end(); ++it) { | ||
total_len += strlen(*it) + 1; | ||
} | ||
argv_ = static_cast<char**>(malloc(nr_args_ * sizeof(char*))); | ||
argv_[0] = static_cast<char*>(malloc(total_len)); | ||
int i = 0; | ||
int offset = 0; | ||
for (auto it = args.begin(); it != args.end(); ++it, ++i) { | ||
int len = strlen(*it) + 1; | ||
snprintf(argv_[0] + offset, len, "%s", *it); | ||
// Skip argv_[0] as it points the correct location already | ||
if (i > 0) { | ||
argv_[i] = argv_[0] + offset; | ||
} | ||
offset += len; | ||
} | ||
} | ||
|
||
~Argv() { | ||
free(argv_[0]); | ||
free(argv_); | ||
} | ||
|
||
int nr_args() const { | ||
return nr_args_; | ||
} | ||
|
||
char** operator*() const { | ||
return argv_; | ||
} | ||
|
||
private: | ||
char** argv_; | ||
int nr_args_; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.