Skip to content

Commit 5e3f615

Browse files
authored
[lldb/NativePDB] Don't create parentless blocks (#117581)
In case of an error GetBlock would return a reference to a Block without adding it to a parent. This doesn't seem like a good idea, and none of the other plugins do that. This patch fixes that by propagating errors (well, null pointers...) up the stack. I don't know of any specific problem that this solves, but given that this occurs only when something goes very wrong (e.g. a corrupted PDB file), it's quite possible noone has run into this situation, so we can't say the code is correct either. It also gets in the way of a refactor I'm contemplating.
1 parent 90f5c8b commit 5e3f615

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -379,18 +379,17 @@ uint32_t SymbolFileNativePDB::CalculateNumCompileUnits() {
379379
return count;
380380
}
381381

382-
Block &SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) {
382+
Block *SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) {
383383
CompilandIndexItem *cii = m_index->compilands().GetCompiland(block_id.modi);
384384
CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(block_id.offset);
385385
CompUnitSP comp_unit = GetOrCreateCompileUnit(*cii);
386386
lldb::user_id_t opaque_block_uid = toOpaqueUid(block_id);
387-
BlockSP child_block = std::make_shared<Block>(opaque_block_uid);
388387
auto ts_or_err = GetTypeSystemForLanguage(comp_unit->GetLanguage());
389388
if (auto err = ts_or_err.takeError())
390-
return *child_block;
389+
return nullptr;
391390
auto ts = *ts_or_err;
392391
if (!ts)
393-
return *child_block;
392+
return nullptr;
394393
PdbAstBuilder* ast_builder = ts->GetNativePDBParser();
395394

396395
switch (sym.kind()) {
@@ -403,7 +402,7 @@ Block &SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) {
403402
Block &block = func->GetBlock(false);
404403
if (block.GetNumRanges() == 0)
405404
block.AddRange(Block::Range(0, func->GetAddressRange().GetByteSize()));
406-
return block;
405+
return &block;
407406
}
408407
break;
409408
}
@@ -416,13 +415,16 @@ Block &SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) {
416415
cantFail(SymbolDeserializer::deserializeAs<BlockSym>(sym, block));
417416
lldbassert(block.Parent != 0);
418417
PdbCompilandSymId parent_id(block_id.modi, block.Parent);
419-
Block &parent_block = GetOrCreateBlock(parent_id);
420-
Function *func = parent_block.CalculateSymbolContextFunction();
418+
Block *parent_block = GetOrCreateBlock(parent_id);
419+
if (!parent_block)
420+
return nullptr;
421+
Function *func = parent_block->CalculateSymbolContextFunction();
421422
lldbassert(func);
422423
lldb::addr_t block_base =
423424
m_index->MakeVirtualAddress(block.Segment, block.CodeOffset);
424425
lldb::addr_t func_base =
425426
func->GetAddressRange().GetBaseAddress().GetFileAddress();
427+
BlockSP child_block = std::make_shared<Block>(opaque_block_uid);
426428
if (block_base >= func_base)
427429
child_block->AddRange(Block::Range(block_base - func_base, block.CodeSize));
428430
else {
@@ -435,7 +437,7 @@ Block &SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) {
435437
block_id.modi, block_id.offset, block_base,
436438
block_base + block.CodeSize, func_base);
437439
}
438-
parent_block.AddChild(child_block);
440+
parent_block->AddChild(child_block);
439441
ast_builder->GetOrCreateBlockDecl(block_id);
440442
m_blocks.insert({opaque_block_uid, child_block});
441443
break;
@@ -445,8 +447,11 @@ Block &SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) {
445447
comp_unit->GetLineTable();
446448

447449
std::shared_ptr<InlineSite> inline_site = m_inline_sites[opaque_block_uid];
448-
Block &parent_block = GetOrCreateBlock(inline_site->parent_id);
449-
parent_block.AddChild(child_block);
450+
Block *parent_block = GetOrCreateBlock(inline_site->parent_id);
451+
if (!parent_block)
452+
return nullptr;
453+
BlockSP child_block = std::make_shared<Block>(opaque_block_uid);
454+
parent_block->AddChild(child_block);
450455
ast_builder->GetOrCreateInlinedFunctionDecl(block_id);
451456
// Copy ranges from InlineSite to Block.
452457
for (size_t i = 0; i < inline_site->ranges.GetSize(); ++i) {
@@ -469,7 +474,7 @@ Block &SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) {
469474
lldbassert(false && "Symbol is not a block!");
470475
}
471476

472-
return *child_block;
477+
return nullptr;
473478
}
474479

475480
lldb::FunctionSP SymbolFileNativePDB::CreateFunction(PdbCompilandSymId func_id,
@@ -997,10 +1002,10 @@ SymbolFileNativePDB::GetOrCreateCompileUnit(const CompilandIndexItem &cci) {
9971002
return emplace_result.first->second;
9981003
}
9991004

1000-
Block &SymbolFileNativePDB::GetOrCreateBlock(PdbCompilandSymId block_id) {
1005+
Block *SymbolFileNativePDB::GetOrCreateBlock(PdbCompilandSymId block_id) {
10011006
auto iter = m_blocks.find(toOpaqueUid(block_id));
10021007
if (iter != m_blocks.end())
1003-
return *iter->second;
1008+
return iter->second.get();
10041009

10051010
return CreateBlock(block_id);
10061011
}
@@ -1124,14 +1129,16 @@ uint32_t SymbolFileNativePDB::ResolveSymbolContext(
11241129
}
11251130

11261131
if (type == PDB_SymType::Block) {
1127-
Block &block = GetOrCreateBlock(csid);
1128-
sc.function = block.CalculateSymbolContextFunction();
1132+
Block *block = GetOrCreateBlock(csid);
1133+
if (!block)
1134+
continue;
1135+
sc.function = block->CalculateSymbolContextFunction();
11291136
if (sc.function) {
11301137
sc.function->GetBlock(true);
11311138
addr_t func_base =
11321139
sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
11331140
addr_t offset = file_addr - func_base;
1134-
sc.block = block.FindInnermostBlockByOffset(offset);
1141+
sc.block = block->FindInnermostBlockByOffset(offset);
11351142
}
11361143
}
11371144
if (sc.function)
@@ -1837,12 +1844,16 @@ VariableSP SymbolFileNativePDB::CreateLocalVariable(PdbCompilandSymId scope_id,
18371844
PdbCompilandSymId var_id,
18381845
bool is_param) {
18391846
ModuleSP module = GetObjectFile()->GetModule();
1840-
Block &block = GetOrCreateBlock(scope_id);
1847+
Block *block = GetOrCreateBlock(scope_id);
1848+
if (!block)
1849+
return nullptr;
1850+
18411851
// Get function block.
1842-
Block *func_block = &block;
1852+
Block *func_block = block;
18431853
while (func_block->GetParent()) {
18441854
func_block = func_block->GetParent();
18451855
}
1856+
18461857
Address addr;
18471858
func_block->GetStartAddress(addr);
18481859
VariableInfo var_info =
@@ -1875,8 +1886,8 @@ VariableSP SymbolFileNativePDB::CreateLocalVariable(PdbCompilandSymId scope_id,
18751886
bool static_member = false;
18761887
Variable::RangeList scope_ranges;
18771888
VariableSP var_sp = std::make_shared<Variable>(
1878-
toOpaqueUid(var_id), name.c_str(), name.c_str(), sftype, var_scope,
1879-
&block, scope_ranges, &decl, var_info.location, external, artificial,
1889+
toOpaqueUid(var_id), name.c_str(), name.c_str(), sftype, var_scope, block,
1890+
scope_ranges, &decl, var_info.location, external, artificial,
18801891
location_is_constant_data, static_member);
18811892
if (!is_param) {
18821893
auto ts_or_err = GetTypeSystemForLanguage(comp_unit_sp->GetLanguage());
@@ -1935,7 +1946,9 @@ TypeSP SymbolFileNativePDB::GetOrCreateTypedef(PdbGlobalSymId id) {
19351946
}
19361947

19371948
size_t SymbolFileNativePDB::ParseVariablesForBlock(PdbCompilandSymId block_id) {
1938-
Block &block = GetOrCreateBlock(block_id);
1949+
Block *block = GetOrCreateBlock(block_id);
1950+
if (!block)
1951+
return 0;
19391952

19401953
size_t count = 0;
19411954

@@ -1977,10 +1990,10 @@ size_t SymbolFileNativePDB::ParseVariablesForBlock(PdbCompilandSymId block_id) {
19771990
return 0;
19781991
}
19791992

1980-
VariableListSP variables = block.GetBlockVariableList(false);
1993+
VariableListSP variables = block->GetBlockVariableList(false);
19811994
if (!variables) {
19821995
variables = std::make_shared<VariableList>();
1983-
block.SetVariableList(variables);
1996+
block->SetVariableList(variables);
19841997
}
19851998

19861999
CVSymbolArray syms = limitSymbolArrayToScope(
@@ -2027,7 +2040,7 @@ size_t SymbolFileNativePDB::ParseVariablesForBlock(PdbCompilandSymId block_id) {
20272040

20282041
// Pass false for set_children, since we call this recursively so that the
20292042
// children will call this for themselves.
2030-
block.SetDidParseVariables(true, false);
2043+
block->SetDidParseVariables(true, false);
20312044

20322045
return count;
20332046
}

lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,15 @@ class SymbolFileNativePDB : public SymbolFileCommon {
226226
lldb::TypeSP GetOrCreateType(PdbTypeSymId type_id);
227227
lldb::TypeSP GetOrCreateType(llvm::codeview::TypeIndex ti);
228228
lldb::VariableSP GetOrCreateGlobalVariable(PdbGlobalSymId var_id);
229-
Block &GetOrCreateBlock(PdbCompilandSymId block_id);
229+
Block *GetOrCreateBlock(PdbCompilandSymId block_id);
230230
lldb::VariableSP GetOrCreateLocalVariable(PdbCompilandSymId scope_id,
231231
PdbCompilandSymId var_id,
232232
bool is_param);
233233
lldb::TypeSP GetOrCreateTypedef(PdbGlobalSymId id);
234234

235235
lldb::FunctionSP CreateFunction(PdbCompilandSymId func_id,
236236
CompileUnit &comp_unit);
237-
Block &CreateBlock(PdbCompilandSymId block_id);
237+
Block *CreateBlock(PdbCompilandSymId block_id);
238238
lldb::VariableSP CreateLocalVariable(PdbCompilandSymId scope_id,
239239
PdbCompilandSymId var_id, bool is_param);
240240
lldb::TypeSP CreateTypedef(PdbGlobalSymId id);

0 commit comments

Comments
 (0)