This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm-ffi.git


The following commit(s) were added to refs/heads/main by this push:
     new a8f05405 [FIX] Prevent integer overflow in GetDataSize on 32-bit 
platforms (#475)
a8f05405 is described below

commit a8f0540556794d39a7e260977948c6719a7648e2
Author: Gabe Guralnick <[email protected]>
AuthorDate: Wed Feb 25 14:19:12 2026 -0800

    [FIX] Prevent integer overflow in GetDataSize on 32-bit platforms (#475)
    
    On 32-bit platforms (e.g. WASM), the intermediate multiplication `numel
    * dtype.bits * dtype.lanes` in `GetDataSize()` can overflow `size_t` for
    large tensors, even when the final byte count (after dividing by 8) fits
    within 32 bits.
    
    This PR casts the intermediate computation to `uint64_t` to avoid the
    overflow, then casts the result back to `size_t`.
    
    I was experiencing this error when trying to run a model with webllm
    using a runtime compiled by mlc-llm - the operation was overflowing,
    causing the allocation of 0-byte buffers.
    
    Co-authored-by: Gabriel Guralnick <[email protected]>
---
 include/tvm/ffi/container/tensor.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/tvm/ffi/container/tensor.h 
b/include/tvm/ffi/container/tensor.h
index eb6c9fe5..b9c6ec89 100644
--- a/include/tvm/ffi/container/tensor.h
+++ b/include/tvm/ffi/container/tensor.h
@@ -103,7 +103,8 @@ inline size_t GetDataSize(size_t numel, DLDataType dtype) {
     return numel;
   }
   // for other sub-byte types, packing is preferred
-  return (numel * dtype.bits * dtype.lanes + 7) / 8;
+  // Use uint64_t to avoid overflow on 32-bit platforms (WASM) for large 
allocations.
+  return static_cast<size_t>((static_cast<uint64_t>(numel) * dtype.bits * 
dtype.lanes + 7) / 8);
 }
 
 /*!

Reply via email to