Skip to content

Commit 41a9ac6

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

File tree

5 files changed

+31
-64
lines changed

5 files changed

+31
-64
lines changed
-95.8 KB
Binary file not shown.

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: 10 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());
@@ -320,6 +327,7 @@ std::unique_ptr<WasmVm> V8::clone() {
320327

321328
clone->module_ = wasm::Module::obtain(clone->store_.get(), shared_module_.get());
322329
clone->function_names_index_ = function_names_index_;
330+
clone->abi_version_ = abi_version_;
323331

324332
return clone;
325333
}
@@ -340,25 +348,7 @@ std::string_view V8::getPrecompiledSectionName() {
340348
return name;
341349
}
342350

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-
}
351+
AbiVersion V8::getAbiVersion() { return abi_version_; }
362352

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

src/wasmtime/wasmtime.cc

Lines changed: 11 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");
@@ -150,6 +157,8 @@ std::unique_ptr<WasmVm> Wasmtime::clone() {
150157
clone->integration().reset(integration()->clone());
151158
clone->store_ = wasm_store_new(engine());
152159
clone->module_ = wasm_module_obtain(clone->store_.get(), shared_module_.get());
160+
clone->abi_version_ = abi_version_;
161+
153162
return clone;
154163
}
155164

@@ -645,27 +654,7 @@ void Wasmtime::getModuleFunctionImpl(std::string_view function_name,
645654
};
646655
};
647656

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-
}
657+
AbiVersion Wasmtime::getAbiVersion() { return abi_version_; }
669658

670659
} // namespace wasmtime
671660

src/wavm/wavm.cc

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ Wavm::~Wavm() {
279279
std::unique_ptr<WasmVm> Wavm::clone() {
280280
auto wavm = std::make_unique<Wavm>();
281281
wavm->integration().reset(integration()->clone());
282+
wavm->abi_version_ = abi_version_;
282283

283284
wavm->compartment_ = WAVM::Runtime::cloneCompartment(compartment_);
284285
wavm->memory_ = WAVM::Runtime::remapToClonedCompartment(memory_, wavm->compartment_);
@@ -302,7 +303,13 @@ bool Wavm::load(const std::string &code, bool allow_precompiled) {
302303
if (!loadModule(code, ir_module_)) {
303304
return false;
304305
}
305-
getAbiVersion(); // Cache ABI version.
306+
307+
// Get ABI version from bytecode.
308+
if (!common::BytecodeUtil::getAbiVersion(code, abi_version_)) {
309+
fail(FailState::UnableToInitializeCode, "Failed to parse corrupted Wasm module");
310+
return false;
311+
}
312+
306313
std::string_view precompiled = {};
307314
if (allow_precompiled) {
308315
if (!common::BytecodeUtil::getCustomSection(code, getPrecompiledSectionName(), precompiled)) {
@@ -319,26 +326,7 @@ bool Wavm::load(const std::string &code, bool allow_precompiled) {
319326
return true;
320327
}
321328

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-
}
329+
AbiVersion Wavm::getAbiVersion() { return abi_version_; }
342330

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

0 commit comments

Comments
 (0)