Skip to content

Commit 3965df5

Browse files
author
Le Yao
committed
Update WAMR implementation based on Common utils
INFO: Analyzed 9 targets (0 packages loaded, 0 targets configured). INFO: Found 1 target and 8 test targets... INFO: Elapsed time: 4.954s, Critical Path: 3.83s INFO: 25 processes: 2 internal, 23 linux-sandbox. INFO: Build completed successfully, 25 total actions //test:context_test PASSED in 0.1s //test:exports_test PASSED in 0.1s //test:null_vm_test PASSED in 0.2s //test:runtime_test PASSED in 0.1s //test:shared_data PASSED in 0.1s //test:shared_queue PASSED in 0.1s //test:vm_id_handle PASSED in 0.1s //test/common:bytecode_util_test PASSED in 0.2s Executed 8 out of 8 tests: 8 tests pass. INFO: Build completed successfully, 25 total actions Signed-off-by: Le Yao <[email protected]>
1 parent 843467f commit 3965df5

File tree

2 files changed

+25
-137
lines changed

2 files changed

+25
-137
lines changed

src/wamr/types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
#include "src/common/types.h"
16-
#include "wamr/include/wasm_c_api.h"
16+
#include "wasm_c_api.h"
1717

1818
namespace proxy_wasm {
1919
namespace wamr {

src/wamr/wamr.cc

Lines changed: 24 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "src/wamr/types.h"
3333
#include "wasm_c_api.h"
34+
#include "src/common/bytecode_util.h"
3435

3536
namespace proxy_wasm {
3637
namespace wamr {
@@ -62,7 +63,6 @@ class Wamr : public WasmVm {
6263
std::unique_ptr<WasmVm> clone() override;
6364

6465
AbiVersion getAbiVersion() override;
65-
std::string_view getCustomSection(std::string_view name) override;
6666

6767
bool load(const std::string &code, bool allow_precompiled = false) override;
6868
bool link(std::string_view debug_name) override;
@@ -88,8 +88,6 @@ class Wamr : public WasmVm {
8888
FOR_ALL_WASM_VM_EXPORTS(_GET_MODULE_FUNCTION)
8989
#undef _GET_MODULE_FUNCTION
9090
private:
91-
bool getStrippedSource(WasmByteVec *out);
92-
9391
template <typename... Args>
9492
void registerHostFunctionImpl(std::string_view module_name, std::string_view function_name,
9593
void (*function)(void *, Args...));
@@ -106,7 +104,6 @@ class Wamr : public WasmVm {
106104
void getModuleFunctionImpl(std::string_view function_name,
107105
std::function<R(ContextBase *, Args...)> *function);
108106

109-
WasmByteVec source_;
110107
WasmStorePtr store_;
111108
WasmModulePtr module_;
112109
WasmInstancePtr instance_;
@@ -116,39 +113,27 @@ class Wamr : public WasmVm {
116113

117114
std::unordered_map<std::string, HostFuncDataPtr> host_functions_;
118115
std::unordered_map<std::string, WasmFuncPtr> module_functions_;
116+
AbiVersion abi_version_;
119117
};
120118

121-
// TODO(mathetake): move to proxy_wasm::common::*
122-
static uint32_t parseVarint(const byte_t *&pos, const byte_t *end) {
123-
uint32_t n = 0;
124-
uint32_t shift = 0;
125-
byte_t b;
126-
do {
127-
if (pos + 1 > end) {
128-
abort();
129-
}
130-
b = *pos++;
131-
n += (b & 0x7f) << shift;
132-
shift += 7;
133-
} while ((b & 0x80) != 0);
134-
return n;
135-
}
136-
137119
bool Wamr::load(const std::string &code, bool allow_precompiled) {
138120
store_ = wasm_store_new(engine());
139121

140-
// Wasm file header is 8 bytes (magic number + version).
141-
static const uint8_t magic_number[4] = {0x00, 0x61, 0x73, 0x6d};
142-
if (code.size() < 8 || ::memcmp(code.data(), magic_number, 4) != 0) {
122+
// Get ABI version from bytecode.
123+
if (!common::BytecodeUtil::getAbiVersion(code, abi_version_)) {
124+
fail(FailState::UnableToInitializeCode, "Failed to parse corrupted Wasm module");
143125
return false;
144126
}
145127

146-
wasm_byte_vec_new_uninitialized(source_.get(), code.size());
147-
::memcpy(source_.get()->data, code.data(), code.size());
128+
std::string stripped;
129+
if (!common::BytecodeUtil::getStrippedSource(code, stripped)) {
130+
fail(FailState::UnableToInitializeCode, "Failed to parse corrupted Wasm module");
131+
return false;
132+
};
148133

149-
WasmByteVec stripped;
150-
module_ =
151-
wasm_module_new(store_.get(), getStrippedSource(&stripped) ? stripped.get() : source_.get());
134+
WasmByteVec stripped_vec;
135+
wasm_byte_vec_new(stripped_vec.get(), stripped.size(), stripped.data());
136+
module_ = wasm_module_new(store_.get(), stripped_vec.get());
152137

153138
return module_ != nullptr;
154139
}
@@ -162,60 +147,19 @@ std::unique_ptr<WasmVm> Wamr::clone() {
162147

163148
clone->store_ = wasm_store_new(engine());
164149

165-
WasmByteVec stripped;
166-
clone->module_ = wasm_module_new(clone->store_.get(),
167-
getStrippedSource(&stripped) ? stripped.get() : source_.get());
168-
169-
return clone;
170-
}
150+
std::string stripped;
151+
if (!common::BytecodeUtil::getStrippedSource("", stripped)) {
152+
fail(FailState::UnableToInitializeCode, "Failed to parse corrupted Wasm module");
153+
return nullptr;
154+
};
171155

172-
// TODO(mathetake): move to proxy_wasm::common::*
173-
bool Wamr::getStrippedSource(WasmByteVec *out) {
174-
std::vector<byte_t> stripped;
156+
WasmByteVec stripped_vec;
157+
wasm_byte_vec_new(stripped_vec.get(), stripped.size(), stripped.data());
158+
clone->module_ = wasm_module_new(store_.get(), stripped_vec.get());
175159

176-
const byte_t *pos = source_.get()->data + 8 /* Wasm header */;
177-
const byte_t *end = source_.get()->data + source_.get()->size;
178-
while (pos < end) {
179-
const auto section_start = pos;
180-
if (pos + 1 > end) {
181-
return false;
182-
}
183-
const auto section_type = *pos++;
184-
const auto section_len = parseVarint(pos, end);
185-
if (section_len == static_cast<uint32_t>(-1) || pos + section_len > end) {
186-
return false;
187-
}
188-
if (section_type == 0 /* custom section */) {
189-
const auto section_data_start = pos;
190-
const auto section_name_len = parseVarint(pos, end);
191-
if (section_name_len == static_cast<uint32_t>(-1) || pos + section_name_len > end) {
192-
return false;
193-
}
194-
auto section_name = std::string_view(pos, section_name_len);
195-
if (section_name.find("precompiled_") != std::string::npos) {
196-
// If this is the first "precompiled_" section, then save everything
197-
// before it, otherwise skip it.
198-
if (stripped.empty()) {
199-
const byte_t *start = source_.get()->data;
200-
stripped.insert(stripped.end(), start, section_start);
201-
}
202-
}
203-
pos = section_data_start + section_len;
204-
} else {
205-
pos += section_len;
206-
// Save this section if we already saw a custom "precompiled_" section.
207-
if (!stripped.empty()) {
208-
stripped.insert(stripped.end(), section_start, pos /* section end */);
209-
}
210-
}
211-
}
160+
clone->abi_version_ = abi_version_;
212161

213-
if (!stripped.empty()) {
214-
wasm_byte_vec_new_uninitialized(out->get(), stripped.size());
215-
::memcpy(out->get()->data, stripped.data(), stripped.size());
216-
return true;
217-
}
218-
return false;
162+
return clone;
219163
}
220164

221165
static bool equalValTypes(const wasm_valtype_vec_t *left, const wasm_valtype_vec_t *right) {
@@ -301,7 +245,6 @@ bool Wamr::link(std::string_view debug_name) {
301245
assert(module_ != nullptr);
302246

303247
WasmImporttypeVec import_types;
304-
This conversation was marked as resolved by leyao-daily
305248
wasm_module_imports(module_.get(), import_types.get());
306249

307250
std::vector<const wasm_extern_t *> imports;
@@ -406,41 +349,6 @@ This conversation was marked as resolved by leyao-daily
406349
return true;
407350
}
408351

409-
std::string_view Wamr::getCustomSection(std::string_view name) {
410-
const byte_t *pos = source_.get()->data + 8 /* Wasm header */;
411-
const byte_t *end = source_.get()->data + source_.get()->size;
412-
while (pos < end) {
413-
if (pos + 1 > end) {
414-
fail(FailState::UnableToInitializeCode, "Failed to parse corrupted Wasm module");
415-
return "";
416-
}
417-
const auto section_type = *pos++;
418-
const auto section_len = parseVarint(pos, end);
419-
if (section_len == static_cast<uint32_t>(-1) || pos + section_len > end) {
420-
fail(FailState::UnableToInitializeCode, "Failed to parse corrupted Wasm module");
421-
return "";
422-
}
423-
424-
if (section_type == 0 /* custom section */) {
425-
const auto section_data_start = pos;
426-
const auto section_name_len = parseVarint(pos, end);
427-
if (section_name_len == static_cast<uint32_t>(-1) || pos + section_name_len > end) {
428-
fail(FailState::UnableToInitializeCode, "Failed to parse corrupted Wasm module");
429-
return "";
430-
}
431-
if (section_name_len == name.size() && ::memcmp(pos, name.data(), section_name_len) == 0) {
432-
pos += section_name_len;
433-
return {pos, static_cast<size_t>(section_data_start + section_len - pos)};
434-
}
435-
pos = section_data_start + section_len;
436-
} else {
437-
pos += section_len;
438-
}
439-
}
440-
441-
return "";
442-
}
443-
444352
uint64_t Wamr::getMemorySize() { return wasm_memory_data_size(memory_.get()); }
445353

446354
std::optional<std::string_view> Wamr::getMemory(uint64_t pointer, uint64_t size) {
@@ -737,27 +645,7 @@ void Wamr::getModuleFunctionImpl(std::string_view function_name,
737645
};
738646
};
739647

740-
AbiVersion Wamr::getAbiVersion() {
741-
assert(module_ != nullptr);
742-
WasmExportTypeVec export_types;
743-
wasm_module_exports(module_.get(), export_types.get());
744-
745-
for (size_t i = 0; i < export_types.get()->size; i++) {
746-
const wasm_externtype_t *exp_extern_type = wasm_exporttype_type(export_types.get()->data[i]);
747-
if (wasm_externtype_kind(exp_extern_type) == WASM_EXTERN_FUNC) {
748-
const wasm_name_t *name_ptr = wasm_exporttype_name(export_types.get()->data[i]);
749-
std::string_view name(name_ptr->data, name_ptr->size);
750-
if (name == "proxy_abi_version_0_1_0") {
751-
return AbiVersion::ProxyWasm_0_1_0;
752-
} else if (name == "proxy_abi_version_0_2_0") {
753-
return AbiVersion::ProxyWasm_0_2_0;
754-
} else if (name == "proxy_abi_version_0_2_1") {
755-
return AbiVersion::ProxyWasm_0_2_1;
756-
}
757-
}
758-
}
759-
return AbiVersion::Unknown;
760-
}
648+
AbiVersion Wamr::getAbiVersion() { return abi_version_; }
761649

762650
} // namespace wamr
763651

0 commit comments

Comments
 (0)