31
31
32
32
#include " src/wamr/types.h"
33
33
#include " wasm_c_api.h"
34
+ #include " src/common/bytecode_util.h"
34
35
35
36
namespace proxy_wasm {
36
37
namespace wamr {
@@ -62,7 +63,6 @@ class Wamr : public WasmVm {
62
63
std::unique_ptr<WasmVm> clone () override ;
63
64
64
65
AbiVersion getAbiVersion () override ;
65
- std::string_view getCustomSection (std::string_view name) override ;
66
66
67
67
bool load (const std::string &code, bool allow_precompiled = false ) override ;
68
68
bool link (std::string_view debug_name) override ;
@@ -88,8 +88,6 @@ class Wamr : public WasmVm {
88
88
FOR_ALL_WASM_VM_EXPORTS (_GET_MODULE_FUNCTION)
89
89
#undef _GET_MODULE_FUNCTION
90
90
private:
91
- bool getStrippedSource (WasmByteVec *out);
92
-
93
91
template <typename ... Args>
94
92
void registerHostFunctionImpl (std::string_view module_name, std::string_view function_name,
95
93
void (*function)(void *, Args...));
@@ -106,7 +104,6 @@ class Wamr : public WasmVm {
106
104
void getModuleFunctionImpl (std::string_view function_name,
107
105
std::function<R(ContextBase *, Args...)> *function);
108
106
109
- WasmByteVec source_;
110
107
WasmStorePtr store_;
111
108
WasmModulePtr module_;
112
109
WasmInstancePtr instance_;
@@ -116,39 +113,27 @@ class Wamr : public WasmVm {
116
113
117
114
std::unordered_map<std::string, HostFuncDataPtr> host_functions_;
118
115
std::unordered_map<std::string, WasmFuncPtr> module_functions_;
116
+ AbiVersion abi_version_;
119
117
};
120
118
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
-
137
119
bool Wamr::load (const std::string &code, bool allow_precompiled) {
138
120
store_ = wasm_store_new (engine ());
139
121
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 " );
143
125
return false ;
144
126
}
145
127
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
+ };
148
133
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 ());
152
137
153
138
return module_ != nullptr ;
154
139
}
@@ -162,60 +147,19 @@ std::unique_ptr<WasmVm> Wamr::clone() {
162
147
163
148
clone->store_ = wasm_store_new (engine ());
164
149
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
+ };
171
155
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 ()) ;
175
159
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_;
212
161
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;
219
163
}
220
164
221
165
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) {
301
245
assert (module_ != nullptr );
302
246
303
247
WasmImporttypeVec import_types;
304
- This conversation was marked as resolved by leyao-daily
305
248
wasm_module_imports (module_.get (), import_types.get ());
306
249
307
250
std::vector<const wasm_extern_t *> imports;
@@ -406,41 +349,6 @@ This conversation was marked as resolved by leyao-daily
406
349
return true ;
407
350
}
408
351
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
-
444
352
uint64_t Wamr::getMemorySize () { return wasm_memory_data_size (memory_.get ()); }
445
353
446
354
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,
737
645
};
738
646
};
739
647
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_; }
761
649
762
650
} // namespace wamr
763
651
0 commit comments