aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CGCall.cpp
diff options
context:
space:
mode:
authorVitaly Buka <vitalybuka@google.com>2022-12-04 18:59:01 -0800
committerVitaly Buka <vitalybuka@google.com>2022-12-05 22:58:29 -0800
commit166c8cccde010ea01fdc20945a2a25fa1971cb73 (patch)
treedb6cd6c2a712794004db08150ec3e10cc0166b71 /clang/lib/CodeGen/CGCall.cpp
parentbc1bdfd6f8122f20c46732fff25216bfe9bfd03b (diff)
downloadllvm-166c8cccde010ea01fdc20945a2a25fa1971cb73.zip
llvm-166c8cccde010ea01fdc20945a2a25fa1971cb73.tar.gz
llvm-166c8cccde010ea01fdc20945a2a25fa1971cb73.tar.bz2
[msan][CodeGen] Set noundef for C return value
Msan needs noundef consistency between interface and implementation. If we call C++ from C we can have noundef on C++ side, and no noundef on caller C side, noundef implementation will not set TLS for return value, no noundef caller will expect it. Then we have false reports in msan. The workaround could be set TLS to zero even for noundef return values. However if we do that always it will increase binary size by about 10%. If we do that selectively we need to handle "address is taken" functions, any non local functions, and probably all function which have musttail callers. Which is still a lot. The existing implementation of HasStrictReturn refers to C standard as the reason not enforcing noundef. I believe it applies only to the case when return statement is omitted. Testing on Google codebase I never see such cases, however I've see tens of cases where C code returns actual uninitialized variables, but we ignore that it because of "omitted return" case. So this patch will: 1. fix false-positives with TLS missmatch. 2. detect bugs returning uninitialized variables for C as well. 3. report "omitted return" cases stricter than C, which is already a warning and very likely a bug in a code anyway. Reviewed By: kda Differential Revision: https://reviews.llvm.org/D139296
Diffstat (limited to 'clang/lib/CodeGen/CGCall.cpp')
-rw-r--r--clang/lib/CodeGen/CGCall.cpp8
1 files changed, 7 insertions, 1 deletions
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 0760687..b94bf40 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -1798,6 +1798,13 @@ bool CodeGenModule::MayDropFunctionReturn(const ASTContext &Context,
static bool HasStrictReturn(const CodeGenModule &Module, QualType RetTy,
const Decl *TargetDecl) {
+ // As-is msan can not tolerate noundef mismatch between caller and
+ // implementation. Mismatch is possible for e.g. indirect calls from C-caller
+ // into C++. Such mismatches lead to confusing false reports. To avoid
+ // expensive workaround on msan we enforce initialization event in uncommon
+ // cases where it's allowed.
+ if (Module.getLangOpts().Sanitize.has(SanitizerKind::Memory))
+ return true;
// C++ explicitly makes returning undefined values UB. C's rule only applies
// to used values, so we never mark them noundef for now.
if (!Module.getLangOpts().CPlusPlus)
@@ -1818,7 +1825,6 @@ static bool HasStrictReturn(const CodeGenModule &Module, QualType RetTy,
// Try to respect what the programmer intended.
return Module.getCodeGenOpts().StrictReturn ||
!Module.MayDropFunctionReturn(Module.getContext(), RetTy) ||
- Module.getLangOpts().Sanitize.has(SanitizerKind::Memory) ||
Module.getLangOpts().Sanitize.has(SanitizerKind::Return);
}