Skip to content

Commit 4d1cfd5

Browse files
committed
reivew: add getAbiVersion().
Signed-off-by: Piotr Sikora <[email protected]>
1 parent aa2158d commit 4d1cfd5

File tree

7 files changed

+61
-27
lines changed

7 files changed

+61
-27
lines changed

include/proxy-wasm/null_vm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct NullVm : public WasmVm {
3636
Cloneable cloneable() override { return Cloneable::InstantiatedModule; };
3737
std::unique_ptr<WasmVm> clone() override;
3838
bool load(const std::string &code, bool allow_precompiled) override;
39+
AbiVersion getAbiVersion() override;
3940
bool link(std::string_view debug_name) override;
4041
uint64_t getMemorySize() override;
4142
std::optional<std::string_view> getMemory(uint64_t pointer, uint64_t size) override;

include/proxy-wasm/wasm.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
122122
virtual void error(std::string_view message) { std::cerr << message << "\n"; }
123123
virtual void unimplemented() { error("unimplemented proxy-wasm API"); }
124124

125+
AbiVersion abiVersion() { return abi_version_; }
126+
125127
bool getEmscriptenVersion(uint32_t *emscripten_metadata_major_version,
126128
uint32_t *emscripten_metadata_minor_version,
127129
uint32_t *emscripten_abi_major_version,
@@ -184,9 +186,6 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
184186
std::unique_ptr<ShutdownHandle> shutdown_handle_;
185187
std::unordered_set<ContextBase *> pending_done_; // Root contexts not done during shutdown.
186188

187-
WasmCallVoid<0> abi_version_0_1_0_;
188-
WasmCallVoid<0> abi_version_0_2_0_;
189-
190189
WasmCallVoid<0> _start_; /* Emscripten v1.39.0+ */
191190
WasmCallVoid<0> __wasm_call_ctors_;
192191

@@ -241,6 +240,9 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
241240
bool allow_precompiled_ = false;
242241
FailState failed_ = FailState::Ok; // Wasm VM fatal error.
243242

243+
// ABI version.
244+
AbiVersion abi_version_ = AbiVersion::Unknown;
245+
244246
bool is_emscripten_ = false;
245247
uint32_t emscripten_metadata_major_version_ = 0;
246248
uint32_t emscripten_metadata_minor_version_ = 0;

include/proxy-wasm/wasm_vm.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ enum class Cloneable {
101101
InstantiatedModule // VMs can be cloned from an instantiated module.
102102
};
103103

104+
enum class AbiVersion { ProxyWasm_0_1_0, ProxyWasm_0_2_0, Unknown };
105+
104106
class NullPlugin;
105107

106108
// Integrator specific WasmVm operations.
@@ -185,6 +187,12 @@ class WasmVm {
185187
*/
186188
virtual bool link(std::string_view debug_name) = 0;
187189

190+
/**
191+
* Get ABI version of the module.
192+
* @return the ABI version.
193+
*/
194+
virtual AbiVersion getAbiVersion() = 0;
195+
188196
/**
189197
* Get size of the currently allocated memory in the VM.
190198
* @return the size of memory in bytes.

src/null/null_vm.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ bool NullVm::load(const std::string &name, bool /* allow_precompiled */) {
5757
return true;
5858
}
5959

60+
AbiVersion NullVm::getAbiVersion() { return AbiVersion::ProxyWasm_0_2_0; }
61+
6062
bool NullVm::link(std::string_view /* name */) { return true; }
6163

6264
uint64_t NullVm::getMemorySize() { return std::numeric_limits<uint64_t>::max(); }

src/v8/v8.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class V8 : public WasmVm {
5454
std::string_view runtime() override { return "v8"; }
5555

5656
bool load(const std::string &code, bool allow_precompiled) override;
57+
AbiVersion getAbiVersion() override;
5758
std::string_view getCustomSection(std::string_view name) override;
5859
std::string_view getPrecompiledSectionName() override;
5960
bool link(std::string_view debug_name) override;
@@ -366,6 +367,24 @@ std::string_view V8::getPrecompiledSectionName() {
366367
return name;
367368
}
368369

370+
AbiVersion V8::getAbiVersion() {
371+
assert(module_ != nullptr);
372+
373+
const auto export_types = module_.get()->exports();
374+
for (size_t i = 0; i < export_types.size(); i++) {
375+
if (export_types[i]->type()->kind() == wasm::EXTERN_FUNC) {
376+
std::string_view name(export_types[i]->name().get(), export_types[i]->name().size());
377+
if (name == "proxy_abi_version_0_1_0") {
378+
return AbiVersion::ProxyWasm_0_1_0;
379+
} else if (name == "proxy_abi_version_0_2_0") {
380+
return AbiVersion::ProxyWasm_0_2_0;
381+
}
382+
}
383+
}
384+
385+
return AbiVersion::Unknown;
386+
}
387+
369388
bool V8::link(std::string_view debug_name) {
370389
assert(module_ != nullptr);
371390

src/wasm.cc

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,12 @@ void WasmBase::registerCallbacks() {
143143
_REGISTER_PROXY(log);
144144
_REGISTER_PROXY(get_log_level);
145145

146-
_REGISTER_PROXY(get_configuration);
147146
_REGISTER_PROXY(get_status);
148147

149148
_REGISTER_PROXY(set_property);
150149
_REGISTER_PROXY(get_property);
151150

152-
_REGISTER_PROXY(continue_request);
153-
_REGISTER_PROXY(continue_response);
154-
_REGISTER_PROXY(continue_stream);
155-
_REGISTER_PROXY(close_stream);
156151
_REGISTER_PROXY(send_local_response);
157-
_REGISTER_PROXY(clear_route_cache);
158152

159153
_REGISTER_PROXY(get_shared_data);
160154
_REGISTER_PROXY(set_shared_data);
@@ -195,6 +189,16 @@ void WasmBase::registerCallbacks() {
195189
_REGISTER_PROXY(set_effective_context);
196190
_REGISTER_PROXY(done);
197191
_REGISTER_PROXY(call_foreign_function);
192+
193+
if (abiVersion() == AbiVersion::ProxyWasm_0_1_0) {
194+
_REGISTER_PROXY(get_configuration);
195+
_REGISTER_PROXY(continue_request);
196+
_REGISTER_PROXY(continue_response);
197+
_REGISTER_PROXY(clear_route_cache);
198+
} else if (abiVersion() == AbiVersion::ProxyWasm_0_2_0) {
199+
_REGISTER_PROXY(continue_stream);
200+
_REGISTER_PROXY(close_stream);
201+
}
198202
#undef _REGISTER_PROXY
199203
}
200204

@@ -211,21 +215,10 @@ void WasmBase::getFunctions() {
211215

212216
#define _GET_PROXY(_fn) wasm_vm_->getFunction("proxy_" #_fn, &_fn##_);
213217
#define _GET_PROXY_ABI(_fn, _abi) wasm_vm_->getFunction("proxy_" #_fn, &_fn##_abi##_);
214-
_GET_PROXY(abi_version_0_1_0);
215-
_GET_PROXY(abi_version_0_2_0);
216-
if (!abi_version_0_1_0_ && !abi_version_0_2_0_) {
217-
fail(FailState::MissingFunction,
218-
"Wasm module is missing the Proxy-Wasm ABI version or requires an unsupported version.");
219-
} else if (abi_version_0_1_0_ && abi_version_0_2_0_) {
220-
fail(FailState::MissingFunction,
221-
"Wasm multiple versions of the Proxy-Wasm ABI are declared by the module.");
222-
}
223-
224218
_GET_PROXY(validate_configuration);
225219
_GET_PROXY(on_vm_start);
226220
_GET_PROXY(on_configure);
227221
_GET_PROXY(on_tick);
228-
_GET_PROXY(on_foreign_function);
229222

230223
_GET_PROXY(on_context_create);
231224

@@ -235,13 +228,6 @@ void WasmBase::getFunctions() {
235228
_GET_PROXY(on_downstream_connection_close);
236229
_GET_PROXY(on_upstream_connection_close);
237230

238-
if (abi_version_0_1_0_) {
239-
_GET_PROXY_ABI(on_request_headers, _abi_01);
240-
_GET_PROXY_ABI(on_response_headers, _abi_01);
241-
} else if (abi_version_0_2_0_) {
242-
_GET_PROXY_ABI(on_request_headers, _abi_02);
243-
_GET_PROXY_ABI(on_response_headers, _abi_02);
244-
}
245231
_GET_PROXY(on_request_body);
246232
_GET_PROXY(on_request_trailers);
247233
_GET_PROXY(on_request_metadata);
@@ -257,6 +243,15 @@ void WasmBase::getFunctions() {
257243
_GET_PROXY(on_done);
258244
_GET_PROXY(on_log);
259245
_GET_PROXY(on_delete);
246+
247+
if (abiVersion() == AbiVersion::ProxyWasm_0_1_0) {
248+
_GET_PROXY_ABI(on_request_headers, _abi_01);
249+
_GET_PROXY_ABI(on_response_headers, _abi_01);
250+
} else if (abiVersion() == AbiVersion::ProxyWasm_0_2_0) {
251+
_GET_PROXY_ABI(on_request_headers, _abi_02);
252+
_GET_PROXY_ABI(on_response_headers, _abi_02);
253+
_GET_PROXY(on_foreign_function);
254+
}
260255
#undef _GET_PROXY_ABI
261256
#undef _GET_PROXY
262257
}
@@ -335,6 +330,11 @@ bool WasmBase::initialize(const std::string &code, bool allow_precompiled) {
335330
allow_precompiled_ = allow_precompiled;
336331
}
337332

333+
abi_version_ = wasm_vm_->getAbiVersion();
334+
if (abi_version_ == AbiVersion::Unknown) {
335+
return false;
336+
}
337+
338338
if (started_from_ != Cloneable::InstantiatedModule) {
339339
registerCallbacks();
340340
wasm_vm_->link(vm_id_);

src/wavm/wavm.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ bool Wavm::load(const std::string &code, bool allow_precompiled) {
272272
return true;
273273
}
274274

275+
AbiVersion Wavm::getAbiVersion() { return AbiVersion::Unknown; }
276+
275277
void Wavm::link(std::string_view debug_name) {
276278
RootResolver rootResolver(compartment_);
277279
for (auto &p : intrinsic_modules_) {

0 commit comments

Comments
 (0)