-
Notifications
You must be signed in to change notification settings - Fork 14.1k
[analyzer] Detect leak of a stack address through output arguments 2/3 #105653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,6 +288,23 @@ void StackAddrEscapeChecker::checkPreStmt(const ReturnStmt *RS, | |
EmitStackError(C, R, RetE); | ||
} | ||
|
||
static const MemSpaceRegion *getStackOrGlobalSpaceRegion(const MemRegion *R) { | ||
assert(R); | ||
if (const auto *MemSpace = R->getMemorySpace()) { | ||
if (const auto *SSR = MemSpace->getAs<StackSpaceRegion>()) | ||
return SSR; | ||
if (const auto *GSR = MemSpace->getAs<GlobalsSpaceRegion>()) | ||
return GSR; | ||
} | ||
// If R describes a lambda capture, it will be a symbolic region | ||
// referring to a field region of another symbolic region. | ||
if (const auto *SymReg = R->getBaseRegion()->getAs<SymbolicRegion>()) { | ||
if (const auto *OriginReg = SymReg->getSymbol()->getOriginRegion()) | ||
return getStackOrGlobalSpaceRegion(OriginReg); | ||
} | ||
return nullptr; | ||
} | ||
|
||
std::optional<std::string> printReferrer(const MemRegion *Referrer) { | ||
assert(Referrer); | ||
const StringRef ReferrerMemorySpace = [](const MemSpaceRegion *Space) { | ||
|
@@ -297,20 +314,31 @@ std::optional<std::string> printReferrer(const MemRegion *Referrer) { | |
return "global"; | ||
assert(isa<StackSpaceRegion>(Space)); | ||
return "stack"; | ||
}(Referrer->getMemorySpace()); | ||
|
||
// We should really only have VarRegions here. | ||
// Anything else is really surprising, and we should get notified if such | ||
// ever happens. | ||
const auto *ReferrerVar = dyn_cast<VarRegion>(Referrer); | ||
if (!ReferrerVar) { | ||
assert(false && "We should have a VarRegion here"); | ||
return std::nullopt; // Defensively skip this one. | ||
}(getStackOrGlobalSpaceRegion(Referrer)); | ||
|
||
while (!Referrer->canPrintPretty()) { | ||
if (const auto *SymReg = dyn_cast<SymbolicRegion>(Referrer); | ||
SymReg && SymReg->getSymbol()->getOriginRegion()) { | ||
Referrer = SymReg->getSymbol()->getOriginRegion()->getBaseRegion(); | ||
} else if (isa<CXXThisRegion>(Referrer)) { | ||
// Skip members of a class, it is handled in CheckExprLifetime.cpp as | ||
// warn_bind_ref_member_to_parameter or | ||
// warn_init_ptr_member_to_parameter_addr | ||
return std::nullopt; | ||
} else { | ||
Referrer->dump(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these dumps are not actionable to end users and we should not expose this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. Here is the fix |
||
assert(false && "Unexpected referrer region type."); | ||
return std::nullopt; | ||
} | ||
} | ||
const std::string ReferrerVarName = | ||
ReferrerVar->getDecl()->getDeclName().getAsString(); | ||
assert(Referrer); | ||
assert(Referrer->canPrintPretty()); | ||
|
||
return (ReferrerMemorySpace + " variable '" + ReferrerVarName + "'").str(); | ||
std::string buf; | ||
llvm::raw_string_ostream os(buf); | ||
os << ReferrerMemorySpace << " variable "; | ||
Referrer->printPretty(os); | ||
return buf; | ||
} | ||
|
||
void StackAddrEscapeChecker::checkEndFunction(const ReturnStmt *RS, | ||
|
@@ -332,16 +360,20 @@ void StackAddrEscapeChecker::checkEndFunction(const ReturnStmt *RS, | |
/// referred by an other stack variable from different stack frame. | ||
bool checkForDanglingStackVariable(const MemRegion *Referrer, | ||
const MemRegion *Referred) { | ||
const auto *ReferrerMemSpace = | ||
Referrer->getMemorySpace()->getAs<StackSpaceRegion>(); | ||
const auto *ReferrerMemSpace = getStackOrGlobalSpaceRegion(Referrer); | ||
const auto *ReferredMemSpace = | ||
Referred->getMemorySpace()->getAs<StackSpaceRegion>(); | ||
|
||
if (!ReferrerMemSpace || !ReferredMemSpace) | ||
return false; | ||
|
||
const auto *ReferrerStackSpace = | ||
ReferrerMemSpace->getAs<StackSpaceRegion>(); | ||
if (!ReferrerStackSpace) | ||
return false; | ||
|
||
if (ReferredMemSpace->getStackFrame() == PoppedFrame && | ||
ReferrerMemSpace->getStackFrame()->isParentOf(PoppedFrame)) { | ||
ReferrerStackSpace->getStackFrame()->isParentOf(PoppedFrame)) { | ||
V.emplace_back(Referrer, Referred); | ||
return true; | ||
} | ||
|
@@ -387,7 +419,7 @@ void StackAddrEscapeChecker::checkEndFunction(const ReturnStmt *RS, | |
if (!BT_stackleak) | ||
BT_stackleak = | ||
std::make_unique<BugType>(CheckNames[CK_StackAddrEscapeChecker], | ||
"Stack address stored into global variable"); | ||
"Stack address leaks outside of stack frame"); | ||
|
||
for (const auto &P : Cb.V) { | ||
const MemRegion *Referrer = P.first->getBaseRegion(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.