assembler-tester.h 2.96 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2018 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.

#ifndef V8_TEST_COMMON_ASSEMBLER_TESTER_H_
#define V8_TEST_COMMON_ASSEMBLER_TESTER_H_

8 9
#include <memory>

10 11
#include "src/codegen/assembler.h"
#include "src/codegen/code-desc.h"
12 13 14 15

namespace v8 {
namespace internal {

16 17
class TestingAssemblerBuffer : public AssemblerBuffer {
 public:
18 19 20
  TestingAssemblerBuffer(
      size_t requested, void* address,
      VirtualMemory::JitPermission jit_permission = VirtualMemory::kNoJit) {
21 22 23
    size_t page_size = v8::internal::AllocatePageSize();
    size_t alloc_size = RoundUp(requested, page_size);
    CHECK_GE(kMaxInt, alloc_size);
24 25 26 27
    reservation_ = VirtualMemory(GetPlatformPageAllocator(), alloc_size,
                                 address, page_size, jit_permission);
    CHECK(reservation_.IsReserved());
    MakeWritable();
28
  }
29

30
  ~TestingAssemblerBuffer() { reservation_.Free(); }
31

32 33 34
  byte* start() const override {
    return reinterpret_cast<byte*>(reservation_.address());
  }
35

36
  int size() const override { return static_cast<int>(reservation_.size()); }
37 38 39 40 41 42

  std::unique_ptr<AssemblerBuffer> Grow(int new_size) override {
    FATAL("Cannot grow TestingAssemblerBuffer");
  }

  std::unique_ptr<AssemblerBuffer> CreateView() const {
43
    return ExternalAssemblerBuffer(start(), size());
44
  }
45

46 47 48 49 50 51
  void MakeExecutable() {
    // Flush the instruction cache as part of making the buffer executable.
    // Note: we do this before setting permissions to ReadExecute because on
    // some older ARM kernels there is a bug which causes an access error on
    // cache flush instructions to trigger access error on non-writable memory.
    // See https://bugs.chromium.org/p/v8/issues/detail?id=8157
52
    FlushInstructionCache(start(), size());
53

54
    bool result = SetPermissions(GetPlatformPageAllocator(), start(), size(),
55 56 57 58 59
                                 v8::PageAllocator::kReadExecute);
    CHECK(result);
  }

  void MakeWritable() {
60
    bool result = SetPermissions(GetPlatformPageAllocator(), start(), size(),
61 62 63 64 65 66
                                 v8::PageAllocator::kReadWrite);
    CHECK(result);
  }

  // TODO(wasm): Only needed for the "test-jump-table-assembler.cc" tests.
  void MakeWritableAndExecutable() {
67
    bool result = SetPermissions(GetPlatformPageAllocator(), start(), size(),
68 69 70 71 72
                                 v8::PageAllocator::kReadWriteExecute);
    CHECK(result);
  }

 private:
73
  VirtualMemory reservation_;
74 75 76
};

static inline std::unique_ptr<TestingAssemblerBuffer> AllocateAssemblerBuffer(
77
    size_t requested = v8::internal::AssemblerBase::kDefaultBufferSize,
78 79 80 81
    void* address = nullptr,
    VirtualMemory::JitPermission jit_permission = VirtualMemory::kNoJit) {
  return std::make_unique<TestingAssemblerBuffer>(requested, address,
                                                  jit_permission);
82 83 84 85 86 87
}

}  // namespace internal
}  // namespace v8

#endif  // V8_TEST_COMMON_ASSEMBLER_TESTER_H_