Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/wabt/binary-reader.h
1 /* 2 * Copyright 2016 WebAssembly Community Group participants 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef WABT_BINARY_READER_H_ 18 #define WABT_BINARY_READER_H_ 19 20 #include <cstddef> 21 #include <cstdint> 22 #include <string_view> 23 24 #include "wabt/binary.h" 25 #include "wabt/common.h" 26 #include "wabt/error.h" 27 #include "wabt/feature.h" 28 #include "wabt/opcode.h" 29 30 namespace wabt { 31 32 class Stream; 33 34 struct ReadBinaryOptions { 35 ReadBinaryOptions() = default; 36 ReadBinaryOptions(const Features& features, 37 Stream* log_stream, 38 bool read_debug_names, 39 bool stop_on_first_error, 40 bool fail_on_custom_section_error) 41 : features(features), 42 log_stream(log_stream), 43 read_debug_names(read_debug_names), 44 stop_on_first_error(stop_on_first_error), 45 fail_on_custom_section_error(fail_on_custom_section_error) {} 46 47 Features features; 48 Stream* log_stream = nullptr; 49 bool read_debug_names = false; 50 bool stop_on_first_error = true; 51 bool fail_on_custom_section_error = true; 52 bool skip_function_bodies = false; 53 }; 54 55 // TODO: Move somewhere else? 56 struct TypeMut { 57 Type type; 58 bool mutable_; 59 }; 60 using TypeMutVector = std::vector<TypeMut>; 61 62 class BinaryReaderDelegate { 63 public: 64 struct State { 65 State(const uint8_t* data, Offset size) 66 : data(data), size(size), offset(0) {} 67 68 const uint8_t* data; 69 Offset size; 70 Offset offset; 71 }; 72 73 virtual ~BinaryReaderDelegate() {} 74 75 virtual bool OnError(const Error&) = 0; 76 virtual void OnSetState(const State* s) { state = s; } 77 78 /* Module */ 79 virtual Result BeginModule(uint32_t version) = 0; 80 virtual Result EndModule() = 0; 81 82 virtual Result BeginSection(Index section_index, 83 BinarySection section_type, 84 Offset size) = 0; 85 86 /* Custom section */ 87 virtual Result BeginCustomSection(Index section_index, 88 Offset size, 89 std::string_view section_name) = 0; 90 virtual Result EndCustomSection() = 0; 91 92 /* Type section */ 93 virtual Result BeginTypeSection(Offset size) = 0; 94 virtual Result OnTypeCount(Index count) = 0; 95 virtual Result OnFuncType(Index index, 96 Index param_count, 97 Type* param_types, 98 Index result_count, 99 Type* result_types) = 0; 100 virtual Result OnStructType(Index index, 101 Index field_count, 102 TypeMut* fields) = 0; 103 virtual Result OnArrayType(Index index, TypeMut field) = 0; 104 virtual Result EndTypeSection() = 0; 105 106 /* Import section */ 107 virtual Result BeginImportSection(Offset size) = 0; 108 virtual Result OnImportCount(Index count) = 0; 109 virtual Result OnImport(Index index, 110 ExternalKind kind, 111 std::string_view module_name, 112 std::string_view field_name) = 0; 113 virtual Result OnImportFunc(Index import_index, 114 std::string_view module_name, 115 std::string_view field_name, 116 Index func_index, 117 Index sig_index) = 0; 118 virtual Result OnImportTable(Index import_index, 119 std::string_view module_name, 120 std::string_view field_name, 121 Index table_index, 122 Type elem_type, 123 const Limits* elem_limits) = 0; 124 virtual Result OnImportMemory(Index import_index, 125 std::string_view module_name, 126 std::string_view field_name, 127 Index memory_index, 128 const Limits* page_limits) = 0; 129 virtual Result OnImportGlobal(Index import_index, 130 std::string_view module_name, 131 std::string_view field_name, 132 Index global_index, 133 Type type, 134 bool mutable_) = 0; 135 virtual Result OnImportTag(Index import_index, 136 std::string_view module_name, 137 std::string_view field_name, 138 Index tag_index, 139 Index sig_index) = 0; 140 virtual Result EndImportSection() = 0; 141 142 /* Function section */ 143 virtual Result BeginFunctionSection(Offset size) = 0; 144 virtual Result OnFunctionCount(Index count) = 0; 145 virtual Result OnFunction(Index index, Index sig_index) = 0; 146 virtual Result EndFunctionSection() = 0; 147 148 /* Table section */ 149 virtual Result BeginTableSection(Offset size) = 0; 150 virtual Result OnTableCount(Index count) = 0; 151 virtual Result OnTable(Index index, 152 Type elem_type, 153 const Limits* elem_limits) = 0; 154 virtual Result EndTableSection() = 0; 155 156 /* Memory section */ 157 virtual Result BeginMemorySection(Offset size) = 0; 158 virtual Result OnMemoryCount(Index count) = 0; 159 virtual Result OnMemory(Index index, const Limits* limits) = 0; 160 virtual Result EndMemorySection() = 0; 161 162 /* Global section */ 163 virtual Result BeginGlobalSection(Offset size) = 0; 164 virtual Result OnGlobalCount(Index count) = 0; 165 virtual Result BeginGlobal(Index index, Type type, bool mutable_) = 0; 166 virtual Result BeginGlobalInitExpr(Index index) = 0; 167 virtual Result EndGlobalInitExpr(Index index) = 0; 168 virtual Result EndGlobal(Index index) = 0; 169 virtual Result EndGlobalSection() = 0; 170 171 /* Exports section */ 172 virtual Result BeginExportSection(Offset size) = 0; 173 virtual Result OnExportCount(Index count) = 0; 174 virtual Result OnExport(Index index, 175 ExternalKind kind, 176 Index item_index, 177 std::string_view name) = 0; 178 virtual Result EndExportSection() = 0; 179 180 /* Start section */ 181 virtual Result BeginStartSection(Offset size) = 0; 182 virtual Result OnStartFunction(Index func_index) = 0; 183 virtual Result EndStartSection() = 0; 184 185 /* Code section */ 186 virtual Result BeginCodeSection(Offset size) = 0; 187 virtual Result OnFunctionBodyCount(Index count) = 0; 188 virtual Result BeginFunctionBody(Index index, Offset size) = 0; 189 virtual Result OnLocalDeclCount(Index count) = 0; 190 virtual Result OnLocalDecl(Index decl_index, Index count, Type type) = 0; 191 192 /* Function expressions; called between BeginFunctionBody and 193 EndFunctionBody */ 194 virtual Result OnOpcode(Opcode Opcode) = 0; 195 virtual Result OnOpcodeBare() = 0; 196 virtual Result OnOpcodeUint32(uint32_t value) = 0; 197 virtual Result OnOpcodeIndex(Index value) = 0; 198 virtual Result OnOpcodeIndexIndex(Index value, Index value2) = 0; 199 virtual Result OnOpcodeUint32Uint32(uint32_t value, uint32_t value2) = 0; 200 virtual Result OnOpcodeUint32Uint32Uint32(uint32_t value, 201 uint32_t value2, 202 uint32_t value3) = 0; 203 virtual Result OnOpcodeUint32Uint32Uint32Uint32(uint32_t value, 204 uint32_t value2, 205 uint32_t value3, 206 uint32_t value4) = 0; 207 virtual Result OnOpcodeUint64(uint64_t value) = 0; 208 virtual Result OnOpcodeF32(uint32_t value) = 0; 209 virtual Result OnOpcodeF64(uint64_t value) = 0; 210 virtual Result OnOpcodeV128(v128 value) = 0; 211 virtual Result OnOpcodeBlockSig(Type sig_type) = 0; 212 virtual Result OnOpcodeType(Type type) = 0; 213 virtual Result OnAtomicLoadExpr(Opcode opcode, 214 Index memidx, 215 Address alignment_log2, 216 Address offset) = 0; 217 virtual Result OnAtomicStoreExpr(Opcode opcode, 218 Index memidx, 219 Address alignment_log2, 220 Address offset) = 0; 221 virtual Result OnAtomicRmwExpr(Opcode opcode, 222 Index memidx, 223 Address alignment_log2, 224 Address offset) = 0; 225 virtual Result OnAtomicRmwCmpxchgExpr(Opcode opcode, 226 Index memidx, 227 Address alignment_log2, 228 Address offset) = 0; 229 virtual Result OnAtomicWaitExpr(Opcode opcode, 230 Index memidx, 231 Address alignment_log2, 232 Address offset) = 0; 233 virtual Result OnAtomicFenceExpr(uint32_t consistency_model) = 0; 234 virtual Result OnAtomicNotifyExpr(Opcode opcode, 235 Index memidx, 236 Address alignment_log2, 237 Address offset) = 0; 238 virtual Result OnBinaryExpr(Opcode opcode) = 0; 239 virtual Result OnBlockExpr(Type sig_type) = 0; 240 virtual Result OnBrExpr(Index depth) = 0; 241 virtual Result OnBrIfExpr(Index depth) = 0; 242 virtual Result OnBrTableExpr(Index num_targets, 243 Index* target_depths, 244 Index default_target_depth) = 0; 245 virtual Result OnCallExpr(Index func_index) = 0; 246 virtual Result OnCallIndirectExpr(Index sig_index, Index table_index) = 0; 247 virtual Result OnCallRefExpr() = 0; 248 virtual Result OnCatchExpr(Index tag_index) = 0; 249 virtual Result OnCatchAllExpr() = 0; 250 virtual Result OnCompareExpr(Opcode opcode) = 0; 251 virtual Result OnConvertExpr(Opcode opcode) = 0; 252 virtual Result OnDelegateExpr(Index depth) = 0; 253 virtual Result OnDropExpr() = 0; 254 virtual Result OnElseExpr() = 0; 255 virtual Result OnEndExpr() = 0; 256 virtual Result OnF32ConstExpr(uint32_t value_bits) = 0; 257 virtual Result OnF64ConstExpr(uint64_t value_bits) = 0; 258 virtual Result OnV128ConstExpr(v128 value_bits) = 0; 259 virtual Result OnGlobalGetExpr(Index global_index) = 0; 260 virtual Result OnGlobalSetExpr(Index global_index) = 0; 261 virtual Result OnI32ConstExpr(uint32_t value) = 0; 262 virtual Result OnI64ConstExpr(uint64_t value) = 0; 263 virtual Result OnIfExpr(Type sig_type) = 0; 264 virtual Result OnLoadExpr(Opcode opcode, 265 Index memidx, 266 Address alignment_log2, 267 Address offset) = 0; 268 virtual Result OnLocalGetExpr(Index local_index) = 0; 269 virtual Result OnLocalSetExpr(Index local_index) = 0; 270 virtual Result OnLocalTeeExpr(Index local_index) = 0; 271 virtual Result OnLoopExpr(Type sig_type) = 0; 272 virtual Result OnMemoryCopyExpr(Index destmemidx, Index srcmemidx) = 0; 273 virtual Result OnDataDropExpr(Index segment_index) = 0; 274 virtual Result OnMemoryFillExpr(Index memidx) = 0; 275 virtual Result OnMemoryGrowExpr(Index memidx) = 0; 276 virtual Result OnMemoryInitExpr(Index segment_index, Index memidx) = 0; 277 virtual Result OnMemorySizeExpr(Index memidx) = 0; 278 virtual Result OnTableCopyExpr(Index dst_index, Index src_index) = 0; 279 virtual Result OnElemDropExpr(Index segment_index) = 0; 280 virtual Result OnTableInitExpr(Index segment_index, Index table_index) = 0; 281 virtual Result OnTableGetExpr(Index table_index) = 0; 282 virtual Result OnTableSetExpr(Index table_index) = 0; 283 virtual Result OnTableGrowExpr(Index table_index) = 0; 284 virtual Result OnTableSizeExpr(Index table_index) = 0; 285 virtual Result OnTableFillExpr(Index table_index) = 0; 286 virtual Result OnRefFuncExpr(Index func_index) = 0; 287 virtual Result OnRefNullExpr(Type type) = 0; 288 virtual Result OnRefIsNullExpr() = 0; 289 virtual Result OnNopExpr() = 0; 290 virtual Result OnRethrowExpr(Index depth) = 0; 291 virtual Result OnReturnExpr() = 0; 292 virtual Result OnReturnCallExpr(Index func_index) = 0; 293 virtual Result OnReturnCallIndirectExpr(Index sig_index, 294 Index table_index) = 0; 295 virtual Result OnSelectExpr(Index result_count, Type* result_types) = 0; 296 virtual Result OnStoreExpr(Opcode opcode, 297 Index memidx, 298 Address alignment_log2, 299 Address offset) = 0; 300 virtual Result OnThrowExpr(Index tag_index) = 0; 301 virtual Result OnTryExpr(Type sig_type) = 0; 302 303 virtual Result OnUnaryExpr(Opcode opcode) = 0; 304 virtual Result OnTernaryExpr(Opcode opcode) = 0; 305 virtual Result OnUnreachableExpr() = 0; 306 virtual Result EndFunctionBody(Index index) = 0; 307 virtual Result EndCodeSection() = 0; 308 309 /* Simd instructions with Lane Imm operand*/ 310 virtual Result OnSimdLaneOpExpr(Opcode opcode, uint64_t value) = 0; 311 virtual Result OnSimdShuffleOpExpr(Opcode opcode, v128 value) = 0; 312 virtual Result OnSimdLoadLaneExpr(Opcode opcode, 313 Index memidx, 314 Address alignment_log2, 315 Address offset, 316 uint64_t value) = 0; 317 virtual Result OnSimdStoreLaneExpr(Opcode opcode, 318 Index memidx, 319 Address alignment_log2, 320 Address offset, 321 uint64_t value) = 0; 322 323 virtual Result OnLoadSplatExpr(Opcode opcode, 324 Index memidx, 325 Address alignment_log2, 326 Address offset) = 0; 327 virtual Result OnLoadZeroExpr(Opcode opcode, 328 Index memidx, 329 Address alignment_log2, 330 Address offset) = 0; 331 332 /* Elem section */ 333 virtual Result BeginElemSection(Offset size) = 0; 334 virtual Result OnElemSegmentCount(Index count) = 0; 335 virtual Result BeginElemSegment(Index index, 336 Index table_index, 337 uint8_t flags) = 0; 338 virtual Result BeginElemSegmentInitExpr(Index index) = 0; 339 virtual Result EndElemSegmentInitExpr(Index index) = 0; 340 virtual Result OnElemSegmentElemType(Index index, Type elem_type) = 0; 341 virtual Result OnElemSegmentElemExprCount(Index index, Index count) = 0; 342 virtual Result BeginElemExpr(Index elem_index, Index expr_index) = 0; 343 virtual Result EndElemExpr(Index elem_index, Index expr_index) = 0; 344 virtual Result EndElemSegment(Index index) = 0; 345 virtual Result EndElemSection() = 0; 346 347 /* Data section */ 348 virtual Result BeginDataSection(Offset size) = 0; 349 virtual Result OnDataSegmentCount(Index count) = 0; 350 virtual Result BeginDataSegment(Index index, 351 Index memory_index, 352 uint8_t flags) = 0; 353 virtual Result BeginDataSegmentInitExpr(Index index) = 0; 354 virtual Result EndDataSegmentInitExpr(Index index) = 0; 355 virtual Result OnDataSegmentData(Index index, 356 const void* data, 357 Address size) = 0; 358 virtual Result EndDataSegment(Index index) = 0; 359 virtual Result EndDataSection() = 0; 360 361 /* DataCount section */ 362 virtual Result BeginDataCountSection(Offset size) = 0; 363 virtual Result OnDataCount(Index count) = 0; 364 virtual Result EndDataCountSection() = 0; 365 366 /* Names section */ 367 virtual Result BeginNamesSection(Offset size) = 0; 368 virtual Result OnModuleNameSubsection(Index index, 369 uint32_t name_type, 370 Offset subsection_size) = 0; 371 virtual Result OnModuleName(std::string_view name) = 0; 372 virtual Result OnFunctionNameSubsection(Index index, 373 uint32_t name_type, 374 Offset subsection_size) = 0; 375 virtual Result OnFunctionNamesCount(Index num_functions) = 0; 376 virtual Result OnFunctionName(Index function_index, 377 std::string_view function_name) = 0; 378 virtual Result OnLocalNameSubsection(Index index, 379 uint32_t name_type, 380 Offset subsection_size) = 0; 381 virtual Result OnLocalNameFunctionCount(Index num_functions) = 0; 382 virtual Result OnLocalNameLocalCount(Index function_index, 383 Index num_locals) = 0; 384 virtual Result OnLocalName(Index function_index, 385 Index local_index, 386 std::string_view local_name) = 0; 387 virtual Result OnNameSubsection(Index index, 388 NameSectionSubsection subsection_type, 389 Offset subsection_size) = 0; 390 virtual Result OnNameCount(Index num_names) = 0; 391 virtual Result OnNameEntry(NameSectionSubsection type, 392 Index index, 393 std::string_view name) = 0; 394 virtual Result EndNamesSection() = 0; 395 396 /* Reloc section */ 397 virtual Result BeginRelocSection(Offset size) = 0; 398 virtual Result OnRelocCount(Index count, Index section_index) = 0; 399 virtual Result OnReloc(RelocType type, 400 Offset offset, 401 Index index, 402 uint32_t addend) = 0; 403 virtual Result EndRelocSection() = 0; 404 405 /* Dylink section */ 406 virtual Result BeginDylinkSection(Offset size) = 0; 407 virtual Result OnDylinkInfo(uint32_t mem_size, 408 uint32_t mem_align_log2, 409 uint32_t table_size, 410 uint32_t table_align_log2) = 0; 411 virtual Result OnDylinkImportCount(Index count) = 0; 412 virtual Result OnDylinkExportCount(Index count) = 0; 413 virtual Result OnDylinkImport(std::string_view module, 414 std::string_view name, 415 uint32_t flags) = 0; 416 virtual Result OnDylinkExport(std::string_view name, uint32_t flags) = 0; 417 virtual Result OnDylinkNeededCount(Index count) = 0; 418 virtual Result OnDylinkNeeded(std::string_view so_name) = 0; 419 virtual Result EndDylinkSection() = 0; 420 421 /* target_features section */ 422 virtual Result BeginTargetFeaturesSection(Offset size) = 0; 423 virtual Result OnFeatureCount(Index count) = 0; 424 virtual Result OnFeature(uint8_t prefix, std::string_view name) = 0; 425 virtual Result EndTargetFeaturesSection() = 0; 426 427 /* Generic custom section */ 428 virtual Result BeginGenericCustomSection(Offset size) = 0; 429 virtual Result OnGenericCustomSection(std::string_view name, 430 const void* data, 431 Offset size) = 0; 432 virtual Result EndGenericCustomSection() = 0; 433 434 /* Linking section */ 435 virtual Result BeginLinkingSection(Offset size) = 0; 436 virtual Result OnSymbolCount(Index count) = 0; 437 virtual Result OnDataSymbol(Index index, 438 uint32_t flags, 439 std::string_view name, 440 Index segment, 441 uint32_t offset, 442 uint32_t size) = 0; 443 virtual Result OnFunctionSymbol(Index index, 444 uint32_t flags, 445 std::string_view name, 446 Index function_index) = 0; 447 virtual Result OnGlobalSymbol(Index index, 448 uint32_t flags, 449 std::string_view name, 450 Index global_index) = 0; 451 virtual Result OnSectionSymbol(Index index, 452 uint32_t flags, 453 Index section_index) = 0; 454 virtual Result OnTagSymbol(Index index, 455 uint32_t flags, 456 std::string_view name, 457 Index tag_index) = 0; 458 virtual Result OnTableSymbol(Index index, 459 uint32_t flags, 460 std::string_view name, 461 Index table_index) = 0; 462 virtual Result OnSegmentInfoCount(Index count) = 0; 463 virtual Result OnSegmentInfo(Index index, 464 std::string_view name, 465 Address alignment_log2, 466 uint32_t flags) = 0; 467 virtual Result OnInitFunctionCount(Index count) = 0; 468 virtual Result OnInitFunction(uint32_t priority, Index symbol_index) = 0; 469 virtual Result OnComdatCount(Index count) = 0; 470 virtual Result OnComdatBegin(std::string_view name, 471 uint32_t flags, 472 Index count) = 0; 473 virtual Result OnComdatEntry(ComdatType kind, Index index) = 0; 474 virtual Result EndLinkingSection() = 0; 475 476 /* Tag section */ 477 virtual Result BeginTagSection(Offset size) = 0; 478 virtual Result OnTagCount(Index count) = 0; 479 virtual Result OnTagType(Index index, Index sig_index) = 0; 480 virtual Result EndTagSection() = 0; 481 482 /* Code Metadata sections */ 483 virtual Result BeginCodeMetadataSection(std::string_view name, 484 Offset size) = 0; 485 virtual Result OnCodeMetadataFuncCount(Index count) = 0; 486 virtual Result OnCodeMetadataCount(Index function_index, Index count) = 0; 487 virtual Result OnCodeMetadata(Offset offset, 488 const void* data, 489 Address size) = 0; 490 virtual Result EndCodeMetadataSection() = 0; 491 492 const State* state = nullptr; 493 }; 494 495 Result ReadBinary(const void* data, 496 size_t size, 497 BinaryReaderDelegate* reader, 498 const ReadBinaryOptions& options); 499 500 size_t ReadU32Leb128(const uint8_t* ptr, 501 const uint8_t* end, 502 uint32_t* out_value); 503 504 size_t ReadI32Leb128(const uint8_t* ptr, 505 const uint8_t* end, 506 uint32_t* out_value); 507 508 } // namespace wabt 509 510 #endif /* WABT_BINARY_READER_H_ */