Skip to content

Commit 9e5a89a

Browse files
committed
use weak ptr of base wasm handle
本次代码评审主要涉及将`base_wasm_handle_`从一个直接的`std::shared_ptr`更改为`std::weak_ptr`(命名为`base_wasm_handle_weak_`),并在访问时增加检查以确保弱引用未失效,增强了内存管理的安全性,避免了潜在的空指针异常。 Link: https://code.alibaba-inc.com/Ingress/proxy-wasm-cpp-host/codereview/18296840 * use weak ptr of base wasm handle * fix typo * fix typo
1 parent 60e8d5c commit 9e5a89a

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

include/proxy-wasm/wasm.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
305305
// is not enforced.
306306
AllowedCapabilitiesMap allowed_capabilities_;
307307

308-
std::shared_ptr<WasmHandleBase> base_wasm_handle_;
308+
std::weak_ptr<WasmHandleBase> base_wasm_handle_weak_;
309309

310310
// Used by the base_wasm to enable non-clonable thread local Wasm(s) to be constructed.
311311
std::string module_bytecode_;
@@ -448,8 +448,9 @@ std::shared_ptr<PluginHandleBase> getOrCreateThreadLocalPlugin(
448448
void clearWasmCachesForTesting();
449449

450450
inline const std::string &WasmBase::vm_configuration() const {
451-
if (base_wasm_handle_)
452-
return base_wasm_handle_->wasm()->vm_configuration_;
451+
auto base_wasm_handle = base_wasm_handle_weak_.lock();
452+
if (base_wasm_handle)
453+
return base_wasm_handle->wasm()->vm_configuration_;
453454
return vm_configuration_;
454455
}
455456

src/wasm.cc

+15-5
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ WasmBase::WasmBase(const std::shared_ptr<WasmHandleBase> &base_wasm_handle,
200200
started_from_(base_wasm_handle->wasm()->wasm_vm()->cloneable()),
201201
envs_(base_wasm_handle->wasm()->envs()),
202202
allowed_capabilities_(base_wasm_handle->wasm()->allowed_capabilities_),
203-
base_wasm_handle_(base_wasm_handle) {
203+
base_wasm_handle_weak_(base_wasm_handle) {
204204
if (started_from_ != Cloneable::NotCloneable) {
205205
wasm_vm_ = base_wasm_handle->wasm()->wasm_vm()->clone();
206206
} else {
@@ -317,17 +317,27 @@ bool WasmBase::initialize() {
317317
}
318318

319319
if (started_from_ == Cloneable::NotCloneable) {
320-
auto ok = wasm_vm_->load(base_wasm_handle_->wasm()->moduleBytecode(),
321-
base_wasm_handle_->wasm()->modulePrecompiled(),
322-
base_wasm_handle_->wasm()->functionNames());
320+
auto base_wasm_handle = base_wasm_handle_weak_.lock();
321+
if (!base_wasm_handle) {
322+
fail(FailState::UnableToInitializeCode, "Base wasm handle is null");
323+
return false;
324+
}
325+
auto ok = wasm_vm_->load(base_wasm_handle->wasm()->moduleBytecode(),
326+
base_wasm_handle->wasm()->modulePrecompiled(),
327+
base_wasm_handle->wasm()->functionNames());
323328
if (!ok) {
324329
fail(FailState::UnableToInitializeCode, "Failed to load Wasm module from base Wasm");
325330
return false;
326331
}
327332
}
328333

329334
if (started_from_.has_value()) {
330-
abi_version_ = base_wasm_handle_->wasm()->abiVersion();
335+
auto base_wasm_handle = base_wasm_handle_weak_.lock();
336+
if (!base_wasm_handle) {
337+
fail(FailState::UnableToInitializeCode, "Base wasm handle is null");
338+
return false;
339+
}
340+
abi_version_ = base_wasm_handle->wasm()->abiVersion();
331341
}
332342

333343
if (started_from_ != Cloneable::InstantiatedModule) {

0 commit comments

Comments
 (0)