[XLA] Heap corruption when tf.slice gets out-of-range begin/size computed at runtime
Issue type
Bug
Have you reproduced the bug with TensorFlow Nightly?
Yes
Source
source
TensorFlow version
2.21.0 and tf-nightly 2.23.0-dev20260930 (CPU)
Custom code
Yes
OS platform and distribution
No response
Mobile device
No response
Python version
No response
Bazel version
No response
GCC/compiler version
No response
CUDA/cuDNN version
No response
GPU model and memory
No response
Current behavior?
tf.slice(v, v, v) on a length-1 tensor v = [19] is out of range. Eager raises InvalidArgumentError: Expected begin[0] in [0, 1], but got 19. Under jit_compile=True, when begin/size come from a runtime computation, the process aborts with glibc heap corruption. The exact message varies: free(): corrupted unsorted chunks, malloc(): unsorted double linked list corrupted, free(): invalid next size (fast). It crashed in 6 of 6 runs across both versions. Usually XLA returns (0,) and the process then aborts; sometimes it aborts before printing.
The uint8 cast matters. With w as int32 (or begin/size passed as constants), XLA either raises the eager error or returns an empty tensor. With the cast, the inferred upper bound for the slice size is large enough for the out-of-range runtime size to write past the buffer.
This is related to #110789 (heap corruption from tf.slice on a dynamically-shaped input), but it still happens in a nightly that includes the fix for #110789 (PR #126676).
Expected: XLA should raise an error (or at least clamp safely). It should never corrupt memory.
Standalone code to reproduce the issue
import tensorflow as tf
def f(m, w):
v = tf.linalg.matvec(m, tf.cast(w, tf.int32)) # runtime value [19], shape [1]
return tf.slice(v, v, v) # begin=19, size=19 on a length-1 tensor
m = tf.constant([[1, 5]])
w = tf.constant([4, 3], dtype=tf.uint8)
try:
print("eager", f(m, w).shape)
except Exception as e:
print("eager", type(e).__name__, str(e).splitlines()[0][:100])
r = tf.function(f, jit_compile=True)(m, w)
print("XLA", tuple(r.shape))Relevant log output
eager InvalidArgumentError {{function_node __wrapped__Slice_device_/job:localhost/replica:0/task:0/device:CPU:0}} Expected begin[0] in [0, 1], but got 19 ...
XLA (0,)
malloc(): unsorted double linked list corrupted
Aborted (core dumped)Source: tensorflow/tensorflow