Skip to content

Commit 39dfbcf

Browse files
dsandersllvmMichael137
authored andcommitted
[lldb] Fix UB cast when encountering DW_LANG_* >= eNumLanguageTypes (llvm#150132)
LanguageType has two kinds of enumerators in it. The first is DWARF-assigned enumerators which must be consecutive and match DW_LANG values. The second is the vendor-assigned enumerators which must be unique and must follow on from the DWARF-assigned values (i.e. the first one is currently eLanguageTypeMojo + 1) even if that collides with DWARF-assigned values that lldb is not yet aware of Only the DWARF-assigned enumerators may be static_cast from DW_LANG since their values match. The vendor-assigned enumerators must be explicitly converted since their values do not match. This needs to handle new languages added to DWARF and not yet implemented in lldb. This fixes a crash when encountering a DW_LANG value >= eNumLanguageTypes and wrong behaviour when encountering DW_LANG values that have not yet been added to LanguageType but happen to coincide with a vendor-assigned enumerator due to the consecutive values requirement described above. Another way to fix the crash is to add the language to LanguageType (and fill any preceeding gaps in the number space) so that the DW_LANG being encountered is correctly handled but this just moves the problem to a new subset of DW_LANG values. Also fix an unnecessary static-cast from LanguageType to LanguageType. (cherry picked from commit 726502d)
1 parent 43776ef commit 39dfbcf

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

lldb/include/lldb/lldb-enumerations.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ enum LanguageType {
518518
eLanguageTypeAssembly = 0x0031,
519519
eLanguageTypeC_sharp = 0x0032,
520520
eLanguageTypeMojo = 0x0033,
521+
eLanguageTypeLastStandardLanguage = eLanguageTypeMojo,
521522

522523
// Vendor Extensions
523524
// Note: Language::GetNameForLanguageType

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4637,13 +4637,16 @@ SymbolFileDWARF::GetContainingDeclContext(const DWARFDIE &die) {
46374637
}
46384638

46394639
LanguageType SymbolFileDWARF::LanguageTypeFromDWARF(uint64_t val) {
4640+
if (val <= eLanguageTypeLastStandardLanguage)
4641+
return static_cast<LanguageType>(val);
4642+
46404643
// Note: user languages between lo_user and hi_user must be handled
46414644
// explicitly here.
46424645
switch (val) {
46434646
case DW_LANG_Mips_Assembler:
46444647
return eLanguageTypeMipsAssembler;
46454648
default:
4646-
return static_cast<LanguageType>(val);
4649+
return eLanguageTypeUnknown;
46474650
}
46484651
}
46494652

lldb/source/Target/Language.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ static uint32_t num_languages =
259259
LanguageType Language::GetLanguageTypeFromString(llvm::StringRef string) {
260260
for (const auto &L : language_names) {
261261
if (string.equals_insensitive(L.name))
262-
return static_cast<LanguageType>(L.type);
262+
return L.type;
263263
}
264264

265265
return eLanguageTypeUnknown;

0 commit comments

Comments
 (0)