Skip to content

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

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 5 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 12 additions & 3 deletions src/wasm-interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4022,16 +4022,25 @@ 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()) {

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"

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");
}
if (droppedDataSegments.count(curr->segment) && end > 0) {
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];
contents.push_back(this->makeFromMemory(addr, element));
}

#pragma GCC diagnostic pop

return self()->makeGCData(std::move(contents), curr->type);
}
Flow visitArrayNewElem(ArrayNewElem* curr) {
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