Skip to content

Commit ed67cbc

Browse files
committed
Use BytecodeUtil in getAbiVersion on WasmVm except NullVm
Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent 305d48d commit ed67cbc

File tree

4 files changed

+27
-64
lines changed

4 files changed

+27
-64
lines changed

src/common/bytecode_util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ bool BytecodeUtil::getAbiVersion(std::string_view bytecode, proxy_wasm::AbiVersi
7777
return false;
7878
}
7979
}
80-
return false;
80+
return true;
8181
} else {
8282
pos += section_len;
8383
}

src/v8/v8.cc

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class V8 : public WasmVm {
123123
std::unordered_map<std::string, FuncDataPtr> host_functions_;
124124
std::unordered_map<std::string, wasm::own<wasm::Func>> module_functions_;
125125

126+
AbiVersion abi_version_;
126127
std::unordered_map<uint32_t, std::string> function_names_index_;
127128
};
128129

@@ -259,6 +260,12 @@ bool V8::load(const std::string &code, bool allow_precompiled) {
259260
return false;
260261
}
261262

263+
// Get ABI version from bytecode.
264+
if (!common::BytecodeUtil::getAbiVersion(code, abi_version_)) {
265+
fail(FailState::UnableToInitializeCode, "Failed to parse corrupted Wasm module");
266+
return false;
267+
}
268+
262269
if (allow_precompiled) {
263270
const auto section_name = getPrecompiledSectionName();
264271
if (!section_name.empty()) {
@@ -290,7 +297,7 @@ bool V8::load(const std::string &code, bool allow_precompiled) {
290297
wasm::vec<byte_t> code_vec = wasm::vec<byte_t>::invalid();
291298
if (stripped.empty()) {
292299
// Use the original bytecode.
293-
code_vec = wasm::vec<byte_t>::make(code.size(), code.data());
300+
code_vec = wasm::vec<byte_t>::make(code.size(), (char *)(code.data()));
294301
} else {
295302
// Othewise use the stripped bytecode.
296303
code_vec = wasm::vec<byte_t>::make(stripped.size(), stripped.data());
@@ -340,25 +347,7 @@ std::string_view V8::getPrecompiledSectionName() {
340347
return name;
341348
}
342349

343-
AbiVersion V8::getAbiVersion() {
344-
assert(module_ != nullptr);
345-
346-
const auto export_types = module_.get()->exports();
347-
for (size_t i = 0; i < export_types.size(); i++) {
348-
if (export_types[i]->type()->kind() == wasm::EXTERN_FUNC) {
349-
std::string_view name(export_types[i]->name().get(), export_types[i]->name().size());
350-
if (name == "proxy_abi_version_0_1_0") {
351-
return AbiVersion::ProxyWasm_0_1_0;
352-
} else if (name == "proxy_abi_version_0_2_0") {
353-
return AbiVersion::ProxyWasm_0_2_0;
354-
} else if (name == "proxy_abi_version_0_2_1") {
355-
return AbiVersion::ProxyWasm_0_2_1;
356-
}
357-
}
358-
}
359-
360-
return AbiVersion::Unknown;
361-
}
350+
AbiVersion V8::getAbiVersion() { return abi_version_; }
362351

363352
bool V8::link(std::string_view debug_name) {
364353
assert(module_ != nullptr);

src/wasmtime/wasmtime.cc

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,13 @@ class Wasmtime : public WasmVm {
103103
WasmModulePtr module_;
104104
WasmSharedModulePtr shared_module_;
105105
WasmInstancePtr instance_;
106-
107106
WasmMemoryPtr memory_;
108107
WasmTablePtr table_;
109108

110109
std::unordered_map<std::string, HostFuncDataPtr> host_functions_;
111110
std::unordered_map<std::string, WasmFuncPtr> module_functions_;
111+
112+
AbiVersion abi_version_;
112113
};
113114

114115
bool Wasmtime::load(const std::string &code, bool allow_precompiled) {
@@ -119,6 +120,12 @@ bool Wasmtime::load(const std::string &code, bool allow_precompiled) {
119120
return false;
120121
}
121122

123+
// Get ABI version from bytecode.
124+
if (!common::BytecodeUtil::getAbiVersion(code, abi_version_)) {
125+
fail(FailState::UnableToInitializeCode, "Failed to parse corrupted Wasm module");
126+
return false;
127+
}
128+
122129
std::string stripped_vec;
123130
if (!common::BytecodeUtil::getStrippedSource(code, stripped_vec)) {
124131
fail(FailState::UnableToInitializeCode, "Failed to parse corrupted Wasm module");
@@ -645,27 +652,7 @@ void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
645652
};
646653
};
647654

648-
AbiVersion Wasmtime::getAbiVersion() {
649-
assert(module_ != nullptr);
650-
WasmExportTypeVec export_types;
651-
wasm_module_exports(module_.get(), export_types.get());
652-
653-
for (size_t i = 0; i < export_types.get()->size; i++) {
654-
const wasm_externtype_t *exp_extern_type = wasm_exporttype_type(export_types.get()->data[i]);
655-
if (wasm_externtype_kind(exp_extern_type) == WASM_EXTERN_FUNC) {
656-
const wasm_name_t *name_ptr = wasm_exporttype_name(export_types.get()->data[i]);
657-
std::string_view name(name_ptr->data, name_ptr->size);
658-
if (name == "proxy_abi_version_0_1_0") {
659-
return AbiVersion::ProxyWasm_0_1_0;
660-
} else if (name == "proxy_abi_version_0_2_0") {
661-
return AbiVersion::ProxyWasm_0_2_0;
662-
} else if (name == "proxy_abi_version_0_2_1") {
663-
return AbiVersion::ProxyWasm_0_2_1;
664-
}
665-
}
666-
}
667-
return AbiVersion::Unknown;
668-
}
655+
AbiVersion Wasmtime::getAbiVersion() { return abi_version_; }
669656

670657
} // namespace wasmtime
671658

src/wavm/wavm.cc

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,13 @@ bool Wavm::load(const std::string &code, bool allow_precompiled) {
302302
if (!loadModule(code, ir_module_)) {
303303
return false;
304304
}
305-
getAbiVersion(); // Cache ABI version.
305+
306+
// Get ABI version from bytecode.
307+
if (!common::BytecodeUtil::getAbiVersion(code, abi_version_)) {
308+
fail(FailState::UnableToInitializeCode, "Failed to parse corrupted Wasm module");
309+
return false;
310+
}
311+
306312
std::string_view precompiled = {};
307313
if (allow_precompiled) {
308314
if (!common::BytecodeUtil::getCustomSection(code, getPrecompiledSectionName(), precompiled)) {
@@ -319,26 +325,7 @@ bool Wavm::load(const std::string &code, bool allow_precompiled) {
319325
return true;
320326
}
321327

322-
AbiVersion Wavm::getAbiVersion() {
323-
if (abi_version_ != AbiVersion::Unknown) {
324-
return abi_version_;
325-
}
326-
for (auto &e : ir_module_.exports) {
327-
if (e.name == "proxy_abi_version_0_1_0") {
328-
abi_version_ = AbiVersion::ProxyWasm_0_1_0;
329-
return abi_version_;
330-
}
331-
if (e.name == "proxy_abi_version_0_2_0") {
332-
abi_version_ = AbiVersion::ProxyWasm_0_2_0;
333-
return abi_version_;
334-
}
335-
if (e.name == "proxy_abi_version_0_2_1") {
336-
abi_version_ = AbiVersion::ProxyWasm_0_2_1;
337-
return abi_version_;
338-
}
339-
}
340-
return AbiVersion::Unknown;
341-
}
328+
AbiVersion Wavm::getAbiVersion() { return abi_version_; }
342329

343330
bool Wavm::link(std::string_view debug_name) {
344331
RootResolver rootResolver(compartment_, this);

0 commit comments

Comments
 (0)