This is an archive of the discontinued LLVM Phabricator instance.

[SelectionDAG] Fix use-after-free introduced in D130881
ClosedPublic

Authored by zero9178 on Oct 1 2022, 1:41 PM.

Details

Summary

The code introduced in https://reviews.llvm.org/D130881 has a bug as it may cause a use-after-free error that can be caught by ASAN.
The bug essentially boils down to iterator invalidation of DenseMap. The expression SDEI[To] = I->second; may cause SDEI to grow if To is inserted for the very first time. When that happens, all existing iterators to the map are invalidated as their backing storage has been freed. Accessing I->second is then invalid and attempts to access freed memory (as I is an iterator of SDEI).

This patch fixes that quite simply by first making a copy of I->second, and then moving into the possibly newly inserted KV of the DenseMap.

No test attached as I am not sure it is practible to test.


Downstream ASAN failure for reference:
https://github.com/zero9178/Pylir/actions/runs/3165945057/jobs/5155328897

As can nicely be seen in the stacktrace, the memor is freed by operator[] and the accessed by operator= afterwards

Diff Detail

Event Timeline

zero9178 created this revision.Oct 1 2022, 1:41 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 1 2022, 1:41 PM
zero9178 requested review of this revision.Oct 1 2022, 1:41 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 1 2022, 1:41 PM
zero9178 edited the summary of this revision. (Show Details)Oct 1 2022, 1:41 PM
melver accepted this revision.Oct 3 2022, 4:37 AM

Thanks for the fix!

This revision is now accepted and ready to land.Oct 3 2022, 4:37 AM
This revision was landed with ongoing or failed builds.Oct 3 2022, 6:11 AM
This revision was automatically updated to reflect the committed changes.