From f1031e8ac4a4eb2ec886c03fa432d5b5b3cc56a0 Mon Sep 17 00:00:00 2001 From: Ben Smith Date: Mon, 15 May 2017 10:07:21 -0700 Subject: Use Index/Address/Offset instead of uint32_t (#433) An `Index` is an index into one of the WebAssembly index spaces. It also is used for counts for these spaces, as well as parameter counts and result counts. An `Address` is an index into linear memory, or the size of a data region in linear memory. An `Offset` is an offset into the host's file or memory buffer. This fixes issue #322. --- src/binary-reader-interpreter.cc | 678 +++++++++++++++++++-------------------- 1 file changed, 339 insertions(+), 339 deletions(-) (limited to 'src/binary-reader-interpreter.cc') diff --git a/src/binary-reader-interpreter.cc b/src/binary-reader-interpreter.cc index 95508acc..0c2396ff 100644 --- a/src/binary-reader-interpreter.cc +++ b/src/binary-reader-interpreter.cc @@ -38,221 +38,225 @@ namespace wabt { namespace { -typedef std::vector Uint32Vector; -typedef std::vector Uint32VectorVector; +typedef std::vector IndexVector; +typedef std::vector IstreamOffsetVector; +typedef std::vector IstreamOffsetVectorVector; struct Label { - Label(uint32_t offset, uint32_t fixup_offset); + Label(IstreamOffset offset, IstreamOffset fixup_offset); - uint32_t offset; /* branch location in the istream */ - uint32_t fixup_offset; + IstreamOffset offset; + IstreamOffset fixup_offset; }; -Label::Label(uint32_t offset, uint32_t fixup_offset) +Label::Label(IstreamOffset offset, IstreamOffset fixup_offset) : offset(offset), fixup_offset(fixup_offset) {} struct ElemSegmentInfo { - ElemSegmentInfo(uint32_t* dst, uint32_t func_index) + ElemSegmentInfo(Index* dst, Index func_index) : dst(dst), func_index(func_index) {} - uint32_t* dst; - uint32_t func_index; + Index* dst; + Index func_index; }; struct DataSegmentInfo { - DataSegmentInfo(void* dst_data, const void* src_data, uint32_t size) + DataSegmentInfo(void* dst_data, const void* src_data, IstreamOffset size) : dst_data(dst_data), src_data(src_data), size(size) {} void* dst_data; // Not owned. const void* src_data; // Not owned. - uint32_t size; + IstreamOffset size; }; class BinaryReaderInterpreter : public BinaryReaderNop { public: BinaryReaderInterpreter(InterpreterEnvironment* env, DefinedInterpreterModule* module, - size_t istream_offset, + IstreamOffset istream_offset, BinaryErrorHandler* error_handler); std::unique_ptr ReleaseOutputBuffer(); - size_t get_istream_offset() { return istream_offset; } + IstreamOffset get_istream_offset() { return istream_offset; } // Implement BinaryReader. - virtual bool OnError(const char* message); - - virtual Result EndModule(); - - virtual Result OnTypeCount(uint32_t count); - virtual Result OnType(uint32_t index, - uint32_t param_count, - Type* param_types, - uint32_t result_count, - Type* result_types); - - virtual Result OnImportCount(uint32_t count); - virtual Result OnImport(uint32_t index, - StringSlice module_name, - StringSlice field_name); - virtual Result OnImportFunc(uint32_t import_index, - StringSlice module_name, - StringSlice field_name, - uint32_t func_index, - uint32_t sig_index); - virtual Result OnImportTable(uint32_t import_index, - StringSlice module_name, - StringSlice field_name, - uint32_t table_index, - Type elem_type, - const Limits* elem_limits); - virtual Result OnImportMemory(uint32_t import_index, - StringSlice module_name, - StringSlice field_name, - uint32_t memory_index, - const Limits* page_limits); - virtual Result OnImportGlobal(uint32_t import_index, - StringSlice module_name, - StringSlice field_name, - uint32_t global_index, - Type type, - bool mutable_); - - virtual Result OnFunctionCount(uint32_t count); - virtual Result OnFunction(uint32_t index, uint32_t sig_index); - - virtual Result OnTable(uint32_t index, - Type elem_type, - const Limits* elem_limits); - - virtual Result OnMemory(uint32_t index, const Limits* limits); - - virtual Result OnGlobalCount(uint32_t count); - virtual Result BeginGlobal(uint32_t index, Type type, bool mutable_); - virtual Result EndGlobalInitExpr(uint32_t index); - - virtual Result OnExport(uint32_t index, - ExternalKind kind, - uint32_t item_index, - StringSlice name); - - virtual Result OnStartFunction(uint32_t func_index); - - virtual Result BeginFunctionBody(uint32_t index); - virtual Result OnLocalDeclCount(uint32_t count); - virtual Result OnLocalDecl(uint32_t decl_index, uint32_t count, Type type); - - virtual Result OnBinaryExpr(Opcode opcode); - virtual Result OnBlockExpr(uint32_t num_types, Type* sig_types); - virtual Result OnBrExpr(uint32_t depth); - virtual Result OnBrIfExpr(uint32_t depth); - virtual Result OnBrTableExpr(uint32_t num_targets, - uint32_t* target_depths, - uint32_t default_target_depth); - virtual Result OnCallExpr(uint32_t func_index); - virtual Result OnCallIndirectExpr(uint32_t sig_index); - virtual Result OnCompareExpr(Opcode opcode); - virtual Result OnConvertExpr(Opcode opcode); - virtual Result OnCurrentMemoryExpr(); - virtual Result OnDropExpr(); - virtual Result OnElseExpr(); - virtual Result OnEndExpr(); - virtual Result OnF32ConstExpr(uint32_t value_bits); - virtual Result OnF64ConstExpr(uint64_t value_bits); - virtual Result OnGetGlobalExpr(uint32_t global_index); - virtual Result OnGetLocalExpr(uint32_t local_index); - virtual Result OnGrowMemoryExpr(); - virtual Result OnI32ConstExpr(uint32_t value); - virtual Result OnI64ConstExpr(uint64_t value); - virtual Result OnIfExpr(uint32_t num_types, Type* sig_types); - virtual Result OnLoadExpr(Opcode opcode, - uint32_t alignment_log2, - uint32_t offset); - virtual Result OnLoopExpr(uint32_t num_types, Type* sig_types); - virtual Result OnNopExpr(); - virtual Result OnReturnExpr(); - virtual Result OnSelectExpr(); - virtual Result OnSetGlobalExpr(uint32_t global_index); - virtual Result OnSetLocalExpr(uint32_t local_index); - virtual Result OnStoreExpr(Opcode opcode, - uint32_t alignment_log2, - uint32_t offset); - virtual Result OnTeeLocalExpr(uint32_t local_index); - virtual Result OnUnaryExpr(Opcode opcode); - virtual Result OnUnreachableExpr(); - virtual Result EndFunctionBody(uint32_t index); - - virtual Result EndElemSegmentInitExpr(uint32_t index); - virtual Result OnElemSegmentFunctionIndex(uint32_t index, - uint32_t func_index); - - virtual Result OnDataSegmentData(uint32_t index, - const void* data, - uint32_t size); - - virtual Result OnInitExprF32ConstExpr(uint32_t index, uint32_t value); - virtual Result OnInitExprF64ConstExpr(uint32_t index, uint64_t value); - virtual Result OnInitExprGetGlobalExpr(uint32_t index, uint32_t global_index); - virtual Result OnInitExprI32ConstExpr(uint32_t index, uint32_t value); - virtual Result OnInitExprI64ConstExpr(uint32_t index, uint64_t value); + bool OnError(const char* message) override; + + Result EndModule() override; + + Result OnTypeCount(Index count) override; + Result OnType(Index index, + Index param_count, + Type* param_types, + Index result_count, + Type* result_types) override; + + Result OnImportCount(Index count) override; + Result OnImport(Index index, + StringSlice module_name, + StringSlice field_name) override; + Result OnImportFunc(Index import_index, + StringSlice module_name, + StringSlice field_name, + Index func_index, + Index sig_index) override; + Result OnImportTable(Index import_index, + StringSlice module_name, + StringSlice field_name, + Index table_index, + Type elem_type, + const Limits* elem_limits) override; + Result OnImportMemory(Index import_index, + StringSlice module_name, + StringSlice field_name, + Index memory_index, + const Limits* page_limits) override; + Result OnImportGlobal(Index import_index, + StringSlice module_name, + StringSlice field_name, + Index global_index, + Type type, + bool mutable_) override; + + Result OnFunctionCount(Index count) override; + Result OnFunction(Index index, Index sig_index) override; + + Result OnTable(Index index, + Type elem_type, + const Limits* elem_limits) override; + + Result OnMemory(Index index, const Limits* limits) override; + + Result OnGlobalCount(Index count) override; + Result BeginGlobal(Index index, Type type, bool mutable_) override; + Result EndGlobalInitExpr(Index index) override; + + Result OnExport(Index index, + ExternalKind kind, + Index item_index, + StringSlice name) override; + + Result OnStartFunction(Index func_index) override; + + Result BeginFunctionBody(Index index) override; + Result OnLocalDeclCount(Index count) override; + Result OnLocalDecl(Index decl_index, Index count, Type type) override; + + Result OnBinaryExpr(Opcode opcode) override; + Result OnBlockExpr(Index num_types, Type* sig_types) override; + Result OnBrExpr(Index depth) override; + Result OnBrIfExpr(Index depth) override; + Result OnBrTableExpr(Index num_targets, + Index* target_depths, + Index default_target_depth) override; + Result OnCallExpr(Index func_index) override; + Result OnCallIndirectExpr(Index sig_index) override; + Result OnCompareExpr(Opcode opcode) override; + Result OnConvertExpr(Opcode opcode) override; + Result OnCurrentMemoryExpr() override; + Result OnDropExpr() override; + Result OnElseExpr() override; + Result OnEndExpr() override; + Result OnF32ConstExpr(uint32_t value_bits) override; + Result OnF64ConstExpr(uint64_t value_bits) override; + Result OnGetGlobalExpr(Index global_index) override; + Result OnGetLocalExpr(Index local_index) override; + Result OnGrowMemoryExpr() override; + Result OnI32ConstExpr(uint32_t value) override; + Result OnI64ConstExpr(uint64_t value) override; + Result OnIfExpr(Index num_types, Type* sig_types) override; + Result OnLoadExpr(Opcode opcode, + uint32_t alignment_log2, + Address offset) override; + Result OnLoopExpr(Index num_types, Type* sig_types) override; + Result OnNopExpr() override; + Result OnReturnExpr() override; + Result OnSelectExpr() override; + Result OnSetGlobalExpr(Index global_index) override; + Result OnSetLocalExpr(Index local_index) override; + Result OnStoreExpr(Opcode opcode, + uint32_t alignment_log2, + Address offset) override; + Result OnTeeLocalExpr(Index local_index) override; + Result OnUnaryExpr(Opcode opcode) override; + Result OnUnreachableExpr() override; + Result EndFunctionBody(Index index) override; + + Result EndElemSegmentInitExpr(Index index) override; + Result OnElemSegmentFunctionIndex(Index index, + Index func_index) override; + + Result OnDataSegmentData(Index index, + const void* data, + Address size) override; + + Result OnInitExprF32ConstExpr(Index index, uint32_t value) override; + Result OnInitExprF64ConstExpr(Index index, uint64_t value) override; + Result OnInitExprGetGlobalExpr(Index index, + Index global_index) override; + Result OnInitExprI32ConstExpr(Index index, uint32_t value) override; + Result OnInitExprI64ConstExpr(Index index, uint64_t value) override; private: - Label* GetLabel(uint32_t depth); + Label* GetLabel(Index depth); Label* TopLabel(); - void PushLabel(uint32_t offset, uint32_t fixup_offset); + void PushLabel(IstreamOffset offset, IstreamOffset fixup_offset); void PopLabel(); - bool HandleError(uint32_t offset, const char* message); + bool HandleError(Offset offset, const char* message); void PrintError(const char* format, ...); static void OnTypecheckerError(const char* msg, void* user_data); - uint32_t TranslateSigIndexToEnv(uint32_t sig_index); - InterpreterFuncSignature* GetSignatureByEnvIndex(uint32_t sig_index); - InterpreterFuncSignature* GetSignatureByModuleIndex(uint32_t sig_index); - uint32_t TranslateFuncIndexToEnv(uint32_t func_index); - uint32_t TranslateModuleFuncIndexToDefined(uint32_t func_index); - InterpreterFunc* GetFuncByEnvIndex(uint32_t func_index); - InterpreterFunc* GetFuncByModuleIndex(uint32_t func_index); - uint32_t TranslateGlobalIndexToEnv(uint32_t global_index); - InterpreterGlobal* GetGlobalByEnvIndex(uint32_t global_index); - InterpreterGlobal* GetGlobalByModuleIndex(uint32_t global_index); - Type GetGlobalTypeByModuleIndex(uint32_t global_index); - uint32_t TranslateLocalIndex(uint32_t local_index); - Type GetLocalTypeByIndex(InterpreterFunc* func, uint32_t local_index); - - uint32_t GetIstreamOffset(); - - Result EmitDataAt(size_t offset, const void* data, size_t size); - Result EmitData(const void* data, size_t size); + Index TranslateSigIndexToEnv(Index sig_index); + InterpreterFuncSignature* GetSignatureByEnvIndex(Index sig_index); + InterpreterFuncSignature* GetSignatureByModuleIndex(Index sig_index); + Index TranslateFuncIndexToEnv(Index func_index); + Index TranslateModuleFuncIndexToDefined(Index func_index); + InterpreterFunc* GetFuncByEnvIndex(Index func_index); + InterpreterFunc* GetFuncByModuleIndex(Index func_index); + Index TranslateGlobalIndexToEnv(Index global_index); + InterpreterGlobal* GetGlobalByEnvIndex(Index global_index); + InterpreterGlobal* GetGlobalByModuleIndex(Index global_index); + Type GetGlobalTypeByModuleIndex(Index global_index); + Index TranslateLocalIndex(Index local_index); + Type GetLocalTypeByIndex(InterpreterFunc* func, Index local_index); + + IstreamOffset GetIstreamOffset(); + + Result EmitDataAt(IstreamOffset offset, + const void* data, + IstreamOffset size); + Result EmitData(const void* data, IstreamOffset size); Result EmitOpcode(Opcode opcode); Result EmitOpcode(InterpreterOpcode opcode); Result EmitI8(uint8_t value); Result EmitI32(uint32_t value); Result EmitI64(uint64_t value); - Result EmitI32At(uint32_t offset, uint32_t value); + Result EmitI32At(IstreamOffset offset, uint32_t value); Result EmitDropKeep(uint32_t drop, uint8_t keep); - Result AppendFixup(Uint32VectorVector* fixups_vector, uint32_t index); - Result EmitBrOffset(uint32_t depth, uint32_t offset); - Result GetBrDropKeepCount(uint32_t depth, - uint32_t* out_drop_count, - uint32_t* out_keep_count); - Result GetReturnDropKeepCount(uint32_t* out_drop_count, - uint32_t* out_keep_count); - Result EmitBr(uint32_t depth, uint32_t drop_count, uint32_t keep_count); - Result EmitBrTableOffset(uint32_t depth); + Result AppendFixup(IstreamOffsetVectorVector* fixups_vector, Index index); + Result EmitBrOffset(Index depth, IstreamOffset offset); + Result GetBrDropKeepCount(Index depth, + Index* out_drop_count, + Index* out_keep_count); + Result GetReturnDropKeepCount(Index* out_drop_count, + Index* out_keep_count); + Result EmitBr(Index depth, Index drop_count, Index keep_count); + Result EmitBrTableOffset(Index depth); Result FixupTopLabel(); - Result EmitFuncOffset(DefinedInterpreterFunc* func, uint32_t func_index); + Result EmitFuncOffset(DefinedInterpreterFunc* func, Index func_index); - Result CheckLocal(uint32_t local_index); - Result CheckGlobal(uint32_t global_index); + Result CheckLocal(Index local_index); + Result CheckGlobal(Index global_index); Result CheckImportKind(InterpreterImport* import, ExternalKind expected_kind); Result CheckImportLimits(const Limits* declared_limits, const Limits* actual_limits); Result CheckHasMemory(Opcode opcode); - Result CheckAlign(uint32_t alignment_log2, uint32_t natural_alignment); + Result CheckAlign(uint32_t alignment_log2, Address natural_alignment); Result AppendExport(InterpreterModule* module, ExternalKind kind, - uint32_t item_index, + Index item_index, StringSlice name); PrintErrorCallback MakePrintErrorCallback(); @@ -265,18 +269,18 @@ class BinaryReaderInterpreter : public BinaryReaderNop { TypeCheckerErrorHandler tc_error_handler; TypeChecker typechecker; std::vector