v8/test/cctest/test-platform.cc
Bill Budge adc52af506 Reland "[Memory] Use OS::Allocate for all OS memory allocations."
This is a reland of 4899bcb66d
This is a reland of b73ee3344a

Original change's description:
> [Memory] Use OS::Allocate for all OS memory allocations.
>
> - Eliminates OS::ReserveRegion and OS::ReserveAlignedRegion.
> - Changes OS::Allocate to take alignment parameter, reorders parameters
>   to match page_allocator.
> - Since the size of memory allocation can be deduced, don't return the
>   amount of memory allocated.
> - Changes reservation of aligned address space. Before we would reserve
>   (size + alignment) rounded up to page size. This is too much, because
>   maximum misalignment is (alignment - page_size).
> - On Windows and Cygwin, we release an oversize allocation and
>   immediately retry at the aligned address in the allocation. If we
>   lose the address due to a race, we just retry.
> - Clean up all the calls to OS::Allocate in codegen and tests by adding
>   helper AllocateSystemPage function (allocation.h) and
>   AllocateAssemblerBuffer (cctest.h).
> - Changes 'assm' to 'masm' in some targets for consistency when using
>   a macro-assembler.
>
> - Eliminates OS::ReleaseRegion, replacing with calls to OS::Free.
> - Adds bool return value to OS::Free.
> - Cleans up types of flags, protection on Windows and Cygwin.

> Bug: chromium:756050
> Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng
> Change-Id: I306dbe042cc867670fdc935abca29db074b0da71

Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng
Change-Id: Iad3c025334e8f8d7d647be99a36a11ee449c9087
Reviewed-on: https://chromium-review.googlesource.com/767014
Commit-Queue: Bill Budge <bbudge@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#49363}
2017-11-14 17:21:58 +00:00

113 lines
4.0 KiB
C++

// Copyright 2013 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.
#include <stdint.h>
#include "src/base/build_config.h"
#include "src/base/platform/platform.h"
#include "test/cctest/cctest.h"
using OS = v8::base::OS;
namespace v8 {
namespace internal {
TEST(OSAllocateAndFree) {
size_t page_size = OS::AllocatePageSize();
CHECK_NE(0, page_size);
// A large allocation, aligned at native allocation granularity.
const size_t kAllocationSize = 1 * MB;
void* mem_addr = OS::Allocate(OS::GetRandomMmapAddr(), kAllocationSize,
page_size, OS::MemoryPermission::kReadWrite);
CHECK_NOT_NULL(mem_addr);
CHECK(OS::Free(mem_addr, kAllocationSize));
// A large allocation, aligned significantly beyond native granularity.
const size_t kBigAlignment = 64 * MB;
void* aligned_mem_addr =
OS::Allocate(OS::GetRandomMmapAddr(), kAllocationSize, kBigAlignment,
OS::MemoryPermission::kReadWrite);
CHECK_NOT_NULL(aligned_mem_addr);
CHECK_EQ(aligned_mem_addr, AlignedAddress(aligned_mem_addr, kBigAlignment));
CHECK(OS::Free(aligned_mem_addr, kAllocationSize));
}
TEST(OSReserveMemory) {
size_t page_size = OS::AllocatePageSize();
const size_t kAllocationSize = 1 * MB;
void* mem_addr = OS::Allocate(OS::GetRandomMmapAddr(), kAllocationSize,
page_size, OS::MemoryPermission::kReadWrite);
CHECK_NE(0, page_size);
CHECK_NOT_NULL(mem_addr);
size_t commit_size = OS::CommitPageSize();
CHECK(OS::CommitRegion(mem_addr, commit_size, false));
// Check whether we can write to memory.
int* addr = static_cast<int*>(mem_addr);
addr[KB - 1] = 2;
CHECK(OS::UncommitRegion(mem_addr, commit_size));
CHECK(OS::Free(mem_addr, kAllocationSize));
}
#ifdef V8_CC_GNU
static uintptr_t sp_addr = 0;
void GetStackPointer(const v8::FunctionCallbackInfo<v8::Value>& args) {
#if V8_HOST_ARCH_X64
__asm__ __volatile__("mov %%rsp, %0" : "=g"(sp_addr));
#elif V8_HOST_ARCH_IA32
__asm__ __volatile__("mov %%esp, %0" : "=g"(sp_addr));
#elif V8_HOST_ARCH_ARM
__asm__ __volatile__("str sp, %0" : "=g"(sp_addr));
#elif V8_HOST_ARCH_ARM64
__asm__ __volatile__("mov x16, sp; str x16, %0" : "=g"(sp_addr));
#elif V8_HOST_ARCH_MIPS
__asm__ __volatile__("sw $sp, %0" : "=g"(sp_addr));
#elif V8_HOST_ARCH_MIPS64
__asm__ __volatile__("sd $sp, %0" : "=g"(sp_addr));
#elif defined(__s390x__) || defined(_ARCH_S390X)
__asm__ __volatile__("stg 15, %0" : "=m"(sp_addr));
#elif defined(__s390__) || defined(_ARCH_S390)
__asm__ __volatile__("st 15, %0" : "=m"(sp_addr));
#elif defined(__PPC64__) || defined(_ARCH_PPC64)
__asm__ __volatile__("std 1, %0" : "=g"(sp_addr));
#elif defined(__PPC__) || defined(_ARCH_PPC)
__asm__ __volatile__("stw 1, %0" : "=g"(sp_addr));
#else
#error Host architecture was not detected as supported by v8
#endif
args.GetReturnValue().Set(v8::Integer::NewFromUnsigned(
args.GetIsolate(), static_cast<uint32_t>(sp_addr)));
}
TEST(StackAlignment) {
v8::Isolate* isolate = CcTest::isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::ObjectTemplate> global_template =
v8::ObjectTemplate::New(isolate);
global_template->Set(v8_str("get_stack_pointer"),
v8::FunctionTemplate::New(isolate, GetStackPointer));
LocalContext env(nullptr, global_template);
CompileRun(
"function foo() {"
" return get_stack_pointer();"
"}");
v8::Local<v8::Object> global_object = env->Global();
v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
global_object->Get(isolate->GetCurrentContext(), v8_str("foo"))
.ToLocalChecked());
v8::Local<v8::Value> result =
foo->Call(isolate->GetCurrentContext(), global_object, 0, nullptr)
.ToLocalChecked();
CHECK_EQ(0u, result->Uint32Value(isolate->GetCurrentContext()).FromJust() %
OS::ActivationFrameAlignment());
}
#endif // V8_CC_GNU
} // namespace internal
} // namespace v8