Skip to content

Commit a46948a

Browse files
committed
[wip] remove interruption request
we can set the premission as RW to force stop the wasm execution. It can save lots of CPU cycle in check point
1 parent 0f99b38 commit a46948a

6 files changed

Lines changed: 24 additions & 21 deletions

File tree

src/WasmModule/WasmModule.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ void WasmModule::requestInterruption(vb::TrapCode const trapCode) VB_NOEXCEPT {
252252
#endif
253253
#if INTERRUPTION_REQUEST
254254
runtime_.requestInterruption(trapCode);
255+
MemUtils::setPermissionRW(const_cast<uint8_t *>(executableMemory_.data()), executableMemory_.size());
255256
#else
256257
static_cast<void>(trapCode);
257258
UNREACHABLE(_, "Should not be called if interruption is not requested");

src/core/compiler/backend/aarch64/aarch64_backend.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,19 +1013,7 @@ void Backend::spillAllVariables(Stack::iterator const below) const {
10131013
}
10141014

10151015
#if INTERRUPTION_REQUEST
1016-
void Backend::checkForInterruptionRequest(REG const scrReg) const {
1017-
as_.INSTR(LDURB_wT_deref_xN_unscSImm9_t)
1018-
.setT(scrReg)
1019-
.setN(WasmABI::REGS::linMem)
1020-
.setUnscSImm9(SafeInt<9>::fromConst<-BD::FromEnd::statusFlags>())();
1021-
1022-
RelPatchObj const statusFlagIsZero{as_.prepareJMPIfRegIsZero(scrReg, false)};
1023-
// Retrieve the trapCode from the actual flag
1024-
if (scrReg != WasmABI::REGS::trapReg) {
1025-
as_.INSTR(MOV_wD_wM_t).setD(WasmABI::REGS::trapReg).setM(scrReg)();
1026-
}
1027-
as_.TRAP(TrapCode::NONE);
1028-
statusFlagIsZero.linkToHere();
1016+
void Backend::checkForInterruptionRequest(REG const) const {
10291017
}
10301018
#endif
10311019

src/core/compiler/backend/x86_64/x86_64_backend.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -614,13 +614,6 @@ void Backend::spillAllVariables(Stack::iterator const below) const {
614614

615615
#if INTERRUPTION_REQUEST
616616
void Backend::checkForInterruptionRequest() const {
617-
as_.INSTR(CMP_rm8_imm8).setM4RM(WasmABI::REGS::linMem, -BD::FromEnd::statusFlags).setImm8(0x0U)();
618-
619-
RelPatchObj const relPatchObj{as_.prepareJMP(true, CC::E)};
620-
// Retrieve the trapCode from the actual flag
621-
as_.INSTR(MOVZX_r32_rm8_t).setR(WasmABI::REGS::trapReg).setM4RM(WasmABI::REGS::linMem, -BD::FromEnd::statusFlags)();
622-
as_.TRAP(TrapCode::NONE, false);
623-
relPatchObj.linkToHere();
624617
}
625618
#endif
626619

src/core/runtime/Runtime.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,12 @@ void Runtime::requestInterruption(TrapCode const trapCode) const VB_NOEXCEPT {
549549
uint8_t const rawTrapCode{static_cast<uint8_t>(static_cast<uint32_t>(trapCode))};
550550
*ptr = rawTrapCode;
551551
}
552+
TrapCode Runtime::getRequestInterruption() const VB_NOEXCEPT {
553+
TrapCode trapCode{TrapCode::NONE};
554+
uint8_t *const ptr{pSubI(getLinearMemoryBase(), Basedata::FromEnd::statusFlags)};
555+
memcpy(&trapCode, ptr, sizeof(TrapCode));
556+
return trapCode;
557+
}
552558
#endif
553559

554560
void Runtime::resetTrapInfo() const VB_NOEXCEPT {

src/core/runtime/Runtime.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "src/core/common/VbExceptions.hpp"
4242
#include "src/core/common/basedataoffsets.hpp"
4343
#include "src/core/common/util.hpp"
44+
#include "src/utils/MemUtils.hpp"
4445

4546
namespace vb {
4647

@@ -363,6 +364,13 @@ class Runtime final {
363364
/// default) NOTE: TrapCode::NONE will not lead to a trap
364365
///
365366
NO_THREAD_SANITIZE void requestInterruption(TrapCode const trapCode = TrapCode::RUNTIME_INTERRUPT_REQUESTED) const VB_NOEXCEPT;
367+
TrapCode getRequestInterruption() const VB_NOEXCEPT;
368+
369+
static void interruptRecoverHandler(Runtime &self) {
370+
MemUtils::setPermissionRX(pRemoveConst(self.binaryModule_.getStartAddress()),
371+
static_cast<size_t>(self.binaryModule_.getEndAddress() - self.binaryModule_.getStartAddress()));
372+
self.tryTrap(self.getRequestInterruption());
373+
}
366374
#endif
367375

368376
#if BUILTIN_FUNCTIONS

src/utils/SignalFunctionWrapper_unix.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,14 @@ void SignalFunctionWrapperUnix::memorySignalHandler(int32_t const signalId, sigi
303303
#else
304304
static_cast<void>(si);
305305
#endif
306-
306+
if (trapCode == 0) {
307+
trapCode = static_cast<uint32_t>(SignalFunctionWrapper::pRuntime_->getRequestInterruption());
308+
if (trapCode != 0U) {
309+
setReturnFromSignalHandler(uc, pCast<void const *>(&Runtime::interruptRecoverHandler));
310+
setParamsForReturn(uc, (uint64_t)SignalFunctionWrapper::pRuntime_, 0U);
311+
return;
312+
}
313+
}
307314
if (trapCode != 0U) {
308315
handleTrap(uc, trapCode);
309316
return;

0 commit comments

Comments
 (0)