Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ALLOCA macro to prevent memory leak. #140

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Walrus.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,16 @@ if (f.type == Type::B) { puts("failed in msvc."); }
static void operator delete(void*) = delete; \
static void operator delete[](void*) = delete;

#define ALLOCA(Type, Result, Bytes, Success) \
Type* Result = (Type*)(LIKELY(Bytes < 512) ? alloca(Bytes) \
: new char[Bytes]); \
bool Success = LIKELY(Bytes < 512) ? true : false;
#define ALLOCA(Type, Result, Bytes) \
std::unique_ptr<uint8_t[]> Result##HolderWhenUsingMalloc; \
size_t bytes##Result = (Bytes); \
Type* Result; \
if (LIKELY(bytes##Result < 512)) { \
Result = (Type*)alloca(bytes##Result); \
} else { \
Result##HolderWhenUsingMalloc = std::unique_ptr<uint8_t[]>(new uint8_t[bytes##Result]); \
Result = (Type*)Result##HolderWhenUsingMalloc.get(); \
}

#if !defined(STACK_GROWS_DOWN) && !defined(STACK_GROWS_UP)
#define STACK_GROWS_DOWN
Expand Down
23 changes: 4 additions & 19 deletions src/interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,15 +1359,15 @@ NEVER_INLINE void Interpreter::callOperation(
Function* target = instance->function(code->index());
const FunctionType* ft = target->functionType();
const ValueTypeVector& param = ft->param();
ALLOCA(Value, paramVector, sizeof(Value) * param.size(), isAllocaParam);
ALLOCA(Value, paramVector, sizeof(Value) * param.size());

size_t c = 0;
for (size_t i = 0; i < param.size(); i++) {
paramVector[i] = Value(param[i], bp + code->stackOffsets()[c++]);
}

const ValueTypeVector& result = ft->result();
ALLOCA(Value, resultVector, sizeof(Value) * result.size(), isAllocaResult);
ALLOCA(Value, resultVector, sizeof(Value) * result.size());
size_t codeExtraOffsetsSize = sizeof(ByteCodeStackOffset) * ft->param().size() + sizeof(ByteCodeStackOffset) * ft->result().size();

target->call(state, param.size(), paramVector, resultVector);
Expand All @@ -1377,13 +1377,6 @@ NEVER_INLINE void Interpreter::callOperation(
resultVector[i].writeToMemory(resultStackPointer);
}

if (UNLIKELY(!isAllocaParam)) {
delete[] paramVector;
}
if (UNLIKELY(!isAllocaResult)) {
delete[] resultVector;
}

programCounter += sizeof(Call) + codeExtraOffsetsSize;
}

Expand All @@ -1409,15 +1402,15 @@ NEVER_INLINE void Interpreter::callIndirectOperation(
Trap::throwException(state, "indirect call type mismatch");
}
const ValueTypeVector& param = ft->param();
ALLOCA(Value, paramVector, sizeof(Value) * param.size(), isAllocaParam);
ALLOCA(Value, paramVector, sizeof(Value) * param.size());

size_t c = 0;
for (size_t i = 0; i < param.size(); i++) {
paramVector[i] = Value(param[i], bp + code->stackOffsets()[c++]);
}

const ValueTypeVector& result = ft->result();
ALLOCA(Value, resultVector, sizeof(Value) * result.size(), isAllocaResult);
ALLOCA(Value, resultVector, sizeof(Value) * result.size());
size_t codeExtraOffsetsSize = sizeof(ByteCodeStackOffset) * ft->param().size() + sizeof(ByteCodeStackOffset) * ft->result().size();

target->call(state, param.size(), paramVector, resultVector);
Expand All @@ -1427,14 +1420,6 @@ NEVER_INLINE void Interpreter::callIndirectOperation(
resultVector[i].writeToMemory(resultStackPointer);
}

if (UNLIKELY(!isAllocaParam)) {
delete[] paramVector;
}
if (UNLIKELY(!isAllocaResult)) {
delete[] resultVector;
}

programCounter += sizeof(CallIndirect) + codeExtraOffsetsSize;
}

} // namespace Walrus
6 changes: 1 addition & 5 deletions src/runtime/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void DefinedFunction::call(ExecutionState& state, const uint32_t argc, Value* ar
{
ExecutionState newState(state, this);
checkStackLimit(newState);
ALLOCA(uint8_t, functionStackBase, m_moduleFunction->requiredStackSize(), isAlloca);
ALLOCA(uint8_t, functionStackBase, m_moduleFunction->requiredStackSize());
uint8_t* functionStackPointer = functionStackBase;

// init parameter space
Expand Down Expand Up @@ -100,10 +100,6 @@ void DefinedFunction::call(ExecutionState& state, const uint32_t argc, Value* ar
for (size_t i = 0; i < resultTypeInfo.size(); i++) {
result[i] = Value(resultTypeInfo[i], functionStackBase + resultOffsets[i]);
}

if (UNLIKELY(!isAlloca)) {
delete[] functionStackBase;
}
}

ImportedFunction* ImportedFunction::createImportedFunction(Store* store,
Expand Down
18 changes: 3 additions & 15 deletions src/runtime/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,12 @@ Instance* Module::instantiate(ExecutionState& state, const ExternVector& imports
Walrus::Trap trap;
trap.run([](Walrus::ExecutionState& state, void* d) {
RunData* data = reinterpret_cast<RunData*>(d);
ALLOCA(uint8_t, functionStackBase, data->mf->requiredStackSize(), isAlloca);
ALLOCA(uint8_t, functionStackBase, data->mf->requiredStackSize());

DefinedFunction fakeFunction(data->instance, data->mf);
ExecutionState newState(state, &fakeFunction);
auto resultOffset = Interpreter::interpret(newState, functionStackBase);
data->instance->m_globals[data->index]->setValue(Value(data->type, functionStackBase + resultOffset[0]));

if (UNLIKELY(!isAlloca)) {
delete[] functionStackBase;
}
},
&data);
}
Expand All @@ -261,7 +257,7 @@ Instance* Module::instantiate(ExecutionState& state, const ExternVector& imports
Walrus::Trap trap;
trap.run([](Walrus::ExecutionState& state, void* d) {
RunData* data = reinterpret_cast<RunData*>(d);
ALLOCA(uint8_t, functionStackBase, data->elem->moduleFunction()->requiredStackSize(), isAlloca);
ALLOCA(uint8_t, functionStackBase, data->elem->moduleFunction()->requiredStackSize());

DefinedFunction fakeFunction(data->instance,
data->elem->moduleFunction());
Expand All @@ -270,10 +266,6 @@ Instance* Module::instantiate(ExecutionState& state, const ExternVector& imports
auto resultOffset = Interpreter::interpret(newState, functionStackBase);
Value offset(Value::I32, functionStackBase + resultOffset[0]);
data->index = offset.asI32();

if (UNLIKELY(!isAlloca)) {
delete[] functionStackBase;
}
},
&data);
}
Expand Down Expand Up @@ -312,7 +304,7 @@ Instance* Module::instantiate(ExecutionState& state, const ExternVector& imports
auto result = trap.run([](Walrus::ExecutionState& state, void* d) {
RunData* data = reinterpret_cast<RunData*>(d);
if (data->init->moduleFunction()->currentByteCodeSize()) {
ALLOCA(uint8_t, functionStackBase, data->init->moduleFunction()->requiredStackSize(), isAlloca);
ALLOCA(uint8_t, functionStackBase, data->init->moduleFunction()->requiredStackSize());

DefinedFunction fakeFunction(data->instance,
data->init->moduleFunction());
Expand All @@ -321,10 +313,6 @@ Instance* Module::instantiate(ExecutionState& state, const ExternVector& imports
auto resultOffset = Interpreter::interpret(newState, functionStackBase);
Value offset(Value::I32, functionStackBase + resultOffset[0]);

if (UNLIKELY(!isAlloca)) {
delete[] functionStackBase;
}

Memory* m = data->instance->memory(0);
const auto& initData = data->init->initData();
if (m->sizeInByte() >= initData.size() && (offset.asI32() + initData.size()) <= m->sizeInByte() && offset.asI32() >= 0) {
Expand Down