Fix some memory management issues

pull/216/head
Volodymyr Shymanskyy 3 years ago
parent e517593d1f
commit 870dd2e767

@ -17,7 +17,9 @@
#include "m3_api_tracer.h"
// Gas metering/limit only applies to pre-instrumented modules
#define GAS_LIMIT 2000000000000
#define GAS_LIMIT 2000000000000
#define MAX_MODULES 16
#define FATAL(msg, ...) { fprintf(stderr, "Error: [Fatal] " msg "\n", ##__VA_ARGS__); goto _onfatal; }
@ -28,6 +30,9 @@
IM3Environment env;
IM3Runtime runtime;
u8* wasm_bins[MAX_MODULES];
int wasm_bins_qty = 0;
#if defined(GAS_LIMIT)
static int64_t current_gas = GAS_LIMIT;
@ -96,29 +101,44 @@ M3Result repl_load (const char* fn)
fseek (f, 0, SEEK_SET);
if (fsize < 8) {
return "file is too small";
result = "file is too small";
goto on_error;
} else if (fsize > 10*1024*1024) {
return "file is too big";
result = "file is too big";
goto on_error;
}
wasm = (u8*) malloc(fsize);
if (!wasm) {
return "cannot allocate memory for wasm binary";
result = "cannot allocate memory for wasm binary";
goto on_error;
}
if (fread (wasm, 1, fsize, f) != fsize) {
return "cannot read file";
result = "cannot read file";
goto on_error;
}
fclose (f);
f = NULL;
IM3Module module;
result = m3_ParseModule (env, &module, wasm, fsize);
if (result) return result;
if (result) goto on_error;
result = m3_LoadModule (runtime, module);
if (result) return result;
if (result) goto on_error;
result = link_all (module);
if (result) goto on_error;
if (wasm_bins_qty < MAX_MODULES) {
wasm_bins[wasm_bins_qty++] = wasm;
}
return result;
on_error:
if (wasm) free(wasm);
if (f) fclose(f);
return result;
}
@ -356,6 +376,11 @@ void repl_free()
m3_FreeRuntime (runtime);
runtime = NULL;
}
for (int i = 0; i < wasm_bins_qty; i++) {
free (wasm_bins[i]);
wasm_bins[i] = NULL;
}
}
M3Result repl_init(unsigned stack)

@ -31,7 +31,9 @@
#ifdef __cplusplus
// Hacks alert
#undef _Static_assert
#define _Static_assert(...)
#undef _Noreturn
#define _Noreturn
#endif

@ -7,12 +7,6 @@
#include "m3_code.h"
#if d_m3RecordBacktraces
// Code mapping page ops
M3CodeMappingPage * NewCodeMappingPage (u32 i_minCapacity);
void FreeCodeMappingPage (M3CodeMappingPage * i_page);
#endif // d_m3RecordBacktraces
//---------------------------------------------------------------------------------------------------------------------------------
@ -34,10 +28,17 @@ IM3CodePage NewCodePage (u32 i_minNumLines)
page->info.numLines = (pageSize - sizeof (M3CodePageHeader)) / sizeof (code_t);
#if d_m3RecordBacktraces
page->info.mapping = NewCodeMappingPage (page->info.numLines);
if (!page->info.mapping)
u32 pageSizeBt = sizeof (M3CodeMappingPage) + sizeof (M3CodeMapEntry) * page->info.numLines;
page->info.mapping = (M3CodeMappingPage *)m3_Malloc (pageSizeBt);
if (page->info.mapping)
{
m3Free (page);
page->info.mapping->size = 0;
page->info.mapping->capacity = page->info.numLines;
}
else
{
m3_Free (page);
return NULL;
}
page->info.mapping->basePC = GetPageStartPC(page);
@ -60,7 +61,7 @@ void FreeCodePages (IM3CodePage * io_list)
IM3CodePage next = page->info.next;
#if d_m3RecordBacktraces
FreeCodeMappingPage (page->info.mapping);
m3_Free (page->info.mapping);
#endif // d_m3RecordBacktraces
m3_Free (page);
page = next;
@ -230,26 +231,3 @@ bool MapPCToOffset (IM3CodePage i_page, pc_t i_pc, u32 * o_moduleOffset)
//---------------------------------------------------------------------------------------------------------------------------------
#if d_m3RecordBacktraces
M3CodeMappingPage * NewCodeMappingPage (u32 i_minCapacity)
{
M3CodeMappingPage * page;
u32 pageSize = sizeof (M3CodeMappingPage) + sizeof (M3CodeMapEntry) * i_minCapacity;
m3Alloc ((void **) & page, u8, pageSize);
if (page)
{
page->size = 0;
page->capacity = i_minCapacity;
}
return page;
}
void FreeCodeMappingPage (M3CodeMappingPage * i_page)
{
m3Free (i_page);
}
#endif // d_m3RecordBacktraces

@ -528,8 +528,7 @@ void PushBacktraceFrame (IM3Runtime io_runtime, pc_t i_pc)
if (UNLIKELY (io_runtime->backtrace.lastFrame == M3_BACKTRACE_TRUNCATED))
return;
M3BacktraceFrame * newFrame;
m3Alloc ((void **) & newFrame, M3BacktraceFrame, 1);
M3BacktraceFrame * newFrame = m3_AllocStruct(M3BacktraceFrame);
if (!newFrame)
{
@ -567,7 +566,7 @@ void ClearBacktrace (IM3Runtime io_runtime)
while (currentFrame)
{
M3BacktraceFrame * nextFrame = currentFrame->next;
m3Free (currentFrame);
m3_Free (currentFrame);
currentFrame = nextFrame;
}

@ -54,13 +54,13 @@ void Function_Release (IM3Function i_function)
FreeImportInfo (& i_function->import);
//if (i_function->ownsWasmCode)
// m3Free (i_function->wasm);
// m3_Free (i_function->wasm);
// Function_FreeCompiledCode (func);
# if (d_m3EnableCodePageRefCounting)
{
m3Free (i_function->codePageRefs);
m3_Free (i_function->codePageRefs);
i_function->numCodePageRefs = 0;
}
# endif
@ -83,7 +83,7 @@ void Function_FreeCompiledCode (IM3Function i_function)
}
}
m3Free (i_function->codePageRefs);
m3_Free (i_function->codePageRefs);
Runtime_ReleaseCodePages (i_function->module->runtime);
}

@ -632,7 +632,7 @@ d_m3Op (CallRawFunction)
#if d_m3EnableStrace
if (UNLIKELY(possible_trap)) {
d_m3TracePrint("%s -> %s", outbuff, possible_trap);
d_m3TracePrint("%s -> %s", outbuff, (char*)possible_trap);
} else {
switch (GetSingleRetType(ftype)) {
case c_m3Type_none: d_m3TracePrint("%s", outbuff); break;

Loading…
Cancel
Save