Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
WASM Namespace Reference

Classes

struct  LocalSet
 
struct  OperatorSerializerStream
 
struct  scoped_skip_checks
 

Enumerations

enum  { magicNumber =0x6d736100 , currentVersion =1 }
 
enum class  SectionType : U8 {
  unknown = 0 , user = 0 , type = 1 , import = 2 ,
  functionDeclarations = 3 , table = 4 , memory = 5 , global = 6 ,
  export_ = 7 , start = 8 , elem = 9 , functionDefinitions = 10 ,
  data = 11
}
 

Functions

WEBASSEMBLY_API void serialize (Serialization::InputStream &stream, IR::Module &module)
 
WEBASSEMBLY_API void serialize (Serialization::OutputStream &stream, const IR::Module &module)
 
FORCEINLINE void serialize (InputStream &stream, Opcode &opcode)
 
FORCEINLINE void serialize (OutputStream &stream, Opcode opcode)
 
template<typename Stream >
void serialize (Stream &stream, NoImm &, const FunctionDef &)
 
template<typename Stream >
void serialize (Stream &stream, ControlStructureImm &imm, const FunctionDef &)
 
template<typename Stream >
void serialize (Stream &stream, BranchImm &imm, const FunctionDef &)
 
void serialize (InputStream &stream, BranchTableImm &imm, FunctionDef &functionDef)
 
void serialize (OutputStream &stream, BranchTableImm &imm, FunctionDef &functionDef)
 
template<typename Stream >
void serialize (Stream &stream, LiteralImm< I32 > &imm, const FunctionDef &)
 
template<typename Stream >
void serialize (Stream &stream, LiteralImm< I64 > &imm, const FunctionDef &)
 
template<typename Stream , bool isGlobal>
void serialize (Stream &stream, GetOrSetVariableImm< isGlobal > &imm, const FunctionDef &)
 
template<typename Stream >
void serialize (Stream &stream, CallImm &imm, const FunctionDef &)
 
template<typename Stream >
void serialize (Stream &stream, CallIndirectImm &imm, const FunctionDef &)
 
template<typename Stream , Uptr naturalAlignmentLog2>
void serialize (Stream &stream, LoadOrStoreImm< naturalAlignmentLog2 > &imm, const FunctionDef &)
 
template<typename Stream >
void serialize (Stream &stream, MemoryImm &imm, const FunctionDef &)
 
template<typename Stream , typename Value >
void serialize (Stream &stream, LiteralImm< Value > &imm, const FunctionDef &)
 
template<typename SerializeSection >
void serializeSection (OutputStream &stream, SectionType type, SerializeSection serializeSectionBody)
 
template<typename SerializeSection >
void serializeSection (InputStream &stream, SectionType expectedType, SerializeSection serializeSectionBody)
 
void serialize (OutputStream &stream, UserSection &userSection)
 
void serialize (InputStream &stream, UserSection &userSection)
 
template<typename Stream >
void serialize (Stream &stream, LocalSet &localSet)
 
void serializeFunctionBody (OutputStream &sectionStream, Module &module, FunctionDef &functionDef)
 
void serializeFunctionBody (InputStream &sectionStream, Module &module, FunctionDef &functionDef)
 
template<typename Stream >
void serializeTypeSection (Stream &moduleStream, Module &module)
 
template<typename Stream >
void serializeImportSection (Stream &moduleStream, Module &module)
 
template<typename Stream >
void serializeFunctionSection (Stream &moduleStream, Module &module)
 
template<typename Stream >
void serializeTableSection (Stream &moduleStream, Module &module)
 
template<typename Stream >
void serializeMemorySection (Stream &moduleStream, Module &module)
 
template<typename Stream >
void serializeGlobalSection (Stream &moduleStream, Module &module)
 
template<typename Stream >
void serializeExportSection (Stream &moduleStream, Module &module)
 
template<typename Stream >
void serializeStartSection (Stream &moduleStream, Module &module)
 
template<typename Stream >
void serializeElementSection (Stream &moduleStream, Module &module)
 
template<typename Stream >
void serializeCodeSection (Stream &moduleStream, Module &module)
 
template<typename Stream >
void serializeDataSection (Stream &moduleStream, Module &module)
 
void serializeModule (OutputStream &moduleStream, Module &module)
 
void serializeModule (InputStream &moduleStream, Module &module)
 

Variables

bool check_limits = true
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
magicNumber 
currentVersion 

Definition at line 185 of file WASMSerialization.cpp.

186 {
187 magicNumber=0x6d736100, // "\0asm"
189 };

◆ SectionType

enum class WASM::SectionType : U8
strong
Enumerator
unknown 
user 
type 
import 
functionDeclarations 
table 
memory 
global 
export_ 
start 
elem 
functionDefinitions 
data 

Definition at line 191 of file WASMSerialization.cpp.

192 {
193 unknown = 0,
194 user = 0,
195 type = 1,
196 import = 2,
197 functionDeclarations = 3,
198 table = 4,
199 memory = 5,
200 global = 6,
201 export_ = 7,
202 start = 8,
203 elem = 9,
204 functionDefinitions = 10,
205 data = 11
206 };
bool unknown

Function Documentation

◆ serialize() [1/20]

void WASM::serialize ( InputStream & stream,
BranchTableImm & imm,
FunctionDef & functionDef )

Definition at line 245 of file WASMSerialization.cpp.

246 {
247 std::vector<U32> branchTable;
248 serializeArray(stream,branchTable,[](InputStream& stream,U32& targetDepth){serializeVarUInt32(stream,targetDepth);});
249 imm.branchTableIndex = functionDef.branchTables.size();
250 functionDef.branchTables.push_back(std::move(branchTable));
252 }
uint32_t U32
Definition BasicTypes.h:9
void serializeVarUInt32(Stream &stream, Value &value)
void serializeArray(Stream &stream, std::vector< Element, Allocator > &vector, SerializeElement serializeElement)
std::vector< std::vector< U32 > > branchTables
Definition Module.h:46
Here is the call graph for this function:

◆ serialize() [2/20]

FORCEINLINE void WASM::serialize ( InputStream & stream,
Opcode & opcode )

Definition at line 208 of file WASMSerialization.cpp.

209 {
210 opcode = (Opcode)0;
211 Serialization::serializeNativeValue(stream,*(U8*)&opcode);
212 if(opcode > Opcode::maxSingleByteOpcode)
213 {
214 opcode = (Opcode)(U16(opcode) << 8);
215 Serialization::serializeNativeValue(stream,*(U8*)&opcode);
216 }
217 }
uint16_t U16
Definition BasicTypes.h:7
uint8_t U8
Definition BasicTypes.h:5
Opcode
Definition Operators.h:576
FORCEINLINE void serializeNativeValue(Stream &stream, Value &value)
Here is the call graph for this function:

◆ serialize() [3/20]

void WASM::serialize ( InputStream & stream,
UserSection & userSection )

Definition at line 393 of file WASMSerialization.cpp.

394 {
395 serializeConstant(stream,"expected user section (section ID 0)",(U8)SectionType::user);
396 Uptr numSectionBytes = 0;
397 serializeVarUInt32(stream,numSectionBytes);
398
399 MemoryInputStream sectionStream(stream.advance(numSectionBytes),numSectionBytes);
400 serialize(sectionStream,userSection.name);
401 throwIfNotValidUTF8(userSection.name);
402 userSection.data.resize(sectionStream.capacity());
403 serializeBytes(sectionStream,userSection.data.data(),userSection.data.size());
404 WAVM_ASSERT_THROW(!sectionStream.capacity());
405 }
PointerIntHelper< sizeof(size_t)>::UnsignedIntType Uptr
Definition BasicTypes.h:22
#define WAVM_ASSERT_THROW(cond)
Definition Errors.h:29
void serializeConstant(InputStream &stream, const char *constantMismatchMessage, Constant constant)
void serialize(Stream &stream, U8 &i)
FORCEINLINE void serializeBytes(OutputStream &stream, const U8 *bytes, Uptr numBytes)
std::string name
Definition Module.h:109
std::vector< U8 > data
Definition Module.h:110
Here is the call graph for this function:

◆ serialize() [4/20]

void WASM::serialize ( OutputStream & stream,
BranchTableImm & imm,
FunctionDef & functionDef )

Definition at line 253 of file WASMSerialization.cpp.

254 {
255 WAVM_ASSERT_THROW(imm.branchTableIndex < functionDef.branchTables.size());
256 std::vector<U32>& branchTable = functionDef.branchTables[imm.branchTableIndex];
257 serializeArray(stream,branchTable,[](OutputStream& stream,U32& targetDepth){serializeVarUInt32(stream,targetDepth);});
259 }
Here is the call graph for this function:

◆ serialize() [5/20]

FORCEINLINE void WASM::serialize ( OutputStream & stream,
Opcode opcode )

Definition at line 218 of file WASMSerialization.cpp.

219 {
220 if(opcode <= Opcode::maxSingleByteOpcode) { Serialization::serializeNativeValue(stream,*(U8*)&opcode); }
221 else
222 {
223 serializeNativeValue(stream,*(((U8*)&opcode) + 1));
224 serializeNativeValue(stream,*(((U8*)&opcode) + 0));
225 }
226 }
Here is the call graph for this function:

◆ serialize() [6/20]

void WASM::serialize ( OutputStream & stream,
UserSection & userSection )

Definition at line 381 of file WASMSerialization.cpp.

382 {
383 serializeConstant(stream,"expected user section (section ID 0)",(U8)SectionType::user);
384 ArrayOutputStream sectionStream;
385 serialize(sectionStream,userSection.name);
386 userSection.data.resize( sectionStream.capacity() ? sectionStream.capacity() : 1 );
387 serializeBytes(sectionStream,userSection.data.data(),userSection.data.size());
388 std::vector<U8> sectionBytes = sectionStream.getBytes();
389 serialize(stream,sectionBytes);
390 if( !sectionStream.capacity() ) throw FatalSerializationException( "empty section" );
391 }
std::vector< U8 > && getBytes()
Here is the call graph for this function:

◆ serialize() [7/20]

void WASM::serialize ( Serialization::InputStream & stream,
IR::Module & module )

Definition at line 841 of file WASMSerialization.cpp.

842 {
843 serializeModule(stream,module);
845 }
IR_API void validateDefinitions(const IR::Module &module)
Definition Validate.cpp:556
void serializeModule(OutputStream &moduleStream, Module &module)
Here is the call graph for this function:

◆ serialize() [8/20]

void WASM::serialize ( Serialization::OutputStream & stream,
const IR::Module & module )

Definition at line 846 of file WASMSerialization.cpp.

847 {
848 serializeModule(stream,const_cast<Module&>(module));
849 }
Here is the call graph for this function:

◆ serialize() [9/20]

template<typename Stream >
void WASM::serialize ( Stream & stream,
BranchImm & imm,
const FunctionDef &  )

Definition at line 240 of file WASMSerialization.cpp.

241 {
243 }
Here is the call graph for this function:

◆ serialize() [10/20]

template<typename Stream >
void WASM::serialize ( Stream & stream,
CallImm & imm,
const FunctionDef &  )

Definition at line 274 of file WASMSerialization.cpp.

275 {
277 }
U32 functionIndex
Definition Operators.h:45
Here is the call graph for this function:

◆ serialize() [11/20]

template<typename Stream >
void WASM::serialize ( Stream & stream,
CallIndirectImm & imm,
const FunctionDef &  )

Definition at line 280 of file WASMSerialization.cpp.

281 {
282 serializeVarUInt32(stream,imm.type.index);
283 serializeConstant(stream,"call_indirect immediate reserved field must be 0",U8(0));
284 }
IndexedFunctionType type
Definition Operators.h:50
Here is the call graph for this function:

◆ serialize() [12/20]

template<typename Stream >
void WASM::serialize ( Stream & stream,
ControlStructureImm & imm,
const FunctionDef &  )

Definition at line 232 of file WASMSerialization.cpp.

233 {
234 I8 encodedResultType = imm.resultType == ResultType::none ? -64 : -(I8)imm.resultType;
235 serializeVarInt7(stream,encodedResultType);
236 if(Stream::isInput) { imm.resultType = encodedResultType == -64 ? ResultType::none : (ResultType)-encodedResultType; }
237 }
int8_t I8
Definition BasicTypes.h:6
ResultType
Definition Types.h:134
void serializeVarInt7(Stream &stream, Value &value)
Here is the call graph for this function:

◆ serialize() [13/20]

template<typename Stream , bool isGlobal>
void WASM::serialize ( Stream & stream,
GetOrSetVariableImm< isGlobal > & imm,
const FunctionDef &  )

Definition at line 270 of file WASMSerialization.cpp.

Here is the call graph for this function:

◆ serialize() [14/20]

template<typename Stream >
void WASM::serialize ( Stream & stream,
LiteralImm< I32 > & imm,
const FunctionDef &  )

Definition at line 262 of file WASMSerialization.cpp.

263 { serializeVarInt32(stream,imm.value); }
void serializeVarInt32(Stream &stream, Value &value)
Here is the call graph for this function:

◆ serialize() [15/20]

template<typename Stream >
void WASM::serialize ( Stream & stream,
LiteralImm< I64 > & imm,
const FunctionDef &  )

Definition at line 266 of file WASMSerialization.cpp.

267 { serializeVarInt64(stream,imm.value); }
void serializeVarInt64(Stream &stream, Value &value)
Here is the call graph for this function:

◆ serialize() [16/20]

template<typename Stream , typename Value >
void WASM::serialize ( Stream & stream,
LiteralImm< Value > & imm,
const FunctionDef &  )

Definition at line 355 of file WASMSerialization.cpp.

356 { serialize(stream,imm.value); }
Here is the call graph for this function:

◆ serialize() [17/20]

template<typename Stream , Uptr naturalAlignmentLog2>
void WASM::serialize ( Stream & stream,
LoadOrStoreImm< naturalAlignmentLog2 > & imm,
const FunctionDef &  )

Definition at line 287 of file WASMSerialization.cpp.

288 {
290 serializeVarUInt32(stream,imm.offset);
291 }
void serializeVarUInt7(Stream &stream, Value &value)
Here is the call graph for this function:

◆ serialize() [18/20]

template<typename Stream >
void WASM::serialize ( Stream & stream,
LocalSet & localSet )

Definition at line 414 of file WASMSerialization.cpp.

415 {
416 serializeVarUInt32(stream,localSet.num);
417 serialize(stream,localSet.type);
418 }
Here is the call graph for this function:

◆ serialize() [19/20]

template<typename Stream >
void WASM::serialize ( Stream & stream,
MemoryImm & imm,
const FunctionDef &  )

Definition at line 293 of file WASMSerialization.cpp.

294 {
295 serializeConstant(stream,"grow_memory/current_memory immediate reserved field must be 0",U8(0));
296 }
Here is the call graph for this function:

◆ serialize() [20/20]

template<typename Stream >
void WASM::serialize ( Stream & stream,
NoImm & ,
const FunctionDef &  )

Definition at line 229 of file WASMSerialization.cpp.

229{}

◆ serializeCodeSection()

template<typename Stream >
void WASM::serializeCodeSection ( Stream & moduleStream,
Module & module )

Definition at line 760 of file WASMSerialization.cpp.

761 {
762 serializeSection(moduleStream,SectionType::functionDefinitions,[&module](Stream& sectionStream)
763 {
764 Uptr numFunctionBodies = module.functions.defs.size();
765 serializeVarUInt32(sectionStream,numFunctionBodies);
766 if(Stream::isInput && numFunctionBodies != module.functions.defs.size())
767 { throw FatalSerializationException("function and code sections have mismatched function counts"); }
768 for(FunctionDef& functionDef : module.functions.defs) { serializeFunctionBody(sectionStream,module,functionDef); }
769 });
770 }
Concept for reading and writing characters.
void serializeSection(OutputStream &stream, SectionType type, SerializeSection serializeSectionBody)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeDataSection()

template<typename Stream >
void WASM::serializeDataSection ( Stream & moduleStream,
Module & module )

Definition at line 773 of file WASMSerialization.cpp.

774 {
775 serializeSection(moduleStream,SectionType::data,[&module](Stream& sectionStream)
776 {
777 serialize(sectionStream,module.dataSegments);
778 });
779 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeElementSection()

template<typename Stream >
void WASM::serializeElementSection ( Stream & moduleStream,
Module & module )

Definition at line 751 of file WASMSerialization.cpp.

752 {
753 serializeSection(moduleStream,SectionType::elem,[&module](Stream& sectionStream)
754 {
755 serialize(sectionStream,module.tableSegments);
756 });
757 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeExportSection()

template<typename Stream >
void WASM::serializeExportSection ( Stream & moduleStream,
Module & module )

Definition at line 733 of file WASMSerialization.cpp.

734 {
735 serializeSection(moduleStream,SectionType::export_,[&module](Stream& sectionStream)
736 {
737 serialize(sectionStream,module.exports);
738 });
739 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeFunctionBody() [1/2]

void WASM::serializeFunctionBody ( InputStream & sectionStream,
Module & module,
FunctionDef & functionDef )

Definition at line 483 of file WASMSerialization.cpp.

484 {
485 Uptr numBodyBytes = 0;
486 serializeVarUInt32(sectionStream,numBodyBytes);
487
488 MemoryInputStream bodyStream(sectionStream.advance(numBodyBytes),numBodyBytes);
489
490 // Deserialize local sets and unpack them into a linear array of local types.
491 Uptr numLocalSets = 0;
492 serializeVarUInt32(bodyStream,numLocalSets);
493
494 constexpr size_t max_size = sysio::chain::wasm_constraints::maximum_code_size;
495 if (numBodyBytes >= max_size && WASM::check_limits)
496 throw FatalSerializationException(std::string("Function body too large"));
497 if (numLocalSets >= 1024 && WASM::check_limits)
498 throw FatalSerializationException(std::string("too many local sets"));
499 size_t locals_accum = 0;
500
501 for(Uptr setIndex = 0;setIndex < numLocalSets;++setIndex)
502 {
503 LocalSet localSet;
504 serialize(bodyStream,localSet);
505 locals_accum += localSet.num*4;
507 throw FatalSerializationException( "too many locals" );
508
509 for(Uptr index = 0;index < localSet.num;++index) { functionDef.nonParameterLocalTypes.push_back(localSet.type); }
510 }
511
512 // Deserialize the function code, validate it, and re-encode it in the IR format.
513 ArrayOutputStream irCodeByteStream;
514 OperatorEncoderStream irEncoderStream(irCodeByteStream);
515 CodeValidationStream codeValidationStream(module,functionDef);
516 while(bodyStream.capacity())
517 {
519 serialize(bodyStream,opcode);
520 switch(opcode)
521 {
522 #define VISIT_OPCODE(_,name,nameString,Imm,...) \
523 case Opcode::name: \
524 { \
525 Imm imm; \
526 serialize(bodyStream,imm,functionDef); \
527 codeValidationStream.name(imm); \
528 irEncoderStream.name(imm); \
529 break; \
530 }
532 #undef VISIT_OPCODE
533 default: throw FatalSerializationException("unknown opcode");
534 };
535 };
536 codeValidationStream.finish();
537 functionDef.code = std::move(irCodeByteStream.getBytes());
538 }
#define ENUM_OPERATORS(visitOp)
Definition Operators.h:571
#define VISIT_OPCODE(_, name, nameString, Imm,...)
bool check_limits
Definition WASM.h:14
variant< > opcode
Definition opcodes.hpp:79
std::vector< U8 > code
Definition Module.h:45
std::vector< ValueType > nonParameterLocalTypes
Definition Module.h:44
const U8 * advance(Uptr numBytes)
Here is the call graph for this function:

◆ serializeFunctionBody() [2/2]

void WASM::serializeFunctionBody ( OutputStream & sectionStream,
Module & module,
FunctionDef & functionDef )

Definition at line 446 of file WASMSerialization.cpp.

447 {
448 ArrayOutputStream bodyStream;
449
450 // Convert the function's local types into LocalSets: runs of locals of the same type.
451 LocalSet* localSets = (LocalSet*)alloca(sizeof(LocalSet)*functionDef.nonParameterLocalTypes.size());
452 Uptr numLocalSets = 0;
453 if(functionDef.nonParameterLocalTypes.size())
454 {
455 localSets[0].type = ValueType::any;
456 localSets[0].num = 0;
457 for(auto localType : functionDef.nonParameterLocalTypes)
458 {
459 if(localSets[numLocalSets].type != localType)
460 {
461 if(localSets[numLocalSets].type != ValueType::any) { ++numLocalSets; }
462 localSets[numLocalSets].type = localType;
463 localSets[numLocalSets].num = 0;
464 }
465 ++localSets[numLocalSets].num;
466 }
467 if(localSets[numLocalSets].type != ValueType::any) { ++numLocalSets; }
468 }
469
470 // Serialize the local sets.
471 serializeVarUInt32(bodyStream,numLocalSets);
472 for(Uptr setIndex = 0;setIndex < numLocalSets;++setIndex) { serialize(bodyStream,localSets[setIndex]); }
473
474 // Serialize the function code.
475 OperatorDecoderStream irDecoderStream(functionDef.code);
476 OperatorSerializerStream wasmOpEncoderStream(bodyStream,functionDef);
477 while(irDecoderStream) { irDecoderStream.decodeOp(wasmOpEncoderStream); };
478
479 std::vector<U8> bodyBytes = bodyStream.getBytes();
480 serialize(sectionStream,bodyBytes);
481 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeFunctionSection()

template<typename Stream >
void WASM::serializeFunctionSection ( Stream & moduleStream,
Module & module )

Definition at line 672 of file WASMSerialization.cpp.

673 {
674 serializeSection(moduleStream,SectionType::functionDeclarations,[&module](Stream& sectionStream)
675 {
676 Uptr numFunctions = module.functions.defs.size();
677 serializeVarUInt32(sectionStream,numFunctions);
678 if(Stream::isInput)
679 {
680 // Grow the vector one element at a time:
681 // try to get a serialization exception before making a huge allocation for malformed input.
682 module.functions.defs.clear();
684 if ( numFunctions >= max_size && WASM::check_limits )
685 throw FatalSerializationException(std::string("Too many function defs"));
686 for(Uptr functionIndex = 0;functionIndex < numFunctions;++functionIndex)
687 {
688 U32 functionTypeIndex = 0;
689 serializeVarUInt32(sectionStream,functionTypeIndex);
690 if(functionTypeIndex >= module.types.size()) { throw FatalSerializationException("invalid function type index"); }
691 module.functions.defs.push_back({{functionTypeIndex},{},{}});
692 }
693 module.functions.defs.shrink_to_fit();
694 }
695 else
696 {
697 for(FunctionDef& function : module.functions.defs)
698 {
699 serializeVarUInt32(sectionStream,function.type.index);
700 }
701 }
702 });
703 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeGlobalSection()

template<typename Stream >
void WASM::serializeGlobalSection ( Stream & moduleStream,
Module & module )

Definition at line 724 of file WASMSerialization.cpp.

725 {
726 serializeSection(moduleStream,SectionType::global,[&module](Stream& sectionStream)
727 {
728 serialize(sectionStream,module.globals.defs);
729 });
730 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeImportSection()

template<typename Stream >
void WASM::serializeImportSection ( Stream & moduleStream,
Module & module )

Definition at line 565 of file WASMSerialization.cpp.

566 {
567 serializeSection(moduleStream,SectionType::import,[&module](Stream& sectionStream)
568 {
569 Uptr size = module.functions.imports.size()
570 + module.tables.imports.size()
571 + module.memories.imports.size()
572 + module.globals.imports.size();
573 serializeVarUInt32(sectionStream,size);
575 if(Stream::isInput)
576 {
577 for(Uptr index = 0;index < size;++index)
578 {
579 std::string moduleName;
580 std::string exportName;
581 ObjectKind kind = ObjectKind::invalid;
582 serialize(sectionStream,moduleName);
583 serialize(sectionStream,exportName);
584 throwIfNotValidUTF8(moduleName);
585 throwIfNotValidUTF8(exportName);
586 serialize(sectionStream,kind);
587 switch(kind)
588 {
589 case ObjectKind::function:
590 {
591 U32 functionTypeIndex = 0;
592 serializeVarUInt32(sectionStream,functionTypeIndex);
593 if(functionTypeIndex >= module.types.size())
594 {
595 throw FatalSerializationException("invalid import function type index");
596 }
597 module.functions.imports.push_back({{functionTypeIndex},std::move(moduleName),std::move(exportName)});
598 if (module.functions.imports.size() >= max_size && WASM::check_limits)
599 throw FatalSerializationException(std::string("Too many function imports"));
600 break;
601 }
602 case ObjectKind::table:
603 {
604 TableType tableType;
605 serialize(sectionStream,tableType);
606 module.tables.imports.push_back({tableType,std::move(moduleName),std::move(exportName)});
607 if (module.functions.imports.size() >= max_size && WASM::check_limits)
608 throw FatalSerializationException(std::string("Too many table imports"));
609 break;
610 }
611 case ObjectKind::memory:
612 {
613 MemoryType memoryType;
614 serialize(sectionStream,memoryType);
615 module.memories.imports.push_back({memoryType,std::move(moduleName),std::move(exportName)});
616 if (module.functions.imports.size() >= max_size && WASM::check_limits)
617 throw FatalSerializationException(std::string("Too many memory imports"));
618 break;
619 }
620 case ObjectKind::global:
621 {
622 GlobalType globalType;
623 serialize(sectionStream,globalType);
624 module.globals.imports.push_back({globalType,std::move(moduleName),std::move(exportName)});
625 if (module.functions.imports.size() >= max_size && WASM::check_limits)
626 throw FatalSerializationException(std::string("Too many global imports"));
627 break;
628 }
629 default: throw FatalSerializationException("invalid ObjectKind");
630 }
631 }
632 }
633 else
634 {
635 for(auto& functionImport : module.functions.imports)
636 {
637 serialize(sectionStream,functionImport.moduleName);
638 serialize(sectionStream,functionImport.exportName);
639 ObjectKind kind = ObjectKind::function;
640 serialize(sectionStream,kind);
641 serializeVarUInt32(sectionStream,functionImport.type.index);
642 }
643 for(auto& tableImport : module.tables.imports)
644 {
645 serialize(sectionStream,tableImport.moduleName);
646 serialize(sectionStream,tableImport.exportName);
647 ObjectKind kind = ObjectKind::table;
648 serialize(sectionStream,kind);
649 serialize(sectionStream,tableImport.type);
650 }
651 for(auto& memoryImport : module.memories.imports)
652 {
653 serialize(sectionStream,memoryImport.moduleName);
654 serialize(sectionStream,memoryImport.exportName);
655 ObjectKind kind = ObjectKind::memory;
656 serialize(sectionStream,kind);
657 serialize(sectionStream,memoryImport.type);
658 }
659 for(auto& globalImport : module.globals.imports)
660 {
661 serialize(sectionStream,globalImport.moduleName);
662 serialize(sectionStream,globalImport.exportName);
663 ObjectKind kind = ObjectKind::global;
664 serialize(sectionStream,kind);
665 serialize(sectionStream,globalImport.type);
666 }
667 }
668 });
669 }
ObjectKind
Definition Types.h:307
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeMemorySection()

template<typename Stream >
void WASM::serializeMemorySection ( Stream & moduleStream,
Module & module )

Definition at line 715 of file WASMSerialization.cpp.

716 {
717 serializeSection(moduleStream,SectionType::memory,[&module](Stream& sectionStream)
718 {
719 serialize(sectionStream,module.memories.defs);
720 });
721 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeModule() [1/2]

void WASM::serializeModule ( InputStream & moduleStream,
Module & module )

Definition at line 803 of file WASMSerialization.cpp.

804 {
805 serializeConstant(moduleStream,"magic number",U32(magicNumber));
806 serializeConstant(moduleStream,"version",U32(currentVersion));
807
808 SectionType lastKnownSectionType = SectionType::unknown;
809 while(moduleStream.capacity())
810 {
811 const SectionType sectionType = *(SectionType*)moduleStream.peek(sizeof(SectionType));
812 if(sectionType != SectionType::user)
813 {
814 if(sectionType > lastKnownSectionType) { lastKnownSectionType = sectionType; }
815 else { throw FatalSerializationException("incorrect order for known section"); }
816 }
817 switch(sectionType)
818 {
819 case SectionType::type: serializeTypeSection(moduleStream,module); break;
820 case SectionType::import: serializeImportSection(moduleStream,module); break;
821 case SectionType::functionDeclarations: serializeFunctionSection(moduleStream,module); break;
822 case SectionType::table: serializeTableSection(moduleStream,module); break;
823 case SectionType::memory: serializeMemorySection(moduleStream,module); break;
824 case SectionType::global: serializeGlobalSection(moduleStream,module); break;
825 case SectionType::export_: serializeExportSection(moduleStream,module); break;
826 case SectionType::start: serializeStartSection(moduleStream,module); break;
827 case SectionType::elem: serializeElementSection(moduleStream,module); break;
828 case SectionType::functionDefinitions: serializeCodeSection(moduleStream,module); break;
829 case SectionType::data: serializeDataSection(moduleStream,module); break;
830 case SectionType::user:
831 {
832 UserSection& userSection = *module.userSections.insert(module.userSections.end(),UserSection());
833 serialize(moduleStream,userSection);
834 break;
835 }
836 default: throw FatalSerializationException("unknown section ID");
837 };
838 };
839 }
void serializeGlobalSection(Stream &moduleStream, Module &module)
void serializeTypeSection(Stream &moduleStream, Module &module)
void serializeElementSection(Stream &moduleStream, Module &module)
void serializeStartSection(Stream &moduleStream, Module &module)
void serializeExportSection(Stream &moduleStream, Module &module)
void serializeMemorySection(Stream &moduleStream, Module &module)
void serializeCodeSection(Stream &moduleStream, Module &module)
void serializeImportSection(Stream &moduleStream, Module &module)
void serializeTableSection(Stream &moduleStream, Module &module)
void serializeDataSection(Stream &moduleStream, Module &module)
void serializeFunctionSection(Stream &moduleStream, Module &module)
const U8 * peek(Uptr numBytes)
virtual Uptr capacity() const =0
Here is the call graph for this function:

◆ serializeModule() [2/2]

void WASM::serializeModule ( OutputStream & moduleStream,
Module & module )

Definition at line 781 of file WASMSerialization.cpp.

782 {
783 serializeConstant(moduleStream,"magic number",U32(magicNumber));
784 serializeConstant(moduleStream,"version",U32(currentVersion));
785
786 if(module.types.size() > 0) { serializeTypeSection(moduleStream,module); }
787 if(module.functions.imports.size() > 0
788 || module.tables.imports.size() > 0
789 || module.memories.imports.size() > 0
790 || module.globals.imports.size() > 0) { serializeImportSection(moduleStream,module); }
791 if(module.functions.defs.size() > 0) { serializeFunctionSection(moduleStream,module); }
792 if(module.tables.defs.size() > 0) { serializeTableSection(moduleStream,module); }
793 if(module.memories.defs.size() > 0) { serializeMemorySection(moduleStream,module); }
794 if(module.globals.defs.size() > 0) { serializeGlobalSection(moduleStream,module); }
795 if(module.exports.size() > 0) { serializeExportSection(moduleStream,module); }
796 if(module.startFunctionIndex != UINTPTR_MAX) { serializeStartSection(moduleStream,module); }
797 if(module.tableSegments.size() > 0) { serializeElementSection(moduleStream,module); }
798 if(module.functions.defs.size() > 0) { serializeCodeSection(moduleStream,module); }
799 if(module.dataSegments.size() > 0) { serializeDataSection(moduleStream,module); }
800
801 for(auto& userSection : module.userSections) { serialize(moduleStream,userSection); }
802 }
#define UINTPTR_MAX
Definition stdint.h:227
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeSection() [1/2]

template<typename SerializeSection >
void WASM::serializeSection ( InputStream & stream,
SectionType expectedType,
SerializeSection serializeSectionBody )

Definition at line 370 of file WASMSerialization.cpp.

371 {
372 WAVM_ASSERT_THROW((SectionType)*stream.peek(sizeof(SectionType)) == expectedType);
373 stream.advance(sizeof(SectionType));
374 Uptr numSectionBytes = 0;
375 serializeVarUInt32(stream,numSectionBytes);
376 MemoryInputStream sectionStream(stream.advance(numSectionBytes),numSectionBytes);
377 serializeSectionBody(sectionStream);
378 if(sectionStream.capacity()) { throw FatalSerializationException("section contained more data than expected"); }
379 }
Here is the call graph for this function:

◆ serializeSection() [2/2]

template<typename SerializeSection >
void WASM::serializeSection ( OutputStream & stream,
SectionType type,
SerializeSection serializeSectionBody )

Definition at line 359 of file WASMSerialization.cpp.

360 {
361 serializeNativeValue(stream,type);
362 ArrayOutputStream sectionStream;
363 serializeSectionBody(sectionStream);
364 std::vector<U8> sectionBytes = sectionStream.getBytes();
365 Uptr sectionNumBytes = sectionBytes.size();
366 serializeVarUInt32(stream,sectionNumBytes);
367 serializeBytes(stream,sectionBytes.data(),sectionBytes.size());
368 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeStartSection()

template<typename Stream >
void WASM::serializeStartSection ( Stream & moduleStream,
Module & module )

Definition at line 742 of file WASMSerialization.cpp.

743 {
744 serializeSection(moduleStream,SectionType::start,[&module](Stream& sectionStream)
745 {
746 serializeVarUInt32(sectionStream,module.startFunctionIndex);
747 });
748 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeTableSection()

template<typename Stream >
void WASM::serializeTableSection ( Stream & moduleStream,
Module & module )

Definition at line 706 of file WASMSerialization.cpp.

707 {
708 serializeSection(moduleStream,SectionType::table,[&module](Stream& sectionStream)
709 {
710 serialize(sectionStream,module.tables.defs);
711 });
712 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ serializeTypeSection()

template<typename Stream >
void WASM::serializeTypeSection ( Stream & moduleStream,
Module & module )

Definition at line 541 of file WASMSerialization.cpp.

542 {
543 serializeSection(moduleStream,SectionType::type,[&module](Stream& sectionStream)
544 {
545 serializeArray(sectionStream,module.types,[](Stream& stream,const FunctionType*& functionType)
546 {
547 serializeConstant(stream,"function type tag",U8(0x60));
548 if(Stream::isInput)
549 {
550 std::vector<ValueType> parameterTypes;
551 ResultType returnType;
552 serialize(stream,parameterTypes);
553 serialize(stream,returnType);
554 functionType = FunctionType::get(returnType,parameterTypes);
555 }
556 else
557 {
558 serialize(stream,const_cast<std::vector<ValueType>&>(functionType->parameters));
559 serialize(stream,const_cast<ResultType&>(functionType->ret));
560 }
561 });
562 });
563 }
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ check_limits

bool WASM::check_limits = true

Definition at line 14 of file WASM.h.