From b1034b86023672e6750da49b01a7d429cd8339d0 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Sat, 15 Nov 2025 18:50:55 +0100 Subject: Check memory linear accesses are within bounds WebAssembly defines how many pages of linear memory are owned by the instance on startup. This limit can then be bumped via the grow_memory operator. Therefore, accesses outside the defined bounds are considered an exception. --- src/interp/linear/check.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/interp/linear/check.c (limited to 'src/interp/linear/check.c') diff --git a/src/interp/linear/check.c b/src/interp/linear/check.c new file mode 100644 index 0000000..f2bea9a --- /dev/null +++ b/src/interp/linear/check.c @@ -0,0 +1,23 @@ +/* + * nanowasm, a tiny WebAssembly/Wasm interpreter + * Copyright (C) 2023-2025 Xavier Del Campo Romero + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include +#include +#include +#include +#include + +int nwp_linear_check(const struct nw_interp *const i, + const unsigned long offset, const size_t n) +{ + const unsigned long pagesize = 64lu << 10lu, + end = i->linear.n_pages * pagesize; + + return offset >= end || n > end || offset >= end - n; +} -- cgit v1.2.3