Skip to content

Commit

Permalink
src: allow embedder-provided PageAllocator in NodePlatform
Browse files Browse the repository at this point in the history
For certain embedder use cases there are more complex memory allocation
requirements that the default V8 page allocator does not handle. For example,
using MAP_JIT when running under a hardened runtime environment on macOS.
This allows embedders like Electron to provide their own allocator that
does handle these cases.
  • Loading branch information
codebytere committed Apr 22, 2021
1 parent 4243ce0 commit 81bea6b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
14 changes: 10 additions & 4 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,11 @@ MultiIsolatePlatform* CreatePlatform(

MultiIsolatePlatform* CreatePlatform(
int thread_pool_size,
v8::TracingController* tracing_controller) {
return MultiIsolatePlatform::Create(thread_pool_size, tracing_controller)
v8::TracingController* tracing_controller,
v8::PageAllocator* page_allocator) {
return MultiIsolatePlatform::Create(thread_pool_size,
tracing_controller,
page_allocator)
.release();
}

Expand All @@ -475,8 +478,11 @@ void FreePlatform(MultiIsolatePlatform* platform) {

std::unique_ptr<MultiIsolatePlatform> MultiIsolatePlatform::Create(
int thread_pool_size,
v8::TracingController* tracing_controller) {
return std::make_unique<NodePlatform>(thread_pool_size, tracing_controller);
v8::TracingController* tracing_controller,
v8::PageAllocator* page_allocator) {
return std::make_unique<NodePlatform>(thread_pool_size,
tracing_controller,
page_allocator);
}

MaybeLocal<Object> GetPerContextExports(Local<Context> context) {
Expand Down
6 changes: 4 additions & 2 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {

static std::unique_ptr<MultiIsolatePlatform> Create(
int thread_pool_size,
v8::TracingController* tracing_controller = nullptr);
v8::TracingController* tracing_controller = nullptr,
v8::PageAllocator* page_allocator = nullptr);
};

enum IsolateSettingsFlags {
Expand Down Expand Up @@ -485,7 +486,8 @@ NODE_EXTERN MultiIsolatePlatform* GetMultiIsolatePlatform(IsolateData* env);
NODE_DEPRECATED("Use MultiIsolatePlatform::Create() instead",
NODE_EXTERN MultiIsolatePlatform* CreatePlatform(
int thread_pool_size,
v8::TracingController* tracing_controller));
v8::TracingController* tracing_controller,
v8::PageAllocator* = nullptr));
NODE_DEPRECATED("Use MultiIsolatePlatform::Create() instead",
NODE_EXTERN void FreePlatform(MultiIsolatePlatform* platform));

Expand Down
11 changes: 10 additions & 1 deletion src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,17 @@ void PerIsolatePlatformData::DecreaseHandleCount() {
}

NodePlatform::NodePlatform(int thread_pool_size,
v8::TracingController* tracing_controller) {
v8::TracingController* tracing_controller,
v8::PageAllocator* page_allocator) {
if (tracing_controller != nullptr) {
tracing_controller_ = tracing_controller;
} else {
tracing_controller_ = new v8::TracingController();
}

// V8 will default to its built in allocator if none is provided.
page_allocator_ = page_allocator;

// TODO(addaleax): It's a bit icky that we use global state here, but we can't
// really do anything about it unless V8 starts exposing a way to access the
// current v8::Platform instance.
Expand Down Expand Up @@ -550,6 +555,10 @@ Platform::StackTracePrinter NodePlatform::GetStackTracePrinter() {
};
}

v8::PageAllocator* NodePlatform::GetPageAllocator() {
return page_allocator_;
}

template <class T>
TaskQueue<T>::TaskQueue()
: lock_(), tasks_available_(), tasks_drained_(),
Expand Down
5 changes: 4 additions & 1 deletion src/node_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ class WorkerThreadsTaskRunner {
class NodePlatform : public MultiIsolatePlatform {
public:
NodePlatform(int thread_pool_size,
v8::TracingController* tracing_controller);
v8::TracingController* tracing_controller,
v8::PageAllocator* page_allocator = nullptr);
~NodePlatform() override;

void DrainTasks(v8::Isolate* isolate) override;
Expand Down Expand Up @@ -170,6 +171,7 @@ class NodePlatform : public MultiIsolatePlatform {
v8::Isolate* isolate) override;

Platform::StackTracePrinter GetStackTracePrinter() override;
v8::PageAllocator* GetPageAllocator() override;

private:
IsolatePlatformDelegate* ForIsolate(v8::Isolate* isolate);
Expand All @@ -181,6 +183,7 @@ class NodePlatform : public MultiIsolatePlatform {
std::unordered_map<v8::Isolate*, DelegatePair> per_isolate_;

v8::TracingController* tracing_controller_;
v8::PageAllocator* page_allocator_;
std::shared_ptr<WorkerThreadsTaskRunner> worker_thread_task_runner_;
bool has_shut_down_ = false;
};
Expand Down

0 comments on commit 81bea6b

Please sign in to comment.