Skip to content

Commit b6de36c

Browse files
[BoundsSafety][NFC] Fix remaining bounds safety test failures in next (#9862)
* [BoundsSafety] Fix missing error diagnostic for explicitly casted null pointers Previously code like this ``` void receive(int* __counted_by(5)); void test(void) { receive((int*) NULL); } ``` would not emit an error diagnostic despite the fact the `test` function would unconditionally trap. The problem was that `Expr::isNullPointerConstant` does not look through explicit casts and so cases where an explicitly NULL pointer constant was converted to a `__counted_by` or `__sized_by` pointer with a constant count were not caught. To fix this `Expr::isNullPointerConstantIgnoreCastsAndOVEs` is used instead. rdar://139748843 * [BoundsSafety][NFC] Fix some remaining test failures in next * Fix BoundsSafety/PCH/count-increment-with-pch.c by adding target triple * Fix Sema/builtin-counted-by-ref.c --------- Co-authored-by: Dan Liew <[email protected]>
1 parent 46176d0 commit b6de36c

10 files changed

+23
-19
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7747,7 +7747,7 @@ bool checkDynamicCountSizeForAssignmentWithKnownCount(
77477747

77487748
const bool IsImplicitInitExpr = isa<ImplicitValueInitExpr>(RHSExpr);
77497749
const bool IsNull =
7750-
IsImplicitInitExpr || RHSExpr->isNullPointerConstant(
7750+
IsImplicitInitExpr || RHSExpr->isNullPointerConstantIgnoreCastsAndOVEs(
77517751
S.Context, Expr::NPC_ValueDependentIsNotNull);
77527752
const bool IsNonnull = CAT; // Array decays to nonnull pointer.
77537753

clang/test/BoundsSafety/AST/redundant-attrs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ my_c_ptr_nullable_bidi_t __bidi_indexable def_c_nullable_bidi_ptr;
2222
// CHECK-NEXT: PointerType {{.*}} 'const int *__bidi_indexable'
2323
// CHECK-NEXT: QualType {{.*}} 'const int' const
2424
// CHECK-NEXT: BuiltinType {{.*}} 'int'
25-
// CHECK-NEXT: VarDecl {{.*}} def_c_nullable_bidi_ptr 'my_c_ptr_nullable_bidi_t':'const int *__bidi_indexable'
25+
// CHECK-NEXT: VarDecl {{.*}} def_c_nullable_bidi_ptr 'const int *__bidi_indexable _Nullable':'const int *__bidi_indexable'

clang/test/BoundsSafety/CodeGen/bounds-safety-introduced-arithmetic-ubsan-checks-O2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ void f_outlen(char *__counted_by(*outLen) derOut, unsigned long long *outLen) {
6666
// UBSAN-NEXT: [[TMP0:%.*]] = ptrtoint ptr [[TMP_SROA_0_0_COPYLOAD]] to i64, {{!nosanitize ![0-9]+}}
6767
// UBSAN-NEXT: [[TMP1:%.*]] = add i64 [[OFFSET]], [[TMP0]], {{!nosanitize ![0-9]+}}
6868
// UBSAN-NEXT: [[TMP2:%.*]] = icmp ne ptr [[TMP_SROA_0_0_COPYLOAD]], null, {{!nosanitize ![0-9]+}}
69-
// UBSAN-NEXT: [[TMP3:%.*]] = icmp ne i64 [[TMP1]], 0, {{!nosanitize ![0-9]+}}
70-
// UBSAN-NEXT: [[TMP4:%.*]] = and i1 [[TMP2]], [[TMP3]], {{!nosanitize ![0-9]+}}
69+
// UBSAN-NEXT: [[TMP3:%.*]] = icmp eq i64 [[TMP1]], 0
70+
// UBSAN-NEXT: [[TMP4:%.*]] = xor i1 [[TMP2]], [[TMP3]]
7171
// UBSAN-NEXT: [[TMP5:%.*]] = icmp uge i64 [[TMP1]], [[TMP0]], {{!nosanitize ![0-9]+}}
7272
// UBSAN-NEXT: [[TMP6:%.*]] = and i1 [[TMP5]], [[TMP4]], {{!nosanitize ![0-9]+}}
7373
// UBSAN-NEXT: br i1 [[TMP6]], label [[CONT:%.*]], label [[TRAP:%.*]], {{!nosanitize ![0-9]+}}

clang/test/BoundsSafety/PCH/count-increment-with-pch.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
22

33
// Test without pch.
4-
// RUN: %clang_cc1 -fbounds-safety -include %s -fsyntax-only -verify %s
4+
// RUN: %clang_cc1 -fbounds-safety -include %s -triple arm64-apple-ios -fsyntax-only -verify %s
55

66
// Test with pch.
7-
// RUN: %clang_cc1 -fbounds-safety -emit-pch -o %t %s
8-
// RUN: %clang_cc1 -fbounds-safety -include-pch %t -verify -emit-llvm -O0 %s -o - | FileCheck %s
7+
// RUN: %clang_cc1 -fbounds-safety -triple arm64-apple-ios -emit-pch -o %t %s
8+
// RUN: %clang_cc1 -fbounds-safety -triple arm64-apple-ios -include-pch %t -verify -emit-llvm -O0 %s -o - | FileCheck %s
99
// expected-no-diagnostics
1010
#include <ptrcheck.h>
1111

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma clang system_header
2+
3+
const char *mock_system_func(void);

clang/test/BoundsSafety/Sema/bounds-safety-call-param-warning-regressions.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11

22
// RUN: %clang_cc1 -fsyntax-only -fbounds-safety -verify %s
3-
// RUN: %clang_cc1 -fsyntax-only -fbounds-safety -clang-vendor-feature=+disableOVEimplicitConv -verify %s
4-
53
// RUN: %clang_cc1 -fsyntax-only -verify %s
6-
// RUN: %clang_cc1 -fsyntax-only -clang-vendor-feature=+disableOVEimplicitConv -verify %s
74

85
#include <stdint.h>
96
#include <ptrcheck.h>

clang/test/BoundsSafety/Sema/complex-typespecs-with-bounds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ typedef typeof(*bar) * my_manual_ptr_t;
5353
void typedefs_of_typeof() {
5454
// expected-error@+1{{initializing 'my_t *__single' (aka 'char *__single') with an expression of incompatible type 'char * _Nullable' casts away '__unsafe_indexable' qualifier; use '__unsafe_forge_single' or '__unsafe_forge_bidi_indexable' to perform this conversion}}
5555
my_t * __single p1 = bar;
56-
// expected-error@+1{{initializing 'my_ptr_t __single' (aka 'char *__single') with an expression of incompatible type 'char * _Nullable' casts away '__unsafe_indexable' qualifier; use '__unsafe_forge_single' or '__unsafe_forge_bidi_indexable' to perform this conversion}}
56+
// expected-error@+1{{initializing 'char *__single _Nullable' with an expression of incompatible type 'char * _Nullable' casts away '__unsafe_indexable' qualifier; use '__unsafe_forge_single' or '__unsafe_forge_bidi_indexable' to perform this conversion}}
5757
my_ptr_t __single p2 = bar;
5858
// expected-error@+1{{initializing 'my_manual_ptr_t __single' (aka 'char *__single') with an expression of incompatible type 'char * _Nullable' casts away '__unsafe_indexable' qualifier; use '__unsafe_forge_single' or '__unsafe_forge_bidi_indexable' to perform this conversion}}
5959
my_manual_ptr_t __single p3 = bar;

clang/test/BoundsSafety/Sema/late-parsing-do-not-regress-others.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
#define UNLOCK_FUNCTION(...) __attribute__ ((unlock_function(__VA_ARGS__)))
1515

1616
void elf_fun_params(int lvar EXCLUSIVE_LOCK_FUNCTION()); // \
17-
// expected-warning {{'exclusive_lock_function' attribute only applies to functions}}
17+
// expected-warning {{'exclusive_lock_function' attribute applies to function parameters only if their type is a reference to a 'scoped_lockable'-annotated type}}
1818
void slf_fun_params(int lvar SHARED_LOCK_FUNCTION()); // \
19-
// expected-warning {{'shared_lock_function' attribute only applies to functions}}
19+
// expected-warning {{'shared_lock_function' attribute applies to function parameters only if their type is a reference to a 'scoped_lockable'-annotated type}}
2020
void uf_fun_params(int lvar UNLOCK_FUNCTION()); // \
21-
// expected-warning {{'unlock_function' attribute only applies to functions}}
21+
// expected-warning {{'unlock_function' attribute applies to function parameters only if their type is a reference to a 'scoped_lockable'-annotated type}}
2222

2323
// regression tests added for rdar://92699615
2424
typedef bool __attribute__((capability("role"))) role_t;

clang/test/BoundsSafety/Sema/static-bound-ptr-init.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11

2-
// RUN: %clang_cc1 -fsyntax-only -fbounds-safety -verify %s
3-
// RUN: %clang_cc1 -fsyntax-only -fbounds-safety -x objective-c -fbounds-attributes-objc-experimental -verify %s
2+
// RUN: %clang_cc1 -fsyntax-only -fbounds-safety -I%S/SystemHeaders/include -verify %s
3+
// RUN: %clang_cc1 -fsyntax-only -fbounds-safety -I%S/SystemHeaders/include -x objective-c -fbounds-attributes-objc-experimental -verify %s
44

55
#include <ptrcheck.h>
6+
#include <static-bound-ptr-init.h>
67
struct T {
78
void (*fp)(const struct T *t);
89
int i;
@@ -57,10 +58,10 @@ void *__single s_p3 = foo;
5758
// T -> U: P
5859
void *__unsafe_indexable ui_p2 = foo;
5960

60-
// Special case: "unspecified" should convert to __unsafe_indexable
61-
const char *__unsafe_indexable ui_p3 = __builtin_xnu_type_signature(struct T);
62-
6361
void Test () {
62+
// Special case: "unspecified" should convert to __unsafe_indexable
63+
const char *__unsafe_indexable ui_p3 = mock_system_func();
64+
6465
static int len1;
6566
// B -> C: R
6667
// expected-warning@+1{{possibly initializing 'c_p1' of type 'int *__single __counted_by(len1)' (aka 'int *__single') and implicit count value of 0 with non-null, which creates a non-dereferenceable pointer; explicitly set count value to 0 to remove this warning}}

clang/test/Sema/builtin-counted-by-ref.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ void test2(struct fam_struct *ptr, int idx) {
3434
void test3(struct fam_struct *ptr, int idx) {
3535
__builtin_counted_by_ref(&ptr->array[0]); // expected-error {{'__builtin_counted_by_ref' argument must reference a flexible array member}}
3636
__builtin_counted_by_ref(&ptr->array[idx]); // expected-error {{'__builtin_counted_by_ref' argument must reference a flexible array member}}
37+
// XXX: These are extra diagnostics that -fbounds-safety emits, which the upstream doesn't have yet.
38+
// expected-note@+2{{remove '&' to get address as 'int *' instead of 'int (*)[] __counted_by(count)' (aka 'int (*)[]')}}
39+
// expected-error@+1{{cannot take address of incomplete __counted_by array}}
3740
__builtin_counted_by_ref(&ptr->array); // expected-error {{'__builtin_counted_by_ref' argument must reference a flexible array member}}
3841
__builtin_counted_by_ref(ptr->x); // expected-error {{'__builtin_counted_by_ref' argument must reference a flexible array member}}
3942
__builtin_counted_by_ref(&ptr->x); // expected-error {{'__builtin_counted_by_ref' argument must reference a flexible array member}}

0 commit comments

Comments
 (0)