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

track heap usage #196

Merged
merged 51 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
e713e1d
mem_context wip
yamt Jun 13, 2024
19ec8a8
mem_context wip
yamt Jun 13, 2024
1b85bfb
format
yamt Jun 13, 2024
cfe5ca0
mem_contexnt wip
yamt Jun 15, 2024
41badae
typo
yamt Jun 15, 2024
c6f5832
mem_context: TOYWASM_ENABLE_DYLD
yamt Jun 15, 2024
b9fe96b
mem.c: add a debug code
yamt Jun 15, 2024
2cc2627
module.c: fix a mem_free with a wrong size
yamt Jun 15, 2024
6ead71c
mem_context: adapt dyld
yamt Jun 15, 2024
1418551
lib/mem.c: include stdint.h for SIZE_MAX
yamt Jun 15, 2024
eb28007
lib/mem.c: more debug code
yamt Jun 15, 2024
57ecec6
_read_vec_with_ctx_impl: fix mem_context accounting on error
yamt Jun 15, 2024
e090d23
mem_context: fix accounting on br_table failure
yamt Jun 15, 2024
34ff965
read_expr_common: fix mem_context accounting
yamt Jun 15, 2024
daf5dc0
read_const_expr: assertions and a comment
yamt Jun 15, 2024
3b8a325
lib: expose mem.h
yamt Jun 15, 2024
6682afd
mem.h: extern C
yamt Jun 15, 2024
3ae7050
examples/fuzz: adapt to mem_context
yamt Jun 15, 2024
1819772
lib/mem.c: fix reservation failure handling
yamt Jun 15, 2024
13b9ef9
module.c: fix a crash on a broken module
yamt Jun 15, 2024
0b95bf4
examples/fuzz: limit memory consumption to 1MB
yamt Jun 15, 2024
784e5e5
module_funcs_clear: fix an assertion
yamt Jun 15, 2024
7f26777
fix TOYWASM_ENABLE_WASI_LITTLEFS build after mem_context changes
yamt Jun 15, 2024
81c17dc
name.c: add a missing mem_context_init
yamt Jun 15, 2024
1554363
repl: fix load-hex command after mem_context changes
yamt Jun 15, 2024
685583f
lib/mem.h: multiple inclusion protection
yamt Jun 15, 2024
86ad173
examples/app: adapt to mem_context
yamt Jun 15, 2024
abfe679
examples/runwasi: adapt to mem_context
yamt Jun 15, 2024
b343be3
examples/hostfunc: adapt to mem_context
yamt Jun 15, 2024
75cc9ff
examples/log_execution: adapt to mem_context
yamt Jun 15, 2024
d488449
examples/callgraph: adapt to mem_context
yamt Jun 15, 2024
ee0f628
module.c: adapt cellidx to mem_context
yamt Jun 15, 2024
2792241
module.c: adapt localchunks to mem_context
yamt Jun 15, 2024
699c43d
insn_impl_control.h: fix an uninitialized use of "mctx"
yamt Jun 16, 2024
03801b8
module.c: fix a null dereference in read_locals
yamt Jun 16, 2024
c94a341
adapt bitmap.h to mem_context
yamt Jun 16, 2024
ce3a6b5
cli: move str_to_uint to a separate file
yamt Jun 16, 2024
ce40887
str_to_uint.c: add str_to_u32
yamt Jun 16, 2024
b6fd7f4
cli: replace a few atoi with str_to_u32
yamt Jun 16, 2024
417434d
str_to_uint.c: add str_to_size
yamt Jun 16, 2024
3572a89
cli: add --max-memory option
yamt Jun 16, 2024
6c1becf
adapt resulttype_alloc/resulttype_free to mem_context
yamt Jun 16, 2024
53ceb67
adapt validation_context to mem_context
yamt Jun 16, 2024
af4b315
restore datacount section handling
yamt Jun 16, 2024
a29a00b
mem.c: add mem_context_setlimit
yamt Jun 16, 2024
5b3aef5
cli: use mem_context_setlimit
yamt Jun 16, 2024
0bc0721
repl.c: log error number
yamt Jun 16, 2024
0696fe5
import_object_create_for_host_funcs: log error number
yamt Jun 16, 2024
ad51ad4
import_object_create_for_host_funcs: fix an uninitialized var
yamt Jun 16, 2024
fc45c7a
cli: make the repl mem_context a parent of others
yamt Jun 16, 2024
90010b1
cli: clean up toywasm_repl_reset memory stats
yamt Jun 16, 2024
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ list(APPEND TEST_ENV "TEST_RUNTIME_EXE=${TOYWASM_CLI}")
set(cli_sources
"cli/main.c"
"cli/repl.c"
"cli/str_to_uint.c"
)

add_executable(toywasm-cli ${cli_sources})
Expand Down
73 changes: 64 additions & 9 deletions cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include <stdlib.h>
#include <string.h>

#include "mem.h"
#include "repl.h"
#include "str_to_uint.h"
#include "toywasm_config.h"
#if defined(TOYWASM_ENABLE_WASI_THREADS)
#include "wasi_threads.h"
Expand All @@ -34,6 +36,7 @@ enum longopt {
opt_invoke,
opt_load,
opt_max_frames,
opt_max_memory,
opt_max_stack_cells,
opt_register,
opt_repl,
Expand Down Expand Up @@ -125,6 +128,12 @@ static const struct option longopts[] = {
NULL,
opt_max_frames,
},
{
"max-memory",
required_argument,
NULL,
opt_max_memory,
},
{
"max-stack-cells",
required_argument,
Expand Down Expand Up @@ -226,6 +235,7 @@ static const char *opt_metavars[] = {
[opt_repl_prompt] = "STRING",
[opt_max_frames] = "NUMBER_OF_FRAMES",
[opt_max_stack_cells] = "NUMBER_OF_CELLS",
[opt_max_memory] = "MEMORY_LIMIT_IN_BYTES",
};

static void
Expand Down Expand Up @@ -272,6 +282,11 @@ print_usage(void)
int
main(int argc, char *const *argv)
{
struct mem_context mctx0, *mctx = &mctx0;
struct mem_context wasi_mctx0, *wasi_mctx = &wasi_mctx0;
struct mem_context dyld_mctx0, *dyld_mctx = &dyld_mctx0;
struct mem_context impobj_mctx0, *impobj_mctx = &impobj_mctx0;

struct repl_state *state;
#if defined(TOYWASM_ENABLE_WASI)
VEC(, const char *) wasi_envs;
Expand Down Expand Up @@ -305,13 +320,25 @@ main(int argc, char *const *argv)
xlog_tracing = 0;
#endif
#endif
mem_context_init(mctx);
mem_context_init(wasi_mctx);
mem_context_init(dyld_mctx);
mem_context_init(impobj_mctx);
wasi_mctx->parent = mctx;
dyld_mctx->parent = mctx;
impobj_mctx->parent = mctx;

state = malloc(sizeof(*state));
if (state == NULL) {
goto fail;
}
toywasm_repl_state_init(state);
state->mctx = mctx;
state->wasi_mctx = wasi_mctx;
state->dyld_mctx = dyld_mctx;
state->impobj_mctx = impobj_mctx;
struct repl_options *opts = &state->opts;
size_t limit;
while ((ret = getopt_long(argc, argv, "", longopts, &longidx)) != -1) {
switch (ret) {
case opt_disable_jump_table:
Expand All @@ -329,7 +356,7 @@ main(int argc, char *const *argv)
break;
#if defined(TOYWASM_ENABLE_DYLD)
case opt_dyld:
if (state->nmodules > 0) {
if (state->modules.lsize > 0) {
ret = EPROTO;
goto fail;
}
Expand All @@ -344,7 +371,7 @@ main(int argc, char *const *argv)
break;
#endif
case opt_dyld_path:
ret = VEC_PREALLOC(dyld_paths, 1);
ret = VEC_PREALLOC(mctx, dyld_paths, 1);
if (ret != 0) {
goto fail;
}
Expand All @@ -353,7 +380,11 @@ main(int argc, char *const *argv)
opts->dyld_options.paths = dyld_paths.p;
break;
case opt_dyld_stack_size:
opts->dyld_options.stack_size = atoi(optarg);
ret = str_to_u32(optarg, 0,
&opts->dyld_options.stack_size);
if (ret != 0) {
goto fail;
}
break;
#endif
case opt_invoke:
Expand All @@ -371,10 +402,31 @@ main(int argc, char *const *argv)
might_need_help = false;
break;
case opt_max_frames:
opts->exec_options.max_frames = atoi(optarg);
ret = str_to_u32(optarg, 0,
&opts->exec_options.max_frames);
if (ret != 0) {
goto fail;
}
break;
case opt_max_memory:
ret = str_to_size(optarg, 0, &limit);
if (ret != 0) {
goto fail;
}
ret = mem_context_setlimit(mctx, limit);
if (ret != 0) {
xlog_error("failed to set limit %zu (current "
"allocation: %zu)",
limit, mctx->allocated);
goto fail;
}
break;
case opt_max_stack_cells:
opts->exec_options.max_stackcells = atoi(optarg);
ret = str_to_u32(optarg, 0,
&opts->exec_options.max_stackcells);
if (ret != 0) {
goto fail;
}
break;
case opt_register:
ret = toywasm_repl_register(state, NULL, optarg);
Expand Down Expand Up @@ -420,7 +472,7 @@ main(int argc, char *const *argv)
}
break;
case opt_wasi_env:
ret = VEC_PREALLOC(wasi_envs, 1);
ret = VEC_PREALLOC(mctx, wasi_envs, 1);
if (ret != 0) {
goto fail;
}
Expand Down Expand Up @@ -485,7 +537,7 @@ main(int argc, char *const *argv)
#endif
) {
const struct repl_module_state_u *mod_u =
&state->modules[state->nmodules - 1];
&VEC_LASTELEM(state->modules);
const struct repl_module_state *mod = &mod_u->u.repl;
ret = wasi_threads_instance_set_thread_spawn_args(
state->wasi_threads, mod->module, mod->extra_import);
Expand Down Expand Up @@ -538,12 +590,15 @@ main(int argc, char *const *argv)
fail:
toywasm_repl_reset(state);
#if defined(TOYWASM_ENABLE_WASI)
VEC_FREE(wasi_envs);
VEC_FREE(mctx, wasi_envs);
#endif
#if defined(TOYWASM_ENABLE_DYLD)
VEC_FREE(dyld_paths);
VEC_FREE(mctx, dyld_paths);
#endif
free(state);
mem_context_clear(dyld_mctx);
mem_context_clear(wasi_mctx);
mem_context_clear(mctx);
exit(exit_status);
success:
exit_status = 0;
Expand Down
Loading
Loading