Skip to content

Commit d8960ac

Browse files
committed
Canary for the second plugins
Signed-off-by: Ingwon Song <[email protected]>
1 parent 55c93ab commit d8960ac

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

src/wasm.cc

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,38 @@ void WasmBase::finishShutdown() {
446446
}
447447
}
448448

449+
bool canary(std::shared_ptr<WasmHandleBase> &wasm_handle, const std::shared_ptr<PluginBase> &plugin,
450+
const WasmHandleCloneFactory &clone_factory) {
451+
auto configuration_canary_handle = clone_factory(wasm_handle);
452+
if (!configuration_canary_handle) {
453+
wasm_handle->wasm()->fail(FailState::UnableToCloneVm, "Failed to clone Base Wasm");
454+
return false;
455+
}
456+
if (!configuration_canary_handle->wasm()->initialize()) {
457+
configuration_canary_handle->wasm()->fail(FailState::UnableToInitializeCode, "Failed to initialize Wasm code");
458+
return false;
459+
}
460+
auto *root_context = configuration_canary_handle->wasm()->start(plugin);
461+
if (root_context == nullptr) {
462+
configuration_canary_handle->wasm()->fail(FailState::StartFailed, "Failed to start base Wasm");
463+
return false;
464+
}
465+
if (!configuration_canary_handle->wasm()->configure(root_context, plugin)) {
466+
configuration_canary_handle->wasm()->fail(FailState::ConfigureFailed,
467+
"Failed to configure base Wasm plugin");
468+
return false;
469+
}
470+
configuration_canary_handle->kill();
471+
return true;
472+
}
473+
449474
std::shared_ptr<WasmHandleBase> createWasm(const std::string &vm_key, const std::string &code,
450475
const std::shared_ptr<PluginBase> &plugin,
451476
const WasmHandleFactory &factory,
452477
const WasmHandleCloneFactory &clone_factory,
453478
bool allow_precompiled) {
454479
std::shared_ptr<WasmHandleBase> wasm_handle;
480+
bool is_new_wasm = false;
455481
{
456482
std::lock_guard<std::mutex> guard(base_wasms_mutex);
457483
if (base_wasms == nullptr) {
@@ -464,44 +490,31 @@ std::shared_ptr<WasmHandleBase> createWasm(const std::string &vm_key, const std:
464490
base_wasms->erase(it);
465491
}
466492
}
467-
if (wasm_handle) {
468-
return wasm_handle;
469-
}
470-
wasm_handle = factory(vm_key);
471-
if (!wasm_handle) {
472-
return nullptr;
493+
if (!wasm_handle) {
494+
wasm_handle = factory(vm_key);
495+
if (!wasm_handle) {
496+
return nullptr;
497+
}
498+
is_new_wasm = true;
499+
(*base_wasms)[vm_key] = wasm_handle;
473500
}
474-
(*base_wasms)[vm_key] = wasm_handle;
475501
}
476502

477-
if (!wasm_handle->wasm()->load(code, allow_precompiled)) {
478-
wasm_handle->wasm()->fail(FailState::UnableToInitializeCode, "Failed to load Wasm code");
479-
return nullptr;
480-
}
481-
if (!wasm_handle->wasm()->initialize()) {
482-
wasm_handle->wasm()->fail(FailState::UnableToInitializeCode, "Failed to initialize Wasm code");
483-
return nullptr;
484-
}
485-
auto configuration_canary_handle = clone_factory(wasm_handle);
486-
if (!configuration_canary_handle) {
487-
wasm_handle->wasm()->fail(FailState::UnableToCloneVm, "Failed to clone Base Wasm");
488-
return nullptr;
489-
}
490-
if (!configuration_canary_handle->wasm()->initialize()) {
491-
wasm_handle->wasm()->fail(FailState::UnableToInitializeCode, "Failed to initialize Wasm code");
492-
return nullptr;
493-
}
494-
auto *root_context = configuration_canary_handle->wasm()->start(plugin);
495-
if (root_context == nullptr) {
496-
configuration_canary_handle->wasm()->fail(FailState::StartFailed, "Failed to start base Wasm");
497-
return nullptr;
503+
if (is_new_wasm) {
504+
if (!wasm_handle->wasm()->load(code, allow_precompiled)) {
505+
wasm_handle->wasm()->fail(FailState::UnableToInitializeCode, "Failed to load Wasm code");
506+
return nullptr;
507+
}
508+
if (!wasm_handle->wasm()->initialize()) {
509+
wasm_handle->wasm()->fail(FailState::UnableToInitializeCode, "Failed to initialize Wasm code");
510+
return nullptr;
511+
}
498512
}
499-
if (!configuration_canary_handle->wasm()->configure(root_context, plugin)) {
500-
configuration_canary_handle->wasm()->fail(FailState::ConfigureFailed,
501-
"Failed to configure base Wasm plugin");
513+
514+
if (!canary(wasm_handle, plugin, clone_factory)) {
502515
return nullptr;
503516
}
504-
configuration_canary_handle->kill();
517+
505518
return wasm_handle;
506519
};
507520

0 commit comments

Comments
 (0)