|
| 1 | +#include <assert.h> |
| 2 | + |
| 3 | +static void noop() { } |
| 4 | + |
| 5 | +int test(int nondet_int_array[]) { |
| 6 | + int a = 1, b = 2, c = 3; |
| 7 | + int *ptr_to_a = &a, *ptr_to_b = &b, *ptr_to_c = &c, *ptr_to_null = 0; |
| 8 | + |
| 9 | + // Symex knows the value of ptr_to_a, ptr_to_b, ptr_to_c and ptr_to_null, so |
| 10 | + // it should be able to evaluate simple conditions involving them. |
| 11 | + |
| 12 | + // Equality "==" |
| 13 | + |
| 14 | + // A non-null pointer and a matching value |
| 15 | + int unconditionally_reachable_1; |
| 16 | + if(ptr_to_a == &a) |
| 17 | + unconditionally_reachable_1 = 7; |
| 18 | + |
| 19 | + // A non-null pointer and a non-matching value (not a null pointer) |
| 20 | + int unreachable_1; |
| 21 | + if(ptr_to_a == &c) |
| 22 | + unreachable_1 = 7; |
| 23 | + |
| 24 | + // A non-null pointer and a non-matching value (a null pointer) |
| 25 | + int unreachable_2; |
| 26 | + if(ptr_to_a == 0) |
| 27 | + unreachable_2 = 7; |
| 28 | + |
| 29 | + |
| 30 | + // A null pointer and a matching value |
| 31 | + int unconditionally_reachable_2; |
| 32 | + if(ptr_to_null == 0) |
| 33 | + unconditionally_reachable_2 = 7; |
| 34 | + |
| 35 | + // A null pointer and a non-matching value |
| 36 | + int unreachable_3; |
| 37 | + if(ptr_to_null == &a) |
| 38 | + unreachable_3 = 7; |
| 39 | + |
| 40 | + |
| 41 | + // Disequality "!=" |
| 42 | + |
| 43 | + // A non-null pointer and a matching value |
| 44 | + int unreachable_4; |
| 45 | + if(ptr_to_a != &a) |
| 46 | + unreachable_4 = 7; |
| 47 | + |
| 48 | + // A non-null pointer and a non-matching value (not a null pointer) |
| 49 | + int unconditionally_reachable_3; |
| 50 | + if(ptr_to_a != &c) |
| 51 | + unconditionally_reachable_3 = 7; |
| 52 | + |
| 53 | + // A non-null pointer and a non-matching value (a null pointer) |
| 54 | + int unconditionally_reachable_4; |
| 55 | + if(ptr_to_a != 0) |
| 56 | + unconditionally_reachable_4 = 7; |
| 57 | + |
| 58 | + |
| 59 | + // A null pointer and a matching value |
| 60 | + int unreachable_5; |
| 61 | + if(ptr_to_null != 0) |
| 62 | + unreachable_5 = 7; |
| 63 | + |
| 64 | + // A null pointer and a non-matching value |
| 65 | + int unconditionally_reachable_5; |
| 66 | + if(ptr_to_null != &a) |
| 67 | + unconditionally_reachable_5 = 7; |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + // Symex can't tell what ptr_to_a_or_b points to, but we can tell that it |
| 73 | + // doesn't point to some things |
| 74 | + int *ptr_to_a_or_b = nondet_int_array[0] == 0 ? &a : &b; |
| 75 | + int *ptr_to_a_or_b_or_null = |
| 76 | + nondet_int_array[1] == 0 ? &a : nondet_int_array[1] == 1 ? &b : 0; |
| 77 | + |
| 78 | + // Equality "==" |
| 79 | + |
| 80 | + // A non-null pointer and a matching value |
| 81 | + int possibly_reachable_1; |
| 82 | + if(ptr_to_a_or_b == &a) |
| 83 | + possibly_reachable_1 = 7; |
| 84 | + |
| 85 | + // A non-null pointer and a non-matching value (not a null pointer) |
| 86 | + int unreachable_6; |
| 87 | + if(ptr_to_a_or_b == &c) |
| 88 | + unreachable_6 = 7; |
| 89 | + |
| 90 | + // A non-null pointer and a non-matching value (a null pointer) |
| 91 | + int unreachable_7; |
| 92 | + if(ptr_to_a_or_b == 0) |
| 93 | + unreachable_7 = 7; |
| 94 | + |
| 95 | + |
| 96 | + // A possibly-null pointer and a matching value (not a null pointer) |
| 97 | + int possibly_reachable_2; |
| 98 | + if(ptr_to_a_or_b_or_null == &a) |
| 99 | + possibly_reachable_2 = 7; |
| 100 | + |
| 101 | + // A possibly-null pointer and a matching value (a null pointer) |
| 102 | + int possibly_reachable_3; |
| 103 | + if(ptr_to_a_or_b_or_null == 0) |
| 104 | + possibly_reachable_3 = 7; |
| 105 | + |
| 106 | + // A possibly-null pointer and a non-matching value |
| 107 | + int unreachable_8; |
| 108 | + if(ptr_to_a_or_b_or_null == &c) |
| 109 | + unreachable_8 = 7; |
| 110 | + |
| 111 | + |
| 112 | + // Disequality "!=" |
| 113 | + |
| 114 | + // A non-null pointer and a matching value |
| 115 | + int possibly_reachable_4; |
| 116 | + if(ptr_to_a_or_b != &a) |
| 117 | + possibly_reachable_4 = 7; |
| 118 | + |
| 119 | + // A non-null pointer and a non-matching value (not a null pointer) |
| 120 | + int unconditionally_reachable_6; |
| 121 | + if(ptr_to_a_or_b != &c) |
| 122 | + unconditionally_reachable_6 = 7; |
| 123 | + |
| 124 | + // A non-null pointer and a non-matching value (a null pointer) |
| 125 | + int unconditionally_reachable_7; |
| 126 | + if(ptr_to_a_or_b != 0) |
| 127 | + unconditionally_reachable_7 = 7; |
| 128 | + |
| 129 | + |
| 130 | + // A possibly-null pointer and a matching value (not a null pointer) |
| 131 | + int possibly_reachable_5; |
| 132 | + if(ptr_to_a_or_b_or_null != &a) |
| 133 | + possibly_reachable_5 = 7; |
| 134 | + |
| 135 | + // A possibly-null pointer and a matching value (a null pointer) |
| 136 | + int possibly_reachable_6; |
| 137 | + if(ptr_to_a_or_b_or_null != 0) |
| 138 | + possibly_reachable_6 = 7; |
| 139 | + |
| 140 | + // A possibly-null pointer and a non-matching value |
| 141 | + int unconditionally_reachable_8; |
| 142 | + if(ptr_to_a_or_b_or_null != &c) |
| 143 | + unconditionally_reachable_8 = 7; |
| 144 | + |
| 145 | + |
| 146 | + // We should also be able to do all of the above in compound expressions which |
| 147 | + // use logical operators like AND, OR and NOT, or even ternary expressions. |
| 148 | + |
| 149 | + int unconditionally_reachable_9; |
| 150 | + if(!(ptr_to_a == &c) && ptr_to_a_or_b != 0) |
| 151 | + unconditionally_reachable_9 = 7; |
| 152 | + |
| 153 | + int unreachable_9; |
| 154 | + if(!(ptr_to_null == 0) || ptr_to_a_or_b == 0) |
| 155 | + unreachable_9 = 7; |
| 156 | + |
| 157 | + int unconditionally_reachable_10; |
| 158 | + if((ptr_to_a == &a && !(ptr_to_a_or_b == 0)) || ptr_to_a_or_b_or_null == &c) |
| 159 | + unconditionally_reachable_10 = 7; |
| 160 | + |
| 161 | + int unreachable_10; |
| 162 | + if(ptr_to_a_or_b_or_null != 0 ? ptr_to_null != 0 : ptr_to_a_or_b == &c) |
| 163 | + unreachable_10 = 7; |
| 164 | + |
| 165 | + |
| 166 | + // And everything should work with the symbol on the left or the right |
| 167 | + |
| 168 | + int unconditionally_reachable_11; |
| 169 | + if(!(&c == ptr_to_a) && 0 != ptr_to_a_or_b) |
| 170 | + unconditionally_reachable_11 = 7; |
| 171 | + |
| 172 | + int unreachable_11; |
| 173 | + if(!(0 == ptr_to_null) || 0 == ptr_to_a_or_b) |
| 174 | + unreachable_11 = 7; |
| 175 | + |
| 176 | + int unconditionally_reachable_12; |
| 177 | + if((&a == ptr_to_a && !(0 == ptr_to_a_or_b)) || &c == ptr_to_a_or_b_or_null) |
| 178 | + unconditionally_reachable_12 = 7; |
| 179 | + |
| 180 | + int unreachable_12; |
| 181 | + if(0 != ptr_to_a_or_b_or_null ? 0 != ptr_to_null : &c == ptr_to_a_or_b) |
| 182 | + unreachable_12 = 7; |
| 183 | + |
| 184 | + int possibly_reachable_7; |
| 185 | + if(0 != ptr_to_a_or_b_or_null) |
| 186 | + possibly_reachable_7 = 7; |
| 187 | + |
| 188 | + assert(0); |
| 189 | +} |
0 commit comments