-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[analyzer] Fix false double free when including 3rd-party headers with overloaded delete operator as system headers #85224
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 1 commit
Commits
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
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #ifndef OVERLOADED_DELETE_IN_HEADER | ||
| #define OVERLOADED_DELETE_IN_HEADER | ||
|
|
||
| void clang_analyzer_printState(); | ||
|
|
||
| struct DeleteInHeader { | ||
| inline void operator delete(void *ptr) { | ||
| // No matter whether this header file is included as a system header file | ||
| // with -isystem or a user header file with -I, ptr should not be marked as | ||
| // released. | ||
| clang_analyzer_printState(); | ||
|
|
||
| ::operator delete(ptr); // The first place where ptr is marked as released. | ||
| } | ||
| }; | ||
|
|
||
| #endif // OVERLOADED_DELETE_IN_SYSTEM_HEADER |
25 changes: 25 additions & 0 deletions
25
clang/test/Analysis/overloaded-delete-in-system-header.cpp
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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // issue 62985 | ||
| // When 3rd-party header files are included as system headers, their overloaded | ||
| // new and delete operators are also considered as the std ones. However, those | ||
| // overloaded operator functions will also be inlined. This makes the same | ||
| // symbolic memory marked as released twice, which leads to a false uaf alarm. | ||
| // | ||
| // The first run, include as system header. False uaf report before fix. | ||
| // | ||
| // RUN: %clang_analyze_cc1 %s \ | ||
| // RUN: -analyzer-checker=core,cplusplus.NewDelete,debug.ExprInspection \ | ||
| // RUN: -isystem %S/Inputs/ 2>&1 | \ | ||
| // RUN: FileCheck %s | ||
| // | ||
| // The second run, include as user header. Should always silent. | ||
| // | ||
| // RUN: %clang_analyze_cc1 %s \ | ||
| // RUN: -analyzer-checker=core,cplusplus.NewDelete,debug.ExprInspection \ | ||
| // RUN: -I %S/Inputs/ 2>&1 | \ | ||
| // RUN: FileCheck %s | ||
|
|
||
| #include "overloaded-delete-in-header.h" | ||
|
|
||
| void deleteInHeader(DeleteInHeader *p) { delete p; } | ||
|
|
||
| // CHECK-NOT: Released |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check still has some problems.
FDis not guaranteed to be the function decl which has the body, thus consequently, the location ofFDmight not point to the definition, even if FD has a definition.FDlikely points to the last decl declaration spelling.I didn't have time to publish my extra tests demonstrating this, but I'll come back to this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But
FunctionDecl::hasBodywill traverse all re-declarations and verify whether there is a decl has a body defined. So I think it will not affect the correctness no matter which re-declaration it points to.My concern is the situation when the current TU does not have its definition but can be imported from other TUs during CTU inlining. Under this circumstance, the declaration can still pass the check here but trigger a false positive after CTU inlining.
I am not sure whether there are other scenarios like this that can also trigger this false positive. If so, how can we update the conditions for these scenarios?
The current version is just a workaround for this bug we encountered here, rather than a robust verifier for checking whether a given overload is a standard one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to move the check into
checkPostCall? Then this situation can not happen.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an excellent question! :D