-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Component] share loading entry for component and module (#2945)
* loading entry share component & module Signed-off-by: Lîm Tsú-thuàn <dannypsnl@secondstate.io> * share the same loading entry Signed-off-by: Lîm Tsú-thuàn <dannypsnl@secondstate.io> * the missing validation for the prototype Signed-off-by: Lîm Tsú-thuàn <dannypsnl@secondstate.io> * replace loadModule with loadWasmUnit leave an error message for future component model validating & instantiation Signed-off-by: Lîm Tsú-thuàn <dannypsnl@secondstate.io> * move to a proper location of the module's AOT section Signed-off-by: Lîm Tsú-thuàn <dannypsnl@secondstate.io> * set the actual version Signed-off-by: Lîm Tsú-thuàn <dannypsnl@secondstate.io> * setup unsafe run component for future work Signed-off-by: Lîm Tsú-thuàn <dannypsnl@secondstate.io> * remove shadowing Signed-off-by: Lîm Tsú-thuàn <dannypsnl@secondstate.io> --------- Signed-off-by: Lîm Tsú-thuàn <dannypsnl@secondstate.io>
- Loading branch information
Showing
11 changed files
with
407 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2019-2022 Second State INC | ||
|
||
#include "loader/loader.h" | ||
|
||
#include <bitset> | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <memory> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
namespace WasmEdge { | ||
namespace Loader { | ||
|
||
Expect<std::variant<AST::Component, AST::Module>> Loader::loadUnit() { | ||
// component ::= <preamble> s*:<section>* => (component flatten(s*)) | ||
// preamble ::= <magic> <version> <layer> | ||
// magic ::= 0x00 0x61 0x73 0x6D | ||
// version ::= 0x0a 0x00 | ||
// layer ::= 0x01 0x00 | ||
|
||
// The combination of version and layer is corresponding to the version of | ||
// core wasm. | ||
// The core module has same magic but the different version: | ||
// 0x01 0x00 0x00 0x00 | ||
auto Magic = FMgr.readBytes(4); | ||
if (!Magic) { | ||
return logLoadError(Magic.error(), FMgr.getLastOffset(), | ||
ASTNodeAttr::Component); | ||
} | ||
std::vector<Byte> WasmMagic = {0x00, 0x61, 0x73, 0x6D}; | ||
if (*Magic != WasmMagic) { | ||
return logLoadError(ErrCode::Value::MalformedMagic, FMgr.getLastOffset(), | ||
ASTNodeAttr::Component); | ||
} | ||
auto Ver = FMgr.readBytes(4); | ||
if (!Ver) { | ||
return logLoadError(Ver.error(), FMgr.getLastOffset(), | ||
ASTNodeAttr::Component); | ||
} | ||
std::vector<Byte> ModuleVersion = {0x01, 0x00, 0x00, 0x00}; | ||
// spec says 0x0a, but it's actually 0x0d, where cargo component compiled out | ||
std::vector<Byte> ComponentVersion = {0x0d, 0x00, 0x01, 0x00}; | ||
if (*Ver == ModuleVersion) { | ||
auto Mod = std::make_unique<AST::Module>(); | ||
Mod->getMagic() = WasmMagic; | ||
Mod->getVersion() = *Ver; | ||
if (!Conf.getRuntimeConfigure().isForceInterpreter()) { | ||
|
||
if (auto Res = loadModuleAOT(Mod->getAOTSection()); !Res) { | ||
return Unexpect(Res); | ||
} | ||
} | ||
// Seek to the position after the binary header. | ||
FMgr.seek(8); | ||
if (auto Res = loadModule(*Mod); !Res) { | ||
return Unexpect(Res); | ||
} | ||
|
||
// Load library from AOT Section for the universal WASM case. | ||
// For the force interpreter mode, skip this. | ||
if (!Conf.getRuntimeConfigure().isForceInterpreter() && | ||
WASMType == InputType::UniversalWASM) { | ||
if (auto Res = loadUniversalWASM(*Mod); !Res) { | ||
return Unexpect(Res); | ||
} | ||
} | ||
return *Mod; | ||
} else if (*Ver == ComponentVersion) { | ||
auto Comp = std::make_unique<AST::Component>(); | ||
Comp->getMagic() = WasmMagic; | ||
Comp->getVersion() = {(*Ver)[0], (*Ver)[1]}; | ||
Comp->getLayer() = {(*Ver)[2], (*Ver)[3]}; | ||
spdlog::error("Component model is not fully parsed yet!"); | ||
return *Comp; | ||
} else { | ||
return logLoadError(ErrCode::Value::MalformedVersion, FMgr.getLastOffset(), | ||
ASTNodeAttr::Component); | ||
} | ||
} | ||
|
||
} // namespace Loader | ||
} // namespace WasmEdge |
Oops, something went wrong.