| 1 |
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.StoreToImmutable -verify %s |
| 2 |
|
| 3 |
// Test basic functionality of StoreToImmutable checker for the C programming language. |
| 4 |
|
| 5 |
const int tentative_global_const; // expected-note {{Memory region is declared as immutable here}} |
| 6 |
|
| 7 |
void test_direct_write_to_tentative_const_global() { |
| 8 |
*(int*)&tentative_global_const = 100; // expected-warning {{Trying to write to immutable memory in global read-only storage}} |
| 9 |
} |
| 10 |
|
| 11 |
const int global_const = 42; // expected-note {{Memory region is declared as immutable here}} |
| 12 |
|
| 13 |
void test_direct_write_to_const_global() { |
| 14 |
// This should trigger a warning about writing to immutable memory |
| 15 |
*(int*)&global_const = 100; // expected-warning {{Trying to write to immutable memory in global read-only storage}} |
| 16 |
} |
| 17 |
|
| 18 |
void test_write_through_const_pointer() { |
| 19 |
const int local_const = 10; // expected-note {{Memory region is declared as immutable here}} |
| 20 |
int *ptr = (int*)&local_const; |
| 21 |
*ptr = 20; // expected-warning {{Trying to write to immutable memory}} |
| 22 |
} |
| 23 |
|
| 24 |
void test_write_to_const_array() { |
| 25 |
const int arr[5] = {1, 2, 3, 4, 5}; // expected-note {{Enclosing memory region is declared as immutable here}} |
| 26 |
int *ptr = (int*)arr; |
| 27 |
ptr[0] = 10; // expected-warning {{Trying to write to immutable memory}} |
| 28 |
} |
| 29 |
|
| 30 |
struct TestStruct { |
| 31 |
const int x; // expected-note 2 {{Memory region is declared as immutable here}} |
| 32 |
int y; |
| 33 |
}; |
| 34 |
|
| 35 |
void test_write_to_const_struct_member() { |
| 36 |
struct TestStruct s = {1, 2}; |
| 37 |
int *ptr = (int*)&s.x; |
| 38 |
*ptr = 10; // expected-warning {{Trying to write to immutable memory}} |
| 39 |
} |
| 40 |
|
| 41 |
const int global_array[3] = {1, 2, 3}; // expected-note {{Enclosing memory region is declared as immutable here}} |
| 42 |
|
| 43 |
void test_write_to_const_global_array() { |
| 44 |
int *ptr = (int*)global_array; |
| 45 |
ptr[0] = 10; // expected-warning {{Trying to write to immutable memory in global read-only storage}} |
| 46 |
} |
| 47 |
|
| 48 |
const struct TestStruct global_struct = {1, 2}; |
| 49 |
|
| 50 |
void test_write_to_const_global_struct() { |
| 51 |
int *ptr = (int*)&global_struct.x; |
| 52 |
*ptr = 10; // expected-warning {{Trying to write to immutable memory in global read-only storage}} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
void test_write_to_const_param(const int param) { // expected-note {{Memory region is declared as immutable here}} |
| 57 |
*(int*)¶m = 100; // expected-warning {{Trying to write to immutable memory}} |
| 58 |
} |
| 59 |
|
| 60 |
void test_write_to_const_ptr_param(const int *param) { |
| 61 |
*(int*)param = 100; // expected-warning {{Trying to write to immutable memory}} |
| 62 |
} |
| 63 |
|
| 64 |
void test_write_to_const_array_param(const int arr[5]) { |
| 65 |
*(int*)arr = 100; // expected-warning {{Trying to write to immutable memory}} |
| 66 |
} |
| 67 |
|
| 68 |
struct ParamStruct { |
| 69 |
const int z; // expected-note 2 {{Memory region is declared as immutable here}} |
| 70 |
int w; |
| 71 |
}; |
| 72 |
|
| 73 |
void test_write_to_const_struct_param(const struct ParamStruct s) { |
| 74 |
*(int*)&s.z = 100; // expected-warning {{Trying to write to immutable memory}} |
| 75 |
} |
| 76 |
|
| 77 |
void test_write_to_const_struct_ptr_param(const struct ParamStruct *s) { |
| 78 |
*(int*)&s->z = 100; // expected-warning {{Trying to write to immutable memory}} |
| 79 |
} |
| 80 |
|
| 81 |
void test_write_to_nonconst() { |
| 82 |
int non_const = 42; |
| 83 |
*(int*)&non_const = 100; // No warning expected |
| 84 |
} |
| 85 |
|
| 86 |
int global_non_const = 42; |
| 87 |
|
| 88 |
void test_write_to_nonconst_global() { |
| 89 |
*(int*)&global_non_const = 100; // No warning expected |
| 90 |
} |
| 91 |
|
| 92 |
struct NonConstStruct { |
| 93 |
int x; |
| 94 |
int y; |
| 95 |
}; |
| 96 |
|
| 97 |
void test_write_to_nonconst_struct_member() { |
| 98 |
struct NonConstStruct s = {1, 2}; |
| 99 |
*(int*)&s.x = 100; // No warning expected |
| 100 |
} |
| 101 |
|
| 102 |
void test_write_to_nonconst_param(int param) { |
| 103 |
*(int*)¶m = 100; // No warning expected |
| 104 |
} |
| 105 |
|
| 106 |
void test_normal_assignment() { |
| 107 |
int x = 42; |
| 108 |
x = 100; // No warning expected |
| 109 |
} |
| 110 |
|
| 111 |
void test_const_ptr_to_nonconst_data() { |
| 112 |
int data = 42; |
| 113 |
const int *ptr = &data; |
| 114 |
*(int*)ptr = 100; // No warning expected |
| 115 |
} |
| 116 |
|
| 117 |
void test_const_ptr_to_const_data() { |
| 118 |
const int data = 42; // expected-note {{Memory region is declared as immutable here}} |
| 119 |
const int *ptr = &data; |
| 120 |
*(int*)ptr = 100; // expected-warning {{Trying to write to immutable memory}} |
| 121 |
} |