[mlir] Fix a use after free when loading dependent dialects
authorBenjamin Kramer <benny.kra@googlemail.com>
Wed, 5 Apr 2023 13:35:02 +0000 (15:35 +0200)
committerBenjamin Kramer <benny.kra@googlemail.com>
Wed, 5 Apr 2023 13:44:29 +0000 (15:44 +0200)
commit74a8a1e038022fb4ca9b8e444489e910f16a9741
tree7a29a3f08b7cf9291d12cc675459f1078146dde7
parentb7f9bb7188368366cc8bff02e1e96d496a3b612c
[mlir] Fix a use after free when loading dependent dialects

The way dependent dialects are implemented is by recursively calling
loadDialect in the constructor. This means we have to reload from the
dialect table because the constructor might have rehashed that table.

The steps for loading a dialect are
  1. Insert a nullptr into loadedDialects. This indicates the dialect is
     loading
  2. Call ctor(). This recursively loads dependent dialects
  3. Insert the new dialect into the table.

We had a conflict between steps 2 and 3 here. You have to be extremely
unlucky though as rehashing is rare and operator[] does no generation
checking on DenseMap. Changing that to an iterator would've uncovered
this issue immediately.
mlir/lib/IR/MLIRContext.cpp