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

[GC] Fix trapping on array.new_data of dropped segments of offset > 0 #7124

Merged
merged 5 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 7 additions & 3 deletions src/wasm-interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4022,11 +4022,15 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {

const auto& seg = *wasm.getDataSegment(curr->segment);
auto elemBytes = element.getByteSize();
auto end = offset + size * elemBytes;
if ((size != 0ull && droppedDataSegments.count(curr->segment)) ||
end > seg.data.size()) {
uint64_t end;
if (std::ckd_add(&end, offset, size * elemBytes) || end > seg.data.size()) {
trap("out of bounds segment access in array.new_data");
}
uint64_t sum;
if (std::ckd_add(&sum, offset, size) ||
(sum > 0 && droppedDataSegments.count(curr->segment))) {
kripken marked this conversation as resolved.
Show resolved Hide resolved
trap("dropped segment access in array.new_data");
}
contents.reserve(size);
for (Index i = offset; i < end; i += elemBytes) {
auto addr = (void*)&seg.data[i];
Expand Down
21 changes: 21 additions & 0 deletions test/lit/exec/array.wast
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

(elem $passive $func)

(data $data "a")

;; CHECK: [fuzz-exec] calling func
;; CHECK-NEXT: [fuzz-exec] note result: func => 1
(func $func (export "func") (result i32)
Expand Down Expand Up @@ -98,6 +100,21 @@
(i32.const 0)
)
)

;; CHECK: [fuzz-exec] calling drop_array.new_data
;; CHECK-NEXT: [trap dropped segment access in array.new_data]
(func $drop_array.new_data (export "drop_array.new_data")
;; Dropping the data segment causes the next instruction to trap, even though
;; the size there is 0, because the offset is > 0.
(data.drop $data)
(drop
(array.new_data $array $data
(i32.const 1)
(i32.const 0)
)
)
)

)
;; CHECK: [fuzz-exec] calling func
;; CHECK-NEXT: [fuzz-exec] note result: func => 1
Expand All @@ -115,6 +132,10 @@
;; CHECK: [fuzz-exec] calling init_active_in_bounds

;; CHECK: [fuzz-exec] calling init_passive

;; CHECK: [fuzz-exec] calling drop_array.new_data
;; CHECK-NEXT: [trap dropped segment access in array.new_data]
;; CHECK-NEXT: [fuzz-exec] comparing drop_array.new_data
;; CHECK-NEXT: [fuzz-exec] comparing func
;; CHECK-NEXT: [fuzz-exec] comparing init_active
;; CHECK-NEXT: [fuzz-exec] comparing init_active_in_bounds
Expand Down
Loading