pdfium/fxjs/cfx_v8_array_buffer_allocator.cpp
Tom Sepez a7e6db4486 Support V8_ENABLE_SANDBOX in CFX_ArrayBufferAllocator
Allow tests to run under a full chromium checkout, without forcing a
fallback from PA to malloc() for standalone checkouts.

Bug: chromium:1325244
Change-Id: Ib7596aeb02ca32746bb840573c874de78129c5a9
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/103216
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
2023-01-14 01:36:29 +00:00

54 lines
1.6 KiB
C++

// Copyright 2021 The PDFium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "fxjs/cfx_v8_array_buffer_allocator.h"
#include "core/fxcrt/fx_memory.h"
CFX_V8ArrayBufferAllocator::CFX_V8ArrayBufferAllocator() = default;
CFX_V8ArrayBufferAllocator::~CFX_V8ArrayBufferAllocator() = default;
// NOTE: Under V8 sandbox mode, defer NewDefaultAllocator() call until
// first use, since V8 must be initialized first for it to succeed, but
// we need the allocator in order to initialize V8.
void* CFX_V8ArrayBufferAllocator::Allocate(size_t length) {
if (length > kMaxAllowedBytes)
return nullptr;
#ifdef V8_ENABLE_SANDBOX
if (!wrapped_) {
wrapped_.reset(v8::ArrayBuffer::Allocator::NewDefaultAllocator());
}
return wrapped_->Allocate(length);
#else // V8_ENABLE_SANDBOX
return FX_ArrayBufferAllocate(length);
#endif // V8_ENABLE_SANDBOX
}
void* CFX_V8ArrayBufferAllocator::AllocateUninitialized(size_t length) {
if (length > kMaxAllowedBytes)
return nullptr;
#ifdef V8_ENABLE_SANDBOX
if (!wrapped_) {
wrapped_.reset(v8::ArrayBuffer::Allocator::NewDefaultAllocator());
}
return wrapped_->AllocateUninitialized(length);
#else // V8_ENABLE_SANDBOX
return FX_ArrayBufferAllocateUninitialized(length);
#endif
}
void CFX_V8ArrayBufferAllocator::Free(void* data, size_t length) {
#ifdef V8_ENABLE_SANDBOX
if (wrapped_) {
wrapped_->Free(data, length);
}
#else // V8_ENABLE_SANDBOX
FX_ArrayBufferFree(data);
#endif
}