Skip to content

Commit 50b648f

Browse files
committed
fix(uninitialized): a stopped callee in the same file counts as a declaration
A summary from a dataflow stopped before its fixpoint was absent for its effects only when imported. In the same file its callers still applied it: a write downgraded to an unknown write left the local uninitialized, so a later read was reported, and a parameter without effect read as "no effect". Treat it as absent there too, as the cross-TU design prescribes (§4.1): the call gets the presumption for declarations, not the fallback for defined functions without a summary. The caller stays incomplete. Fixes #168
1 parent 0baa830 commit 50b648f

1 file changed

Lines changed: 34 additions & 34 deletions

File tree

‎src/analysis/UninitializedVarAnalysis.cpp‎

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,9 +1897,9 @@ namespace ctrace::stack::analysis
18971897
};
18981898

18991899
static std::optional<ExternalReadSinkSignature>
1900-
resolveExternalReadSinkSignature(const llvm::Function* callee)
1900+
resolveExternalReadSinkSignature(const llvm::Function* callee, bool asDeclaration)
19011901
{
1902-
if (!callee || !callee->isDeclaration() || callee->isIntrinsic())
1902+
if (!callee || !asDeclaration || callee->isIntrinsic())
19031903
return std::nullopt;
19041904

19051905
const llvm::StringRef calleeName = canonicalExternalCalleeName(callee->getName());
@@ -1921,10 +1921,11 @@ namespace ctrace::stack::analysis
19211921
};
19221922

19231923
static std::optional<ExternalReadSinkSpec>
1924-
resolveExternalReadSinkSpec(const llvm::CallBase& CB, const llvm::Function* callee)
1924+
resolveExternalReadSinkSpec(const llvm::CallBase& CB, const llvm::Function* callee,
1925+
bool asDeclaration)
19251926
{
19261927
const std::optional<ExternalReadSinkSignature> signature =
1927-
resolveExternalReadSinkSignature(callee);
1928+
resolveExternalReadSinkSignature(callee, asDeclaration);
19281929
if (!signature)
19291930
return std::nullopt;
19301931

@@ -2098,9 +2099,10 @@ namespace ctrace::stack::analysis
20982099
}
20992100

21002101
static bool declarationCallArgMayWriteThrough(const llvm::CallBase& CB,
2101-
const llvm::Function* callee, unsigned argIdx)
2102+
const llvm::Function* callee, unsigned argIdx,
2103+
bool asDeclaration)
21022104
{
2103-
if (!callee || !callee->isDeclaration() || callee->isIntrinsic())
2105+
if (!callee || !asDeclaration || callee->isIntrinsic())
21042106
return false;
21052107
if (argIdx >= CB.arg_size())
21062108
return false;
@@ -2113,7 +2115,7 @@ namespace ctrace::stack::analysis
21132115

21142116
// Known output sinks (write/send/fwrite families) consume this pointer as read-only.
21152117
if (const std::optional<ExternalReadSinkSignature> sink =
2116-
resolveExternalReadSinkSignature(callee))
2118+
resolveExternalReadSinkSignature(callee, asDeclaration))
21172119
{
21182120
if (argIdx == sink->pointerArgIndex)
21192121
return false;
@@ -2176,9 +2178,9 @@ namespace ctrace::stack::analysis
21762178
const llvm::CallBase& CB, const llvm::Function* callee,
21772179
const TrackedObjectContext& tracked, const llvm::DataLayout& DL,
21782180
InitRangeState& initialized, llvm::BitVector* writeSeen,
2179-
FunctionSummary* currentSummary)
2181+
FunctionSummary* currentSummary, bool asDeclaration)
21802182
{
2181-
if (!callee || !callee->isDeclaration())
2183+
if (!callee || !asDeclaration)
21822184
return false;
21832185

21842186
const llvm::StringRef calleeName = normalizeDeclarationCalleeName(callee->getName());
@@ -2220,26 +2222,24 @@ namespace ctrace::stack::analysis
22202222
return true;
22212223
}
22222224

2223-
static void applyExternalDeclarationCallWriteEffects(const llvm::CallBase& CB,
2224-
const llvm::Function* callee,
2225-
const TrackedObjectContext& tracked,
2226-
const llvm::DataLayout& DL,
2227-
InitRangeState& initialized,
2228-
llvm::BitVector* writeSeen,
2229-
FunctionSummary* currentSummary)
2225+
static void applyExternalDeclarationCallWriteEffects(
2226+
const llvm::CallBase& CB, const llvm::Function* callee,
2227+
const TrackedObjectContext& tracked, const llvm::DataLayout& DL,
2228+
InitRangeState& initialized, llvm::BitVector* writeSeen,
2229+
FunctionSummary* currentSummary, bool asDeclaration)
22302230
{
2231-
if (!callee || !callee->isDeclaration())
2231+
if (!callee || !asDeclaration)
22322232
return;
22332233

22342234
if (tryApplyBoundedMemTransferDeclarationWriteEffects(
2235-
CB, callee, tracked, DL, initialized, writeSeen, currentSummary))
2235+
CB, callee, tracked, DL, initialized, writeSeen, currentSummary, asDeclaration))
22362236
{
22372237
return;
22382238
}
22392239

22402240
for (unsigned argIdx = 0; argIdx < CB.arg_size(); ++argIdx)
22412241
{
2242-
if (!declarationCallArgMayWriteThrough(CB, callee, argIdx))
2242+
if (!declarationCallArgMayWriteThrough(CB, callee, argIdx, asDeclaration))
22432243
continue;
22442244

22452245
const llvm::Value* ptrOperand = CB.getArgOperand(argIdx);
@@ -2254,10 +2254,10 @@ namespace ctrace::stack::analysis
22542254
const TrackedObjectContext& tracked, const llvm::DataLayout& DL,
22552255
const InitRangeState& initialized, llvm::BitVector* readBeforeInitSeen,
22562256
FunctionSummary* currentSummary,
2257-
std::vector<UninitializedLocalReadIssue>* emittedIssues)
2257+
std::vector<UninitializedLocalReadIssue>* emittedIssues, bool asDeclaration)
22582258
{
22592259
const std::optional<ExternalReadSinkSpec> sink =
2260-
resolveExternalReadSinkSpec(CB, callee);
2260+
resolveExternalReadSinkSpec(CB, callee, asDeclaration);
22612261
if (!sink)
22622262
return;
22632263
if (sink->pointerArgIndex >= CB.arg_size())
@@ -3142,7 +3142,6 @@ namespace ctrace::stack::analysis
31423142

31433143
const llvm::Function* callee = directCallee(*CB);
31443144
const FunctionSummary* calleeSummary = nullptr;
3145-
bool imported = false;
31463145
if (callee)
31473146
{
31483147
auto itSummary = summaries.find(callee);
@@ -3163,21 +3162,19 @@ namespace ctrace::stack::analysis
31633162
symbolName ? externalSummariesByName->find(*symbolName)
31643163
: externalSummariesByName->find(linkerSymbolName(*callee));
31653164
if (itExternal != externalSummariesByName->end())
3166-
{
31673165
calleeSummary = &itExternal->second;
3168-
imported = true;
3169-
}
31703166
}
31713167
}
3168+
bool asDeclaration = callee && callee->isDeclaration();
31723169
if (calleeSummary && !calleeSummary->complete)
31733170
{
3174-
// The caller is no more complete than what it used. Imported, an incomplete
3175-
// summary counts as absent for its effects: the call gets the presumption for
3176-
// declarations, exactly as an unknown function (#157).
3171+
// The caller is no more complete than what it used. Imported or defined in this
3172+
// module, an incomplete summary counts as absent for its effects: the call gets
3173+
// the presumption for declarations, exactly as an unknown function (#157, #168).
31773174
if (currentSummary)
31783175
currentSummary->complete = false;
3179-
if (imported)
3180-
calleeSummary = nullptr;
3176+
calleeSummary = nullptr;
3177+
asDeclaration = true;
31813178
}
31823179
const bool hasSummary = (calleeSummary != nullptr);
31833180
if (!hasSummary)
@@ -3186,11 +3183,14 @@ namespace ctrace::stack::analysis
31863183
constructedSeen, defaultCtorSeen, currentSummary);
31873184
applyExternalDeclarationCallReadEffects(*CB, callee, tracked, DL, initialized,
31883185
readBeforeInitSeen, currentSummary,
3189-
emittedIssues);
3186+
emittedIssues, asDeclaration);
31903187
applyExternalDeclarationCallWriteEffects(*CB, callee, tracked, DL, initialized,
3191-
writeSeen, currentSummary);
3192-
applyUnsummarizedDefinedCallWriteEffects(*CB, callee, tracked, DL, initialized,
3193-
writeSeen, currentSummary);
3188+
writeSeen, currentSummary, asDeclaration);
3189+
if (!asDeclaration)
3190+
{
3191+
applyUnsummarizedDefinedCallWriteEffects(*CB, callee, tracked, DL, initialized,
3192+
writeSeen, currentSummary);
3193+
}
31943194
}
31953195
if (!callee)
31963196
return;

0 commit comments

Comments
 (0)