Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,13 @@ if (BUILD_TOOLS)
INSTALL
)

# component
wabt_executable(
NAME component
SOURCES src/tools/component.cc
INSTALL
)

if(BUILD_FUZZ_TOOLS)
# wasm2wat-fuzz
wabt_executable(
Expand Down
8 changes: 8 additions & 0 deletions include/wabt/binary-reader-ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace wabt {

struct Component;
struct Module;
struct ReadBinaryOptions;

Expand All @@ -32,6 +33,13 @@ Result ReadBinaryIr(const char* filename,
Errors*,
Module* out_module);

Result ReadBinaryComponentIr(const char* filename,
const void* data,
size_t size,
const ReadBinaryOptions& options,
Errors*,
Component* out_component);

} // namespace wabt

#endif /* WABT_BINARY_READER_IR_H_ */
38 changes: 35 additions & 3 deletions include/wabt/binary-reader-logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,20 @@ class BinaryReaderLogging : public BinaryReaderDelegate {

Result BeginTypeSection(Offset size) override;
Result OnTypeCount(Index count) override;
Result OnRecursiveType(Index first_type_index, Index type_count) override;
Result OnFuncType(Index index,
Index param_count,
Type* param_types,
Index result_count,
Type* result_types) override;
Result OnStructType(Index index, Index field_count, TypeMut* fields) override;
Result OnArrayType(Index index, TypeMut field) override;
Type* result_types,
SupertypesInfo* supertypes) override;
Result OnStructType(Index index,
Index field_count,
TypeMut* fields,
SupertypesInfo* supertypes) override;
Result OnArrayType(Index index,
TypeMut field,
SupertypesInfo* supertypes) override;
Result EndTypeSection() override;

Result BeginImportSection(Offset size) override;
Expand Down Expand Up @@ -158,6 +165,17 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
Result OnOpcodeV128(v128 value) override;
Result OnOpcodeBlockSig(Type sig_type) override;
Result OnOpcodeType(Type type) override;
Result OnArrayCopyExpr(Index dst_type_index, Index src_type_index) override;
Result OnArrayFillExpr(Index type_index) override;
Result OnArrayGetExpr(Opcode opcode, Index type_index) override;
Result OnArrayInitDataExpr(Index type_index, Index data_index) override;
Result OnArrayInitElemExpr(Index type_index, Index elem_index) override;
Result OnArrayNewExpr(Index type_index) override;
Result OnArrayNewDataExpr(Index type_index, Index data_index) override;
Result OnArrayNewDefaultExpr(Index type_index) override;
Result OnArrayNewElemExpr(Index type_index, Index elem_index) override;
Result OnArrayNewFixedExpr(Index type_index, Index count) override;
Result OnArraySetExpr(Index type_index) override;
Result OnAtomicLoadExpr(Opcode opcode,
Index memidx,
Address alignment_log2,
Expand All @@ -178,6 +196,10 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
Result OnBlockExpr(Type sig_type) override;
Result OnBrExpr(Index depth) override;
Result OnBrIfExpr(Index depth) override;
Result OnBrOnCastExpr(Opcode opcode,
Index depth,
Type type1,
Type type2) override;
Result OnBrOnNonNullExpr(Index depth) override;
Result OnBrOnNullExpr(Index depth) override;
Result OnBrTableExpr(Index num_targets,
Expand All @@ -197,6 +219,7 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
Result OnF32ConstExpr(uint32_t value_bits) override;
Result OnF64ConstExpr(uint64_t value_bits) override;
Result OnV128ConstExpr(v128 value_bits) override;
Result OnGCUnaryExpr(Opcode opcode) override;
Result OnGlobalGetExpr(Index global_index) override;
Result OnGlobalSetExpr(Index global_index) override;
Result OnI32ConstExpr(uint32_t value) override;
Expand Down Expand Up @@ -225,9 +248,11 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
Result OnTableSizeExpr(Index table) override;
Result OnTableFillExpr(Index table) override;
Result OnRefAsNonNullExpr() override;
Result OnRefCastExpr(Type type) override;
Result OnRefFuncExpr(Index index) override;
Result OnRefNullExpr(Type type) override;
Result OnRefIsNullExpr() override;
Result OnRefTestExpr(Type type) override;
Result OnNopExpr() override;
Result OnRethrowExpr(Index depth) override;
Result OnReturnCallExpr(Index func_index) override;
Expand All @@ -239,6 +264,12 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
Index memidx,
Address alignment_log2,
Address offset) override;
Result OnStructGetExpr(Opcode opcode,
Index type_index,
Index field_index) override;
Result OnStructNewExpr(Index type_index) override;
Result OnStructNewDefaultExpr(Index type_index) override;
Result OnStructSetExpr(Index type_index, Index field_index) override;
Result OnThrowExpr(Index tag_index) override;
Result OnThrowRefExpr() override;
Result OnTryExpr(Type sig_type) override;
Expand Down Expand Up @@ -432,6 +463,7 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
void LogType(Type type);
void LogTypes(Index type_count, Type* types);
void LogTypes(TypeVector& types);
void LogSupertypesInfo(SupertypesInfo* supertypes);
void LogField(TypeMut field);

Stream* stream_;
Expand Down
61 changes: 58 additions & 3 deletions include/wabt/binary-reader-nop.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,29 @@ class BinaryReaderNop : public BinaryReaderDelegate {

/* Type section */
Result BeginTypeSection(Offset size) override { return Result::Ok; }
Result OnRecursiveType(Index first_type_index, Index type_count) override {
return Result::Ok;
}
Result OnTypeCount(Index count) override { return Result::Ok; }
Result OnFuncType(Index index,
Index param_count,
Type* param_types,
Index result_count,
Type* result_types) override {
Type* result_types,
SupertypesInfo* supertypes) override {
return Result::Ok;
}
Result OnStructType(Index index,
Index field_count,
TypeMut* fields) override {
TypeMut* fields,
SupertypesInfo* supertypes) override {
return Result::Ok;
}
Result OnArrayType(Index index,
TypeMut field,
SupertypesInfo* supertypes) override {
return Result::Ok;
}
Result OnArrayType(Index index, TypeMut field) override { return Result::Ok; }
Result EndTypeSection() override { return Result::Ok; }

/* Import section */
Expand Down Expand Up @@ -210,6 +219,31 @@ class BinaryReaderNop : public BinaryReaderDelegate {
Result OnOpcodeV128(v128 value) override { return Result::Ok; }
Result OnOpcodeBlockSig(Type sig_type) override { return Result::Ok; }
Result OnOpcodeType(Type type) override { return Result::Ok; }
Result OnArrayCopyExpr(Index dst_type_index, Index src_type_index) override {
return Result::Ok;
}
Result OnArrayFillExpr(Index type_index) override { return Result::Ok; }
Result OnArrayGetExpr(Opcode opcode, Index type_index) override {
return Result::Ok;
}
Result OnArrayInitDataExpr(Index type_index, Index data_index) override {
return Result::Ok;
}
Result OnArrayInitElemExpr(Index type_index, Index elem_index) override {
return Result::Ok;
}
Result OnArrayNewExpr(Index type_index) override { return Result::Ok; }
Result OnArrayNewDataExpr(Index type_index, Index data_index) override {
return Result::Ok;
}
Result OnArrayNewDefaultExpr(Index type_index) override { return Result::Ok; }
Result OnArrayNewElemExpr(Index type_index, Index elem_index) override {
return Result::Ok;
}
Result OnArrayNewFixedExpr(Index type_index, Index count) override {
return Result::Ok;
}
Result OnArraySetExpr(Index type_index) override { return Result::Ok; }
Result OnAtomicLoadExpr(Opcode opcode,
Index memidx,
Address alignment_log2,
Expand Down Expand Up @@ -245,6 +279,12 @@ class BinaryReaderNop : public BinaryReaderDelegate {
Result OnBlockExpr(Type sig_type) override { return Result::Ok; }
Result OnBrExpr(Index depth) override { return Result::Ok; }
Result OnBrIfExpr(Index depth) override { return Result::Ok; }
Result OnBrOnCastExpr(Opcode opcode,
Index depth,
Type type1,
Type type2) override {
return Result::Ok;
}
Result OnBrOnNonNullExpr(Index depth) override { return Result::Ok; }
Result OnBrOnNullExpr(Index depth) override { return Result::Ok; }
Result OnBrTableExpr(Index num_targets,
Expand All @@ -268,6 +308,7 @@ class BinaryReaderNop : public BinaryReaderDelegate {
Result OnF32ConstExpr(uint32_t value_bits) override { return Result::Ok; }
Result OnF64ConstExpr(uint64_t value_bits) override { return Result::Ok; }
Result OnV128ConstExpr(v128 value_bits) override { return Result::Ok; }
Result OnGCUnaryExpr(Opcode opcode) override { return Result::Ok; }
Result OnGlobalGetExpr(Index global_index) override { return Result::Ok; }
Result OnGlobalSetExpr(Index global_index) override { return Result::Ok; }
Result OnI32ConstExpr(uint32_t value) override { return Result::Ok; }
Expand Down Expand Up @@ -306,9 +347,11 @@ class BinaryReaderNop : public BinaryReaderDelegate {
Result OnTableSizeExpr(Index table_index) override { return Result::Ok; }
Result OnTableFillExpr(Index table_index) override { return Result::Ok; }
Result OnRefAsNonNullExpr() override { return Result::Ok; }
Result OnRefCastExpr(Type type) override { return Result::Ok; }
Result OnRefFuncExpr(Index func_index) override { return Result::Ok; }
Result OnRefNullExpr(Type type) override { return Result::Ok; }
Result OnRefIsNullExpr() override { return Result::Ok; }
Result OnRefTestExpr(Type type) override { return Result::Ok; }
Result OnNopExpr() override { return Result::Ok; }
Result OnRethrowExpr(Index depth) override { return Result::Ok; }
Result OnReturnCallExpr(Index sig_index) override { return Result::Ok; }
Expand All @@ -326,6 +369,18 @@ class BinaryReaderNop : public BinaryReaderDelegate {
Address offset) override {
return Result::Ok;
}
Result OnStructGetExpr(Opcode opcode,
Index type_index,
Index field_index) override {
return Result::Ok;
}
Result OnStructNewExpr(Index type_index) override { return Result::Ok; }
Result OnStructNewDefaultExpr(Index type_index) override {
return Result::Ok;
}
Result OnStructSetExpr(Index type_index, Index field_index) override {
return Result::Ok;
}
Result OnThrowExpr(Index depth) override { return Result::Ok; }
Result OnThrowRefExpr() override { return Result::Ok; }
Result OnTryExpr(Type sig_type) override { return Result::Ok; }
Expand Down
65 changes: 61 additions & 4 deletions include/wabt/binary-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,20 @@ struct ReadBinaryOptions {
bool skip_function_bodies = false;
};

// TODO: Move somewhere else?
// TODO: Move both TypeMut and GCTypeInformation somewhere else?
struct TypeMut {
Type type;
bool mutable_;
};
using TypeMutVector = std::vector<TypeMut>;

// Type extension introduced by the Garbage Collector proposal
struct SupertypesInfo {
bool is_final_sub_type;
Index sub_type_count;
Index* sub_types;
};

struct CatchClause {
CatchKind kind;
Index tag;
Expand Down Expand Up @@ -104,15 +111,20 @@ class BinaryReaderDelegate {
/* Type section */
virtual Result BeginTypeSection(Offset size) = 0;
virtual Result OnTypeCount(Index count) = 0;
virtual Result OnRecursiveType(Index first_type_index, Index type_count) = 0;
virtual Result OnFuncType(Index index,
Index param_count,
Type* param_types,
Index result_count,
Type* result_types) = 0;
Type* result_types,
SupertypesInfo* supertypes) = 0;
virtual Result OnStructType(Index index,
Index field_count,
TypeMut* fields) = 0;
virtual Result OnArrayType(Index index, TypeMut field) = 0;
TypeMut* fields,
SupertypesInfo* supertypes) = 0;
virtual Result OnArrayType(Index index,
TypeMut field,
SupertypesInfo* supertypes) = 0;
virtual Result EndTypeSection() = 0;

/* Import section */
Expand Down Expand Up @@ -230,6 +242,17 @@ class BinaryReaderDelegate {
virtual Result OnOpcodeV128(v128 value) = 0;
virtual Result OnOpcodeBlockSig(Type sig_type) = 0;
virtual Result OnOpcodeType(Type type) = 0;
virtual Result OnArrayCopyExpr(Index dst_type_index, Index src_type_index) = 0;
virtual Result OnArrayFillExpr(Index type_index) = 0;
virtual Result OnArrayGetExpr(Opcode opcode, Index type_index) = 0;
virtual Result OnArrayInitDataExpr(Index type_index, Index data_index) = 0;
virtual Result OnArrayInitElemExpr(Index type_index, Index elem_index) = 0;
virtual Result OnArrayNewExpr(Index type_index) = 0;
virtual Result OnArrayNewDataExpr(Index type_index, Index data_index) = 0;
virtual Result OnArrayNewDefaultExpr(Index type_index) = 0;
virtual Result OnArrayNewElemExpr(Index type_index, Index elem_index) = 0;
virtual Result OnArrayNewFixedExpr(Index type_index, Index count) = 0;
virtual Result OnArraySetExpr(Index type_index) = 0;
virtual Result OnAtomicLoadExpr(Opcode opcode,
Index memidx,
Address alignment_log2,
Expand Down Expand Up @@ -259,6 +282,10 @@ class BinaryReaderDelegate {
virtual Result OnBlockExpr(Type sig_type) = 0;
virtual Result OnBrExpr(Index depth) = 0;
virtual Result OnBrIfExpr(Index depth) = 0;
virtual Result OnBrOnCastExpr(Opcode opcode,
Index depth,
Type type1,
Type type2) = 0;
virtual Result OnBrOnNonNullExpr(Index depth) = 0;
virtual Result OnBrOnNullExpr(Index depth) = 0;
virtual Result OnBrTableExpr(Index num_targets,
Expand All @@ -278,6 +305,7 @@ class BinaryReaderDelegate {
virtual Result OnF32ConstExpr(uint32_t value_bits) = 0;
virtual Result OnF64ConstExpr(uint64_t value_bits) = 0;
virtual Result OnV128ConstExpr(v128 value_bits) = 0;
virtual Result OnGCUnaryExpr(Opcode opcode) = 0;
virtual Result OnGlobalGetExpr(Index global_index) = 0;
virtual Result OnGlobalSetExpr(Index global_index) = 0;
virtual Result OnI32ConstExpr(uint32_t value) = 0;
Expand Down Expand Up @@ -306,9 +334,11 @@ class BinaryReaderDelegate {
virtual Result OnTableSizeExpr(Index table_index) = 0;
virtual Result OnTableFillExpr(Index table_index) = 0;
virtual Result OnRefAsNonNullExpr() = 0;
virtual Result OnRefCastExpr(Type type) = 0;
virtual Result OnRefFuncExpr(Index func_index) = 0;
virtual Result OnRefNullExpr(Type type) = 0;
virtual Result OnRefIsNullExpr() = 0;
virtual Result OnRefTestExpr(Type type) = 0;
virtual Result OnNopExpr() = 0;
virtual Result OnRethrowExpr(Index depth) = 0;
virtual Result OnReturnExpr() = 0;
Expand All @@ -321,6 +351,12 @@ class BinaryReaderDelegate {
Index memidx,
Address alignment_log2,
Address offset) = 0;
virtual Result OnStructGetExpr(Opcode opcode,
Index type_index,
Index field_index) = 0;
virtual Result OnStructNewExpr(Index type_index) = 0;
virtual Result OnStructNewDefaultExpr(Index type_index) = 0;
virtual Result OnStructSetExpr(Index type_index, Index field_index) = 0;
virtual Result OnThrowExpr(Index tag_index) = 0;
virtual Result OnThrowRefExpr() = 0;
virtual Result OnTryExpr(Type sig_type) = 0;
Expand Down Expand Up @@ -519,11 +555,32 @@ class BinaryReaderDelegate {
const State* state = nullptr;
};

class ComponentBinaryReaderDelegate {
public:
virtual ~ComponentBinaryReaderDelegate() {}

virtual bool OnError(const Error&) = 0;
virtual void OnSetState(const BinaryReaderDelegate::State* s) { state = s; }

virtual Result OnCoreModule(const void* data,
size_t size,
const ReadBinaryOptions& options) = 0;
virtual Result BeginComponent(uint32_t version, size_t depth) = 0;
virtual Result EndComponent() = 0;

const BinaryReaderDelegate::State* state = nullptr;
};

Result ReadBinary(const void* data,
size_t size,
BinaryReaderDelegate* reader,
const ReadBinaryOptions& options);

Result ReadBinaryComponent(const void* data,
size_t size,
ComponentBinaryReaderDelegate* component_delegate,
const ReadBinaryOptions& options);

size_t ReadU32Leb128(const uint8_t* ptr,
const uint8_t* end,
uint32_t* out_value);
Expand Down
Loading
Loading