Skip to content

Commit 2471301

Browse files
authored
[Strings] Handle overflow in string.encode_wtf16_array (#6422)
1 parent 57dc0c9 commit 2471301

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/wasm-interpreter.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "ir/module-utils.h"
3333
#include "support/bits.h"
3434
#include "support/safe_integer.h"
35+
#include "support/stdckdint.h"
3536
#include "wasm-builder.h"
3637
#include "wasm-traversal.h"
3738
#include "wasm.h"
@@ -2001,10 +2002,12 @@ class ExpressionRunner : public OverriddenVisitor<SubType, Flow> {
20012002
if (!refData || !ptrData) {
20022003
trap("null ref");
20032004
}
2004-
auto startVal = start.getSingleValue().getInteger();
2005+
auto startVal = start.getSingleValue().getUnsigned();
20052006
auto& refValues = refData->values;
20062007
auto& ptrValues = ptrData->values;
2007-
if (startVal + refValues.size() > ptrValues.size()) {
2008+
size_t end;
2009+
if (std::ckd_add<size_t>(&end, startVal, refValues.size()) ||
2010+
end > ptrValues.size()) {
20082011
trap("oob");
20092012
}
20102013

test/lit/exec/strings.wast

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,36 @@
245245
)
246246
)
247247

248+
;; CHECK: [fuzz-exec] calling encode-unsigned
249+
;; CHECK-NEXT: [trap oob]
250+
(func $encode-unsigned (export "encode-unsigned")
251+
(drop
252+
(string.encode_wtf16_array
253+
(string.const "ab")
254+
(array.new_default $array16
255+
(i32.const 28)
256+
)
257+
;; This is a huge unsigned offset, so we will trap on oob.
258+
(i32.const -2)
259+
)
260+
)
261+
)
262+
263+
;; CHECK: [fuzz-exec] calling encode-overflow
264+
;; CHECK-NEXT: [trap oob]
265+
(func $encode-overflow (export "encode-overflow")
266+
;; The string's size + the offset lead to an overflow here in the array.
267+
(drop
268+
(string.encode_wtf16_array
269+
(string.const "ab")
270+
(array.new_default $array16
271+
(i32.const 10)
272+
)
273+
(i32.const 9)
274+
)
275+
)
276+
)
277+
248278
;; CHECK: [fuzz-exec] calling slice
249279
;; CHECK-NEXT: [fuzz-exec] note result: slice => string("def")
250280
(func $slice (export "slice") (result (ref string))
@@ -332,6 +362,12 @@
332362
;; CHECK-NEXT: [LoggingExternalInterface logging 99]
333363
;; CHECK-NEXT: [LoggingExternalInterface logging 0]
334364

365+
;; CHECK: [fuzz-exec] calling encode-unsigned
366+
;; CHECK-NEXT: [trap oob]
367+
368+
;; CHECK: [fuzz-exec] calling encode-overflow
369+
;; CHECK-NEXT: [trap oob]
370+
335371
;; CHECK: [fuzz-exec] calling slice
336372
;; CHECK-NEXT: [fuzz-exec] note result: slice => string("def")
337373

@@ -349,6 +385,8 @@
349385
;; CHECK-NEXT: [fuzz-exec] comparing compare.9
350386
;; CHECK-NEXT: [fuzz-exec] comparing const
351387
;; CHECK-NEXT: [fuzz-exec] comparing encode
388+
;; CHECK-NEXT: [fuzz-exec] comparing encode-overflow
389+
;; CHECK-NEXT: [fuzz-exec] comparing encode-unsigned
352390
;; CHECK-NEXT: [fuzz-exec] comparing eq.1
353391
;; CHECK-NEXT: [fuzz-exec] comparing eq.2
354392
;; CHECK-NEXT: [fuzz-exec] comparing eq.3

0 commit comments

Comments
 (0)