Skip to content

Commit 81bea6b

Browse files
committed
src: allow embedder-provided PageAllocator in NodePlatform
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.
1 parent 4243ce0 commit 81bea6b

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

src/api/environment.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,11 @@ MultiIsolatePlatform* CreatePlatform(
464464

465465
MultiIsolatePlatform* CreatePlatform(
466466
int thread_pool_size,
467-
v8::TracingController* tracing_controller) {
468-
return MultiIsolatePlatform::Create(thread_pool_size, tracing_controller)
467+
v8::TracingController* tracing_controller,
468+
v8::PageAllocator* page_allocator) {
469+
return MultiIsolatePlatform::Create(thread_pool_size,
470+
tracing_controller,
471+
page_allocator)
469472
.release();
470473
}
471474

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

476479
std::unique_ptr<MultiIsolatePlatform> MultiIsolatePlatform::Create(
477480
int thread_pool_size,
478-
v8::TracingController* tracing_controller) {
479-
return std::make_unique<NodePlatform>(thread_pool_size, tracing_controller);
481+
v8::TracingController* tracing_controller,
482+
v8::PageAllocator* page_allocator) {
483+
return std::make_unique<NodePlatform>(thread_pool_size,
484+
tracing_controller,
485+
page_allocator);
480486
}
481487

482488
MaybeLocal<Object> GetPerContextExports(Local<Context> context) {

src/node.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
310310

311311
static std::unique_ptr<MultiIsolatePlatform> Create(
312312
int thread_pool_size,
313-
v8::TracingController* tracing_controller = nullptr);
313+
v8::TracingController* tracing_controller = nullptr,
314+
v8::PageAllocator* page_allocator = nullptr);
314315
};
315316

316317
enum IsolateSettingsFlags {
@@ -485,7 +486,8 @@ NODE_EXTERN MultiIsolatePlatform* GetMultiIsolatePlatform(IsolateData* env);
485486
NODE_DEPRECATED("Use MultiIsolatePlatform::Create() instead",
486487
NODE_EXTERN MultiIsolatePlatform* CreatePlatform(
487488
int thread_pool_size,
488-
v8::TracingController* tracing_controller));
489+
v8::TracingController* tracing_controller,
490+
v8::PageAllocator* = nullptr));
489491
NODE_DEPRECATED("Use MultiIsolatePlatform::Create() instead",
490492
NODE_EXTERN void FreePlatform(MultiIsolatePlatform* platform));
491493

src/node_platform.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,17 @@ void PerIsolatePlatformData::DecreaseHandleCount() {
324324
}
325325

326326
NodePlatform::NodePlatform(int thread_pool_size,
327-
v8::TracingController* tracing_controller) {
327+
v8::TracingController* tracing_controller,
328+
v8::PageAllocator* page_allocator) {
328329
if (tracing_controller != nullptr) {
329330
tracing_controller_ = tracing_controller;
330331
} else {
331332
tracing_controller_ = new v8::TracingController();
332333
}
334+
335+
// V8 will default to its built in allocator if none is provided.
336+
page_allocator_ = page_allocator;
337+
333338
// TODO(addaleax): It's a bit icky that we use global state here, but we can't
334339
// really do anything about it unless V8 starts exposing a way to access the
335340
// current v8::Platform instance.
@@ -550,6 +555,10 @@ Platform::StackTracePrinter NodePlatform::GetStackTracePrinter() {
550555
};
551556
}
552557

558+
v8::PageAllocator* NodePlatform::GetPageAllocator() {
559+
return page_allocator_;
560+
}
561+
553562
template <class T>
554563
TaskQueue<T>::TaskQueue()
555564
: lock_(), tasks_available_(), tasks_drained_(),

src/node_platform.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ class WorkerThreadsTaskRunner {
138138
class NodePlatform : public MultiIsolatePlatform {
139139
public:
140140
NodePlatform(int thread_pool_size,
141-
v8::TracingController* tracing_controller);
141+
v8::TracingController* tracing_controller,
142+
v8::PageAllocator* page_allocator = nullptr);
142143
~NodePlatform() override;
143144

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

172173
Platform::StackTracePrinter GetStackTracePrinter() override;
174+
v8::PageAllocator* GetPageAllocator() override;
173175

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

183185
v8::TracingController* tracing_controller_;
186+
v8::PageAllocator* page_allocator_;
184187
std::shared_ptr<WorkerThreadsTaskRunner> worker_thread_task_runner_;
185188
bool has_shut_down_ = false;
186189
};

0 commit comments

Comments
 (0)