Line data Source code
1 : // Copyright 2018 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 : #ifndef V8_TEST_COMMON_ASSEMBLER_TESTER_H_
6 : #define V8_TEST_COMMON_ASSEMBLER_TESTER_H_
7 :
8 : #include "src/assembler.h"
9 :
10 : namespace v8 {
11 : namespace internal {
12 :
13 : class TestingAssemblerBuffer : public AssemblerBuffer {
14 : public:
15 3022 : TestingAssemblerBuffer(size_t requested, void* address) {
16 1511 : size_t page_size = v8::internal::AllocatePageSize();
17 1511 : size_t alloc_size = RoundUp(requested, page_size);
18 1511 : CHECK_GE(kMaxInt, alloc_size);
19 1511 : size_ = static_cast<int>(alloc_size);
20 : buffer_ = static_cast<byte*>(AllocatePages(GetPlatformPageAllocator(),
21 : address, alloc_size, page_size,
22 1511 : v8::PageAllocator::kReadWrite));
23 1511 : CHECK_NOT_NULL(buffer_);
24 1511 : }
25 :
26 3022 : ~TestingAssemblerBuffer() {
27 1511 : CHECK(FreePages(GetPlatformPageAllocator(), buffer_, size_));
28 3022 : }
29 :
30 1876 : byte* start() const override { return buffer_; }
31 :
32 375 : int size() const override { return size_; }
33 :
34 0 : std::unique_ptr<AssemblerBuffer> Grow(int new_size) override {
35 0 : FATAL("Cannot grow TestingAssemblerBuffer");
36 : }
37 :
38 : std::unique_ptr<AssemblerBuffer> CreateView() const {
39 301 : return ExternalAssemblerBuffer(buffer_, size_);
40 : }
41 :
42 1433 : void MakeExecutable() {
43 : // Flush the instruction cache as part of making the buffer executable.
44 : // Note: we do this before setting permissions to ReadExecute because on
45 : // some older ARM kernels there is a bug which causes an access error on
46 : // cache flush instructions to trigger access error on non-writable memory.
47 : // See https://bugs.chromium.org/p/v8/issues/detail?id=8157
48 1433 : Assembler::FlushICache(buffer_, size_);
49 :
50 : bool result = SetPermissions(GetPlatformPageAllocator(), buffer_, size_,
51 1433 : v8::PageAllocator::kReadExecute);
52 1433 : CHECK(result);
53 1433 : }
54 :
55 : void MakeWritable() {
56 : bool result = SetPermissions(GetPlatformPageAllocator(), buffer_, size_,
57 : v8::PageAllocator::kReadWrite);
58 : CHECK(result);
59 : }
60 :
61 : // TODO(wasm): Only needed for the "test-jump-table-assembler.cc" tests.
62 5 : void MakeWritableAndExecutable() {
63 : bool result = SetPermissions(GetPlatformPageAllocator(), buffer_, size_,
64 5 : v8::PageAllocator::kReadWriteExecute);
65 5 : CHECK(result);
66 5 : }
67 :
68 : private:
69 : byte* buffer_;
70 : int size_;
71 : };
72 :
73 : static inline std::unique_ptr<TestingAssemblerBuffer> AllocateAssemblerBuffer(
74 : size_t requested = v8::internal::AssemblerBase::kMinimalBufferSize,
75 : void* address = nullptr) {
76 1511 : return base::make_unique<TestingAssemblerBuffer>(requested, address);
77 : }
78 :
79 : } // namespace internal
80 : } // namespace v8
81 :
82 : #endif // V8_TEST_COMMON_ASSEMBLER_TESTER_H_
|