[mlir] Fix undefined behavior in Linalg utils getViewSizes
authorAlex Zinenko <zinenko@google.com>
Tue, 21 Jul 2020 07:53:28 +0000 (09:53 +0200)
committerAlex Zinenko <zinenko@google.com>
Tue, 21 Jul 2020 07:57:41 +0000 (09:57 +0200)
commitaa84e6e579bedffda43f8ebffa553cf277947e96
tree2c3785091f44360dcf92ff653bb6bdd5683139d0
parent05d3160c9c4f9fa651f865a679750c63d23602fd
[mlir] Fix undefined behavior in Linalg utils getViewSizes

The utility function getViewSizes in Linalg has been recently updated to
support a different form of Linalg operations. In doing so, the code looking
like `smallvector.push_back(smallvector[i])` was introduced. Unlike std
vectors, this can lead to undefined behavior if the vector must grow upon
insertion: `smallvector[i]` returns a reference to the element, `push_back`
takes a const reference to the element, and then grows the vector storage
before accessing the referenced value. After the resize, the reference may
become dangling, which leads to undefined behavior detected by ASAN as
use-after-free. Work around the issue by forcing the value to be copied by
putting it into a temporary variable.
mlir/include/mlir/Dialect/Linalg/Utils/Utils.h