X86AsmParser::ParseIntelExpression has a while loop. In the body,
calls to MCAsmLexer::UnLex can force a reallocation in the MCAsmLexer's
CurToken SmallVector, invalidating saved references to
MCAsmLexer::getTok().
const MCAsmToken &Tok is such a saved reference, and this moves it
from outside the while loop to inside the body, fixing a
use-after-realloc.
Tok will still be reused across calls to Lex(), each of which
effectively destroys and constructs the pointed-to token. I'm a bit
skeptical of this usage pattern, but it seems broadly used in the
X86AsmParser (and others) so I'm leaving it alone (for now).
Somehow this bug was exposed by https://reviews.llvm.org/D94739,
resulting in test failures in dot-operator related tests in
llvm/test/tools/llvm-ml. I suspect the exposure path is related to
optimizer changes from splitting up the grow operation, but I haven't
dug all the way in. Regardless, there are already tests in tree that
cover this; they might fail consistently if we added ASan
instrumentation to SmallVector.