Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Teach UBSan to detect when a value with the _Nonnull type annotation assumes a null value. Call expressions, initializers, assignments, and return statements are all checked. Because _Nonnull does not affect IRGen, the new checks are disabled by default. The new driver flags are: -fsanitize=nullability-arg (_Nonnull violation in call) -fsanitize=nullability-assign (_Nonnull violation in assignment) -fsanitize=nullability-return (_Nonnull violation in return stmt) -fsanitize=nullability (all of the above) This patch builds on top of UBSan's existing support for detecting violations of the nonnull attributes ('nonnull' and 'returns_nonnull'), and relies on the compiler-rt support for those checks. Eventually we will need to update the diagnostic messages in compiler-rt (there are FIXME's for this, which will be addressed in a follow-up). One point of note is that the nullability-return check is only allowed to kick in if all arguments to the function satisfy their nullability preconditions. This makes it necessary to emit some null checks in the function body itself. Testing: check-clang and check-ubsan. I also built some Apple ObjC frameworks with an asserts-enabled compiler, and verified that we get valid reports. Differential Revision: https://reviews.llvm.org/D30762 llvm-svn: 297700
- Loading branch information
Showing
11 changed files
with
398 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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,31 @@ | ||
// REQUIRES: asserts | ||
// RUN: %clang_cc1 -x objective-c -emit-llvm -triple x86_64-apple-macosx10.10.0 -fsanitize=nullability-return,returns-nonnull-attribute,nullability-arg,nonnull-attribute %s -o - -w | FileCheck %s | ||
|
||
// If both the annotation and the attribute are present, prefer the attribute, | ||
// since it actually affects IRGen. | ||
|
||
// CHECK-LABEL: define nonnull i32* @f1 | ||
__attribute__((returns_nonnull)) int *_Nonnull f1(int *_Nonnull p) { | ||
// CHECK: entry: | ||
// CHECK-NEXT: [[ADDR:%.*]] = alloca i32* | ||
// CHECK-NEXT: store i32* [[P:%.*]], i32** [[ADDR]] | ||
// CHECK-NEXT: [[ARG:%.*]] = load i32*, i32** [[ADDR]] | ||
// CHECK-NEXT: [[ICMP:%.*]] = icmp ne i32* [[ARG]], null, !nosanitize | ||
// CHECK-NEXT: br i1 [[ICMP]], label %[[CONT:.+]], label %[[HANDLE:[^,]+]] | ||
// CHECK: [[HANDLE]]: | ||
// CHECK-NEXT: call void @__ubsan_handle_nonnull_return_abort | ||
// CHECK-NEXT: unreachable, !nosanitize | ||
// CHECK: [[CONT]]: | ||
// CHECK-NEXT: ret i32* | ||
return p; | ||
} | ||
|
||
// CHECK-LABEL: define void @f2 | ||
void f2(int *_Nonnull __attribute__((nonnull)) p) {} | ||
|
||
// CHECK-LABEL: define void @call_f2 | ||
void call_f2() { | ||
// CHECK: call void @__ubsan_handle_nonnull_arg_abort | ||
// CHECK-NOT: call void @__ubsan_handle_nonnull_arg_abort | ||
f2((void *)0); | ||
} |
Oops, something went wrong.