diff --git a/llvm/include/llvm/MC/MCContext.h b/llvm/include/llvm/MC/MCContext.h index e74e592b41d863d062f320406753495ff2fc57ff..b42b1d6a397c860976fa980bf3c8e2a01ef70185 100644 --- a/llvm/include/llvm/MC/MCContext.h +++ b/llvm/include/llvm/MC/MCContext.h @@ -104,10 +104,10 @@ namespace llvm { DenseMap<unsigned, MCLabel *> Instances; /// NextInstance() creates the next instance of the directional local label /// for the LocalLabelVal and adds it to the map if needed. - unsigned NextInstance(unsigned LocalLabelVal); + unsigned NextInstance(unsigned LocalLabelVal, bool &valid); /// GetInstance() gets the current instance of the directional local label /// for the LocalLabelVal and adds it to the map if needed. - unsigned GetInstance(unsigned LocalLabelVal); + unsigned GetInstance(unsigned LocalLabelVal, bool &valid); /// The file name of the log file from the environment variable /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique @@ -275,11 +275,11 @@ namespace llvm { /// Create the definition of a directional local symbol for numbered label /// (used for "1:" definitions). - MCSymbol *createDirectionalLocalSymbol(unsigned LocalLabelVal); + MCSymbol *createDirectionalLocalSymbol(unsigned LocalLabelVal, bool &valid); /// Create and return a directional local symbol for numbered label (used /// for "1b" or 1f" references). - MCSymbol *getDirectionalLocalSymbol(unsigned LocalLabelVal, bool Before); + MCSymbol *getDirectionalLocalSymbol(unsigned LocalLabelVal, bool Before, bool &valid); /// Lookup the symbol inside with the specified \p Name. If it exists, /// return it. If not, create a forward reference and return it. diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp index 9ff66e48824684eb5f7c610de6b5cc85a1945838..dbaa5df1cd4617f04ce329bb42ad8d72e06a5848 100644 --- a/llvm/lib/MC/MCContext.cpp +++ b/llvm/lib/MC/MCContext.cpp @@ -222,14 +222,24 @@ MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) { return createTempSymbol("tmp", true, CanBeUnnamed); } -unsigned MCContext::NextInstance(unsigned LocalLabelVal) { +unsigned MCContext::NextInstance(unsigned LocalLabelVal, bool &valid) +{ + if (LocalLabelVal > Instances.size()) { + valid = false; + return 0; + } MCLabel *&Label = Instances[LocalLabelVal]; if (!Label) Label = new (*this) MCLabel(0); return Label->incInstance(); } -unsigned MCContext::GetInstance(unsigned LocalLabelVal) { +unsigned MCContext::GetInstance(unsigned LocalLabelVal, bool &valid) +{ + if (LocalLabelVal > Instances.size()) { + valid = false; + return 0; + } MCLabel *&Label = Instances[LocalLabelVal]; if (!Label) Label = new (*this) MCLabel(0); @@ -244,14 +254,22 @@ MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal, return Sym; } -MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) { - unsigned Instance = NextInstance(LocalLabelVal); +MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal, bool &valid) +{ + valid = true; + unsigned Instance = NextInstance(LocalLabelVal, valid); + if (!valid) + return nullptr; return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance); } MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal, - bool Before) { - unsigned Instance = GetInstance(LocalLabelVal); + bool Before, bool &valid) +{ + valid = true; + unsigned Instance = GetInstance(LocalLabelVal, valid); + if (!valid) + return nullptr; if (!Before) ++Instance; return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance); diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp index 3c6141d5724828c44ad8cae7c9eca5b6375c6220..90d71bdaa0cdd4741c35d36896dd5107bfd578a2 100644 --- a/llvm/lib/MC/MCParser/AsmParser.cpp +++ b/llvm/lib/MC/MCParser/AsmParser.cpp @@ -987,8 +987,11 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) IDVal = Split.first; } if (IDVal == "f" || IDVal == "b") { + bool valid; MCSymbol *Sym = - Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b"); + Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b", valid); + if (!valid) + return true; Res = MCSymbolRefExpr::create(Sym, Variant, getContext()); if (IDVal == "b" && Sym->isUndefined()) { //return Error(Loc, "invalid reference to undefined symbol"); @@ -1602,8 +1605,14 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info, return true; } Sym = getContext().getOrCreateSymbol(IDVal); - } else - Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal); + } else { + bool valid; + Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal, valid); + if (!valid) { + Info.KsError = KS_ERR_ASM_LABEL_INVALID; + return true; + } + } Sym->redefineIfPossible(); diff --git a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp index a1c46f75e3464b11ccf441d988dd0a0db3eeffc2..4c1e13c29165e60e3c53eea9834f86e2647ca992 100644 --- a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp +++ b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp @@ -1468,8 +1468,11 @@ bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) if (getLexer().getKind() == AsmToken::Identifier) { StringRef IDVal = getTok().getString(); if (IDVal == "f" || IDVal == "b") { + bool valid; MCSymbol *Sym = - getContext().getDirectionalLocalSymbol(IntVal, IDVal == "b"); + getContext().getDirectionalLocalSymbol(IntVal, IDVal == "b", valid); + if (!valid) + return true; MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; const MCExpr *Val = MCSymbolRefExpr::create(Sym, Variant, getContext());