From 14708b2925dd77d67a5cc8fc8a6dbce8e1cbb2d3 Mon Sep 17 00:00:00 2001 From: GorogPeter Date: Fri, 29 Sep 2023 11:40:45 +0200 Subject: [PATCH] Align memory to pointer size --- src/interpreter/ByteCode.cpp | 14 +++++++------- src/interpreter/ByteCode.h | 5 +++++ src/interpreter/Interpreter.cpp | 8 ++++---- src/parser/WASMParser.cpp | 15 ++++++++++----- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/interpreter/ByteCode.cpp b/src/interpreter/ByteCode.cpp index 069f75cf6..c33f76207 100644 --- a/src/interpreter/ByteCode.cpp +++ b/src/interpreter/ByteCode.cpp @@ -54,25 +54,25 @@ size_t ByteCode::getSize() switch (this->opcode()) { case ThrowOpcode: { Throw* throwCode = reinterpret_cast(this); - return sizeof(Throw) + sizeof(ByteCodeStackOffset) * throwCode->offsetsSize(); + return ByteCode::pointerAlignedSize(sizeof(Throw) + sizeof(ByteCodeStackOffset) * throwCode->offsetsSize()); } case CallOpcode: { Call* call = reinterpret_cast(this); - return sizeof(Call) + sizeof(ByteCodeStackOffset) * call->parameterOffsetsSize() - + sizeof(ByteCodeStackOffset) * call->resultOffsetsSize(); + return ByteCode::pointerAlignedSize(sizeof(Call) + sizeof(ByteCodeStackOffset) * call->parameterOffsetsSize() + + sizeof(ByteCodeStackOffset) * call->resultOffsetsSize()); } case BrTableOpcode: { BrTable* brTable = reinterpret_cast(this); - return sizeof(BrTable) + sizeof(int32_t) * brTable->tableSize(); + return ByteCode::pointerAlignedSize(sizeof(BrTable) + sizeof(int32_t) * brTable->tableSize()); } case CallIndirectOpcode: { CallIndirect* callIndirect = reinterpret_cast(this); - return sizeof(CallIndirect) + sizeof(ByteCodeStackOffset) * callIndirect->parameterOffsetsSize() - + sizeof(ByteCodeStackOffset) * callIndirect->resultOffsetsSize(); + return ByteCode::pointerAlignedSize(sizeof(CallIndirect) + sizeof(ByteCodeStackOffset) * callIndirect->parameterOffsetsSize() + + sizeof(ByteCodeStackOffset) * callIndirect->resultOffsetsSize()); } case EndOpcode: { End* end = reinterpret_cast(this); - return sizeof(End) + sizeof(ByteCodeStackOffset) * end->offsetsSize(); + return ByteCode::pointerAlignedSize(sizeof(End) + sizeof(ByteCodeStackOffset) * end->offsetsSize()); } default: { return g_byteCodeSize[this->opcode()]; diff --git a/src/interpreter/ByteCode.h b/src/interpreter/ByteCode.h index 023012536..0ae59467b 100644 --- a/src/interpreter/ByteCode.h +++ b/src/interpreter/ByteCode.h @@ -528,6 +528,11 @@ class ByteCode { }; // clang-format on + static size_t pointerAlignedSize(const size_t originalSize) + { + return (originalSize + (sizeof(void*) - 1)) & ~(sizeof(void*) - 1); + } + Opcode opcode() const; size_t getSize(); diff --git a/src/interpreter/Interpreter.cpp b/src/interpreter/Interpreter.cpp index d4ef27a9b..d36b64a89 100644 --- a/src/interpreter/Interpreter.cpp +++ b/src/interpreter/Interpreter.cpp @@ -1313,8 +1313,8 @@ NEVER_INLINE void Interpreter::callOperation( Call* code = (Call*)programCounter; Function* target = instance->function(code->index()); target->interpreterCall(state, bp, code->stackOffsets(), code->parameterOffsetsSize(), code->resultOffsetsSize()); - programCounter += (sizeof(Call) + sizeof(ByteCodeStackOffset) * code->parameterOffsetsSize() - + sizeof(ByteCodeStackOffset) * code->resultOffsetsSize()); + programCounter += ByteCode::pointerAlignedSize(sizeof(Call) + sizeof(ByteCodeStackOffset) * code->parameterOffsetsSize() + + sizeof(ByteCodeStackOffset) * code->resultOffsetsSize()); } NEVER_INLINE void Interpreter::callIndirectOperation( @@ -1340,7 +1340,7 @@ NEVER_INLINE void Interpreter::callIndirectOperation( } target->interpreterCall(state, bp, code->stackOffsets(), code->parameterOffsetsSize(), code->resultOffsetsSize()); - programCounter += (sizeof(CallIndirect) + sizeof(ByteCodeStackOffset) * code->parameterOffsetsSize() - + sizeof(ByteCodeStackOffset) * code->resultOffsetsSize()); + programCounter += ByteCode::pointerAlignedSize(sizeof(CallIndirect) + sizeof(ByteCodeStackOffset) * code->parameterOffsetsSize() + + sizeof(ByteCodeStackOffset) * code->resultOffsetsSize()); } } // namespace Walrus diff --git a/src/parser/WASMParser.cpp b/src/parser/WASMParser.cpp index eaed0d442..0c71373bc 100644 --- a/src/parser/WASMParser.cpp +++ b/src/parser/WASMParser.cpp @@ -1165,7 +1165,8 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate { auto resultCount = computeFunctionParameterOrResultOffsetCount(functionType->result()); pushByteCode(Walrus::Call(index, parameterCount, resultCount), WASMOpcode::CallOpcode); - m_currentFunction->expandByteCode(sizeof(Walrus::ByteCodeStackOffset) * (parameterCount + resultCount)); + m_currentFunction->expandByteCode(Walrus::ByteCode::pointerAlignedSize(sizeof(Walrus::ByteCodeStackOffset) * (parameterCount + resultCount))); + ASSERT(m_currentFunction->currentByteCodeSize() % sizeof(void*) == 0); auto code = m_currentFunction->peekByteCode(callPos); generateCallExpr(code, parameterCount, resultCount, functionType); @@ -1180,7 +1181,8 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate { auto resultCount = computeFunctionParameterOrResultOffsetCount(functionType->result()); pushByteCode(Walrus::CallIndirect(popVMStack(), tableIndex, functionType, parameterCount, resultCount), WASMOpcode::CallIndirectOpcode); - m_currentFunction->expandByteCode(sizeof(Walrus::ByteCodeStackOffset) * (parameterCount + resultCount)); + m_currentFunction->expandByteCode(Walrus::ByteCode::pointerAlignedSize(sizeof(Walrus::ByteCodeStackOffset) * (parameterCount + resultCount))); + ASSERT(m_currentFunction->currentByteCodeSize() % sizeof(void*) == 0); auto code = m_currentFunction->peekByteCode(callPos); generateCallExpr(code, parameterCount, resultCount, functionType); @@ -1627,7 +1629,8 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate { pushByteCode(Walrus::End(offsetCount), WASMOpcode::EndOpcode); auto& result = m_currentFunctionType->result(); - m_currentFunction->expandByteCode(sizeof(Walrus::ByteCodeStackOffset) * offsetCount); + m_currentFunction->expandByteCode(Walrus::ByteCode::pointerAlignedSize(sizeof(Walrus::ByteCodeStackOffset) * offsetCount)); + ASSERT(m_currentFunction->currentByteCodeSize() % sizeof(void*) == 0); Walrus::End* end = m_currentFunction->peekByteCode(pos); size_t offsetIndex = 0; for (size_t i = 0; i < result.size(); i++) { @@ -1821,7 +1824,8 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate { pushByteCode(Walrus::BrTable(stackPos, numTargets), WASMOpcode::BrTableOpcode); if (numTargets) { - m_currentFunction->expandByteCode(sizeof(int32_t) * numTargets); + m_currentFunction->expandByteCode(Walrus::ByteCode::pointerAlignedSize(sizeof(int32_t) * numTargets)); + ASSERT(m_currentFunction->currentByteCodeSize() % sizeof(void*) == 0); for (Index i = 0; i < numTargets; i++) { emitBrTableCase(brTableCode, targetDepths[i], sizeof(Walrus::BrTable) + i * sizeof(int32_t)); @@ -1861,7 +1865,8 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate { if (tagIndex != std::numeric_limits::max()) { auto functionType = m_result.m_functionTypes[m_result.m_tagTypes[tagIndex]->sigIndex()]; auto& param = functionType->param(); - m_currentFunction->expandByteCode(sizeof(Walrus::ByteCodeStackOffset) * param.size()); + m_currentFunction->expandByteCode(Walrus::ByteCode::pointerAlignedSize(sizeof(Walrus::ByteCodeStackOffset) * param.size())); + ASSERT(m_currentFunction->currentByteCodeSize() % sizeof(void*) == 0); Walrus::Throw* code = m_currentFunction->peekByteCode(pos); for (size_t i = 0; i < param.size(); i++) { code->dataOffsets()[param.size() - i - 1] = (m_vmStack.rbegin() + i)->position();