Line data Source code
1 : // Copyright 2014 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include <cmath>
6 : #include <iostream>
7 : #include <limits>
8 :
9 : #include "src/handles-inl.h"
10 : #include "src/heap/heap.h"
11 : #include "src/heap/spaces-inl.h"
12 : #include "src/objects-inl.h"
13 : #include "test/unittests/test-utils.h"
14 : #include "testing/gtest/include/gtest/gtest.h"
15 :
16 : namespace v8 {
17 : namespace internal {
18 :
19 : typedef TestWithIsolate HeapTest;
20 : typedef TestWithIsolateAndPointerCompression HeapWithPointerCompressionTest;
21 :
22 15188 : TEST(Heap, SemiSpaceSize) {
23 : const size_t KB = static_cast<size_t>(i::KB);
24 : const size_t MB = static_cast<size_t>(i::MB);
25 : const size_t pm = i::Heap::kPointerMultiplier;
26 2 : ASSERT_EQ(1u * pm * MB / 2, i::Heap::ComputeMaxSemiSpaceSize(0u) * KB);
27 2 : ASSERT_EQ(1u * pm * MB / 2, i::Heap::ComputeMaxSemiSpaceSize(512u * MB) * KB);
28 2 : ASSERT_EQ(2u * pm * MB, i::Heap::ComputeMaxSemiSpaceSize(1024u * MB) * KB);
29 2 : ASSERT_EQ(5u * pm * MB, i::Heap::ComputeMaxSemiSpaceSize(2024u * MB) * KB);
30 2 : ASSERT_EQ(8u * pm * MB, i::Heap::ComputeMaxSemiSpaceSize(4095u * MB) * KB);
31 : }
32 :
33 15189 : TEST_F(HeapTest, ASLR) {
34 : #if V8_TARGET_ARCH_X64
35 : #if V8_OS_MACOSX
36 : Heap* heap = i_isolate()->heap();
37 : std::set<void*> hints;
38 : for (int i = 0; i < 1000; i++) {
39 : hints.insert(heap->GetRandomMmapAddr());
40 : }
41 : if (hints.size() == 1) {
42 : EXPECT_TRUE((*hints.begin()) == nullptr);
43 : EXPECT_TRUE(i::GetRandomMmapAddr() == nullptr);
44 : } else {
45 : // It is unlikely that 1000 random samples will collide to less then 500
46 : // values.
47 : EXPECT_GT(hints.size(), 500u);
48 : const uintptr_t kRegionMask = 0xFFFFFFFFu;
49 : void* first = *hints.begin();
50 : for (void* hint : hints) {
51 : uintptr_t diff = reinterpret_cast<uintptr_t>(first) ^
52 : reinterpret_cast<uintptr_t>(hint);
53 : EXPECT_LE(diff, kRegionMask);
54 : }
55 : }
56 : #endif // V8_OS_MACOSX
57 : #endif // V8_TARGET_ARCH_X64
58 1 : }
59 :
60 15189 : TEST_F(HeapTest, ExternalLimitDefault) {
61 1 : Heap* heap = i_isolate()->heap();
62 2 : EXPECT_EQ(kExternalAllocationSoftLimit,
63 0 : heap->isolate()->isolate_data()->external_memory_limit_);
64 1 : }
65 :
66 15189 : TEST_F(HeapTest, ExternalLimitStaysAboveDefaultForExplicitHandling) {
67 : v8_isolate()->AdjustAmountOfExternalAllocatedMemory(+10 * MB);
68 : v8_isolate()->AdjustAmountOfExternalAllocatedMemory(-10 * MB);
69 1 : Heap* heap = i_isolate()->heap();
70 1 : EXPECT_GE(heap->isolate()->isolate_data()->external_memory_limit_,
71 0 : kExternalAllocationSoftLimit);
72 1 : }
73 :
74 : #if V8_TARGET_ARCH_64_BIT
75 15190 : TEST_F(HeapWithPointerCompressionTest, HeapLayout) {
76 : // Produce some garbage.
77 : RunJS(
78 : "let ar = [];"
79 : "for (let i = 0; i < 100; i++) {"
80 : " ar.push(Array(i));"
81 : "}"
82 1 : "ar.push(Array(32 * 1024 * 1024));");
83 :
84 : Address isolate_root = i_isolate()->isolate_root();
85 1 : EXPECT_TRUE(IsAligned(isolate_root, size_t{4} * GB));
86 :
87 : // Check that all memory chunks belong this region.
88 : base::AddressRegion heap_reservation(isolate_root - size_t{2} * GB,
89 1 : size_t{4} * GB);
90 :
91 1 : OldGenerationMemoryChunkIterator iter(i_isolate()->heap());
92 : for (;;) {
93 13 : MemoryChunk* chunk = iter.next();
94 7 : if (chunk == nullptr) break;
95 :
96 : Address address = chunk->address();
97 6 : size_t size = chunk->area_end() - address;
98 6 : EXPECT_TRUE(heap_reservation.contains(address, size));
99 6 : }
100 1 : }
101 : #endif // V8_TARGET_ARCH_64_BIT
102 :
103 : } // namespace internal
104 9111 : } // namespace v8
|