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/objects.h"
10 : #include "src/objects-inl.h"
11 :
12 : #include "src/handles.h"
13 : #include "src/handles-inl.h"
14 :
15 : #include "src/heap/heap.h"
16 : #include "test/unittests/test-utils.h"
17 : #include "testing/gtest/include/gtest/gtest.h"
18 :
19 : namespace v8 {
20 : namespace internal {
21 :
22 : typedef TestWithIsolate HeapTest;
23 :
24 0 : double Round(double x) {
25 : // Round to three digits.
26 12 : return floor(x * 1000 + 0.5) / 1000;
27 : }
28 :
29 :
30 12 : void CheckEqualRounded(double expected, double actual) {
31 : expected = Round(expected);
32 : actual = Round(actual);
33 12 : EXPECT_DOUBLE_EQ(expected, actual);
34 12 : }
35 :
36 :
37 13158 : TEST(Heap, HeapGrowingFactor) {
38 : CheckEqualRounded(Heap::kMaxHeapGrowingFactor,
39 1 : Heap::HeapGrowingFactor(34, 1, 4.0));
40 1 : CheckEqualRounded(3.553, Heap::HeapGrowingFactor(45, 1, 4.0));
41 1 : CheckEqualRounded(2.830, Heap::HeapGrowingFactor(50, 1, 4.0));
42 1 : CheckEqualRounded(1.478, Heap::HeapGrowingFactor(100, 1, 4.0));
43 1 : CheckEqualRounded(1.193, Heap::HeapGrowingFactor(200, 1, 4.0));
44 1 : CheckEqualRounded(1.121, Heap::HeapGrowingFactor(300, 1, 4.0));
45 : CheckEqualRounded(Heap::HeapGrowingFactor(300, 1, 4.0),
46 1 : Heap::HeapGrowingFactor(600, 2, 4.0));
47 : CheckEqualRounded(Heap::kMinHeapGrowingFactor,
48 1 : Heap::HeapGrowingFactor(400, 1, 4.0));
49 1 : }
50 :
51 13158 : TEST(Heap, MaxHeapGrowingFactor) {
52 : CheckEqualRounded(
53 1 : 1.3, Heap::MaxHeapGrowingFactor(Heap::kMinOldGenerationSize * MB));
54 : CheckEqualRounded(
55 1 : 1.600, Heap::MaxHeapGrowingFactor(Heap::kMaxOldGenerationSize / 2 * MB));
56 : CheckEqualRounded(
57 : 1.999,
58 : Heap::MaxHeapGrowingFactor(
59 1 : (Heap::kMaxOldGenerationSize - Heap::kPointerMultiplier) * MB));
60 : CheckEqualRounded(4.0,
61 : Heap::MaxHeapGrowingFactor(
62 1 : static_cast<size_t>(Heap::kMaxOldGenerationSize) * MB));
63 1 : }
64 :
65 13158 : TEST(Heap, SemiSpaceSize) {
66 : const size_t KB = static_cast<size_t>(i::KB);
67 : const size_t MB = static_cast<size_t>(i::MB);
68 : const size_t pm = i::Heap::kPointerMultiplier;
69 2 : ASSERT_EQ(1u * pm * MB / 2, i::Heap::ComputeMaxSemiSpaceSize(0u) * KB);
70 2 : ASSERT_EQ(1u * pm * MB / 2, i::Heap::ComputeMaxSemiSpaceSize(512u * MB) * KB);
71 2 : ASSERT_EQ(2u * pm * MB, i::Heap::ComputeMaxSemiSpaceSize(1024u * MB) * KB);
72 2 : ASSERT_EQ(5u * pm * MB, i::Heap::ComputeMaxSemiSpaceSize(2024u * MB) * KB);
73 2 : ASSERT_EQ(8u * pm * MB, i::Heap::ComputeMaxSemiSpaceSize(4095u * MB) * KB);
74 : }
75 :
76 13158 : TEST(Heap, OldGenerationSize) {
77 : uint64_t configurations[][2] = {
78 : {0, i::Heap::kMinOldGenerationSize},
79 : {512, i::Heap::kMinOldGenerationSize},
80 : {1 * i::GB, 256 * i::Heap::kPointerMultiplier},
81 : {2 * static_cast<uint64_t>(i::GB), 512 * i::Heap::kPointerMultiplier},
82 : {4 * static_cast<uint64_t>(i::GB), i::Heap::kMaxOldGenerationSize},
83 1 : {8 * static_cast<uint64_t>(i::GB), i::Heap::kMaxOldGenerationSize}};
84 :
85 7 : for (auto configuration : configurations) {
86 19 : ASSERT_EQ(configuration[1],
87 : static_cast<uint64_t>(
88 : i::Heap::ComputeMaxOldGenerationSize(configuration[0])));
89 : }
90 : }
91 :
92 13160 : TEST_F(HeapTest, ASLR) {
93 : #if V8_TARGET_ARCH_X64
94 : #if V8_OS_MACOSX
95 : Heap* heap = i_isolate()->heap();
96 : std::set<void*> hints;
97 : for (int i = 0; i < 1000; i++) {
98 : hints.insert(heap->GetRandomMmapAddr());
99 : }
100 : if (hints.size() == 1) {
101 : EXPECT_TRUE((*hints.begin()) == nullptr);
102 : EXPECT_TRUE(base::OS::GetRandomMmapAddr() == nullptr);
103 : } else {
104 : // It is unlikely that 1000 random samples will collide to less then 500
105 : // values.
106 : EXPECT_GT(hints.size(), 500u);
107 : const uintptr_t kRegionMask = 0xFFFFFFFFu;
108 : void* first = *hints.begin();
109 : for (void* hint : hints) {
110 : uintptr_t diff = reinterpret_cast<uintptr_t>(first) ^
111 : reinterpret_cast<uintptr_t>(hint);
112 : EXPECT_LE(diff, kRegionMask);
113 : }
114 : }
115 : #endif // V8_OS_MACOSX
116 : #endif // V8_TARGET_ARCH_X64
117 1 : }
118 :
119 : } // namespace internal
120 7893 : } // namespace v8
|