Skip to content

Commit 1fc3ee6

Browse files
author
Github Executorch
committed
Use safe_numel() in wasm bindings
The wasm bindings' assert_valid_numel() helper previously computed numel without overflow detection, letting a malicious tensor shape wrap to a small value that bypassed the data-size check. Replace with safe_numel() (returns Result<ssize_t>) and propagate the error via THROW_IF_ERROR as a JavaScript exception, matching the rest of the wasm error plumbing. Authored with Claude. Differential Revision: [D102082911](https://our.internmc.facebook.com/intern/diff/D102082911/) [ghstack-poisoned]
1 parent 25e8f81 commit 1fc3ee6

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

extension/wasm/wasm_bindings.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,23 @@ inline void js_array_push(val_array<T>& array, const T& value) {
8484
_(float, Float) \
8585
_(int64_t, Long)
8686

87-
inline ssize_t compute_expected_numel(
87+
inline ::executorch::runtime::Result<ssize_t> compute_expected_numel(
8888
const std::vector<torch::executor::Tensor::SizesType>& sizes) {
89-
return executorch::aten::compute_numel(sizes.data(), sizes.size());
89+
return executorch::aten::safe_numel(sizes.data(), sizes.size());
9090
}
9191

9292
template <typename T>
9393
inline void assert_valid_numel(
9494
const std::vector<T>& data,
9595
const std::vector<torch::executor::Tensor::SizesType>& sizes) {
9696
auto computed_numel = compute_expected_numel(sizes);
97+
THROW_IF_ERROR(
98+
computed_numel.error(),
99+
"Invalid tensor sizes: numel computation failed");
97100
THROW_IF_FALSE(
98-
data.size() >= computed_numel,
101+
data.size() >= static_cast<size_t>(computed_numel.get()),
99102
"Required %ld elements, given %ld",
100-
computed_numel,
103+
computed_numel.get(),
101104
data.size());
102105
}
103106

0 commit comments

Comments
 (0)