Skip to content

wasi_nn_llamacpp.c: reject invalid graph and execution context #4422

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

Merged
merged 1 commit into from
Jul 1, 2025
Merged
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
33 changes: 33 additions & 0 deletions core/iwasm/libraries/wasi-nn/src/wasi_nn_llamacpp.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ __load_by_name_with_configuration(void *ctx, const char *filename, graph *g)
{
struct LlamaContext *backend_ctx = (struct LlamaContext *)ctx;

if (backend_ctx->model != NULL) {
// we only implement a single graph
return unsupported_operation;
}

// make sure backend_ctx->config is initialized

struct llama_model_params model_params =
Expand All @@ -323,6 +328,7 @@ __load_by_name_with_configuration(void *ctx, const char *filename, graph *g)
#endif

backend_ctx->model = model;
*g = 0;

return success;
}
Expand Down Expand Up @@ -363,6 +369,16 @@ init_execution_context(void *ctx, graph g, graph_execution_context *exec_ctx)
{
struct LlamaContext *backend_ctx = (struct LlamaContext *)ctx;

if (g != 0 || backend_ctx->model == NULL) {
// we only implement a single graph
return runtime_error;
}

if (backend_ctx->ctx != NULL) {
// we only implement a single context
return unsupported_operation;
}

struct llama_context_params ctx_params =
llama_context_params_from_wasi_nn_llama_config(&backend_ctx->config);
struct llama_context *llama_ctx =
Expand All @@ -373,6 +389,7 @@ init_execution_context(void *ctx, graph g, graph_execution_context *exec_ctx)
}

backend_ctx->ctx = llama_ctx;
*exec_ctx = 0;

NN_INFO_PRINTF("n_predict = %d, n_ctx = %d", backend_ctx->config.n_predict,
llama_n_ctx(backend_ctx->ctx));
Expand All @@ -384,6 +401,12 @@ set_input(void *ctx, graph_execution_context exec_ctx, uint32_t index,
tensor *wasi_nn_tensor)
{
struct LlamaContext *backend_ctx = (struct LlamaContext *)ctx;

if (exec_ctx != 0 || backend_ctx->ctx == NULL) {
// we only implement a single context
return runtime_error;
}

// tensor->data is the prompt string.
char *prompt_text = (char *)wasi_nn_tensor->data.buf;
uint32_t prompt_text_len = wasi_nn_tensor->data.size;
Expand Down Expand Up @@ -433,6 +456,11 @@ compute(void *ctx, graph_execution_context exec_ctx)
struct LlamaContext *backend_ctx = (struct LlamaContext *)ctx;
wasi_nn_error ret = runtime_error;

if (exec_ctx != 0 || backend_ctx->ctx == NULL) {
// we only implement a single context
return runtime_error;
}

// reset the generation buffer
if (backend_ctx->generation == NULL) {
backend_ctx->generation =
Expand Down Expand Up @@ -554,6 +582,11 @@ get_output(void *ctx, graph_execution_context exec_ctx, uint32_t index,
{
struct LlamaContext *backend_ctx = (struct LlamaContext *)ctx;

if (exec_ctx != 0 || backend_ctx->ctx == NULL) {
// we only implement a single context
return runtime_error;
}

// Compatibility with WasmEdge
if (index > 1) {
NN_ERR_PRINTF("Invalid output index %d", index);
Expand Down
Loading