PR #124930
tensorflow/tensorflow · XLA · Merged · 2 files changed
In XLA's TensorArrayScatterOp, a missing rank >= 1 invariant check prior to RemoveDim(0) allowed rank-0 scalar tensors to trigger out-of-bounds shape indexing and fatal segmentation faults during compilation. This contribution introduces an explicit compile-time OP_REQUIRES guard and an end-to-end XLA regression test.
Checkout Pull Request
Inspect the full commit history and diff locally from upstream tensorflow/tensorflow.
$gh pr checkout 124930 --repo tensorflow/tensorflowIn XLA's TensorArrayScatterOp, a missing rank >= 1 invariant check prior to RemoveDim(0) allowed rank-0 scalar tensors to trigger out-of-bounds shape indexing and fatal segmentation faults during compilation. This contribution introduces an explicit compile-time OP_REQUIRES guard and an end-to-end XLA regression test.
File-by-File Modification Breakdown (2)
- +14 / -0
tensorflow/compiler/tests/tensor_array_ops_test.py
Python/XLA regression test suite verifying edge-case behavior
- +4 / -0
tensorflow/compiler/tf2xla/kernels/tensor_array_ops.cc
XLA C++ kernel shape-lowering and compile-time invariant checks
Changed Files (2)
1. Introduction & Motivation
Compiled execution in TensorFlow via XLA relies on strict static shape inference during the lowering phase from TensorFlow graph nodes to HLO (High Level Optimizer) instructions [1]. Unlike eager execution, where Python runtime checks or dynamic tensor metadata can raise informative exceptions early, XLA kernel constructors operate directly on C++ TensorShape descriptors. When a kernel assumes a structural invariant that the frontend did not enforce, the compiler can dereference invalid dimension offsets and terminate with a segmentation fault rather than a diagnostic error.
In TensorArrayScatterOp, the operator scatters a batch of values into designated indices of a TensorArray. By contract, the scattered input tensor must have at least one leading dimension corresponding to the index axis, with the remaining dimensions matching the element shape of the array. However, the XLA kernel omitted a rank check before stripping axis 0, leaving the compiler vulnerable whenever a user passed a rank-0 scalar tensor.
2. Root-Cause Mechanics & Shape Formulation
Let denote the input tensor with shape tuple , where is the tensor rank. Scattering elements along axis requires decomposing into slice elements whose shape is obtained by projecting away the first coordinate:
When a rank-0 scalar is supplied (), the projection interval in Equation (1) is ill-formed. Calling value_shape.RemoveDim(0) attempts to erase a non-existent dimension from an empty dimension vector, causing undefined memory behavior inside XLA lowering.
| Input Rank | Before Fix | After Fix |
|---|---|---|
| (scalar) | Out-of-bounds RemoveDim(0) segfault | Clean InvalidArgumentError |
| (tensor) | Lowers slice shape | Unchanged XLA lowering |
3. Implementation & Code Walkthrough
In tensorflow/compiler/tf2xla/kernels/tensor_array_ops.cc, I inserted an explicit OP_REQUIRES precondition immediately after retrieving ctx->InputShape(2) and before acquiring the XlaResource or invoking RemoveDim(0):
4. Empirical Verification
In tensorflow/compiler/tests/tensor_array_ops_test.py, I authored testTensorArrayScatterScalarRaisesError, which constructs a scalar placeholder, lowers ta.scatter through xla.compile, and verifies that TensorFlow cleanly raises InvalidArgumentError instead of crashing the compiler process.
5. References
- XLA Team, XLA: Optimizing Compiler for Machine Learning (Shape & HLO Semantics), TensorFlow Architecture Documentation [2].
- Anandhu P Shaji, Hunting Compiler Bugs and Numerical Traps in TensorFlow,
anandhupshaji.me/posts/tensorflow-stories.html#xla-compiler-segfault.