2018-09-07 09:03:35 +00:00
|
|
|
// Copyright 2018 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 INCLUDE_V8_INTERNAL_H_
|
|
|
|
#define INCLUDE_V8_INTERNAL_H_
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
2019-03-19 10:36:40 +00:00
|
|
|
#include <string.h>
|
2022-08-25 12:24:48 +00:00
|
|
|
|
|
|
|
#include <atomic>
|
2018-09-07 09:03:35 +00:00
|
|
|
#include <type_traits>
|
|
|
|
|
2020-04-27 16:29:03 +00:00
|
|
|
#include "v8-version.h" // NOLINT(build/include_directory)
|
|
|
|
#include "v8config.h" // NOLINT(build/include_directory)
|
2018-09-07 09:03:35 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
|
2021-07-23 18:29:30 +00:00
|
|
|
class Array;
|
2018-09-07 09:03:35 +00:00
|
|
|
class Context;
|
|
|
|
class Data;
|
|
|
|
class Isolate;
|
2021-07-23 18:29:30 +00:00
|
|
|
template <typename T>
|
|
|
|
class Local;
|
2018-09-07 09:03:35 +00:00
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
2019-01-08 09:48:09 +00:00
|
|
|
class Isolate;
|
|
|
|
|
2018-10-11 23:23:33 +00:00
|
|
|
typedef uintptr_t Address;
|
|
|
|
static const Address kNullAddress = 0;
|
2018-09-07 09:03:35 +00:00
|
|
|
|
[sandbox] Implement GC for the external pointer table
The external pointer table is now managed by the GC, which marks entries
that are alive during major GC, then sweeps the table afterwards to free
all dead entries and build a free list from them. For now, only major GCs
are supported, Scavenger GCs do not interact with the external pointer table.
In more detail, garbage collection of the external pointer table works
as follows:
1. The external pointer table now reserves a large region of virtual
address space for its backing buffer and is then never reallocated,
only grown in place until the maximum size is reached.
2. When the GC's marking visitor marks a HeapObject with an external
pointer as alive, it also marks the corresponding external pointer
table entry as alive. This can happen on a background thread.
3. For that, it uses the MSB of each entry in the table to indicate
whether the entry has been marked or not. This works because the MSB
is always cleared during the AND-based type check performed when
accessing an external pointer.
4. After marking, the external pointer table is swept while the mutator
is stopped. This builds an inline, singly-linked freelist of all
newly-dead and previously-free entries.
5. When allocating an entry from the table, the first entry on the
freelist is used. If the freelist is empty, the table grows,
populating the freelist with the new entries.
6. Every newly-allocated entry is marked as alive, and every store to an
existing entry also automatically marks that entry as alive (by also
setting the MSB). This simplifies the design of the table GC with
regards to concurrency (See ExternalPointerTable::Mark).
Bug: v8:10391
Change-Id: I8877fdf5576af3761bde65298951bb09e601bd14
Cq-Include-Trybots: luci.v8.try:v8_linux64_heap_sandbox_dbg_ng,v8_linux_arm64_sim_heap_sandbox_dbg_ng
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3359625
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Maya Lekova <mslekova@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Samuel Groß <saelo@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78708}
2022-01-20 16:01:41 +00:00
|
|
|
constexpr int KB = 1024;
|
|
|
|
constexpr int MB = KB * 1024;
|
|
|
|
constexpr int GB = MB * 1024;
|
|
|
|
#ifdef V8_TARGET_ARCH_X64
|
|
|
|
constexpr size_t TB = size_t{GB} * 1024;
|
|
|
|
#endif
|
|
|
|
|
2018-09-07 09:03:35 +00:00
|
|
|
/**
|
|
|
|
* Configuration of tagging scheme.
|
|
|
|
*/
|
2018-11-20 16:03:16 +00:00
|
|
|
const int kApiSystemPointerSize = sizeof(void*);
|
|
|
|
const int kApiDoubleSize = sizeof(double);
|
2019-03-01 15:34:48 +00:00
|
|
|
const int kApiInt32Size = sizeof(int32_t);
|
2018-11-20 16:03:16 +00:00
|
|
|
const int kApiInt64Size = sizeof(int64_t);
|
2021-04-13 12:12:02 +00:00
|
|
|
const int kApiSizetSize = sizeof(size_t);
|
2018-09-07 09:03:35 +00:00
|
|
|
|
|
|
|
// Tag information for HeapObject.
|
|
|
|
const int kHeapObjectTag = 1;
|
|
|
|
const int kWeakHeapObjectTag = 3;
|
|
|
|
const int kHeapObjectTagSize = 2;
|
|
|
|
const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
|
[csa][codegen] Optimize IsStrong/IsWeakOrCleared
The way to determine whether a MaybeObject is a strong or weak
reference to the heap object is to check its lowest two bits.
However, if the MaybeObject is known to not be a smi, that is, the
lowest bit is known to be 1, we can check one bit instead. This
allows Turbofan to select better instructions:
x64:
Before:
movl r9,r11
andl r9,0x3
cmpb r9l,0x1
After:
testb r11,0x2
arm64:
Before:
and w8, w7, #0x3
cmp w8, #0x1 (1)
b.ne #+0x320
After:
tbnz w7, #1, #+0x320
Change-Id: I03623183406ad7d920c96a752651e0116a22832e
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3861310
Reviewed-by: Nico Hartmann <nicohartmann@chromium.org>
Commit-Queue: Hao A Xu <hao.a.xu@intel.com>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/main@{#83153}
2022-09-13 13:40:05 +00:00
|
|
|
const intptr_t kHeapObjectReferenceTagMask = 1 << (kHeapObjectTagSize - 1);
|
2018-09-07 09:03:35 +00:00
|
|
|
|
2021-04-06 12:01:44 +00:00
|
|
|
// Tag information for fowarding pointers stored in object headers.
|
|
|
|
// 0b00 at the lowest 2 bits in the header indicates that the map word is a
|
|
|
|
// forwarding pointer.
|
|
|
|
const int kForwardingTag = 0;
|
|
|
|
const int kForwardingTagSize = 2;
|
|
|
|
const intptr_t kForwardingTagMask = (1 << kForwardingTagSize) - 1;
|
|
|
|
|
2018-09-07 09:03:35 +00:00
|
|
|
// Tag information for Smi.
|
|
|
|
const int kSmiTag = 0;
|
|
|
|
const int kSmiTagSize = 1;
|
|
|
|
const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
|
|
|
|
|
|
|
|
template <size_t tagged_ptr_size>
|
|
|
|
struct SmiTagging;
|
|
|
|
|
2019-05-09 14:15:47 +00:00
|
|
|
constexpr intptr_t kIntptrAllBitsSet = intptr_t{-1};
|
|
|
|
constexpr uintptr_t kUintptrAllBitsSet =
|
|
|
|
static_cast<uintptr_t>(kIntptrAllBitsSet);
|
|
|
|
|
2018-09-07 09:03:35 +00:00
|
|
|
// Smi constants for systems where tagged pointer is a 32-bit value.
|
|
|
|
template <>
|
|
|
|
struct SmiTagging<4> {
|
|
|
|
enum { kSmiShiftSize = 0, kSmiValueSize = 31 };
|
2019-05-09 14:15:47 +00:00
|
|
|
|
|
|
|
static constexpr intptr_t kSmiMinValue =
|
|
|
|
static_cast<intptr_t>(kUintptrAllBitsSet << (kSmiValueSize - 1));
|
|
|
|
static constexpr intptr_t kSmiMaxValue = -(kSmiMinValue + 1);
|
|
|
|
|
2018-10-11 23:23:33 +00:00
|
|
|
V8_INLINE static int SmiToInt(const internal::Address value) {
|
2018-09-07 09:03:35 +00:00
|
|
|
int shift_bits = kSmiTagSize + kSmiShiftSize;
|
2019-08-23 12:40:12 +00:00
|
|
|
// Truncate and shift down (requires >> to be sign extending).
|
|
|
|
return static_cast<int32_t>(static_cast<uint32_t>(value)) >> shift_bits;
|
2018-09-07 09:03:35 +00:00
|
|
|
}
|
|
|
|
V8_INLINE static constexpr bool IsValidSmi(intptr_t value) {
|
2019-05-09 14:15:47 +00:00
|
|
|
// Is value in range [kSmiMinValue, kSmiMaxValue].
|
|
|
|
// Use unsigned operations in order to avoid undefined behaviour in case of
|
|
|
|
// signed integer overflow.
|
|
|
|
return (static_cast<uintptr_t>(value) -
|
|
|
|
static_cast<uintptr_t>(kSmiMinValue)) <=
|
|
|
|
(static_cast<uintptr_t>(kSmiMaxValue) -
|
|
|
|
static_cast<uintptr_t>(kSmiMinValue));
|
2018-09-07 09:03:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Smi constants for systems where tagged pointer is a 64-bit value.
|
|
|
|
template <>
|
|
|
|
struct SmiTagging<8> {
|
|
|
|
enum { kSmiShiftSize = 31, kSmiValueSize = 32 };
|
2019-05-09 14:15:47 +00:00
|
|
|
|
|
|
|
static constexpr intptr_t kSmiMinValue =
|
|
|
|
static_cast<intptr_t>(kUintptrAllBitsSet << (kSmiValueSize - 1));
|
|
|
|
static constexpr intptr_t kSmiMaxValue = -(kSmiMinValue + 1);
|
|
|
|
|
2018-10-11 23:23:33 +00:00
|
|
|
V8_INLINE static int SmiToInt(const internal::Address value) {
|
2018-09-07 09:03:35 +00:00
|
|
|
int shift_bits = kSmiTagSize + kSmiShiftSize;
|
|
|
|
// Shift down and throw away top 32 bits.
|
2018-10-11 23:23:33 +00:00
|
|
|
return static_cast<int>(static_cast<intptr_t>(value) >> shift_bits);
|
2018-09-07 09:03:35 +00:00
|
|
|
}
|
|
|
|
V8_INLINE static constexpr bool IsValidSmi(intptr_t value) {
|
|
|
|
// To be representable as a long smi, the value must be a 32-bit integer.
|
|
|
|
return (value == static_cast<int32_t>(value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-01 15:34:48 +00:00
|
|
|
#ifdef V8_COMPRESS_POINTERS
|
[sandbox] Implement GC for the external pointer table
The external pointer table is now managed by the GC, which marks entries
that are alive during major GC, then sweeps the table afterwards to free
all dead entries and build a free list from them. For now, only major GCs
are supported, Scavenger GCs do not interact with the external pointer table.
In more detail, garbage collection of the external pointer table works
as follows:
1. The external pointer table now reserves a large region of virtual
address space for its backing buffer and is then never reallocated,
only grown in place until the maximum size is reached.
2. When the GC's marking visitor marks a HeapObject with an external
pointer as alive, it also marks the corresponding external pointer
table entry as alive. This can happen on a background thread.
3. For that, it uses the MSB of each entry in the table to indicate
whether the entry has been marked or not. This works because the MSB
is always cleared during the AND-based type check performed when
accessing an external pointer.
4. After marking, the external pointer table is swept while the mutator
is stopped. This builds an inline, singly-linked freelist of all
newly-dead and previously-free entries.
5. When allocating an entry from the table, the first entry on the
freelist is used. If the freelist is empty, the table grows,
populating the freelist with the new entries.
6. Every newly-allocated entry is marked as alive, and every store to an
existing entry also automatically marks that entry as alive (by also
setting the MSB). This simplifies the design of the table GC with
regards to concurrency (See ExternalPointerTable::Mark).
Bug: v8:10391
Change-Id: I8877fdf5576af3761bde65298951bb09e601bd14
Cq-Include-Trybots: luci.v8.try:v8_linux64_heap_sandbox_dbg_ng,v8_linux_arm64_sim_heap_sandbox_dbg_ng
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3359625
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Maya Lekova <mslekova@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Samuel Groß <saelo@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78708}
2022-01-20 16:01:41 +00:00
|
|
|
// See v8:7703 or src/common/ptr-compr-inl.h for details about pointer
|
|
|
|
// compression.
|
|
|
|
constexpr size_t kPtrComprCageReservationSize = size_t{1} << 32;
|
|
|
|
constexpr size_t kPtrComprCageBaseAlignment = size_t{1} << 32;
|
|
|
|
|
2018-09-07 09:03:35 +00:00
|
|
|
static_assert(
|
2018-11-20 16:03:16 +00:00
|
|
|
kApiSystemPointerSize == kApiInt64Size,
|
2018-09-07 09:03:35 +00:00
|
|
|
"Pointer compression can be enabled only for 64-bit architectures");
|
2019-03-06 10:24:13 +00:00
|
|
|
const int kApiTaggedSize = kApiInt32Size;
|
|
|
|
#else
|
|
|
|
const int kApiTaggedSize = kApiSystemPointerSize;
|
2018-11-30 12:17:47 +00:00
|
|
|
#endif
|
|
|
|
|
2020-04-14 12:33:12 +00:00
|
|
|
constexpr bool PointerCompressionIsEnabled() {
|
|
|
|
return kApiTaggedSize != kApiSystemPointerSize;
|
|
|
|
}
|
|
|
|
|
[sandbox] Implement GC for the external pointer table
The external pointer table is now managed by the GC, which marks entries
that are alive during major GC, then sweeps the table afterwards to free
all dead entries and build a free list from them. For now, only major GCs
are supported, Scavenger GCs do not interact with the external pointer table.
In more detail, garbage collection of the external pointer table works
as follows:
1. The external pointer table now reserves a large region of virtual
address space for its backing buffer and is then never reallocated,
only grown in place until the maximum size is reached.
2. When the GC's marking visitor marks a HeapObject with an external
pointer as alive, it also marks the corresponding external pointer
table entry as alive. This can happen on a background thread.
3. For that, it uses the MSB of each entry in the table to indicate
whether the entry has been marked or not. This works because the MSB
is always cleared during the AND-based type check performed when
accessing an external pointer.
4. After marking, the external pointer table is swept while the mutator
is stopped. This builds an inline, singly-linked freelist of all
newly-dead and previously-free entries.
5. When allocating an entry from the table, the first entry on the
freelist is used. If the freelist is empty, the table grows,
populating the freelist with the new entries.
6. Every newly-allocated entry is marked as alive, and every store to an
existing entry also automatically marks that entry as alive (by also
setting the MSB). This simplifies the design of the table GC with
regards to concurrency (See ExternalPointerTable::Mark).
Bug: v8:10391
Change-Id: I8877fdf5576af3761bde65298951bb09e601bd14
Cq-Include-Trybots: luci.v8.try:v8_linux64_heap_sandbox_dbg_ng,v8_linux_arm64_sim_heap_sandbox_dbg_ng
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3359625
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Maya Lekova <mslekova@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Samuel Groß <saelo@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78708}
2022-01-20 16:01:41 +00:00
|
|
|
#ifdef V8_31BIT_SMIS_ON_64BIT_ARCH
|
|
|
|
using PlatformSmiTagging = SmiTagging<kApiInt32Size>;
|
|
|
|
#else
|
|
|
|
using PlatformSmiTagging = SmiTagging<kApiTaggedSize>;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// TODO(ishell): Consinder adding kSmiShiftBits = kSmiShiftSize + kSmiTagSize
|
|
|
|
// since it's used much more often than the inividual constants.
|
|
|
|
const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
|
|
|
|
const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
|
|
|
|
const int kSmiMinValue = static_cast<int>(PlatformSmiTagging::kSmiMinValue);
|
|
|
|
const int kSmiMaxValue = static_cast<int>(PlatformSmiTagging::kSmiMaxValue);
|
|
|
|
constexpr bool SmiValuesAre31Bits() { return kSmiValueSize == 31; }
|
|
|
|
constexpr bool SmiValuesAre32Bits() { return kSmiValueSize == 32; }
|
|
|
|
|
|
|
|
V8_INLINE static constexpr internal::Address IntToSmi(int value) {
|
|
|
|
return (static_cast<Address>(value) << (kSmiTagSize + kSmiShiftSize)) |
|
|
|
|
kSmiTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sandbox related types, constants, and functions.
|
|
|
|
*/
|
|
|
|
constexpr bool SandboxIsEnabled() {
|
2022-05-13 11:10:04 +00:00
|
|
|
#ifdef V8_ENABLE_SANDBOX
|
[sandbox] Implement GC for the external pointer table
The external pointer table is now managed by the GC, which marks entries
that are alive during major GC, then sweeps the table afterwards to free
all dead entries and build a free list from them. For now, only major GCs
are supported, Scavenger GCs do not interact with the external pointer table.
In more detail, garbage collection of the external pointer table works
as follows:
1. The external pointer table now reserves a large region of virtual
address space for its backing buffer and is then never reallocated,
only grown in place until the maximum size is reached.
2. When the GC's marking visitor marks a HeapObject with an external
pointer as alive, it also marks the corresponding external pointer
table entry as alive. This can happen on a background thread.
3. For that, it uses the MSB of each entry in the table to indicate
whether the entry has been marked or not. This works because the MSB
is always cleared during the AND-based type check performed when
accessing an external pointer.
4. After marking, the external pointer table is swept while the mutator
is stopped. This builds an inline, singly-linked freelist of all
newly-dead and previously-free entries.
5. When allocating an entry from the table, the first entry on the
freelist is used. If the freelist is empty, the table grows,
populating the freelist with the new entries.
6. Every newly-allocated entry is marked as alive, and every store to an
existing entry also automatically marks that entry as alive (by also
setting the MSB). This simplifies the design of the table GC with
regards to concurrency (See ExternalPointerTable::Mark).
Bug: v8:10391
Change-Id: I8877fdf5576af3761bde65298951bb09e601bd14
Cq-Include-Trybots: luci.v8.try:v8_linux64_heap_sandbox_dbg_ng,v8_linux_arm64_sim_heap_sandbox_dbg_ng
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3359625
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Maya Lekova <mslekova@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Samuel Groß <saelo@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78708}
2022-01-20 16:01:41 +00:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// SandboxedPointers are guaranteed to point into the sandbox. This is achieved
|
|
|
|
// for example by storing them as offset rather than as raw pointers.
|
|
|
|
using SandboxedPointer_t = Address;
|
|
|
|
|
2022-05-13 11:10:04 +00:00
|
|
|
#ifdef V8_ENABLE_SANDBOX
|
[sandbox] Implement GC for the external pointer table
The external pointer table is now managed by the GC, which marks entries
that are alive during major GC, then sweeps the table afterwards to free
all dead entries and build a free list from them. For now, only major GCs
are supported, Scavenger GCs do not interact with the external pointer table.
In more detail, garbage collection of the external pointer table works
as follows:
1. The external pointer table now reserves a large region of virtual
address space for its backing buffer and is then never reallocated,
only grown in place until the maximum size is reached.
2. When the GC's marking visitor marks a HeapObject with an external
pointer as alive, it also marks the corresponding external pointer
table entry as alive. This can happen on a background thread.
3. For that, it uses the MSB of each entry in the table to indicate
whether the entry has been marked or not. This works because the MSB
is always cleared during the AND-based type check performed when
accessing an external pointer.
4. After marking, the external pointer table is swept while the mutator
is stopped. This builds an inline, singly-linked freelist of all
newly-dead and previously-free entries.
5. When allocating an entry from the table, the first entry on the
freelist is used. If the freelist is empty, the table grows,
populating the freelist with the new entries.
6. Every newly-allocated entry is marked as alive, and every store to an
existing entry also automatically marks that entry as alive (by also
setting the MSB). This simplifies the design of the table GC with
regards to concurrency (See ExternalPointerTable::Mark).
Bug: v8:10391
Change-Id: I8877fdf5576af3761bde65298951bb09e601bd14
Cq-Include-Trybots: luci.v8.try:v8_linux64_heap_sandbox_dbg_ng,v8_linux_arm64_sim_heap_sandbox_dbg_ng
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3359625
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Maya Lekova <mslekova@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Samuel Groß <saelo@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78708}
2022-01-20 16:01:41 +00:00
|
|
|
|
|
|
|
// Size of the sandbox, excluding the guard regions surrounding it.
|
2022-06-15 10:26:40 +00:00
|
|
|
#ifdef V8_TARGET_OS_ANDROID
|
2022-05-19 09:35:10 +00:00
|
|
|
// On Android, most 64-bit devices seem to be configured with only 39 bits of
|
|
|
|
// virtual address space for userspace. As such, limit the sandbox to 128GB (a
|
|
|
|
// quarter of the total available address space).
|
|
|
|
constexpr size_t kSandboxSizeLog2 = 37; // 128 GB
|
|
|
|
#else
|
|
|
|
// Everywhere else use a 1TB sandbox.
|
[sandbox] Implement GC for the external pointer table
The external pointer table is now managed by the GC, which marks entries
that are alive during major GC, then sweeps the table afterwards to free
all dead entries and build a free list from them. For now, only major GCs
are supported, Scavenger GCs do not interact with the external pointer table.
In more detail, garbage collection of the external pointer table works
as follows:
1. The external pointer table now reserves a large region of virtual
address space for its backing buffer and is then never reallocated,
only grown in place until the maximum size is reached.
2. When the GC's marking visitor marks a HeapObject with an external
pointer as alive, it also marks the corresponding external pointer
table entry as alive. This can happen on a background thread.
3. For that, it uses the MSB of each entry in the table to indicate
whether the entry has been marked or not. This works because the MSB
is always cleared during the AND-based type check performed when
accessing an external pointer.
4. After marking, the external pointer table is swept while the mutator
is stopped. This builds an inline, singly-linked freelist of all
newly-dead and previously-free entries.
5. When allocating an entry from the table, the first entry on the
freelist is used. If the freelist is empty, the table grows,
populating the freelist with the new entries.
6. Every newly-allocated entry is marked as alive, and every store to an
existing entry also automatically marks that entry as alive (by also
setting the MSB). This simplifies the design of the table GC with
regards to concurrency (See ExternalPointerTable::Mark).
Bug: v8:10391
Change-Id: I8877fdf5576af3761bde65298951bb09e601bd14
Cq-Include-Trybots: luci.v8.try:v8_linux64_heap_sandbox_dbg_ng,v8_linux_arm64_sim_heap_sandbox_dbg_ng
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3359625
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Maya Lekova <mslekova@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Samuel Groß <saelo@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78708}
2022-01-20 16:01:41 +00:00
|
|
|
constexpr size_t kSandboxSizeLog2 = 40; // 1 TB
|
2022-10-11 10:53:04 +00:00
|
|
|
#endif // V8_TARGET_OS_ANDROID
|
[sandbox] Implement GC for the external pointer table
The external pointer table is now managed by the GC, which marks entries
that are alive during major GC, then sweeps the table afterwards to free
all dead entries and build a free list from them. For now, only major GCs
are supported, Scavenger GCs do not interact with the external pointer table.
In more detail, garbage collection of the external pointer table works
as follows:
1. The external pointer table now reserves a large region of virtual
address space for its backing buffer and is then never reallocated,
only grown in place until the maximum size is reached.
2. When the GC's marking visitor marks a HeapObject with an external
pointer as alive, it also marks the corresponding external pointer
table entry as alive. This can happen on a background thread.
3. For that, it uses the MSB of each entry in the table to indicate
whether the entry has been marked or not. This works because the MSB
is always cleared during the AND-based type check performed when
accessing an external pointer.
4. After marking, the external pointer table is swept while the mutator
is stopped. This builds an inline, singly-linked freelist of all
newly-dead and previously-free entries.
5. When allocating an entry from the table, the first entry on the
freelist is used. If the freelist is empty, the table grows,
populating the freelist with the new entries.
6. Every newly-allocated entry is marked as alive, and every store to an
existing entry also automatically marks that entry as alive (by also
setting the MSB). This simplifies the design of the table GC with
regards to concurrency (See ExternalPointerTable::Mark).
Bug: v8:10391
Change-Id: I8877fdf5576af3761bde65298951bb09e601bd14
Cq-Include-Trybots: luci.v8.try:v8_linux64_heap_sandbox_dbg_ng,v8_linux_arm64_sim_heap_sandbox_dbg_ng
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3359625
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Maya Lekova <mslekova@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Samuel Groß <saelo@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78708}
2022-01-20 16:01:41 +00:00
|
|
|
constexpr size_t kSandboxSize = 1ULL << kSandboxSizeLog2;
|
|
|
|
|
|
|
|
// Required alignment of the sandbox. For simplicity, we require the
|
|
|
|
// size of the guard regions to be a multiple of this, so that this specifies
|
|
|
|
// the alignment of the sandbox including and excluding surrounding guard
|
|
|
|
// regions. The alignment requirement is due to the pointer compression cage
|
|
|
|
// being located at the start of the sandbox.
|
|
|
|
constexpr size_t kSandboxAlignment = kPtrComprCageBaseAlignment;
|
|
|
|
|
|
|
|
// Sandboxed pointers are stored inside the heap as offset from the sandbox
|
|
|
|
// base shifted to the left. This way, it is guaranteed that the offset is
|
|
|
|
// smaller than the sandbox size after shifting it to the right again. This
|
|
|
|
// constant specifies the shift amount.
|
|
|
|
constexpr uint64_t kSandboxedPointerShift = 64 - kSandboxSizeLog2;
|
|
|
|
|
|
|
|
// Size of the guard regions surrounding the sandbox. This assumes a worst-case
|
|
|
|
// scenario of a 32-bit unsigned index used to access an array of 64-bit
|
|
|
|
// values.
|
|
|
|
constexpr size_t kSandboxGuardRegionSize = 32ULL * GB;
|
|
|
|
|
|
|
|
static_assert((kSandboxGuardRegionSize % kSandboxAlignment) == 0,
|
|
|
|
"The size of the guard regions around the sandbox must be a "
|
|
|
|
"multiple of its required alignment.");
|
|
|
|
|
|
|
|
// On OSes where reserving virtual memory is too expensive to reserve the
|
|
|
|
// entire address space backing the sandbox, notably Windows pre 8.1, we create
|
|
|
|
// a partially reserved sandbox that doesn't actually reserve most of the
|
|
|
|
// memory, and so doesn't have the desired security properties as unrelated
|
|
|
|
// memory allocations could end up inside of it, but which still ensures that
|
|
|
|
// objects that should be located inside the sandbox are allocated within
|
|
|
|
// kSandboxSize bytes from the start of the sandbox. The minimum size of the
|
|
|
|
// region that is actually reserved for such a sandbox is specified by this
|
|
|
|
// constant and should be big enough to contain the pointer compression cage as
|
|
|
|
// well as the ArrayBuffer partition.
|
|
|
|
constexpr size_t kSandboxMinimumReservationSize = 8ULL * GB;
|
|
|
|
|
|
|
|
static_assert(kSandboxMinimumReservationSize > kPtrComprCageReservationSize,
|
|
|
|
"The minimum reservation size for a sandbox must be larger than "
|
|
|
|
"the pointer compression cage contained within it.");
|
|
|
|
|
2022-10-11 10:53:04 +00:00
|
|
|
// The maximum buffer size allowed inside the sandbox. This is mostly dependent
|
|
|
|
// on the size of the guard regions around the sandbox: an attacker must not be
|
|
|
|
// able to construct a buffer that appears larger than the guard regions and
|
|
|
|
// thereby "reach out of" the sandbox.
|
|
|
|
constexpr size_t kMaxSafeBufferSizeForSandbox = 32ULL * GB - 1;
|
|
|
|
static_assert(kMaxSafeBufferSizeForSandbox <= kSandboxGuardRegionSize,
|
|
|
|
"The maximum allowed buffer size must not be larger than the "
|
|
|
|
"sandbox's guard regions");
|
|
|
|
|
|
|
|
constexpr size_t kBoundedSizeShift = 29;
|
|
|
|
static_assert(1ULL << (64 - kBoundedSizeShift) ==
|
|
|
|
kMaxSafeBufferSizeForSandbox + 1,
|
|
|
|
"The maximum size of a BoundedSize must be synchronized with the "
|
|
|
|
"kMaxSafeBufferSizeForSandbox");
|
|
|
|
|
2022-07-26 19:35:32 +00:00
|
|
|
#endif // V8_ENABLE_SANDBOX
|
|
|
|
|
|
|
|
#ifdef V8_COMPRESS_POINTERS
|
|
|
|
|
2022-01-28 10:21:09 +00:00
|
|
|
// The size of the virtual memory reservation for an external pointer table.
|
|
|
|
// This determines the maximum number of entries in a table. Using a maximum
|
|
|
|
// size allows omitting bounds checks on table accesses if the indices are
|
|
|
|
// guaranteed (e.g. through shifting) to be below the maximum index. This
|
|
|
|
// value must be a power of two.
|
2022-09-19 18:28:30 +00:00
|
|
|
static const size_t kExternalPointerTableReservationSize = 512 * MB;
|
2022-01-28 10:21:09 +00:00
|
|
|
|
|
|
|
// The maximum number of entries in an external pointer table.
|
2022-07-26 19:35:32 +00:00
|
|
|
static const size_t kMaxExternalPointers =
|
2022-01-28 10:21:09 +00:00
|
|
|
kExternalPointerTableReservationSize / kApiSystemPointerSize;
|
|
|
|
|
|
|
|
// The external pointer table indices stored in HeapObjects as external
|
|
|
|
// pointers are shifted to the left by this amount to guarantee that they are
|
|
|
|
// smaller than the maximum table size.
|
2022-09-19 18:28:30 +00:00
|
|
|
static const uint32_t kExternalPointerIndexShift = 6;
|
2022-07-26 19:35:32 +00:00
|
|
|
static_assert((1 << (32 - kExternalPointerIndexShift)) == kMaxExternalPointers,
|
2022-01-28 10:21:09 +00:00
|
|
|
"kExternalPointerTableReservationSize and "
|
|
|
|
"kExternalPointerIndexShift don't match");
|
|
|
|
|
2022-07-26 19:35:32 +00:00
|
|
|
#else // !V8_COMPRESS_POINTERS
|
|
|
|
|
|
|
|
// Needed for the V8.SandboxedExternalPointersCount histogram.
|
|
|
|
static const size_t kMaxExternalPointers = 0;
|
|
|
|
|
|
|
|
#endif // V8_COMPRESS_POINTERS
|
[sandbox] Implement GC for the external pointer table
The external pointer table is now managed by the GC, which marks entries
that are alive during major GC, then sweeps the table afterwards to free
all dead entries and build a free list from them. For now, only major GCs
are supported, Scavenger GCs do not interact with the external pointer table.
In more detail, garbage collection of the external pointer table works
as follows:
1. The external pointer table now reserves a large region of virtual
address space for its backing buffer and is then never reallocated,
only grown in place until the maximum size is reached.
2. When the GC's marking visitor marks a HeapObject with an external
pointer as alive, it also marks the corresponding external pointer
table entry as alive. This can happen on a background thread.
3. For that, it uses the MSB of each entry in the table to indicate
whether the entry has been marked or not. This works because the MSB
is always cleared during the AND-based type check performed when
accessing an external pointer.
4. After marking, the external pointer table is swept while the mutator
is stopped. This builds an inline, singly-linked freelist of all
newly-dead and previously-free entries.
5. When allocating an entry from the table, the first entry on the
freelist is used. If the freelist is empty, the table grows,
populating the freelist with the new entries.
6. Every newly-allocated entry is marked as alive, and every store to an
existing entry also automatically marks that entry as alive (by also
setting the MSB). This simplifies the design of the table GC with
regards to concurrency (See ExternalPointerTable::Mark).
Bug: v8:10391
Change-Id: I8877fdf5576af3761bde65298951bb09e601bd14
Cq-Include-Trybots: luci.v8.try:v8_linux64_heap_sandbox_dbg_ng,v8_linux_arm64_sim_heap_sandbox_dbg_ng
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3359625
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Maya Lekova <mslekova@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Samuel Groß <saelo@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78708}
2022-01-20 16:01:41 +00:00
|
|
|
|
2022-07-11 12:21:59 +00:00
|
|
|
// A ExternalPointerHandle represents a (opaque) reference to an external
|
|
|
|
// pointer that can be stored inside the sandbox. A ExternalPointerHandle has
|
|
|
|
// meaning only in combination with an (active) Isolate as it references an
|
|
|
|
// external pointer stored in the currently active Isolate's
|
|
|
|
// ExternalPointerTable. Internally, an ExternalPointerHandles is simply an
|
|
|
|
// index into an ExternalPointerTable that is shifted to the left to guarantee
|
|
|
|
// that it is smaller than the size of the table.
|
|
|
|
using ExternalPointerHandle = uint32_t;
|
|
|
|
|
|
|
|
// ExternalPointers point to objects located outside the sandbox. When
|
|
|
|
// sandboxed external pointers are enabled, these are stored on heap as
|
|
|
|
// ExternalPointerHandles, otherwise they are simply raw pointers.
|
2022-09-12 07:20:02 +00:00
|
|
|
#ifdef V8_ENABLE_SANDBOX
|
2022-07-11 12:21:59 +00:00
|
|
|
using ExternalPointer_t = ExternalPointerHandle;
|
|
|
|
#else
|
|
|
|
using ExternalPointer_t = Address;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// When the sandbox is enabled, external pointers are stored in an external
|
|
|
|
// pointer table and are referenced from HeapObjects through an index (a
|
|
|
|
// "handle"). When stored in the table, the pointers are tagged with per-type
|
|
|
|
// tags to prevent type confusion attacks between different external objects.
|
2022-07-25 09:07:30 +00:00
|
|
|
// Besides type information bits, these tags also contain the GC marking bit
|
|
|
|
// which indicates whether the pointer table entry is currently alive. When a
|
2022-07-11 12:21:59 +00:00
|
|
|
// pointer is written into the table, the tag is ORed into the top bits. When
|
|
|
|
// that pointer is later loaded from the table, it is ANDed with the inverse of
|
2022-07-25 09:07:30 +00:00
|
|
|
// the expected tag. If the expected and actual type differ, this will leave
|
|
|
|
// some of the top bits of the pointer set, rendering the pointer inaccessible.
|
|
|
|
// The AND operation also removes the GC marking bit from the pointer.
|
|
|
|
//
|
|
|
|
// The tags are constructed such that UNTAG(TAG(0, T1), T2) != 0 for any two
|
|
|
|
// (distinct) tags T1 and T2. In practice, this is achieved by generating tags
|
|
|
|
// that all have the same number of zeroes and ones but different bit patterns.
|
|
|
|
// With N type tag bits, this allows for (N choose N/2) possible type tags.
|
|
|
|
// Besides the type tag bits, the tags also have the GC marking bit set so that
|
|
|
|
// the marking bit is automatically set when a pointer is written into the
|
|
|
|
// external pointer table (in which case it is clearly alive) and is cleared
|
|
|
|
// when the pointer is loaded. The exception to this is the free entry tag,
|
|
|
|
// which doesn't have the mark bit set, as the entry is not alive. This
|
|
|
|
// construction allows performing the type check and removing GC marking bits
|
|
|
|
// from the pointer in one efficient operation (bitwise AND). The number of
|
|
|
|
// available bits is limited in the following way: on x64, bits [47, 64) are
|
|
|
|
// generally available for tagging (userspace has 47 address bits available).
|
|
|
|
// On Arm64, userspace typically has a 40 or 48 bit address space. However, due
|
|
|
|
// to top-byte ignore (TBI) and memory tagging (MTE), the top byte is unusable
|
|
|
|
// for type checks as type-check failures would go unnoticed or collide with
|
|
|
|
// MTE bits. Some bits of the top byte can, however, still be used for the GC
|
|
|
|
// marking bit. The bits available for the type tags are therefore limited to
|
|
|
|
// [48, 56), i.e. (8 choose 4) = 70 different types.
|
|
|
|
// The following options exist to increase the number of possible types:
|
|
|
|
// - Using multiple ExternalPointerTables since tags can safely be reused
|
|
|
|
// across different tables
|
|
|
|
// - Using "extended" type checks, where additional type information is stored
|
|
|
|
// either in an adjacent pointer table entry or at the pointed-to location
|
|
|
|
// - Using a different tagging scheme, for example based on XOR which would
|
|
|
|
// allow for 2**8 different tags but require a separate operation to remove
|
|
|
|
// the marking bit
|
|
|
|
//
|
|
|
|
// The external pointer sandboxing mechanism ensures that every access to an
|
|
|
|
// external pointer field will result in a valid pointer of the expected type
|
|
|
|
// even in the presence of an attacker able to corrupt memory inside the
|
|
|
|
// sandbox. However, if any data related to the external object is stored
|
|
|
|
// inside the sandbox it may still be corrupted and so must be validated before
|
|
|
|
// use or moved into the external object. Further, an attacker will always be
|
|
|
|
// able to substitute different external pointers of the same type for each
|
|
|
|
// other. Therefore, code using external pointers must be written in a
|
|
|
|
// "substitution-safe" way, i.e. it must always be possible to substitute
|
|
|
|
// external pointers of the same type without causing memory corruption outside
|
|
|
|
// of the sandbox. Generally this is achieved by referencing any group of
|
|
|
|
// related external objects through a single external pointer.
|
|
|
|
//
|
|
|
|
// Currently we use bit 62 for the marking bit which should always be unused as
|
|
|
|
// it's part of the non-canonical address range. When Arm's top-byte ignore
|
|
|
|
// (TBI) is enabled, this bit will be part of the ignored byte, and we assume
|
|
|
|
// that the Embedder is not using this byte (really only this one bit) for any
|
|
|
|
// other purpose. This bit also does not collide with the memory tagging
|
|
|
|
// extension (MTE) which would use bits [56, 60).
|
2022-11-17 14:20:53 +00:00
|
|
|
//
|
|
|
|
// External pointer tables are also available even when the sandbox is off but
|
|
|
|
// pointer compression is on. In that case, the mechanism can be used to easy
|
|
|
|
// alignment requirements as it turns unaligned 64-bit raw pointers into
|
|
|
|
// aligned 32-bit indices. To "opt-in" to the external pointer table mechanism
|
|
|
|
// for this purpose, instead of using the ExternalPointer accessors one needs to
|
|
|
|
// use ExternalPointerHandles directly and use them to access the pointers in an
|
|
|
|
// ExternalPointerTable.
|
2022-07-25 09:07:30 +00:00
|
|
|
constexpr uint64_t kExternalPointerMarkBit = 1ULL << 62;
|
|
|
|
constexpr uint64_t kExternalPointerTagMask = 0x40ff000000000000;
|
2022-03-15 16:06:24 +00:00
|
|
|
constexpr uint64_t kExternalPointerTagShift = 48;
|
2022-07-25 09:07:30 +00:00
|
|
|
|
|
|
|
// All possible 8-bit type tags.
|
|
|
|
// These are sorted so that tags can be grouped together and it can efficiently
|
|
|
|
// be checked if a tag belongs to a given group. See for example the
|
|
|
|
// IsSharedExternalPointerType routine.
|
|
|
|
constexpr uint64_t kAllExternalPointerTypeTags[] = {
|
|
|
|
0b00001111, 0b00010111, 0b00011011, 0b00011101, 0b00011110, 0b00100111,
|
|
|
|
0b00101011, 0b00101101, 0b00101110, 0b00110011, 0b00110101, 0b00110110,
|
|
|
|
0b00111001, 0b00111010, 0b00111100, 0b01000111, 0b01001011, 0b01001101,
|
|
|
|
0b01001110, 0b01010011, 0b01010101, 0b01010110, 0b01011001, 0b01011010,
|
|
|
|
0b01011100, 0b01100011, 0b01100101, 0b01100110, 0b01101001, 0b01101010,
|
|
|
|
0b01101100, 0b01110001, 0b01110010, 0b01110100, 0b01111000, 0b10000111,
|
|
|
|
0b10001011, 0b10001101, 0b10001110, 0b10010011, 0b10010101, 0b10010110,
|
|
|
|
0b10011001, 0b10011010, 0b10011100, 0b10100011, 0b10100101, 0b10100110,
|
|
|
|
0b10101001, 0b10101010, 0b10101100, 0b10110001, 0b10110010, 0b10110100,
|
|
|
|
0b10111000, 0b11000011, 0b11000101, 0b11000110, 0b11001001, 0b11001010,
|
|
|
|
0b11001100, 0b11010001, 0b11010010, 0b11010100, 0b11011000, 0b11100001,
|
|
|
|
0b11100010, 0b11100100, 0b11101000, 0b11110000};
|
2022-06-27 12:15:33 +00:00
|
|
|
|
2022-11-17 14:20:53 +00:00
|
|
|
#define TAG(i) \
|
|
|
|
((kAllExternalPointerTypeTags[i] << kExternalPointerTagShift) | \
|
|
|
|
kExternalPointerMarkBit)
|
|
|
|
|
[sandbox] Implement GC for the external pointer table
The external pointer table is now managed by the GC, which marks entries
that are alive during major GC, then sweeps the table afterwards to free
all dead entries and build a free list from them. For now, only major GCs
are supported, Scavenger GCs do not interact with the external pointer table.
In more detail, garbage collection of the external pointer table works
as follows:
1. The external pointer table now reserves a large region of virtual
address space for its backing buffer and is then never reallocated,
only grown in place until the maximum size is reached.
2. When the GC's marking visitor marks a HeapObject with an external
pointer as alive, it also marks the corresponding external pointer
table entry as alive. This can happen on a background thread.
3. For that, it uses the MSB of each entry in the table to indicate
whether the entry has been marked or not. This works because the MSB
is always cleared during the AND-based type check performed when
accessing an external pointer.
4. After marking, the external pointer table is swept while the mutator
is stopped. This builds an inline, singly-linked freelist of all
newly-dead and previously-free entries.
5. When allocating an entry from the table, the first entry on the
freelist is used. If the freelist is empty, the table grows,
populating the freelist with the new entries.
6. Every newly-allocated entry is marked as alive, and every store to an
existing entry also automatically marks that entry as alive (by also
setting the MSB). This simplifies the design of the table GC with
regards to concurrency (See ExternalPointerTable::Mark).
Bug: v8:10391
Change-Id: I8877fdf5576af3761bde65298951bb09e601bd14
Cq-Include-Trybots: luci.v8.try:v8_linux64_heap_sandbox_dbg_ng,v8_linux_arm64_sim_heap_sandbox_dbg_ng
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3359625
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Maya Lekova <mslekova@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Samuel Groß <saelo@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78708}
2022-01-20 16:01:41 +00:00
|
|
|
// clang-format off
|
2022-11-17 14:20:53 +00:00
|
|
|
|
2022-07-25 09:07:30 +00:00
|
|
|
// When adding new tags, please ensure that the code using these tags is
|
|
|
|
// "substitution-safe", i.e. still operate safely if external pointers of the
|
|
|
|
// same type are swapped by an attacker. See comment above for more details.
|
2022-07-11 12:21:59 +00:00
|
|
|
|
|
|
|
// Shared external pointers are owned by the shared Isolate and stored in the
|
|
|
|
// shared external pointer table associated with that Isolate, where they can
|
|
|
|
// be accessed from multiple threads at the same time. The objects referenced
|
|
|
|
// in this way must therefore always be thread-safe.
|
2022-11-17 14:20:53 +00:00
|
|
|
#define SHARED_EXTERNAL_POINTER_TAGS(V) \
|
|
|
|
V(kFirstSharedTag, TAG(0)) \
|
|
|
|
V(kWaiterQueueNodeTag, TAG(0)) \
|
|
|
|
V(kExternalStringResourceTag, TAG(1)) \
|
|
|
|
V(kExternalStringResourceDataTag, TAG(2)) \
|
|
|
|
V(kLastSharedTag, TAG(2))
|
2022-07-25 09:07:30 +00:00
|
|
|
|
|
|
|
// External pointers using these tags are kept in a per-Isolate external
|
|
|
|
// pointer table and can only be accessed when this Isolate is active.
|
2022-11-17 14:20:53 +00:00
|
|
|
#define PER_ISOLATE_EXTERNAL_POINTER_TAGS(V) \
|
|
|
|
V(kForeignForeignAddressTag, TAG(10)) \
|
|
|
|
V(kNativeContextMicrotaskQueueTag, TAG(11)) \
|
|
|
|
V(kEmbedderDataSlotPayloadTag, TAG(12)) \
|
2022-12-01 13:30:10 +00:00
|
|
|
/* This tag essentially stands for a `void*` pointer in the V8 API, and */ \
|
|
|
|
/* it is the Embedder's responsibility to ensure type safety (against */ \
|
|
|
|
/* substitution) and lifetime validity of these objects. */ \
|
2022-11-17 14:20:53 +00:00
|
|
|
V(kExternalObjectValueTag, TAG(13)) \
|
|
|
|
V(kCallHandlerInfoCallbackTag, TAG(14)) \
|
|
|
|
V(kAccessorInfoGetterTag, TAG(15)) \
|
|
|
|
V(kAccessorInfoSetterTag, TAG(16)) \
|
|
|
|
V(kWasmInternalFunctionCallTargetTag, TAG(17)) \
|
|
|
|
V(kWasmTypeInfoNativeTypeTag, TAG(18)) \
|
|
|
|
V(kWasmExportedFunctionDataSignatureTag, TAG(19)) \
|
2022-11-29 14:14:58 +00:00
|
|
|
V(kWasmContinuationJmpbufTag, TAG(20)) \
|
|
|
|
V(kArrayBufferExtensionTag, TAG(21))
|
2022-07-25 09:07:30 +00:00
|
|
|
|
|
|
|
// All external pointer tags.
|
|
|
|
#define ALL_EXTERNAL_POINTER_TAGS(V) \
|
|
|
|
SHARED_EXTERNAL_POINTER_TAGS(V) \
|
|
|
|
PER_ISOLATE_EXTERNAL_POINTER_TAGS(V)
|
2022-06-27 12:15:33 +00:00
|
|
|
|
2022-11-17 14:20:53 +00:00
|
|
|
#define EXTERNAL_POINTER_TAG_ENUM(Name, Tag) Name = Tag,
|
2022-07-25 09:07:30 +00:00
|
|
|
#define MAKE_TAG(HasMarkBit, TypeTag) \
|
|
|
|
((static_cast<uint64_t>(TypeTag) << kExternalPointerTagShift) | \
|
|
|
|
(HasMarkBit ? kExternalPointerMarkBit : 0))
|
2022-06-27 12:15:33 +00:00
|
|
|
enum ExternalPointerTag : uint64_t {
|
2022-07-25 09:07:30 +00:00
|
|
|
// Empty tag value. Mostly used as placeholder.
|
2022-08-29 14:03:22 +00:00
|
|
|
kExternalPointerNullTag = MAKE_TAG(0, 0b00000000),
|
2022-07-25 09:07:30 +00:00
|
|
|
// External pointer tag that will match any external pointer. Use with care!
|
2022-08-29 14:03:22 +00:00
|
|
|
kAnyExternalPointerTag = MAKE_TAG(1, 0b11111111),
|
2022-07-25 09:07:30 +00:00
|
|
|
// The free entry tag has all type bits set so every type check with a
|
|
|
|
// different type fails. It also doesn't have the mark bit set as free
|
|
|
|
// entries are (by definition) not alive.
|
2022-08-29 14:03:22 +00:00
|
|
|
kExternalPointerFreeEntryTag = MAKE_TAG(0, 0b11111111),
|
[sandbox] Implement external pointer table compaction
The external pointer table is already to some degree self-compacting: as
the freelist is sorted in ascending order, free entries at the start of
the table should quickly fill up. However, any live entry at the end of
the table makes it impossible to shrink the table, thereby causing
higher memory consumption. To solve this, this CL implements a simple
table compaction algorithm, used when the freelist has become
sufficiently large (currently >= 10% of the table capacity):
- The goal of the algorithm is to shrink the table by freelist_size/2
entries at the end of compaction (during sweeping).
- At the start of the marking phase, the compaction area is computed as
roughly [capacity - freelist_size/2, capacity).
- When an entry is marked as alive that lies inside the compaction
area, a new "relocation entry" is allocated for it from the freelist
and the address of the handle for that entry is stored in that entry.
If there are no more free entries before the compaction area,
compaction is aborted. This is expected to happen rarely and is
logged into a histogram.
- During sweeping, all relocation entries are "resolved": the content
of the old entry is copied into the new entry and the handle is
updated to point to the new entry.
- Finally, the table is shrunk and the last initial_freelist_size/2
entries are decommitted.
See also the comments in the ExternalPointerTable class for more details.
Bug: v8:10391
Change-Id: I28d475c3596590e860421f0a054e2ad4dbebd487
Cq-Include-Trybots: luci.v8.try:v8_linux64_heap_sandbox_dbg_ng,v8_linux_arm64_sim_heap_sandbox_dbg_ng
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3794645
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Commit-Queue: Samuel Groß <saelo@chromium.org>
Cr-Commit-Position: refs/heads/main@{#82484}
2022-08-12 20:08:47 +00:00
|
|
|
// Evacuation entries are used during external pointer table compaction.
|
2022-08-29 14:03:22 +00:00
|
|
|
kExternalPointerEvacuationEntryTag = MAKE_TAG(1, 0b11100111),
|
2022-07-25 09:07:30 +00:00
|
|
|
|
|
|
|
ALL_EXTERNAL_POINTER_TAGS(EXTERNAL_POINTER_TAG_ENUM)
|
2022-06-27 12:15:33 +00:00
|
|
|
};
|
|
|
|
|
2022-07-25 09:07:30 +00:00
|
|
|
#undef MAKE_TAG
|
|
|
|
#undef TAG
|
2022-06-27 12:15:33 +00:00
|
|
|
#undef EXTERNAL_POINTER_TAG_ENUM
|
|
|
|
|
2022-07-25 09:07:30 +00:00
|
|
|
// clang-format on
|
|
|
|
|
2022-07-11 12:21:59 +00:00
|
|
|
// True if the external pointer must be accessed from the shared isolate's
|
|
|
|
// external pointer table.
|
|
|
|
V8_INLINE static constexpr bool IsSharedExternalPointerType(
|
|
|
|
ExternalPointerTag tag) {
|
2022-07-25 09:07:30 +00:00
|
|
|
return tag >= kFirstSharedTag && tag <= kLastSharedTag;
|
2022-07-11 12:21:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 12:15:33 +00:00
|
|
|
// Sanity checks.
|
2022-11-17 14:20:53 +00:00
|
|
|
#define CHECK_SHARED_EXTERNAL_POINTER_TAGS(Tag, ...) \
|
|
|
|
static_assert(IsSharedExternalPointerType(Tag));
|
2022-06-27 12:15:33 +00:00
|
|
|
#define CHECK_NON_SHARED_EXTERNAL_POINTER_TAGS(Tag, ...) \
|
2022-11-17 14:20:53 +00:00
|
|
|
static_assert(!IsSharedExternalPointerType(Tag));
|
2022-06-27 12:15:33 +00:00
|
|
|
|
|
|
|
SHARED_EXTERNAL_POINTER_TAGS(CHECK_SHARED_EXTERNAL_POINTER_TAGS)
|
2022-07-25 09:07:30 +00:00
|
|
|
PER_ISOLATE_EXTERNAL_POINTER_TAGS(CHECK_NON_SHARED_EXTERNAL_POINTER_TAGS)
|
2022-06-27 12:15:33 +00:00
|
|
|
|
|
|
|
#undef CHECK_NON_SHARED_EXTERNAL_POINTER_TAGS
|
|
|
|
#undef CHECK_SHARED_EXTERNAL_POINTER_TAGS
|
|
|
|
|
|
|
|
#undef SHARED_EXTERNAL_POINTER_TAGS
|
|
|
|
#undef EXTERNAL_POINTER_TAGS
|
2021-05-10 10:08:01 +00:00
|
|
|
|
2020-05-13 08:36:23 +00:00
|
|
|
// {obj} must be the raw tagged pointer representation of a HeapObject
|
|
|
|
// that's guaranteed to never be in ReadOnlySpace.
|
|
|
|
V8_EXPORT internal::Isolate* IsolateFromNeverReadOnlySpaceObject(Address obj);
|
|
|
|
|
|
|
|
// Returns if we need to throw when an error occurs. This infers the language
|
|
|
|
// mode based on the current context and the closure. This returns true if the
|
|
|
|
// language mode is strict.
|
|
|
|
V8_EXPORT bool ShouldThrowOnError(v8::internal::Isolate* isolate);
|
2018-09-07 09:03:35 +00:00
|
|
|
/**
|
|
|
|
* This class exports constants and functionality from within v8 that
|
|
|
|
* is necessary to implement inline functions in the v8 api. Don't
|
|
|
|
* depend on functions and constants defined here.
|
|
|
|
*/
|
|
|
|
class Internals {
|
2021-04-06 12:01:44 +00:00
|
|
|
#ifdef V8_MAP_PACKING
|
|
|
|
V8_INLINE static constexpr internal::Address UnpackMapWord(
|
|
|
|
internal::Address mapword) {
|
|
|
|
// TODO(wenyuzhao): Clear header metadata.
|
|
|
|
return mapword ^ kMapWordXorMask;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-09-07 09:03:35 +00:00
|
|
|
public:
|
|
|
|
// These values match non-compiler-dependent values defined within
|
|
|
|
// the implementation of v8.
|
|
|
|
static const int kHeapObjectMapOffset = 0;
|
2019-03-01 15:34:48 +00:00
|
|
|
static const int kMapInstanceTypeOffset = 1 * kApiTaggedSize + kApiInt32Size;
|
|
|
|
static const int kStringResourceOffset =
|
|
|
|
1 * kApiTaggedSize + 2 * kApiInt32Size;
|
2018-11-20 16:03:16 +00:00
|
|
|
|
|
|
|
static const int kOddballKindOffset = 4 * kApiTaggedSize + kApiDoubleSize;
|
|
|
|
static const int kJSObjectHeaderSize = 3 * kApiTaggedSize;
|
|
|
|
static const int kFixedArrayHeaderSize = 2 * kApiTaggedSize;
|
|
|
|
static const int kEmbedderDataArrayHeaderSize = 2 * kApiTaggedSize;
|
2019-03-06 10:24:13 +00:00
|
|
|
static const int kEmbedderDataSlotSize = kApiSystemPointerSize;
|
2022-08-25 12:24:48 +00:00
|
|
|
#ifdef V8_ENABLE_SANDBOX
|
2022-07-11 12:21:59 +00:00
|
|
|
static const int kEmbedderDataSlotExternalPointerOffset = kApiTaggedSize;
|
|
|
|
#else
|
|
|
|
static const int kEmbedderDataSlotExternalPointerOffset = 0;
|
2020-09-29 13:40:21 +00:00
|
|
|
#endif
|
2019-10-17 15:58:38 +00:00
|
|
|
static const int kNativeContextEmbedderDataOffset = 6 * kApiTaggedSize;
|
2021-12-04 00:06:11 +00:00
|
|
|
static const int kStringRepresentationAndEncodingMask = 0x0f;
|
2018-09-07 09:03:35 +00:00
|
|
|
static const int kStringEncodingMask = 0x8;
|
|
|
|
static const int kExternalTwoByteRepresentationTag = 0x02;
|
|
|
|
static const int kExternalOneByteRepresentationTag = 0x0a;
|
|
|
|
|
2018-10-26 13:30:12 +00:00
|
|
|
static const uint32_t kNumIsolateDataSlots = 4;
|
2021-09-20 13:29:46 +00:00
|
|
|
static const int kStackGuardSize = 7 * kApiSystemPointerSize;
|
2022-06-03 07:28:58 +00:00
|
|
|
static const int kBuiltinTier0EntryTableSize = 7 * kApiSystemPointerSize;
|
|
|
|
static const int kBuiltinTier0TableSize = 7 * kApiSystemPointerSize;
|
2018-10-26 13:30:12 +00:00
|
|
|
|
2022-06-20 18:50:10 +00:00
|
|
|
// ExternalPointerTable layout guarantees.
|
|
|
|
static const int kExternalPointerTableBufferOffset = 0;
|
[sandbox] Implement external pointer table compaction
The external pointer table is already to some degree self-compacting: as
the freelist is sorted in ascending order, free entries at the start of
the table should quickly fill up. However, any live entry at the end of
the table makes it impossible to shrink the table, thereby causing
higher memory consumption. To solve this, this CL implements a simple
table compaction algorithm, used when the freelist has become
sufficiently large (currently >= 10% of the table capacity):
- The goal of the algorithm is to shrink the table by freelist_size/2
entries at the end of compaction (during sweeping).
- At the start of the marking phase, the compaction area is computed as
roughly [capacity - freelist_size/2, capacity).
- When an entry is marked as alive that lies inside the compaction
area, a new "relocation entry" is allocated for it from the freelist
and the address of the handle for that entry is stored in that entry.
If there are no more free entries before the compaction area,
compaction is aborted. This is expected to happen rarely and is
logged into a histogram.
- During sweeping, all relocation entries are "resolved": the content
of the old entry is copied into the new entry and the handle is
updated to point to the new entry.
- Finally, the table is shrunk and the last initial_freelist_size/2
entries are decommitted.
See also the comments in the ExternalPointerTable class for more details.
Bug: v8:10391
Change-Id: I28d475c3596590e860421f0a054e2ad4dbebd487
Cq-Include-Trybots: luci.v8.try:v8_linux64_heap_sandbox_dbg_ng,v8_linux_arm64_sim_heap_sandbox_dbg_ng
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3794645
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Commit-Queue: Samuel Groß <saelo@chromium.org>
Cr-Commit-Position: refs/heads/main@{#82484}
2022-08-12 20:08:47 +00:00
|
|
|
static const int kExternalPointerTableSize = 4 * kApiSystemPointerSize;
|
2022-06-20 18:50:10 +00:00
|
|
|
|
2019-08-13 10:37:59 +00:00
|
|
|
// IsolateData layout guarantees.
|
2021-09-20 13:29:46 +00:00
|
|
|
static const int kIsolateCageBaseOffset = 0;
|
|
|
|
static const int kIsolateStackGuardOffset =
|
|
|
|
kIsolateCageBaseOffset + kApiSystemPointerSize;
|
2022-08-29 10:11:43 +00:00
|
|
|
static const int kVariousBooleanFlagsOffset =
|
2021-09-20 13:29:46 +00:00
|
|
|
kIsolateStackGuardOffset + kStackGuardSize;
|
2022-08-29 10:11:43 +00:00
|
|
|
static const int kBuiltinTier0EntryTableOffset =
|
2022-11-16 15:31:37 +00:00
|
|
|
kVariousBooleanFlagsOffset + 8;
|
2021-09-20 13:29:46 +00:00
|
|
|
static const int kBuiltinTier0TableOffset =
|
|
|
|
kBuiltinTier0EntryTableOffset + kBuiltinTier0EntryTableSize;
|
|
|
|
static const int kIsolateEmbedderDataOffset =
|
|
|
|
kBuiltinTier0TableOffset + kBuiltinTier0TableSize;
|
2019-08-13 10:37:59 +00:00
|
|
|
static const int kIsolateFastCCallCallerFpOffset =
|
2021-09-20 13:29:46 +00:00
|
|
|
kIsolateEmbedderDataOffset + kNumIsolateDataSlots * kApiSystemPointerSize;
|
2019-08-13 10:37:59 +00:00
|
|
|
static const int kIsolateFastCCallCallerPcOffset =
|
|
|
|
kIsolateFastCCallCallerFpOffset + kApiSystemPointerSize;
|
2020-11-18 11:19:56 +00:00
|
|
|
static const int kIsolateFastApiCallTargetOffset =
|
2019-08-13 10:37:59 +00:00
|
|
|
kIsolateFastCCallCallerPcOffset + kApiSystemPointerSize;
|
2021-04-13 12:12:02 +00:00
|
|
|
static const int kIsolateLongTaskStatsCounterOffset =
|
2021-09-20 13:29:46 +00:00
|
|
|
kIsolateFastApiCallTargetOffset + kApiSystemPointerSize;
|
2022-07-26 19:35:32 +00:00
|
|
|
#ifdef V8_COMPRESS_POINTERS
|
2022-06-20 18:50:10 +00:00
|
|
|
static const int kIsolateExternalPointerTableOffset =
|
|
|
|
kIsolateLongTaskStatsCounterOffset + kApiSizetSize;
|
2022-06-21 15:13:01 +00:00
|
|
|
static const int kIsolateSharedExternalPointerTableAddressOffset =
|
2022-06-20 18:50:10 +00:00
|
|
|
kIsolateExternalPointerTableOffset + kExternalPointerTableSize;
|
2022-06-21 15:13:01 +00:00
|
|
|
static const int kIsolateRootsOffset =
|
|
|
|
kIsolateSharedExternalPointerTableAddressOffset + kApiSystemPointerSize;
|
2022-06-20 18:50:10 +00:00
|
|
|
#else
|
2019-08-13 10:37:59 +00:00
|
|
|
static const int kIsolateRootsOffset =
|
2021-09-20 13:29:46 +00:00
|
|
|
kIsolateLongTaskStatsCounterOffset + kApiSizetSize;
|
2022-06-20 18:50:10 +00:00
|
|
|
#endif
|
2020-09-29 13:40:21 +00:00
|
|
|
|
2018-09-07 09:03:35 +00:00
|
|
|
static const int kUndefinedValueRootIndex = 4;
|
|
|
|
static const int kTheHoleValueRootIndex = 5;
|
|
|
|
static const int kNullValueRootIndex = 6;
|
|
|
|
static const int kTrueValueRootIndex = 7;
|
|
|
|
static const int kFalseValueRootIndex = 8;
|
|
|
|
static const int kEmptyStringRootIndex = 9;
|
|
|
|
|
2019-03-01 15:34:48 +00:00
|
|
|
static const int kNodeClassIdOffset = 1 * kApiSystemPointerSize;
|
|
|
|
static const int kNodeFlagsOffset = 1 * kApiSystemPointerSize + 3;
|
2022-07-30 00:07:40 +00:00
|
|
|
static const int kNodeStateMask = 0x3;
|
2018-09-07 09:03:35 +00:00
|
|
|
static const int kNodeStateIsWeakValue = 2;
|
|
|
|
|
2022-11-08 17:32:17 +00:00
|
|
|
static const int kTracedNodeClassIdOffset = kApiSystemPointerSize;
|
2022-10-21 13:31:46 +00:00
|
|
|
|
2021-11-10 22:42:10 +00:00
|
|
|
static const int kFirstNonstringType = 0x80;
|
|
|
|
static const int kOddballType = 0x83;
|
|
|
|
static const int kForeignType = 0xcc;
|
2018-09-07 09:03:35 +00:00
|
|
|
static const int kJSSpecialApiObjectType = 0x410;
|
|
|
|
static const int kJSObjectType = 0x421;
|
2021-08-09 07:44:18 +00:00
|
|
|
static const int kFirstJSApiObjectType = 0x422;
|
|
|
|
static const int kLastJSApiObjectType = 0x80A;
|
2018-09-07 09:03:35 +00:00
|
|
|
|
|
|
|
static const int kUndefinedOddballKind = 5;
|
|
|
|
static const int kNullOddballKind = 3;
|
|
|
|
|
2019-02-07 13:19:57 +00:00
|
|
|
// Constants used by PropertyCallbackInfo to check if we should throw when an
|
|
|
|
// error occurs.
|
|
|
|
static const int kThrowOnError = 0;
|
|
|
|
static const int kDontThrow = 1;
|
|
|
|
static const int kInferShouldThrowMode = 2;
|
|
|
|
|
2018-09-07 09:03:35 +00:00
|
|
|
// Soft limit for AdjustAmountofExternalAllocatedMemory. Trigger an
|
|
|
|
// incremental GC once the external memory reaches this limit.
|
|
|
|
static constexpr int kExternalAllocationSoftLimit = 64 * 1024 * 1024;
|
|
|
|
|
2021-04-06 12:01:44 +00:00
|
|
|
#ifdef V8_MAP_PACKING
|
|
|
|
static const uintptr_t kMapWordMetadataMask = 0xffffULL << 48;
|
|
|
|
// The lowest two bits of mapwords are always `0b10`
|
|
|
|
static const uintptr_t kMapWordSignature = 0b10;
|
|
|
|
// XORing a (non-compressed) map with this mask ensures that the two
|
|
|
|
// low-order bits are 0b10. The 0 at the end makes this look like a Smi,
|
|
|
|
// although real Smis have all lower 32 bits unset. We only rely on these
|
|
|
|
// values passing as Smis in very few places.
|
|
|
|
static const int kMapWordXorMask = 0b11;
|
|
|
|
#endif
|
|
|
|
|
2018-09-07 09:03:35 +00:00
|
|
|
V8_EXPORT static void CheckInitializedImpl(v8::Isolate* isolate);
|
|
|
|
V8_INLINE static void CheckInitialized(v8::Isolate* isolate) {
|
|
|
|
#ifdef V8_ENABLE_CHECKS
|
|
|
|
CheckInitializedImpl(isolate);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-10-11 23:23:33 +00:00
|
|
|
V8_INLINE static bool HasHeapObjectTag(const internal::Address value) {
|
|
|
|
return (value & kHeapObjectTagMask) == static_cast<Address>(kHeapObjectTag);
|
2018-09-07 09:03:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 23:23:33 +00:00
|
|
|
V8_INLINE static int SmiValue(const internal::Address value) {
|
2018-09-07 09:03:35 +00:00
|
|
|
return PlatformSmiTagging::SmiToInt(value);
|
|
|
|
}
|
|
|
|
|
2018-11-03 00:13:22 +00:00
|
|
|
V8_INLINE static constexpr internal::Address IntToSmi(int value) {
|
|
|
|
return internal::IntToSmi(value);
|
2018-09-07 09:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
V8_INLINE static constexpr bool IsValidSmi(intptr_t value) {
|
|
|
|
return PlatformSmiTagging::IsValidSmi(value);
|
|
|
|
}
|
|
|
|
|
2018-10-11 23:23:33 +00:00
|
|
|
V8_INLINE static int GetInstanceType(const internal::Address obj) {
|
|
|
|
typedef internal::Address A;
|
2018-12-27 09:49:08 +00:00
|
|
|
A map = ReadTaggedPointerField(obj, kHeapObjectMapOffset);
|
2021-04-06 12:01:44 +00:00
|
|
|
#ifdef V8_MAP_PACKING
|
|
|
|
map = UnpackMapWord(map);
|
|
|
|
#endif
|
2018-12-27 09:49:08 +00:00
|
|
|
return ReadRawField<uint16_t>(map, kMapInstanceTypeOffset);
|
2018-09-07 09:03:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 23:23:33 +00:00
|
|
|
V8_INLINE static int GetOddballKind(const internal::Address obj) {
|
2018-12-27 09:49:08 +00:00
|
|
|
return SmiValue(ReadTaggedSignedField(obj, kOddballKindOffset));
|
2018-09-07 09:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
V8_INLINE static bool IsExternalTwoByteString(int instance_type) {
|
2021-12-04 00:06:11 +00:00
|
|
|
int representation = (instance_type & kStringRepresentationAndEncodingMask);
|
2018-09-07 09:03:35 +00:00
|
|
|
return representation == kExternalTwoByteRepresentationTag;
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:24:52 +00:00
|
|
|
V8_INLINE static constexpr bool CanHaveInternalField(int instance_type) {
|
|
|
|
static_assert(kJSObjectType + 1 == kFirstJSApiObjectType);
|
|
|
|
static_assert(kJSObjectType < kLastJSApiObjectType);
|
|
|
|
static_assert(kFirstJSApiObjectType < kLastJSApiObjectType);
|
|
|
|
// Check for IsJSObject() || IsJSSpecialApiObject() || IsJSApiObject()
|
|
|
|
return instance_type == kJSSpecialApiObjectType ||
|
|
|
|
// inlined version of base::IsInRange
|
|
|
|
(static_cast<unsigned>(static_cast<unsigned>(instance_type) -
|
|
|
|
static_cast<unsigned>(kJSObjectType)) <=
|
|
|
|
static_cast<unsigned>(kLastJSApiObjectType - kJSObjectType));
|
|
|
|
}
|
|
|
|
|
2018-10-11 23:23:33 +00:00
|
|
|
V8_INLINE static uint8_t GetNodeFlag(internal::Address* obj, int shift) {
|
2018-09-07 09:03:35 +00:00
|
|
|
uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
|
|
|
|
return *addr & static_cast<uint8_t>(1U << shift);
|
|
|
|
}
|
|
|
|
|
2018-10-11 23:23:33 +00:00
|
|
|
V8_INLINE static void UpdateNodeFlag(internal::Address* obj, bool value,
|
2018-09-07 09:03:35 +00:00
|
|
|
int shift) {
|
|
|
|
uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
|
|
|
|
uint8_t mask = static_cast<uint8_t>(1U << shift);
|
|
|
|
*addr = static_cast<uint8_t>((*addr & ~mask) | (value << shift));
|
|
|
|
}
|
|
|
|
|
2018-10-11 23:23:33 +00:00
|
|
|
V8_INLINE static uint8_t GetNodeState(internal::Address* obj) {
|
2018-09-07 09:03:35 +00:00
|
|
|
uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
|
|
|
|
return *addr & kNodeStateMask;
|
|
|
|
}
|
|
|
|
|
2018-10-11 23:23:33 +00:00
|
|
|
V8_INLINE static void UpdateNodeState(internal::Address* obj, uint8_t value) {
|
2018-09-07 09:03:35 +00:00
|
|
|
uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
|
|
|
|
*addr = static_cast<uint8_t>((*addr & ~kNodeStateMask) | value);
|
|
|
|
}
|
|
|
|
|
|
|
|
V8_INLINE static void SetEmbedderData(v8::Isolate* isolate, uint32_t slot,
|
|
|
|
void* data) {
|
2018-10-11 23:23:33 +00:00
|
|
|
internal::Address addr = reinterpret_cast<internal::Address>(isolate) +
|
|
|
|
kIsolateEmbedderDataOffset +
|
2018-11-20 16:03:16 +00:00
|
|
|
slot * kApiSystemPointerSize;
|
2018-09-07 09:03:35 +00:00
|
|
|
*reinterpret_cast<void**>(addr) = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
V8_INLINE static void* GetEmbedderData(const v8::Isolate* isolate,
|
|
|
|
uint32_t slot) {
|
2018-10-11 23:23:33 +00:00
|
|
|
internal::Address addr = reinterpret_cast<internal::Address>(isolate) +
|
|
|
|
kIsolateEmbedderDataOffset +
|
2018-11-20 16:03:16 +00:00
|
|
|
slot * kApiSystemPointerSize;
|
2018-09-07 09:03:35 +00:00
|
|
|
return *reinterpret_cast<void* const*>(addr);
|
|
|
|
}
|
|
|
|
|
2021-04-13 12:12:02 +00:00
|
|
|
V8_INLINE static void IncrementLongTasksStatsCounter(v8::Isolate* isolate) {
|
|
|
|
internal::Address addr = reinterpret_cast<internal::Address>(isolate) +
|
|
|
|
kIsolateLongTaskStatsCounterOffset;
|
|
|
|
++(*reinterpret_cast<size_t*>(addr));
|
|
|
|
}
|
|
|
|
|
2018-10-11 23:23:33 +00:00
|
|
|
V8_INLINE static internal::Address* GetRoot(v8::Isolate* isolate, int index) {
|
2018-11-20 16:03:16 +00:00
|
|
|
internal::Address addr = reinterpret_cast<internal::Address>(isolate) +
|
|
|
|
kIsolateRootsOffset +
|
|
|
|
index * kApiSystemPointerSize;
|
|
|
|
return reinterpret_cast<internal::Address*>(addr);
|
2018-09-07 09:03:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 12:21:59 +00:00
|
|
|
#ifdef V8_ENABLE_SANDBOX
|
2022-06-20 18:50:10 +00:00
|
|
|
V8_INLINE static internal::Address* GetExternalPointerTableBase(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
internal::Address addr = reinterpret_cast<internal::Address>(isolate) +
|
|
|
|
kIsolateExternalPointerTableOffset +
|
|
|
|
kExternalPointerTableBufferOffset;
|
|
|
|
return *reinterpret_cast<internal::Address**>(addr);
|
|
|
|
}
|
2022-06-21 15:13:01 +00:00
|
|
|
|
|
|
|
V8_INLINE static internal::Address* GetSharedExternalPointerTableBase(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
internal::Address addr = reinterpret_cast<internal::Address>(isolate) +
|
|
|
|
kIsolateSharedExternalPointerTableAddressOffset;
|
|
|
|
addr = *reinterpret_cast<internal::Address*>(addr);
|
|
|
|
addr += kExternalPointerTableBufferOffset;
|
|
|
|
return *reinterpret_cast<internal::Address**>(addr);
|
|
|
|
}
|
2022-06-20 18:50:10 +00:00
|
|
|
#endif
|
|
|
|
|
2018-09-07 09:03:35 +00:00
|
|
|
template <typename T>
|
2018-12-27 09:49:08 +00:00
|
|
|
V8_INLINE static T ReadRawField(internal::Address heap_object_ptr,
|
|
|
|
int offset) {
|
2018-10-11 23:23:33 +00:00
|
|
|
internal::Address addr = heap_object_ptr + offset - kHeapObjectTag;
|
2019-03-19 10:36:40 +00:00
|
|
|
#ifdef V8_COMPRESS_POINTERS
|
|
|
|
if (sizeof(T) > kApiTaggedSize) {
|
|
|
|
// TODO(ishell, v8:8875): When pointer compression is enabled 8-byte size
|
|
|
|
// fields (external pointers, doubles and BigInt data) are only
|
|
|
|
// kTaggedSize aligned so we have to use unaligned pointer friendly way of
|
|
|
|
// accessing them in order to avoid undefined behavior in C++ code.
|
|
|
|
T r;
|
|
|
|
memcpy(&r, reinterpret_cast<void*>(addr), sizeof(T));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
#endif
|
2018-09-07 09:03:35 +00:00
|
|
|
return *reinterpret_cast<const T*>(addr);
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:49:08 +00:00
|
|
|
V8_INLINE static internal::Address ReadTaggedPointerField(
|
|
|
|
internal::Address heap_object_ptr, int offset) {
|
|
|
|
#ifdef V8_COMPRESS_POINTERS
|
2020-02-13 11:35:54 +00:00
|
|
|
uint32_t value = ReadRawField<uint32_t>(heap_object_ptr, offset);
|
2021-04-05 19:42:59 +00:00
|
|
|
internal::Address base =
|
|
|
|
GetPtrComprCageBaseFromOnHeapAddress(heap_object_ptr);
|
|
|
|
return base + static_cast<internal::Address>(static_cast<uintptr_t>(value));
|
2018-12-27 09:49:08 +00:00
|
|
|
#else
|
|
|
|
return ReadRawField<internal::Address>(heap_object_ptr, offset);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
V8_INLINE static internal::Address ReadTaggedSignedField(
|
|
|
|
internal::Address heap_object_ptr, int offset) {
|
|
|
|
#ifdef V8_COMPRESS_POINTERS
|
2020-02-13 11:35:54 +00:00
|
|
|
uint32_t value = ReadRawField<uint32_t>(heap_object_ptr, offset);
|
|
|
|
return static_cast<internal::Address>(static_cast<uintptr_t>(value));
|
2018-12-27 09:49:08 +00:00
|
|
|
#else
|
|
|
|
return ReadRawField<internal::Address>(heap_object_ptr, offset);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-06-20 18:50:10 +00:00
|
|
|
V8_INLINE static v8::Isolate* GetIsolateForSandbox(internal::Address obj) {
|
2022-07-11 12:21:59 +00:00
|
|
|
#ifdef V8_ENABLE_SANDBOX
|
2022-06-20 18:50:10 +00:00
|
|
|
return reinterpret_cast<v8::Isolate*>(
|
|
|
|
internal::IsolateFromNeverReadOnlySpaceObject(obj));
|
2020-05-13 08:36:23 +00:00
|
|
|
#else
|
|
|
|
// Not used in non-sandbox mode.
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-06-27 12:00:24 +00:00
|
|
|
template <ExternalPointerTag tag>
|
2020-05-05 11:29:36 +00:00
|
|
|
V8_INLINE static internal::Address ReadExternalPointerField(
|
2022-06-27 12:00:24 +00:00
|
|
|
v8::Isolate* isolate, internal::Address heap_object_ptr, int offset) {
|
2022-07-11 12:21:59 +00:00
|
|
|
#ifdef V8_ENABLE_SANDBOX
|
2022-11-17 14:20:53 +00:00
|
|
|
static_assert(tag != kExternalPointerNullTag);
|
|
|
|
// See src/sandbox/external-pointer-table-inl.h. Logic duplicated here so
|
|
|
|
// it can be inlined and doesn't require an additional call.
|
|
|
|
internal::Address* table = IsSharedExternalPointerType(tag)
|
|
|
|
? GetSharedExternalPointerTableBase(isolate)
|
|
|
|
: GetExternalPointerTableBase(isolate);
|
|
|
|
internal::ExternalPointerHandle handle =
|
|
|
|
ReadRawField<ExternalPointerHandle>(heap_object_ptr, offset);
|
|
|
|
uint32_t index = handle >> kExternalPointerIndexShift;
|
|
|
|
std::atomic<internal::Address>* ptr =
|
|
|
|
reinterpret_cast<std::atomic<internal::Address>*>(&table[index]);
|
|
|
|
internal::Address entry =
|
|
|
|
std::atomic_load_explicit(ptr, std::memory_order_relaxed);
|
|
|
|
return entry & ~tag;
|
|
|
|
#else
|
2022-07-11 12:21:59 +00:00
|
|
|
return ReadRawField<Address>(heap_object_ptr, offset);
|
2022-11-17 14:20:53 +00:00
|
|
|
#endif // V8_ENABLE_SANDBOX
|
2020-05-05 11:29:36 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:49:08 +00:00
|
|
|
#ifdef V8_COMPRESS_POINTERS
|
2021-04-05 19:42:59 +00:00
|
|
|
V8_INLINE static internal::Address GetPtrComprCageBaseFromOnHeapAddress(
|
2018-12-27 09:49:08 +00:00
|
|
|
internal::Address addr) {
|
2021-04-05 19:42:59 +00:00
|
|
|
return addr & -static_cast<intptr_t>(kPtrComprCageBaseAlignment);
|
2018-12-27 09:49:08 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 10:24:13 +00:00
|
|
|
V8_INLINE static internal::Address DecompressTaggedAnyField(
|
2020-02-13 11:35:54 +00:00
|
|
|
internal::Address heap_object_ptr, uint32_t value) {
|
2021-04-05 19:42:59 +00:00
|
|
|
internal::Address base =
|
|
|
|
GetPtrComprCageBaseFromOnHeapAddress(heap_object_ptr);
|
|
|
|
return base + static_cast<internal::Address>(static_cast<uintptr_t>(value));
|
2018-09-07 09:03:35 +00:00
|
|
|
}
|
2020-05-05 11:29:36 +00:00
|
|
|
|
2018-12-27 09:49:08 +00:00
|
|
|
#endif // V8_COMPRESS_POINTERS
|
2018-09-07 09:03:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Only perform cast check for types derived from v8::Data since
|
|
|
|
// other types do not implement the Cast method.
|
|
|
|
template <bool PerformCheck>
|
|
|
|
struct CastCheck {
|
|
|
|
template <class T>
|
|
|
|
static void Perform(T* data);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
template <class T>
|
|
|
|
void CastCheck<true>::Perform(T* data) {
|
|
|
|
T::Cast(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
template <class T>
|
|
|
|
void CastCheck<false>::Perform(T* data) {}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
V8_INLINE void PerformCastCheck(T* data) {
|
2020-08-31 10:36:52 +00:00
|
|
|
CastCheck<std::is_base_of<Data, T>::value &&
|
2020-09-09 03:25:22 +00:00
|
|
|
!std::is_same<Data, std::remove_cv_t<T>>::value>::Perform(data);
|
2018-09-07 09:03:35 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 11:04:58 +00:00
|
|
|
// A base class for backing stores, which is needed due to vagaries of
|
|
|
|
// how static casts work with std::shared_ptr.
|
|
|
|
class BackingStoreBase {};
|
|
|
|
|
2021-12-13 16:25:42 +00:00
|
|
|
// The maximum value in enum GarbageCollectionReason, defined in heap.h.
|
|
|
|
// This is needed for histograms sampling garbage collection reasons.
|
2022-08-30 15:27:49 +00:00
|
|
|
constexpr int kGarbageCollectionReasonMaxValue = 27;
|
2021-12-13 16:25:42 +00:00
|
|
|
|
2018-09-07 09:03:35 +00:00
|
|
|
} // namespace internal
|
2021-07-23 18:29:30 +00:00
|
|
|
|
2018-09-07 09:03:35 +00:00
|
|
|
} // namespace v8
|
|
|
|
|
|
|
|
#endif // INCLUDE_V8_INTERNAL_H_
|