@@ -35,6 +35,32 @@ static_assert(false, "OS not supported");
3535#endif
3636
3737namespace vb {
38+
39+ // /
40+ // / @brief RAII wrapper to set and reset runtime pointer for signal handling
41+ // /
42+ // / Sets the thread-local runtime pointer in constructor and resets to nullptr in destructor
43+ // /
44+ class ScopedRuntimeGuard final {
45+ public:
46+ // /
47+ // / @brief Construct and set the runtime pointer
48+ // /
49+ // / @param runtime The runtime to set
50+ // /
51+ explicit ScopedRuntimeGuard (Runtime const &runtime) VB_NOEXCEPT;
52+
53+ // /
54+ // / @brief Destructor resets runtime pointer to nullptr
55+ // /
56+ ~ScopedRuntimeGuard () VB_NOEXCEPT;
57+
58+ ScopedRuntimeGuard (ScopedRuntimeGuard const &) = delete ;
59+ ScopedRuntimeGuard &operator =(ScopedRuntimeGuard const &) & = delete ;
60+ ScopedRuntimeGuard (ScopedRuntimeGuard &&) = delete ;
61+ ScopedRuntimeGuard &operator =(ScopedRuntimeGuard &&) & = delete ;
62+ };
63+
3864// /
3965// / @brief A wrapper to run Wasm function inside signal handler
4066// /
@@ -51,7 +77,7 @@ class SignalFunctionWrapper final {
5177 // / @throws std::runtime_error signal handler setup failed
5278 // /
5379 static inline void start (Runtime &runtime) {
54- trySetRuntime ( runtime) ;
80+ ScopedRuntimeGuard const guard{ runtime} ;
5581 SignalFunction::call_raw (start_wrapper, runtime);
5682 }
5783
@@ -77,7 +103,7 @@ class SignalFunctionWrapper final {
77103 template <size_t NumReturnValue, typename ... FunctionArguments>
78104 static std::array<WasmValue, NumReturnValue> call (ModuleFunction<NumReturnValue, FunctionArguments...> const &function,
79105 FunctionArguments... args) VB_THROW {
80- trySetRuntime ( function.getRuntime ()) ;
106+ ScopedRuntimeGuard const guard{ function.getRuntime ()} ;
81107 return SignalFunction::call_raw<NumReturnValue>(function, args...);
82108 }
83109
@@ -91,7 +117,7 @@ class SignalFunctionWrapper final {
91117 // / @throws std::runtime_error signal handler setup failed
92118 // /
93119 static void call (RawModuleFunction const &function, uint8_t const *const serializedArgs, uint8_t *const results) {
94- trySetRuntime ( function.getRuntime ()) ;
120+ ScopedRuntimeGuard const guard{ function.getRuntime ()} ;
95121 return SignalFunction::call_raw (callRawModuleFunction_wrapper, function, serializedArgs, results);
96122 }
97123
@@ -105,7 +131,7 @@ class SignalFunctionWrapper final {
105131 // / @throws std::runtime_error signal handler setup failed
106132 // /
107133 static void call (RawModuleFunction const &function, WasmValue const *const serializedArgs, WasmValue *const results) {
108- trySetRuntime ( function.getRuntime ()) ;
134+ ScopedRuntimeGuard const guard{ function.getRuntime ()} ;
109135 return SignalFunction::call_raw (callRawModuleFunction_wrapper, function, pCast<uint8_t const *const >(serializedArgs),
110136 pCast<uint8_t *const >(results));
111137 }
@@ -131,18 +157,6 @@ class SignalFunctionWrapper final {
131157#endif
132158
133159private:
134- // /
135- // / @brief Set the runtime pointer of current thread
136- // /
137- // / @param runtime
138- // / @note If signal handler is not required by current build config, this function will do nothing
139- static void trySetRuntime (Runtime const &runtime) VB_NOEXCEPT {
140- #if !LINEAR_MEMORY_BOUNDS_CHECKS || !ACTIVE_STACK_OVERFLOW_CHECK
141- pRuntime_ = &runtime;
142- #else
143- static_cast <void >(runtime);
144- #endif
145- }
146160 // /
147161 // / @brief wrap the raw module function in a function type
148162 // /
@@ -191,8 +205,25 @@ class SignalFunctionWrapper final {
191205 static void probeLinearMemoryOffset () VB_NOEXCEPT;
192206#endif
193207
194- friend SignalFunction; // /< Allow OS specific signal function to access private members
208+ friend SignalFunction; // /< Allow OS specific signal function to access private members
209+ friend ScopedRuntimeGuard; // /< Allow RAII guard to access pRuntime_
195210};
196211
212+ // ScopedRuntimeGuard implementation (after SignalFunctionWrapper is complete)
213+ // coverity[autosar_cpp14_a3_1_5_violation]
214+ inline ScopedRuntimeGuard::ScopedRuntimeGuard (Runtime const &runtime) VB_NOEXCEPT {
215+ static_cast <void >(runtime);
216+ #if !LINEAR_MEMORY_BOUNDS_CHECKS || !ACTIVE_STACK_OVERFLOW_CHECK
217+ SignalFunctionWrapper::pRuntime_ = &runtime;
218+ #endif
219+ }
220+ // coverity[autosar_cpp14_a3_1_5_violation]
221+ // coverity[autosar_cpp14_a12_7_1_violation]
222+ inline ScopedRuntimeGuard::~ScopedRuntimeGuard () VB_NOEXCEPT {
223+ #if !LINEAR_MEMORY_BOUNDS_CHECKS || !ACTIVE_STACK_OVERFLOW_CHECK
224+ SignalFunctionWrapper::pRuntime_ = nullptr ;
225+ #endif
226+ }
227+
197228} // namespace vb
198229#endif
0 commit comments