function-body-decoder.h 6.41 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5 6
#ifndef V8_WASM_FUNCTION_BODY_DECODER_H_
#define V8_WASM_FUNCTION_BODY_DECODER_H_
7

8
#include "src/base/compiler-specific.h"
9
#include "src/base/iterator.h"
10
#include "src/globals.h"
11
#include "src/wasm/decoder.h"
12 13 14 15 16 17
#include "src/wasm/wasm-opcodes.h"
#include "src/wasm/wasm-result.h"

namespace v8 {
namespace internal {

18 19
class BitVector;  // forward declaration

20 21
namespace wasm {

22
struct WasmModule;  // forward declaration of module interface.
23 24
struct WasmFeatures;

25
// A wrapper around the signature and bytes of a function.
26 27
struct FunctionBody {
  FunctionSig* sig;   // function signature
28
  uint32_t offset;    // offset in the module bytes, for error reporting
29 30
  const byte* start;  // start of the function body
  const byte* end;    // end of the function body
31

32 33 34 35
  FunctionBody(FunctionSig* sig, uint32_t offset, const byte* start,
               const byte* end)
      : sig(sig), offset(offset), start(start), end(end) {}
};
36

37
V8_EXPORT_PRIVATE DecodeResult VerifyWasmCode(AccountingAllocator* allocator,
38
                                              const WasmFeatures& enabled,
39
                                              const WasmModule* module,
40
                                              WasmFeatures* detected,
41
                                              FunctionBody& body);
42

43 44
enum PrintLocals { kPrintLocals, kOmitLocals };
V8_EXPORT_PRIVATE
45
bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body,
46
                      const WasmModule* module, PrintLocals print_locals);
47

48 49
V8_EXPORT_PRIVATE
bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body,
50
                      const WasmModule* module, PrintLocals print_locals,
51 52
                      std::ostream& out,
                      std::vector<int>* line_numbers = nullptr);
53

54
// A simplified form of AST printing, e.g. from a debugger.
55
void PrintRawWasmCode(const byte* start, const byte* end);
56

57
struct BodyLocalDecls {
58
  // The size of the encoded declarations.
59
  uint32_t encoded_size = 0;  // size of encoded declarations
60

61
  ZoneVector<ValueType> type_list;
62

63
  explicit BodyLocalDecls(Zone* zone) : type_list(zone) {}
64 65
};

66 67
V8_EXPORT_PRIVATE bool DecodeLocalDecls(const WasmFeatures& enabled,
                                        BodyLocalDecls* decls,
68
                                        const byte* start, const byte* end);
69

70 71 72 73
V8_EXPORT_PRIVATE BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone,
                                                             size_t num_locals,
                                                             const byte* start,
                                                             const byte* end);
74

75
// Computes the length of the opcode at the given address.
76
V8_EXPORT_PRIVATE unsigned OpcodeLength(const byte* pc, const byte* end);
77

78 79 80 81 82 83 84 85 86
// Computes the stack effect of the opcode at the given address.
// Returns <pop count, push count>.
// Be cautious with control opcodes: This function only covers their immediate,
// local stack effect (e.g. BrIf pops 1, Br pops 0). Those opcodes can have
// non-local stack effect though, which are not covered here.
std::pair<uint32_t, uint32_t> StackEffect(const WasmModule* module,
                                          FunctionSig* sig, const byte* pc,
                                          const byte* end);

87
// A simple forward iterator for bytecodes.
88
class V8_EXPORT_PRIVATE BytecodeIterator : public NON_EXPORTED_BASE(Decoder) {
89 90
  // Base class for both iterators defined below.
  class iterator_base {
91
   public:
92
    inline iterator_base& operator++() {
93 94 95 96
      DCHECK_LT(ptr_, end_);
      ptr_ += OpcodeLength(ptr_, end_);
      return *this;
    }
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    inline bool operator==(const iterator_base& that) {
      return this->ptr_ == that.ptr_;
    }
    inline bool operator!=(const iterator_base& that) {
      return this->ptr_ != that.ptr_;
    }

   protected:
    const byte* ptr_;
    const byte* end_;
    iterator_base(const byte* ptr, const byte* end) : ptr_(ptr), end_(end) {}
  };

 public:
  // If one wants to iterate over the bytecode without looking at {pc_offset()}.
  class opcode_iterator
      : public iterator_base,
114
        public base::iterator<std::input_iterator_tag, WasmOpcode> {
115
   public:
116 117 118 119
    inline WasmOpcode operator*() {
      DCHECK_LT(ptr_, end_);
      return static_cast<WasmOpcode>(*ptr_);
    }
120 121 122 123 124 125 126 127 128 129

   private:
    friend class BytecodeIterator;
    opcode_iterator(const byte* ptr, const byte* end)
        : iterator_base(ptr, end) {}
  };
  // If one wants to iterate over the instruction offsets without looking at
  // opcodes.
  class offset_iterator
      : public iterator_base,
130
        public base::iterator<std::input_iterator_tag, uint32_t> {
131 132 133 134
   public:
    inline uint32_t operator*() {
      DCHECK_LT(ptr_, end_);
      return static_cast<uint32_t>(ptr_ - start_);
135 136 137
    }

   private:
138
    const byte* start_;
139
    friend class BytecodeIterator;
140 141
    offset_iterator(const byte* start, const byte* ptr, const byte* end)
        : iterator_base(ptr, end), start_(start) {}
142 143 144 145 146 147
  };

  // Create a new {BytecodeIterator}. If the {decls} pointer is non-null,
  // assume the bytecode starts with local declarations and decode them.
  // Otherwise, do not decode local decls.
  BytecodeIterator(const byte* start, const byte* end,
148
                   BodyLocalDecls* decls = nullptr);
149

150 151 152 153 154 155 156 157 158 159
  base::iterator_range<opcode_iterator> opcodes() {
    return base::iterator_range<opcode_iterator>(opcode_iterator(pc_, end_),
                                                 opcode_iterator(end_, end_));
  }

  base::iterator_range<offset_iterator> offsets() {
    return base::iterator_range<offset_iterator>(
        offset_iterator(start_, pc_, end_),
        offset_iterator(start_, end_, end_));
  }
160 161

  WasmOpcode current() {
162 163
    return static_cast<WasmOpcode>(
        read_u8<Decoder::kNoValidate>(pc_, "expected bytecode"));
164 165 166 167 168 169 170 171 172 173
  }

  void next() {
    if (pc_ < end_) {
      pc_ += OpcodeLength(pc_, end_);
      if (pc_ >= end_) pc_ = end_;
    }
  }

  bool has_next() { return pc_ < end_; }
174 175

  WasmOpcode prefixed_opcode() {
176 177
    byte prefix = read_u8<Decoder::kNoValidate>(pc_, "expected prefix");
    byte index = read_u8<Decoder::kNoValidate>(pc_ + 1, "expected index");
178 179
    return static_cast<WasmOpcode>(prefix << 8 | index);
  }
180 181
};

182 183 184 185
}  // namespace wasm
}  // namespace internal
}  // namespace v8

186
#endif  // V8_WASM_FUNCTION_BODY_DECODER_H_