summaryrefslogtreecommitdiff
path: root/deps/v8/src/instruction-stream.cc
blob: 7d00ea543454ea396ab6b9932fa1e5940067960e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 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.

#include "src/instruction-stream.h"

#include "src/builtins/builtins.h"
#include "src/heap/heap.h"
#include "src/objects-inl.h"
#include "src/objects/code-inl.h"

namespace v8 {
namespace internal {

InstructionStream::InstructionStream(Code* code)
    : builtin_index_(code->builtin_index()) {
  DCHECK(Builtins::IsOffHeapBuiltin(code));
  const size_t page_size = AllocatePageSize();
  byte_length_ =
      RoundUp(static_cast<size_t>(code->instruction_size()), page_size);

  bytes_ = static_cast<uint8_t*>(AllocatePages(
      GetRandomMmapAddr(), byte_length_, page_size, PageAllocator::kReadWrite));
  CHECK_NOT_NULL(bytes_);

  std::memcpy(bytes_, code->instruction_start(), code->instruction_size());
  CHECK(SetPermissions(bytes_, byte_length_, PageAllocator::kReadExecute));
}

InstructionStream::~InstructionStream() {
  CHECK(FreePages(bytes_, byte_length_));
}

// static
Code* InstructionStream::TryLookupCode(Isolate* isolate, Address address) {
  DCHECK(FLAG_stress_off_heap_code);
  // TODO(jgruber,v8:6666): Replace with binary search through range checks
  // once off-heap code is mapped into a contiguous memory space.
  for (const InstructionStream* stream : isolate->off_heap_code_) {
    if (stream->Contains(address)) {
      return isolate->builtins()->builtin(stream->builtin_index());
    }
  }
  return nullptr;
}

// static
InstructionStream* InstructionStream::TryLookupInstructionStream(
    Isolate* isolate, Code* code) {
  DCHECK(FLAG_stress_off_heap_code);
  // TODO(jgruber,v8:6666): Replace with binary search through range checks
  // once off-heap code is mapped into a contiguous memory space.
  const int builtin_index = code->builtin_index();
  DCHECK(Builtins::IsBuiltinId(builtin_index));
  for (InstructionStream* stream : isolate->off_heap_code_) {
    if (stream->builtin_index() == builtin_index) return stream;
  }
  return nullptr;
}

bool InstructionStream::Contains(Address address) const {
  return bytes_ <= address && address < bytes_ + byte_length_;
}

}  // namespace internal
}  // namespace v8