Coverage Report

Created: 2019-02-20 00:17

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Object/Wasm.h
Line
Count
Source (jump to first uncovered line)
1
//===- Wasm.h - Wasm object file implementation -----------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file declares the WasmObjectFile class, which implements the ObjectFile
10
// interface for Wasm files.
11
//
12
// See: https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
13
//
14
//===----------------------------------------------------------------------===//
15
16
#ifndef LLVM_OBJECT_WASM_H
17
#define LLVM_OBJECT_WASM_H
18
19
#include "llvm/ADT/ArrayRef.h"
20
#include "llvm/ADT/StringMap.h"
21
#include "llvm/ADT/StringRef.h"
22
#include "llvm/BinaryFormat/Wasm.h"
23
#include "llvm/Config/llvm-config.h"
24
#include "llvm/MC/MCSymbolWasm.h"
25
#include "llvm/Object/Binary.h"
26
#include "llvm/Object/ObjectFile.h"
27
#include "llvm/Support/Error.h"
28
#include "llvm/Support/MemoryBuffer.h"
29
#include <cstddef>
30
#include <cstdint>
31
#include <vector>
32
33
namespace llvm {
34
namespace object {
35
36
class WasmSymbol {
37
public:
38
  WasmSymbol(const wasm::WasmSymbolInfo &Info,
39
             const wasm::WasmGlobalType *GlobalType,
40
             const wasm::WasmEventType *EventType,
41
             const wasm::WasmSignature *Signature)
42
      : Info(Info), GlobalType(GlobalType), EventType(EventType),
43
1.17k
        Signature(Signature) {}
44
45
  const wasm::WasmSymbolInfo &Info;
46
  const wasm::WasmGlobalType *GlobalType;
47
  const wasm::WasmEventType *EventType;
48
  const wasm::WasmSignature *Signature;
49
50
589
  bool isTypeFunction() const {
51
589
    return Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION;
52
589
  }
53
54
378
  bool isTypeData() const { return Info.Kind == wasm::WASM_SYMBOL_TYPE_DATA; }
55
56
43
  bool isTypeGlobal() const {
57
43
    return Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL;
58
43
  }
59
60
33
  bool isTypeSection() const {
61
33
    return Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION;
62
33
  }
63
64
7
  bool isTypeEvent() const { return Info.Kind == wasm::WASM_SYMBOL_TYPE_EVENT; }
65
66
884
  bool isDefined() const { return !isUndefined(); }
67
68
911
  bool isUndefined() const {
69
911
    return (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) != 0;
70
911
  }
71
72
213
  bool isBindingWeak() const {
73
213
    return getBinding() == wasm::WASM_SYMBOL_BINDING_WEAK;
74
213
  }
75
76
  bool isBindingGlobal() const {
77
    return getBinding() == wasm::WASM_SYMBOL_BINDING_GLOBAL;
78
  }
79
80
768
  bool isBindingLocal() const {
81
768
    return getBinding() == wasm::WASM_SYMBOL_BINDING_LOCAL;
82
768
  }
83
84
981
  unsigned getBinding() const {
85
981
    return Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK;
86
981
  }
87
88
213
  bool isHidden() const {
89
213
    return getVisibility() == wasm::WASM_SYMBOL_VISIBILITY_HIDDEN;
90
213
  }
91
92
213
  unsigned getVisibility() const {
93
213
    return Info.Flags & wasm::WASM_SYMBOL_VISIBILITY_MASK;
94
213
  }
95
96
  void print(raw_ostream &Out) const;
97
98
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
99
  LLVM_DUMP_METHOD void dump() const;
100
#endif
101
};
102
103
struct WasmSection {
104
407
  WasmSection() = default;
105
106
  uint32_t Type = 0;         // Section type (See below)
107
  uint32_t Offset = 0;       // Offset with in the file
108
  StringRef Name;            // Section name (User-defined sections only)
109
  ArrayRef<uint8_t> Content; // Section content
110
  std::vector<wasm::WasmRelocation> Relocations; // Relocations for this section
111
};
112
113
struct WasmSegment {
114
  uint32_t SectionOffset;
115
  wasm::WasmDataSegment Data;
116
};
117
118
class WasmObjectFile : public ObjectFile {
119
120
public:
121
  WasmObjectFile(MemoryBufferRef Object, Error &Err);
122
123
  const wasm::WasmObjectHeader &getHeader() const;
124
  const WasmSymbol &getWasmSymbol(const DataRefImpl &Symb) const;
125
  const WasmSymbol &getWasmSymbol(const SymbolRef &Symbol) const;
126
  const WasmSection &getWasmSection(const SectionRef &Section) const;
127
  const wasm::WasmRelocation &getWasmRelocation(const RelocationRef &Ref) const;
128
129
  static bool classof(const Binary *v) { return v->isWasm(); }
130
131
  const wasm::WasmDylinkInfo &dylinkInfo() const { return DylinkInfo; }
132
  const wasm::WasmProducerInfo &getProducerInfo() const { return ProducerInfo; }
133
  ArrayRef<wasm::WasmSignature> types() const { return Signatures; }
134
  ArrayRef<uint32_t> functionTypes() const { return FunctionTypes; }
135
  ArrayRef<wasm::WasmImport> imports() const { return Imports; }
136
  ArrayRef<wasm::WasmTable> tables() const { return Tables; }
137
  ArrayRef<wasm::WasmLimits> memories() const { return Memories; }
138
  ArrayRef<wasm::WasmGlobal> globals() const { return Globals; }
139
  ArrayRef<wasm::WasmEvent> events() const { return Events; }
140
  ArrayRef<wasm::WasmExport> exports() const { return Exports; }
141
0
  ArrayRef<WasmSymbol> syms() const { return Symbols; }
142
  const wasm::WasmLinkingData &linkingData() const { return LinkingData; }
143
  uint32_t getNumberOfSymbols() const { return Symbols.size(); }
144
  ArrayRef<wasm::WasmElemSegment> elements() const { return ElemSegments; }
145
  ArrayRef<WasmSegment> dataSegments() const { return DataSegments; }
146
  ArrayRef<wasm::WasmFunction> functions() const { return Functions; }
147
  ArrayRef<wasm::WasmFunctionName> debugNames() const { return DebugNames; }
148
  uint32_t startFunction() const { return StartFunction; }
149
  uint32_t getNumImportedGlobals() const { return NumImportedGlobals; }
150
  uint32_t getNumImportedFunctions() const { return NumImportedFunctions; }
151
  uint32_t getNumImportedEvents() const { return NumImportedEvents; }
152
  void moveSymbolNext(DataRefImpl &Symb) const override;
153
154
  uint32_t getSymbolFlags(DataRefImpl Symb) const override;
155
156
  basic_symbol_iterator symbol_begin() const override;
157
158
  basic_symbol_iterator symbol_end() const override;
159
  Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
160
161
  Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
162
  uint64_t getWasmSymbolValue(const WasmSymbol &Sym) const;
163
  uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
164
  uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
165
  uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
166
  Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
167
  Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
168
169
  // Overrides from SectionRef.
170
  void moveSectionNext(DataRefImpl &Sec) const override;
171
  std::error_code getSectionName(DataRefImpl Sec,
172
                                 StringRef &Res) const override;
173
  uint64_t getSectionAddress(DataRefImpl Sec) const override;
174
  uint64_t getSectionIndex(DataRefImpl Sec) const override;
175
  uint64_t getSectionSize(DataRefImpl Sec) const override;
176
  std::error_code getSectionContents(DataRefImpl Sec,
177
                                     StringRef &Res) const override;
178
  uint64_t getSectionAlignment(DataRefImpl Sec) const override;
179
  bool isSectionCompressed(DataRefImpl Sec) const override;
180
  bool isSectionText(DataRefImpl Sec) const override;
181
  bool isSectionData(DataRefImpl Sec) const override;
182
  bool isSectionBSS(DataRefImpl Sec) const override;
183
  bool isSectionVirtual(DataRefImpl Sec) const override;
184
  bool isSectionBitcode(DataRefImpl Sec) const override;
185
  relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
186
  relocation_iterator section_rel_end(DataRefImpl Sec) const override;
187
188
  // Overrides from RelocationRef.
189
  void moveRelocationNext(DataRefImpl &Rel) const override;
190
  uint64_t getRelocationOffset(DataRefImpl Rel) const override;
191
  symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
192
  uint64_t getRelocationType(DataRefImpl Rel) const override;
193
  void getRelocationTypeName(DataRefImpl Rel,
194
                             SmallVectorImpl<char> &Result) const override;
195
196
  section_iterator section_begin() const override;
197
  section_iterator section_end() const override;
198
  uint8_t getBytesInAddress() const override;
199
  StringRef getFileFormatName() const override;
200
  Triple::ArchType getArch() const override;
201
  SubtargetFeatures getFeatures() const override;
202
  bool isRelocatableObject() const override;
203
  bool isSharedObject() const;
204
205
  struct ReadContext {
206
    const uint8_t *Start;
207
    const uint8_t *Ptr;
208
    const uint8_t *End;
209
  };
210
211
private:
212
  bool isValidFunctionIndex(uint32_t Index) const;
213
  bool isDefinedFunctionIndex(uint32_t Index) const;
214
  bool isValidGlobalIndex(uint32_t Index) const;
215
  bool isDefinedGlobalIndex(uint32_t Index) const;
216
  bool isValidEventIndex(uint32_t Index) const;
217
  bool isDefinedEventIndex(uint32_t Index) const;
218
  bool isValidFunctionSymbol(uint32_t Index) const;
219
  bool isValidGlobalSymbol(uint32_t Index) const;
220
  bool isValidEventSymbol(uint32_t Index) const;
221
  bool isValidDataSymbol(uint32_t Index) const;
222
  bool isValidSectionSymbol(uint32_t Index) const;
223
  wasm::WasmFunction &getDefinedFunction(uint32_t Index);
224
  const wasm::WasmFunction &getDefinedFunction(uint32_t Index) const;
225
  wasm::WasmGlobal &getDefinedGlobal(uint32_t Index);
226
  wasm::WasmEvent &getDefinedEvent(uint32_t Index);
227
228
  const WasmSection &getWasmSection(DataRefImpl Ref) const;
229
  const wasm::WasmRelocation &getWasmRelocation(DataRefImpl Ref) const;
230
231
  Error parseSection(WasmSection &Sec);
232
  Error parseCustomSection(WasmSection &Sec, ReadContext &Ctx);
233
234
  // Standard section types
235
  Error parseTypeSection(ReadContext &Ctx);
236
  Error parseImportSection(ReadContext &Ctx);
237
  Error parseFunctionSection(ReadContext &Ctx);
238
  Error parseTableSection(ReadContext &Ctx);
239
  Error parseMemorySection(ReadContext &Ctx);
240
  Error parseGlobalSection(ReadContext &Ctx);
241
  Error parseEventSection(ReadContext &Ctx);
242
  Error parseExportSection(ReadContext &Ctx);
243
  Error parseStartSection(ReadContext &Ctx);
244
  Error parseElemSection(ReadContext &Ctx);
245
  Error parseCodeSection(ReadContext &Ctx);
246
  Error parseDataSection(ReadContext &Ctx);
247
248
  // Custom section types
249
  Error parseDylinkSection(ReadContext &Ctx);
250
  Error parseNameSection(ReadContext &Ctx);
251
  Error parseLinkingSection(ReadContext &Ctx);
252
  Error parseLinkingSectionSymtab(ReadContext &Ctx);
253
  Error parseLinkingSectionComdat(ReadContext &Ctx);
254
  Error parseProducersSection(ReadContext &Ctx);
255
  Error parseRelocSection(StringRef Name, ReadContext &Ctx);
256
257
  wasm::WasmObjectHeader Header;
258
  std::vector<WasmSection> Sections;
259
  wasm::WasmDylinkInfo DylinkInfo;
260
  wasm::WasmProducerInfo ProducerInfo;
261
  std::vector<wasm::WasmSignature> Signatures;
262
  std::vector<uint32_t> FunctionTypes;
263
  std::vector<wasm::WasmTable> Tables;
264
  std::vector<wasm::WasmLimits> Memories;
265
  std::vector<wasm::WasmGlobal> Globals;
266
  std::vector<wasm::WasmEvent> Events;
267
  std::vector<wasm::WasmImport> Imports;
268
  std::vector<wasm::WasmExport> Exports;
269
  std::vector<wasm::WasmElemSegment> ElemSegments;
270
  std::vector<WasmSegment> DataSegments;
271
  std::vector<wasm::WasmFunction> Functions;
272
  std::vector<WasmSymbol> Symbols;
273
  std::vector<wasm::WasmFunctionName> DebugNames;
274
  uint32_t StartFunction = -1;
275
  bool HasLinkingSection = false;
276
  bool HasDylinkSection = false;
277
  wasm::WasmLinkingData LinkingData;
278
  uint32_t NumImportedGlobals = 0;
279
  uint32_t NumImportedFunctions = 0;
280
  uint32_t NumImportedEvents = 0;
281
  uint32_t CodeSection = 0;
282
  uint32_t DataSection = 0;
283
  uint32_t GlobalSection = 0;
284
  uint32_t EventSection = 0;
285
};
286
287
class WasmSectionOrderChecker {
288
public:
289
  // We define orders for all core wasm sections and known custom sections.
290
  enum : int {
291
    // Core sections
292
    // The order of standard sections is precisely given by the spec.
293
    WASM_SEC_ORDER_TYPE = 1,
294
    WASM_SEC_ORDER_IMPORT = 2,
295
    WASM_SEC_ORDER_FUNCTION = 3,
296
    WASM_SEC_ORDER_TABLE = 4,
297
    WASM_SEC_ORDER_MEMORY = 5,
298
    WASM_SEC_ORDER_GLOBAL = 6,
299
    WASM_SEC_ORDER_EVENT = 7,
300
    WASM_SEC_ORDER_EXPORT = 8,
301
    WASM_SEC_ORDER_START = 9,
302
    WASM_SEC_ORDER_ELEM = 10,
303
    WASM_SEC_ORDER_DATACOUNT = 11,
304
    WASM_SEC_ORDER_CODE = 12,
305
    WASM_SEC_ORDER_DATA = 13,
306
307
    // Custom sections
308
    // "dylink" should be the very first section in the module
309
    WASM_SEC_ORDER_DYLINK = 0,
310
    // "linking" section requires DATA section in order to validate data symbols
311
    WASM_SEC_ORDER_LINKING = 100,
312
    // Must come after "linking" section in order to validate reloc indexes.
313
    WASM_SEC_ORDER_RELOC = 101,
314
    // "name" section must appear after DATA. Comes after "linking" to allow
315
    // symbol table to set default function name.
316
    WASM_SEC_ORDER_NAME = 102,
317
    // "producers" section must appear after "name" section.
318
    WASM_SEC_ORDER_PRODUCERS = 103
319
  };
320
321
  bool isValidSectionOrder(unsigned ID, StringRef CustomSectionName = "");
322
323
private:
324
  int LastOrder = -1; // Lastly seen known section's order
325
326
  // Returns -1 for unknown sections.
327
  int getSectionOrder(unsigned ID, StringRef CustomSectionName = "");
328
};
329
330
} // end namespace object
331
332
0
inline raw_ostream &operator<<(raw_ostream &OS, const object::WasmSymbol &Sym) {
333
0
  Sym.print(OS);
334
0
  return OS;
335
0
}
336
337
} // end namespace llvm
338
339
#endif // LLVM_OBJECT_WASM_H