Skip to content

Commit 20127e0

Browse files
bnoordhuisitaloacasas
authored andcommitted
deps: back-port b049d1a from V8 upstream
Original commit message: Ensure we align zone memory at 8 byte boundaries on all platforms BUG=v8:5668 [email protected] Review-Url: https://codereview.chromium.org/2672203002 Cr-Commit-Position: refs/heads/master@{#42959} PR-URL: #11204 Reviewed-By: Ali Ijaz Sheikh <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ae39dcb commit 20127e0

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

deps/v8/src/zone/zone.cc

+6-14
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,7 @@ Zone::~Zone() {
5858

5959
void* Zone::New(size_t size) {
6060
// Round up the requested size to fit the alignment.
61-
size = RoundUp(size, kAlignment);
62-
63-
// If the allocation size is divisible by 8 then we return an 8-byte aligned
64-
// address.
65-
if (kPointerSize == 4 && kAlignment == 4) {
66-
position_ += ((~size) & 4) & (reinterpret_cast<intptr_t>(position_) & 4);
67-
} else {
68-
DCHECK(kAlignment >= kPointerSize);
69-
}
61+
size = RoundUp(size, kAlignmentInBytes);
7062

7163
// Check if the requested size is available without expanding.
7264
Address result = position_;
@@ -86,7 +78,7 @@ void* Zone::New(size_t size) {
8678
ASAN_POISON_MEMORY_REGION(redzone_position, kASanRedzoneBytes);
8779

8880
// Check that the result has the proper alignment and return it.
89-
DCHECK(IsAddressAligned(result, kAlignment, 0));
81+
DCHECK(IsAddressAligned(result, kAlignmentInBytes, 0));
9082
allocation_size_ += size;
9183
return reinterpret_cast<void*>(result);
9284
}
@@ -121,7 +113,7 @@ void Zone::DeleteAll() {
121113
// force a new segment to be allocated on demand.
122114
if (keep) {
123115
Address start = keep->start();
124-
position_ = RoundUp(start, kAlignment);
116+
position_ = RoundUp(start, kAlignmentInBytes);
125117
limit_ = keep->end();
126118
// Un-poison so we can re-use the segment later.
127119
ASAN_UNPOISON_MEMORY_REGION(start, keep->capacity());
@@ -167,7 +159,7 @@ Segment* Zone::NewSegment(size_t size) {
167159
Address Zone::NewExpand(size_t size) {
168160
// Make sure the requested size is already properly aligned and that
169161
// there isn't enough room in the Zone to satisfy the request.
170-
DCHECK_EQ(size, RoundDown(size, kAlignment));
162+
DCHECK_EQ(size, RoundDown(size, kAlignmentInBytes));
171163
DCHECK(limit_ < position_ ||
172164
reinterpret_cast<uintptr_t>(limit_) -
173165
reinterpret_cast<uintptr_t>(position_) <
@@ -179,7 +171,7 @@ Address Zone::NewExpand(size_t size) {
179171
// is to avoid excessive malloc() and free() overhead.
180172
Segment* head = segment_head_;
181173
const size_t old_size = (head == nullptr) ? 0 : head->size();
182-
static const size_t kSegmentOverhead = sizeof(Segment) + kAlignment;
174+
static const size_t kSegmentOverhead = sizeof(Segment) + kAlignmentInBytes;
183175
const size_t new_size_no_overhead = size + (old_size << 1);
184176
size_t new_size = kSegmentOverhead + new_size_no_overhead;
185177
const size_t min_new_size = kSegmentOverhead + size;
@@ -208,7 +200,7 @@ Address Zone::NewExpand(size_t size) {
208200
}
209201

210202
// Recompute 'top' and 'limit' based on the new segment.
211-
Address result = RoundUp(segment->start(), kAlignment);
203+
Address result = RoundUp(segment->start(), kAlignmentInBytes);
212204
position_ = result + size;
213205
// Check for address overflow.
214206
// (Should not happen since the segment is guaranteed to accomodate

deps/v8/src/zone/zone.h

+3-10
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,8 @@ class V8_EXPORT_PRIVATE Zone final {
6363
AccountingAllocator* allocator() const { return allocator_; }
6464

6565
private:
66-
// All pointers returned from New() have this alignment. In addition, if the
67-
// object being allocated has a size that is divisible by 8 then its alignment
68-
// will be 8. ASan requires 8-byte alignment.
69-
#ifdef V8_USE_ADDRESS_SANITIZER
70-
static const size_t kAlignment = 8;
71-
STATIC_ASSERT(kPointerSize <= 8);
72-
#else
73-
static const size_t kAlignment = kPointerSize;
74-
#endif
66+
// All pointers returned from New() are 8-byte aligned.
67+
static const size_t kAlignmentInBytes = 8;
7568

7669
// Never allocate segments smaller than this size in bytes.
7770
static const size_t kMinimumSegmentSize = 8 * KB;
@@ -105,7 +98,7 @@ class V8_EXPORT_PRIVATE Zone final {
10598

10699
// The free region in the current (front) segment is represented as
107100
// the half-open interval [position, limit). The 'position' variable
108-
// is guaranteed to be aligned as dictated by kAlignment.
101+
// is guaranteed to be aligned as dictated by kAlignmentInBytes.
109102
Address position_;
110103
Address limit_;
111104

deps/v8/test/unittests/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ v8_executable("unittests") {
128128
"wasm/switch-logic-unittest.cc",
129129
"wasm/wasm-macro-gen-unittest.cc",
130130
"wasm/wasm-module-builder-unittest.cc",
131+
"zone/zone-unittest.cc",
131132
]
132133

133134
if (v8_current_cpu == "arm") {

deps/v8/test/unittests/unittests.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
'test-utils.h',
117117
'test-utils.cc',
118118
'value-serializer-unittest.cc',
119+
'zone/zone-unittest.cc',
119120
'wasm/asm-types-unittest.cc',
120121
'wasm/ast-decoder-unittest.cc',
121122
'wasm/control-transfer-unittest.cc',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2017 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 "src/zone/zone.h"
6+
7+
#include "src/zone/accounting-allocator.h"
8+
#include "testing/gtest/include/gtest/gtest.h"
9+
10+
namespace v8 {
11+
namespace internal {
12+
13+
TEST(Zone, 8ByteAlignment) {
14+
AccountingAllocator allocator;
15+
Zone zone(&allocator);
16+
17+
for (size_t i = 0; i < 16; ++i) {
18+
ASSERT_EQ(reinterpret_cast<intptr_t>(zone.New(i)) % 8, 0);
19+
}
20+
}
21+
22+
} // namespace internal
23+
} // namespace v8

0 commit comments

Comments
 (0)