add initializers for initialization required variables, and some test cases for array/struct/union unit testing. (#287)

Add test cases to test whether an array, or an object of struct/union type that contains a checked value is initialized
This commit is contained in:
Shen Liu 2018-06-28 15:08:21 -07:00 коммит произвёл GitHub
Родитель 8b08ff3111
Коммит 360a48bb8c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 217 добавлений и 25 удалений

Просмотреть файл

@ -126,8 +126,8 @@ extern void f6(int *arr checked[] : count(5)) {
int ((*arr5 checked[5])(int, int)) : count(len);
int ((*arr6 checked[5])(int, int)) : bounds(arr5, arr5 + len);
// Checked array of checked pointers to functions
ptr<int (int, int)> arr7 checked[5] : count(5);
ptr<int (int, int)> arr8 checked[5] : bounds(arr8, arr8 + 5);
ptr<int(int, int)> arr7 checked[5] : count(5) = {0};
ptr<int(int, int)> arr8 checked[5] : bounds(arr8, arr8 + 5) = {0};
// Array_ptrs to checked pointers to functions.
array_ptr<ptr<int (int, int)>> u : count(5) = 0;
array_ptr<ptr<int (int, int)>> v : bounds(v, v + 5) = 0;

Просмотреть файл

@ -85,8 +85,8 @@ extern void f6(int *arr checked[] : count(5)) {
int((*arr1 checked[5])(int, int)) : bounds(arr, arr + len) rel_align(char);
int((*arr2 checked[5])(int, int)) : bounds(arr2, arr2 + len) rel_align_value(len); // expected-error {{expression is not an integer constant expression}}
ptr<int(int, int)> arr3 checked[5] : bounds(arr3, arr3 + 5) rel_align(char);
ptr<int(int, int)> arr4 checked[5] : bounds(arr4, arr4 + 5) rel_align_value(sizeof(char));
ptr<int(int, int)> arr3 checked[5] : bounds(arr3, arr3 + 5) rel_align(char) = {0};
ptr<int(int, int)> arr4 checked[5] : bounds(arr4, arr4 + 5) rel_align_value(sizeof(char)) = {0};
array_ptr<ptr<int(int, int)>> v : bounds(v, v + 5) rel_align(char) = 0;
array_ptr<ptr<int(int, int)>> w : bounds(v, v + 5) rel_align_value(sizeof(char)) = 0;

Просмотреть файл

@ -357,7 +357,7 @@ extern void check_exprs_nullterm(nt_array_ptr<int> arg1 : bounds(unknown),
--(*t3);
// operations involving struct members
struct S2 s;
struct S2 s = {0};
s.f1 = t1;
s.f1 = t2;
s.f1 = t3;

Просмотреть файл

@ -46,7 +46,7 @@ char g23 checked[3]nt_checked[4] = { "abc", "def", "fgh" };
//
nt_array_ptr<int> g30 : count(4) = (int[]) { 0, [2] = 2, 3, 5, 0 };
nt_array_ptr<int> g31 : count(0) = (int checked[]) { 0, [2] = 2, 3, 5, 0};
nt_array_ptr<int> g31 : count(0) = (int checked[]) { 0, [2] = 2, 3, 5, 0 };
nt_array_ptr<char> g32 : count(5) = "abcde";
array_ptr<char> g33 : count(5) = "abcde";
array_ptr<char> g34 : count(5) = (char[5]) { 'a', 'b', 'c', 'd', 'e' };
@ -61,7 +61,7 @@ nt_array_ptr<char> g37 nt_checked[] = { "the", "brown", "fox", "jumped",
void callback1(int a);
void callback2(int a);
nt_array_ptr<ptr<void (int)>> callback_table = (ptr<void (int)>[]) { callback1, callback2, 0 };
nt_array_ptr<ptr<void(int)>> callback_table = (ptr<void(int)>[]) { callback1, callback2, 0 };
void f3(char *escape) {
}
@ -69,7 +69,7 @@ void f3(char *escape) {
// Checked that the arrays and pointers have checked type.
void f4(void) checked {
char t1 = g1[0];
int t2 = g2[0];
int t2 = g2[0];
int t3 = g3[0][1];
char t4 = g4[0];
char t5 = g5[0];
@ -166,11 +166,9 @@ void f6(void) {
ptr<int> data checked[10] = { 0 }; // initializer for array required.
struct VariableBuffer stack checked[10] = { 0 }; // initializer for array required.
struct VariableBuffer buf_missing_init; // TODO: checkedc-clang issue #445.
// This should produce a compiler error.
ptr<int> data_missing_init checked[10]; // TODO: checkedc-clang issue #445.
// This should produce a compiler error.
struct VariableBuffer buf_missing_init; // expected-error {{containing a checked pointer must have an initializer}}
ptr<int> data_missing_init checked[10]; // expected-error {{elements containing checked pointers must have an initializer}}
// Check { 0 } initialization idiom where first member is a floating point number.
struct FloatWithVariableBuffer {
float weight;
@ -179,4 +177,197 @@ void f6(void) {
};
struct FloatWithVariableBuffer w = { 0 };
// test cases for checking the array/struct/union variables initializers
// array with/without initializers
ptr<int> data_with_init checked[10] = { 0 }; // initialized, pass.
ptr<char> data_no_init checked[20]; // expected-error {{elements containing checked pointers must have an initializer}}
struct has_uninitialized_ptr_member {
int x;
ptr<int> uninitialized_ptr_member;
float y;
};
struct has_uninitialized_ptr_member uninit_S; // expected-error {{containing a checked pointer must have an initializer}}
struct checked_value_no_bounds {
int x;
array_ptr<int> lower;
array_ptr<int> upper;
float y;
};
struct checked_value_no_bounds init_S2; // no bounds; initializer not required, should pass
struct checked_value_no_bounds uninit_S2; // no bounds; initializer not required, should pass
ptr<struct checked_value_no_bounds> arr_init checked[20] = { 0 }; // initialized arry, should pass
ptr<struct checked_value_no_bounds> arr_uninit checked[20]; // expected-error {{elements containing checked pointers must have an initializer}}
struct checked_value_has_bounds {
int x;
array_ptr<char> lower : count(5);
array_ptr<char> upper : count(5);
float y;
};
struct checked_value_has_bounds init_S3 = { 0 }; // has bounds; initializer required and we did, should pass
struct checked_value_has_bounds uninit_S3; // expected-error {{containing a checked pointer must have an initializer}}
struct struct_with_checked_field_has_bounds {
int x;
struct checked_value_has_bounds s;
};
struct struct_with_checked_field_has_bounds init_nested_S = { 0 }; // has bounds, initializer required and we did, should pass
struct struct_with_checked_field_has_bounds uninit_nested_S; // expected-error {{containing a checked pointer must have an initializer}}
union u_checked_value_no_bounds {
int x;
array_ptr<int> lower;
array_ptr<int> upper;
float y;
};
union u_checked_value_no_bounds init_U; // no bounds; initializer not required, should pass
union u_checked_value_no_bounds uninit_U; // no bounds; initializer not required, should pass
union u_checked_value_has_bounds {
int x;
array_ptr<char> lower : count(5);
array_ptr<char> upper : count(5);
float y;
};
union u_checked_value_has_bounds init_U2 = { 0 }; // has bounds; initializer required and we did, should pass
union u_checked_value_has_bounds uninit_U2; // expected-error {{containing a checked pointer must have an initializer}}
struct struct_with_checked_union_field_has_bounds {
int x;
union u_checked_value_has_bounds u;
};
struct struct_with_checked_union_field_has_bounds init_SU = { 0 }; // has bounds, initializer required and we did, should pass
struct struct_with_checked_union_field_has_bounds uninit_SU; // expected-error {{containing a checked pointer must have an initializer}}
typedef struct {
int data;
array_ptr<char> name : count(20);
struct Node* next;
} Node;
Node n_err; // expected-error {{containing a checked pointer must have an initializer}}
Node n = { 0 };
typedef struct {
struct Range r;
Node center;
} Circle;
Circle C_err; // expected-error {{containing a checked pointer must have an initializer}}
Circle C = { 0 };
typedef struct {
Circle Outer;
Circle Inner;
} Annulus;
Annulus a_err; // expected-error {{containing a checked pointer must have an initializer}}
Annulus a = { 0 };
// array of struct with array_ptr member with bounds
Annulus anls_arr checked[100]; // expected-error {{elements containing checked pointers must have an initializer}}
Annulus anls checked[100] = {0};
// nested structs
typedef struct NA {
int data;
struct NA *next;
} NA;
}
void f7() {
int a;
float b;
// integer with a bounds expression must have an initializer
int x : count(5); // expected-error {{with a bounds expression must have an initializer}} expected-error {{have a pointer or array type}}
int y : count(6) = 0; // expected-error {{have a pointer or array type}}
}
// integer with bounds expression needs to be checked
void f8 (void) {
// for bounds expr kind like "bounds((array_ptr) i, (array_ptr) i + 10)"
typedef struct {
int a;
unsigned long long b : bounds( (array_ptr<int>) b, (array_ptr<int>) b + 10);
} S0;
S0 s0; // expected-error {{containing an integer variable with a bounds expression must have an initializer}}
typedef struct {
int a;
S0 s;
} SS0;
SS0 ss0; // expected-error {{containing an integer variable with a bounds expression must have an initializer}}
// for bounds expr kind like "int i : count(len)"
typedef struct {
int a;
int b : count(10); // expected-error {{have a pointer or array type}}
} S;
S s1; // expected-error {{containing an integer variable with a bounds expression must have an initializer}}
typedef struct {
int aa;
float ff;
S s;
} SS;
SS ss; // expected-error {{containing an integer variable with a bounds expression must have an initializer}}
typedef struct {
int aaa;
float fff;
SS ss;
} SSS;
SSS sss; // expected-error {{containing an integer variable with a bounds expression must have an initializer}}
}
// An unchecked pointer with a bounds expression in a checked scope must have an initializer
// TODO: remove the restriction to allow local variables with unchecked pointer type to be declared
void f9 (void) checked {
int a;
float b;
int* p : count(1) = &a; // expected-error {{bounds declaration not allowed for local variable with unchecked pointer type}}
char* s : count(10); // expected-error {{bounds declaration not allowed for local variable with unchecked pointer type}}
}
// test unchecked pointer with bounds expression, in a checked scope
void f10 (void) checked {
char* p : count(5); // expected-error {{bounds declaration not allowed for local variable with unchecked pointer type}}
}
// For unchecked_pointer_with_bounds_expr_in_checked_scope, we need to consider struct also
void f11 (void) checked {
int a;
int* p : count(5); // expected-error {{bounds declaration not allowed for local variable with unchecked pointer type}}
void* src : count(10); // expected-error {{bounds declaration not allowed for local variable with unchecked pointer type}}
//a struct with unchecked pointers with bounds exprs in a checked scope
typedef struct {
int x;
int* p : count(1);
char* cp : count(5);
} S;
S s1; // expected-error {{containing an unchecked pointer with a bounds expression in a checked scope must have an initializer}}
typedef struct {
int x;
S s; // contains an unchecked pointer with bounds expr
} SS;
SS ss; // expected-error {{containing an unchecked pointer with a bounds expression in a checked scope must have an initializer}}
typedef struct {
int x;
SS ss;
} SSS;
SSS sss; // expected-error {{containing an unchecked pointer with a bounds expression in a checked scope must have an initializer}}
}

Просмотреть файл

@ -245,7 +245,7 @@ extern void check_dimensions1(void) {
int(t7 checked[10])[5][5]checked[5]; // multiple checked modifiers are allowed
int (t8[10])checked[10]; // expected-error {{unchecked array of checked array not allowed}}
int ((t9[10]))checked[10]; // expected-error {{unchecked array of checked array not allowed}}
dim_unchecked (t10 checked[10])[10]; // expected-error {{checked array of unchecked array not allowed \
dim_unchecked(t10 checked[10])[10]; // expected-error {{checked array of unchecked array not allowed \
('dim_unchecked' is an unchecked array)}}
dim_checked (t11[10])[10]; // expected-error {{unchecked array of checked array not allowed \
('dim_checked' is a checked array)}}

Просмотреть файл

@ -79,14 +79,14 @@ scope must have a pointer, array or function type that uses only checked types o
int *t43 checked[10]; // expected-error {{local variable in a checked \
scope must have a pointer, array or function type that uses only checked types or parameter/return types with bounds-safe interfaces}}
ptr<int> t44 checked[10];
array_ptr<int> t45 checked[10];
ptr<int> t44 checked[10] = {0};
array_ptr<int> t45 checked[10] = {0};
int *t46 checked[10][15]; // expected-error {{local variable in a checked \
scope must have a pointer, array or function type that uses only checked types or parameter/return types with bounds-safe interfaces}}
ptr<int> *t47 checked[10][15]; // expected-error {{local variable in a checked \
scope must have a pointer, array or function type that uses only checked types or parameter/return types with bounds-safe interfaces}}
array_ptr<int> t48 checked[3][2];
array_ptr<int> t48 checked[3][2] = {0};
//
// Checked pointers to function types that use constructed types.
@ -391,7 +391,7 @@ int func29(void) {
short e[10]; // expected-error {{member in a checked scope must have a checked type}}
int *f : itype(ptr<int>);
char *g : itype(array_ptr<char>);
} a;
} a = {0};
}
}
}
@ -565,7 +565,7 @@ int func49(void) {
char f[10]; // expected-error {{member in a checked scope must have a checked type}}
int *g : itype(ptr<int>);
char *h : itype(array_ptr<char>);
} a;
} a = {0};
struct s1 unchecked {
int *a;
char *b;
@ -576,7 +576,7 @@ int func49(void) {
char f[10];
int *g : itype(ptr<int>);
char *h : itype(array_ptr<char>);
} b;
} b; // expected-error {{containing a checked pointer must have an initializer}}
}
}
@ -745,7 +745,7 @@ unchecked int func59(void) {
int len;
short e[10]; // expected-error {{member in a checked scope must have a checked type}}
char f[10]; // expected-error {{member in a checked scope must have a checked type}}
} a;
} a = {0};
struct s1 unchecked {
int *a;
char *b;
@ -754,7 +754,7 @@ unchecked int func59(void) {
int len;
short e[10];
char f[10];
} b;
} b; // expected-error {{containing a checked pointer must have an initializer}}
}
}
@ -803,7 +803,7 @@ checked int func60(ptr<struct s0> st0, ptr<struct s1> st1) {
sum += *(st0->pd) + *(st1->pd);
sum += *(st0->e) + *(st1->e); // expected-error {{expression has unknown bounds}}
struct s2 sta;
struct s2 sta; // expected-error {{containing a checked pointer must have an initializer}}
ptr<struct s2> pstb = 0;
sum += *(sta.a) + *(sta.b) + *(sta.pc) + *(sta.pd); // expected-error 2 {{member used in a checked scope must have a checked type or a bounds-safe interface}}
sum += *(sta.e);
@ -1700,3 +1700,4 @@ extern void test_function_pointer(void) checked {
ptr<int> pa = &val0;
array_ptr<int> apa = &val0;
}

Просмотреть файл

@ -529,7 +529,7 @@ int checked_func_with_checked_struct(void) {
short e[10]; // expected-error {{member in a checked scope must have a checked type or a bounds-safe interface}}
char f[10]; // expected-error {{member in a checked scope must have a checked type or a bounds-safe interface}}
int len;
} a;
} a; // expected-error {{containing a checked pointer must have an initializer}}
return 0;
}
@ -543,7 +543,7 @@ int checked_func_with_unchecked_struct(void) {
short e[10];
char f[10];
int len;
} a;
} a; // expected-error {{containing a checked pointer must have an initializer}}
typedef struct _S {
#pragma CHECKED_SCOPE ON
int *a; // expected-error {{member in a checked scope must have a checked type or a bounds-safe interface}}