// Copyright 2023 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_SANDBOX_ISOLATE_H_
#define V8_SANDBOX_ISOLATE_H_
#include "src/sandbox/cppheap-pointer-table.h"
#include "src/sandbox/external-pointer-table.h"
#include "src/sandbox/indirect-pointer-tag.h"
#include "src/sandbox/js-dispatch-table.h"
#include "src/sandbox/trusted-pointer-table.h"
namespace v8::internal {
class Isolate;
// A reference to an Isolate that only exposes the sandbox-related parts of an
// isolate, in particular the various pointer tables. Can be used off-thread
// and implicitly constructed from both an Isolate* and a LocalIsolate*.
#ifdef V8_ENABLE_SANDBOX
class V8_EXPORT_PRIVATE IsolateForSandbox final {
public:
template <typename IsolateT>
IsolateForSandbox(IsolateT* isolate) // NOLINT(runtime/explicit)
: isolate_(isolate->ForSandbox()) {}
inline ExternalPointerTable& GetExternalPointerTableFor(
ExternalPointerTagRange tag_range);
inline ExternalPointerTable::Space* GetExternalPointerTableSpaceFor(
ExternalPointerTagRange tag_range, Address host);
inline TrustedPointerTable& GetTrustedPointerTableFor(
IndirectPointerTagRange tag_range);
inline TrustedPointerTable::Space* GetTrustedPointerTableSpaceFor(
IndirectPointerTagRange tag_range, Address host);
inline JSDispatchTable& GetJSDispatchTable();
// Object is needed in case it comes from shared space.
inline ExternalPointerTag GetExternalPointerTableTagFor(
Tagged<HeapObject> witness, ExternalPointerHandle handle);
// Check whether the shared pointer tables of two IsolateForSandbox objects
// are the same.
inline bool SharesPointerTablesWith(IsolateForSandbox other) const;
inline Isolate* GetIsolate() const { return isolate_; }
private:
friend class IsolateForPointerCompression;
Isolate* const isolate_;
};
V8_INLINE IsolateForSandbox GetCurrentIsolateForSandbox();
#else // V8_ENABLE_SANDBOX
class V8_EXPORT_PRIVATE IsolateForSandbox final {
public:
template <typename IsolateT>
constexpr IsolateForSandbox(IsolateT*) {} // NOLINT(runtime/explicit)
constexpr IsolateForSandbox() = default;
};
V8_INLINE IsolateForSandbox GetCurrentIsolateForSandbox() { return {}; }
#endif // V8_ENABLE_SANDBOX
#ifdef V8_COMPRESS_POINTERS
class V8_EXPORT_PRIVATE IsolateForPointerCompression final {
public:
template <typename IsolateT>
IsolateForPointerCompression(IsolateT* isolate) // NOLINT(runtime/explicit)
: isolate_(isolate->ForSandbox()) {}
#ifdef V8_ENABLE_SANDBOX
IsolateForPointerCompression(
IsolateForSandbox isolate) // NOLINT(runtime/explicit)
: isolate_(isolate.isolate_) {}
#else
constexpr IsolateForPointerCompression(
IsolateForSandbox) // NOLINT(runtime/explicit)
: isolate_(nullptr) {}
#endif
inline ExternalPointerTable& GetExternalPointerTableFor(
ExternalPointerTagRange tag_range);
inline ExternalPointerTable::Space* GetExternalPointerTableSpaceFor(
ExternalPointerTagRange tag_range, Address host);
inline CppHeapPointerTable& GetCppHeapPointerTable();
inline CppHeapPointerTable::Space* GetCppHeapPointerTableSpace();
inline Isolate* GetIsolate() const { return isolate_; }
private:
Isolate* const isolate_;
};
#else // V8_COMPRESS_POINTERS
class V8_EXPORT_PRIVATE IsolateForPointerCompression final {
public:
template <typename IsolateT>
constexpr IsolateForPointerCompression(IsolateT*) // NOLINT(runtime/explicit)
{}
constexpr IsolateForPointerCompression(
IsolateForSandbox) // NOLINT(runtime/explicit)
{}
};
#endif // V8_COMPRESS_POINTERS
} // namespace v8::internal
#endif // V8_SANDBOX_ISOLATE_H_