-
Notifications
You must be signed in to change notification settings - Fork 80
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
Add a dynamic check for pointer arithmetic of null pointers #663
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 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 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
112 changes: 112 additions & 0 deletions
112
test/CheckedC/dynamic-checks/ptr-arithmetic-null-checks-code-gen.c
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,112 @@ | ||
// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s | ||
// RUN: %clang_cc1 %s -emit-llvm -fno-checkedc-null-ptr-checks -o - | FileCheck %s --check-prefix NOCHECK | ||
|
||
#include <stdchecked.h> | ||
#include <stddef.h> | ||
|
||
array_ptr<char> p : count(3) = "abc"; | ||
array_ptr<char> q : count(3) = "abc"; | ||
// NOCHECK-NOT: _Dynamic_check.non_null | ||
|
||
void f1() { | ||
// CHECK-LABEL: f1 | ||
// CHECK: [[REG0:%[a-zA-Z0-9.]*]] = load i8*, i8** @p, align 8 | ||
// CHECK-NEXT: [[REG_DYNN:%_Dynamic_check.non_null[a-zA-Z0-9.]*]] = icmp ne i8* [[REG0]], null | ||
// CHECK-NEXT: br i1 [[REG_DYNN]], label %[[LAB_DYSUC:_Dynamic_check.succeeded[a-zA-Z0-9.]*]], label %[[LAB_DYFAIL:_Dynamic_check.failed[a-zA-Z0-9.]*]] | ||
p++; | ||
} | ||
|
||
void f2() { | ||
// CHECK-LABEL: f2 | ||
// CHECK: [[REG0:%[a-zA-Z0-9.]*]] = load i8*, i8** @p, align 8 | ||
// CHECK-NEXT: [[REG_DYNN:%_Dynamic_check.non_null[a-zA-Z0-9.]*]] = icmp ne i8* [[REG0]], null | ||
// CHECK-NEXT: br i1 [[REG_DYNN]], label %[[LAB_DYSUC:_Dynamic_check.succeeded[a-zA-Z0-9.]*]], label %[[LAB_DYFAIL:_Dynamic_check.failed[a-zA-Z0-9.]*]] | ||
++p; | ||
} | ||
|
||
void f3() { | ||
// CHECK-LABEL: f3 | ||
// CHECK: [[REG0:%[a-zA-Z0-9.]*]] = load i8*, i8** @p, align 8 | ||
// CHECK-NEXT: [[REG_DYNN:%_Dynamic_check.non_null[a-zA-Z0-9.]*]] = icmp ne i8* [[REG0]], null | ||
// CHECK-NEXT: br i1 [[REG_DYNN]], label %[[LAB_DYSUC:_Dynamic_check.succeeded[a-zA-Z0-9.]*]], label %[[LAB_DYFAIL:_Dynamic_check.failed[a-zA-Z0-9.]*]] | ||
p--; | ||
} | ||
|
||
void f4() { | ||
// CHECK-LABEL: f4 | ||
// CHECK: [[REG0:%[a-zA-Z0-9.]*]] = load i8*, i8** @p, align 8 | ||
// CHECK-NEXT: [[REG_DYNN:%_Dynamic_check.non_null[a-zA-Z0-9.]*]] = icmp ne i8* [[REG0]], null | ||
// CHECK-NEXT: br i1 [[REG_DYNN]], label %[[LAB_DYSUC:_Dynamic_check.succeeded[a-zA-Z0-9.]*]], label %[[LAB_DYFAIL:_Dynamic_check.failed[a-zA-Z0-9.]*]] | ||
--p; | ||
} | ||
|
||
void f5() { | ||
// CHECK-LABEL: f5 | ||
// CHECK: [[REG0:%[a-zA-Z0-9.]*]] = load i8*, i8** @p, align 8 | ||
// CHECK-NEXT: [[REG_DYNN:%_Dynamic_check.non_null[a-zA-Z0-9.]*]] = icmp ne i8* [[REG0]], null | ||
// CHECK-NEXT: br i1 [[REG_DYNN]], label %[[LAB_DYSUC:_Dynamic_check.succeeded[a-zA-Z0-9.]*]], label %[[LAB_DYFAIL:_Dynamic_check.failed[a-zA-Z0-9.]*]] | ||
p += 5; | ||
} | ||
|
||
void f6() { | ||
// CHECK-LABEL: f6 | ||
// CHECK: [[REG0:%[a-zA-Z0-9.]*]] = load i8*, i8** @p, align 8 | ||
// CHECK-NEXT: [[REG_DYNN:%_Dynamic_check.non_null[a-zA-Z0-9.]*]] = icmp ne i8* [[REG0]], null | ||
// CHECK-NEXT: br i1 [[REG_DYNN]], label %[[LAB_DYSUC:_Dynamic_check.succeeded[a-zA-Z0-9.]*]], label %[[LAB_DYFAIL:_Dynamic_check.failed[a-zA-Z0-9.]*]] | ||
p -= 10; | ||
} | ||
|
||
void f7() { | ||
// CHECK-LABEL: f7 | ||
// CHECK: [[REG0:%[a-zA-Z0-9.]*]] = load i8*, i8** @p, align 8 | ||
// CHECK-NEXT: [[REG_DYNN:%_Dynamic_check.non_null[a-zA-Z0-9.]*]] = icmp ne i8* [[REG0]], null | ||
// CHECK-NEXT: br i1 [[REG_DYNN]], label %[[LAB_DYSUC:_Dynamic_check.succeeded[a-zA-Z0-9.]*]], label %[[LAB_DYFAIL:_Dynamic_check.failed[a-zA-Z0-9.]*]] | ||
p = 2 + p; | ||
} | ||
|
||
void f8() { | ||
// CHECK-LABEL: f8 | ||
// CHECK: [[REG0:%[a-zA-Z0-9.]*]] = load i8*, i8** @p, align 8 | ||
// CHECK-NEXT: [[REG_DYNN:%_Dynamic_check.non_null[a-zA-Z0-9.]*]] = icmp ne i8* [[REG0]], null | ||
// CHECK-NEXT: br i1 [[REG_DYNN]], label %[[LAB_DYSUC:_Dynamic_check.succeeded[a-zA-Z0-9.]*]], label %[[LAB_DYFAIL:_Dynamic_check.failed[a-zA-Z0-9.]*]] | ||
p = p - 100; | ||
} | ||
|
||
void f9() { | ||
// CHECK-LABEL: f9 | ||
// CHECK: [[REG0:%[a-zA-Z0-9.]*]] = load i8*, i8** @p, align 8 | ||
// CHECK-NEXT: [[REG_DYNN:%_Dynamic_check.non_null[a-zA-Z0-9.]*]] = icmp ne i8* [[REG0]], null | ||
// CHECK-NEXT: br i1 [[REG_DYNN]], label %[[LAB_DYSUC:_Dynamic_check.succeeded[a-zA-Z0-9.]*]], label %[[LAB_DYFAIL:_Dynamic_check.failed[a-zA-Z0-9.]*]] | ||
p[1]++; | ||
} | ||
|
||
void f10() { | ||
// CHECK-LABEL: f10 | ||
// CHECK: [[REG0:%[a-zA-Z0-9.]*]] = load i8*, i8** @p, align 8 | ||
// CHECK-NEXT: [[REG_DYNN:%_Dynamic_check.non_null[a-zA-Z0-9.]*]] = icmp ne i8* [[REG0]], null | ||
// CHECK-NEXT: br i1 [[REG_DYNN]], label %[[LAB_DYSUC:_Dynamic_check.succeeded[a-zA-Z0-9.]*]], label %[[LAB_DYFAIL:_Dynamic_check.failed[a-zA-Z0-9.]*]] | ||
|
||
// CHECK: [[REG1:%[a-zA-Z0-9.]*]] = load i8*, i8** @p, align 8 | ||
// CHECK: [[REG2:%[a-zA-Z0-9.]*]] = load i8*, i8** @p, align 8 | ||
// CHECK-NEXT: [[REG_DYNN:%_Dynamic_check.non_null[a-zA-Z0-9.]*]] = icmp ne i8* [[REG2]], null | ||
// CHECK-NEXT: br i1 [[REG_DYNN]], label %[[LAB_DYSUC:_Dynamic_check.succeeded[a-zA-Z0-9.]*]], label %[[LAB_DYFAIL:_Dynamic_check.failed[a-zA-Z0-9.]*]] | ||
p[1] = p[2] + (*p)++; | ||
} | ||
|
||
void f11(array_ptr<char> a); | ||
void f12() { | ||
// CHECK-LABEL: f12 | ||
// CHECK: [[REG0:%[a-zA-Z0-9.]*]] = load i8*, i8** @p, align 8 | ||
// CHECK-NEXT: [[REG_DYNN:%_Dynamic_check.non_null[a-zA-Z0-9.]*]] = icmp ne i8* [[REG0]], null | ||
// CHECK-NEXT: br i1 [[REG_DYNN]], label %[[LAB_DYSUC:_Dynamic_check.succeeded[a-zA-Z0-9.]*]], label %[[LAB_DYFAIL:_Dynamic_check.failed[a-zA-Z0-9.]*]] | ||
f11(++p); | ||
} | ||
|
||
void f13() { | ||
// CHECK-LABEL: f13 | ||
// CHECK: [[REG0:%[a-zA-Z0-9.]*]] = load i8*, i8** @p, align 8 | ||
// CHECK: [[REG1:%[a-zA-Z0-9.]*]] = load i8*, i8** @q, align 8 | ||
// CHECK-NEXT: [[REG_DYNN:%_Dynamic_check.non_null[a-zA-Z0-9.]*]] = icmp ne i8* [[REG1]], null | ||
// CHECK-NEXT: br i1 [[REG_DYNN]], label %[[LAB_DYSUC:_Dynamic_check.succeeded[a-zA-Z0-9.]*]], label %[[LAB_DYFAIL:_Dynamic_check.failed[a-zA-Z0-9.]*]] | ||
p[q[1]++]++; | ||
} |
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.
It looks like EmitDynamicCheckBlocks is being called twice here.