-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[clang] add array out-of-bounds access constraints using llvm.assume #159046
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7fdec0a
[clang] add array out-of-bounds access constraints using llvm.assume
sebpop 702d9dd
add -fassume-array-bounds flag disabled by default for now
sebpop ec1024d
fix UB in testcase
sebpop 77e2606
fix UB in testcase
sebpop 0ed60c2
avoid optimization in testcase
sebpop 1c11e60
handle zero-length array pattern
sebpop 033a1ce
handle VLAs
sebpop f46618d
use Accessed to generate strict vs. size+1 checks
sebpop 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
Some comments aren't visible on the classic Files Changed page.
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
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
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,89 @@ | ||
// RUN: %clang_cc1 -emit-llvm -O2 -fassume-array-bounds %s -o - | FileCheck %s | ||
// Test that array bounds constraints are NOT applied to cases that might | ||
// break real-world code with intentional out-of-bounds access patterns. | ||
|
||
// C18 standard allows one-past-the-end pointers, and some legacy code | ||
// intentionally accesses out-of-bounds for performance or compatibility. | ||
// This test verifies that bounds constraints are only applied to safe cases. | ||
|
||
// CHECK-LABEL: define {{.*}} @test_flexible_array_member | ||
struct Data { | ||
int count; | ||
int items[1]; // Flexible array member pattern (pre-C99 style) | ||
}; | ||
|
||
int test_flexible_array_member(struct Data *d, int i) { | ||
// CHECK-NOT: call void @llvm.assume | ||
// Flexible array member pattern (size 1 array as last field) should NOT | ||
// generate bounds constraints because items[1] is just a placeholder | ||
// for a larger array allocated with `malloc (sizeof (struct Data) + 42)`. | ||
return d->items[i]; | ||
} | ||
|
||
// CHECK-LABEL: define {{.*}} @test_not_flexible_array | ||
struct NotFlexible { | ||
int items[1]; // Size 1 array but NOT the last field. | ||
int count; // Something comes after it. | ||
}; | ||
|
||
int test_not_flexible_array(struct NotFlexible *s, int i) { | ||
// CHECK: call void @llvm.assume | ||
// This is NOT a flexible array pattern (not the last field), | ||
// so we're fine generating `assume(i < 1)`. | ||
return s->items[i]; | ||
} | ||
|
||
// CHECK-LABEL: define {{.*}} @test_pointer_parameter | ||
int test_pointer_parameter(int *arr, int i) { | ||
// CHECK-NOT: call void @llvm.assume | ||
// Pointer parameters should NOT generate bounds constraints | ||
// because we don't know the actual array size. | ||
return arr[i]; | ||
} | ||
|
||
// CHECK-LABEL: define {{.*}} @test_vla | ||
int test_vla(int n, int i) { | ||
int arr[n]; // Variable-length array. | ||
// CHECK-NOT: call void @llvm.assume | ||
// VLAs should NOT generate bounds constraints | ||
// because the size is dynamic. | ||
sebpop marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return arr[i]; | ||
} | ||
|
||
// CHECK-LABEL: define {{.*}} @test_one_past_end | ||
extern int extern_array[100]; | ||
int *test_one_past_end(void) { | ||
// CHECK-NOT: call void @llvm.assume | ||
// Taking address of one-past-the-end is allowed by C standard. | ||
// We should NOT assume anything about this access. | ||
return &extern_array[100]; // Legal: one past the end. | ||
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 tried extern int extern_array[100];
int *test_extern_array_val(int i) {
return &extern_array[i];
} with this PR an it generates %bounds.constraint = icmp ult i32 %i, 100
tail call void @llvm.assume(i1 %bounds.constraint) if Did you consider C++ references?
I think a reference must always point to valid memory, so here one can apply the stricter |
||
} | ||
|
||
// CHECK-LABEL: define {{.*}} @test_extern_array | ||
int test_extern_array(int i) { | ||
// CHECK: call void @llvm.assume | ||
// This will generate bounds constraints. | ||
// The array is a constant-size global array. | ||
// This is the safe case where we want optimization hints. | ||
return extern_array[i]; | ||
} | ||
|
||
// CHECK-LABEL: define {{.*}} @test_local_constant_array | ||
int test_local_constant_array(int i) { | ||
int arr[10]; | ||
// CHECK: call void @llvm.assume | ||
// This will generate bounds constraints. | ||
// We know the exact size of this alloca array. | ||
// This is the safe case where we want optimization hints. | ||
return arr[i]; | ||
} | ||
|
||
// CHECK-LABEL: define {{.*}} @test_malloc_array | ||
int *my_malloc(int); | ||
int test_malloc_array(int i) { | ||
// CHECK-NOT: call void @llvm.assume | ||
// Dynamically allocated arrays accessed via pointers do not get bounds | ||
// constraints. | ||
int *x = my_malloc(100 * sizeof(int)); | ||
return x[i]; | ||
} |
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,42 @@ | ||
// This test verifies that clang generates llvm.assume statements to inform the | ||
// optimizer that array subscripts are within bounds to enable better optimization. | ||
// RUN: %clang_cc1 -emit-llvm -O2 -fassume-array-bounds %s -o - | FileCheck %s | ||
|
||
// Verify no assumes are generated. | ||
// RUN: %clang_cc1 -emit-llvm -O2 -fno-assume-array-bounds %s -o - | FileCheck %s -check-prefix=NO-FLAG | ||
|
||
// CHECK-LABEL: define {{.*}} @test_simple_array | ||
// NO-FLAG-LABEL: define {{.*}} @test_simple_array | ||
int test_simple_array(int i) { | ||
int arr[10]; // C arrays are 0-based: valid indices are [0, 9] | ||
// CHECK: %{{.*}} = icmp ult i32 %i, 10 | ||
// CHECK: call void @llvm.assume(i1 %{{.*}}) | ||
// NO-FLAG-NOT: call void @llvm.assume | ||
return arr[i]; | ||
} | ||
|
||
// CHECK-LABEL: define {{.*}} @test_multidimensional_array | ||
int test_multidimensional_array(int i, int j) { | ||
int arr[5][8]; // Valid indices: i in [0, 4], j in [0, 7] | ||
// CHECK: %{{.*}} = icmp ult i32 %i, 5 | ||
// CHECK: call void @llvm.assume(i1 %{{.*}}) | ||
// CHECK: %{{.*}} = icmp ult i32 %j, 8 | ||
// CHECK: call void @llvm.assume(i1 %{{.*}}) | ||
return arr[i][j]; | ||
} | ||
|
||
// CHECK-LABEL: define {{.*}} @test_unsigned_index | ||
int test_unsigned_index(unsigned int i) { | ||
int arr[10]; | ||
// CHECK: %{{.*}} = icmp ult i32 %i, 10 | ||
// CHECK: call void @llvm.assume(i1 %{{.*}}) | ||
return arr[i]; | ||
} | ||
|
||
// CHECK-LABEL: define {{.*}} @test_store_undef | ||
void test_store_undef(int i, int value) { | ||
int arr[10]; | ||
// CHECK: %{{.*}} = icmp ult i32 %i, 10 | ||
// CHECK: call void @llvm.assume(i1 %{{.*}}) | ||
arr[i] = value; | ||
} |
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.