logo raviqqe's logbook

Audit of an MLIR parser fix for miscounted tensor literals

Summary

Scope and method

Severity

SeverityMeaning
CriticalArbitrary memory reads or writes from IR text
HighMemory errors or crashes from IR text under realistic use
MediumWrong results, aborts or contract violations with limited reach
LowEdge cases, panics and inaccuracies
InformationalTests, documentation and hygiene

Overview

SeverityOpenResolved
Critical00
High00
Medium00
Low21
Informational80

The defect

Results on LLVM 23.1.6 without assertions:

InputResult
dense<(1, 2)> : tensor<5xi32>Two values for five elements; elements 2 to 4 are heap bytes
dense<((1, 2), 3)> : tensor<5xi32>Three values for five elements
dense<(1, 1)> : tensor<5xi32>A valid splat, because equal values collapse while uniquing
dense<("a", "bb")> : tensor<5x!llvm.ptr>String storage; any access or printing segfaults
memref.global "private" @g : memref<5xi32> = dense<(1, 2)>The type is implicit; the operation verifies and reads heap bytes
tensor<5xi32, dense<(1, 2)> : tensor<5xi32>>A type encoding that reads heap bytes
dense<[(1, 2), (3, 4)]> : tensor<2xi32>Silently [1, 2]: wrong, but in bounds
sparse<(0, 1), 5> : tensor<4x4x4xi32>The sparse verifier reads the indices out of bounds while parsing and prints a heap value
Printing dense<(1, 2)> : tensor<5xi32>dense<[1, 2, -993741000, 117, -993709976]> : tensor<5xi32>

Builds with assertions abort on all of these with Assertion failed: (hasSameNumElementsOrSplat(type, values)), except the string literal, which parses and aborts once the printer reads it.

Melior can’t detect these attributes itself: the C API exposes no size of their stored data, and MLIR’s printer reads the same bytes before Melior is involved. Melior 0.30.0 documents that untrusted IR text must not be parsed (#1044).

The fix

   } else if (storage.size() > 1 &&
              storage.size() != static_cast<size_t>(type.getNumElements())) {
     p.emitError(loc) << "parsed " << storage.size() << " elements, but type ("
                      << type << ") expected " << type.getNumElements()
                      << " elements";
     return nullptr;
   }

The branch attaches to the existing complex check in getAttr, so it applies to every non-complex element type. The tests in mlir/test/IR/invalid-builtin-attributes.mlir cover four inputs:

The commit message to submit, which LLVM’s squash merge takes from the pull request title and description:

[mlir][AsmParser] Check element counts of non-complex tensor literals

The parser accepted a parenthesized literal for any element type and
parsed its elements as separate values, so a literal with a non-complex
element type could have a different number of values from the elements
of its type. For example, `dense<(1, 2)> : tensor<5xi32>` has two values
for five elements. Builds with assertions abort in
`DenseElementsAttr::get`, while builds without them create the
attribute, and any read of its third or later element, including by the
printer, goes out of bounds. For sparse elements such as
`sparse<(0, 1), 5> : tensor<4x4x4xi32>`, the verifier reads the indices
out of bounds while parsing.

Reject literals whose number of parsed values is neither one nor the
number of elements of their types, as the parser already does for
complex element types. The check is exactly the precondition that
`DenseElementsAttr::get` asserts for integer and floating-point
elements, so every such literal that builds with assertions accepted is
still accepted. String elements have no such assertion, so the check
newly rejects string literals whose values were truncated, collapsed
into splats or read out of bounds.

Findings

Low

L1: Complex literals of zero-element types still violate the precondition of DenseElementsAttr::get

L2: The commit message misstated the behavior of string literals and of builds without assertions

L3: Element counts that overflow 64 bits wrap in builds without assertions

Informational

I1: The check counts values but not their structure

I2: Builds without assertions reject some inputs they used to accept

I3: Every path to the check was exercised

I4: The tests fail without the fix and pass with it

I5: Diagnostics

I6: No literal in the tree is affected

I7: Upstream

I8: Effect on Melior

Changes after review

Recommendations

  1. Submit the fix with the message in the fix as the pull request title and description.
  2. Fix L3 upstream by rejecting types whose element count overflows. It is the remaining way for literal text to read out of bounds in builds without assertions.
  3. Fix L1 upstream with the change to isSplat and a test.
  4. Optionally add the tests in I4.
  5. In Melior, keep the warning about untrusted IR text, and update its example when moving to an LLVM with the fix.