Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
39 changes: 38 additions & 1 deletion src/wasm-interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -2075,7 +2075,44 @@ class ExpressionRunner : public OverriddenVisitor<SubType, Flow> {
return Flow(NONCONSTANT_FLOW);
}
Flow visitStringSliceWTF(StringSliceWTF* curr) {
return Flow(NONCONSTANT_FLOW);
// For now we only support JS-style strings.
if (curr->op != StringSliceWTF16) {
return Flow(NONCONSTANT_FLOW);
}

Flow ref = visit(curr->ref);
if (ref.breaking()) {
return ref;
}
Flow start = visit(curr->start);
if (start.breaking()) {
return start;
}
Flow end = visit(curr->end);
if (end.breaking()) {
return end;
}

auto refData = ref.getSingleValue().getGCData();
if (!refData) {
trap("null ref");
}
auto& refValues = refData->values;
auto startVal = start.getSingleValue().getUnsigned();
auto endVal = end.getSingleValue().getUnsigned();
if (endVal > refValues.size()) {
trap("array oob");
}
Literals contents;
if (endVal > startVal) {
contents.reserve(endVal - startVal);
for (size_t i = startVal; i < endVal; i++) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check that the string is ascii up to endVal before we execute the slice.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed.

if (i < refValues.size()) {
contents.push_back(refValues[i]);
}
}
}
return makeGCData(contents, curr->type);
}
Flow visitStringSliceIter(StringSliceIter* curr) {
return Flow(NONCONSTANT_FLOW);
Expand Down
15 changes: 15 additions & 0 deletions test/lit/exec/strings.wast
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,17 @@
)
)
)

;; CHECK: [fuzz-exec] calling slice
;; CHECK-NEXT: [fuzz-exec] note result: slice => string("def")
(func $slice (export "slice") (result (ref string))
;; Slicing [3:6] here should definitely output "def".
(stringview_wtf16.slice
(string.const "abcdefgh")
(i32.const 3)
(i32.const 6)
)
)
)
;; CHECK: [fuzz-exec] calling new_wtf16_array
;; CHECK-NEXT: [fuzz-exec] note result: new_wtf16_array => string("ello")
Expand Down Expand Up @@ -309,6 +320,9 @@
;; CHECK-NEXT: [LoggingExternalInterface logging 98]
;; CHECK-NEXT: [LoggingExternalInterface logging 99]
;; CHECK-NEXT: [LoggingExternalInterface logging 0]

;; CHECK: [fuzz-exec] calling slice
;; CHECK-NEXT: [fuzz-exec] note result: slice => string("def")
;; CHECK-NEXT: [fuzz-exec] comparing compare.1
;; CHECK-NEXT: [fuzz-exec] comparing compare.10
;; CHECK-NEXT: [fuzz-exec] comparing compare.2
Expand All @@ -329,3 +343,4 @@
;; CHECK-NEXT: [fuzz-exec] comparing get_codeunit
;; CHECK-NEXT: [fuzz-exec] comparing get_length
;; CHECK-NEXT: [fuzz-exec] comparing new_wtf16_array
;; CHECK-NEXT: [fuzz-exec] comparing slice