| 1 |
// RUN: %clang_cc1 -fsyntax-only -fexperimental-lifetime-safety -Wexperimental-lifetime-safety -Wno-dangling -verify %s |
| 2 |
|
| 3 |
struct View; |
| 4 |
|
| 5 |
struct [[gsl::Owner]] MyObj { |
| 6 |
int id; |
| 7 |
~MyObj() {} // Non-trivial destructor |
| 8 |
MyObj operator+(MyObj); |
| 9 |
|
| 10 |
View getView() const [[clang::lifetimebound]]; |
| 11 |
}; |
| 12 |
|
| 13 |
struct [[gsl::Pointer()]] View { |
| 14 |
View(const MyObj&); // Borrows from MyObj |
| 15 |
View(); |
| 16 |
void use() const; |
| 17 |
}; |
| 18 |
|
| 19 |
class TriviallyDestructedClass { |
| 20 |
View a, b; |
| 21 |
}; |
| 22 |
|
| 23 |
//===----------------------------------------------------------------------===// |
| 24 |
// Basic Definite Use-After-Free (-W...permissive) |
| 25 |
// These are cases where the pointer is guaranteed to be dangling at the use site. |
| 26 |
//===----------------------------------------------------------------------===// |
| 27 |
|
| 28 |
void definite_simple_case() { |
| 29 |
MyObj* p; |
| 30 |
{ |
| 31 |
MyObj s; |
| 32 |
p = &s; // expected-warning {{object whose reference is captured does not live long enough}} |
| 33 |
} // expected-note {{destroyed here}} |
| 34 |
(void)*p; // expected-note {{later used here}} |
| 35 |
} |
| 36 |
|
| 37 |
void definite_simple_case_gsl() { |
| 38 |
View v; |
| 39 |
{ |
| 40 |
MyObj s; |
| 41 |
v = s; // expected-warning {{object whose reference is captured does not live long enough}} |
| 42 |
} // expected-note {{destroyed here}} |
| 43 |
v.use(); // expected-note {{later used here}} |
| 44 |
} |
| 45 |
|
| 46 |
void no_use_no_error() { |
| 47 |
MyObj* p; |
| 48 |
{ |
| 49 |
MyObj s; |
| 50 |
p = &s; |
| 51 |
} |
| 52 |
// 'p' is dangling here, but since it is never used, no warning is issued. |
| 53 |
} |
| 54 |
|
| 55 |
void no_use_no_error_gsl() { |
| 56 |
View v; |
| 57 |
{ |
| 58 |
MyObj s; |
| 59 |
v = s; |
| 60 |
} |
| 61 |
// 'v' is dangling here, but since it is never used, no warning is issued. |
| 62 |
} |
| 63 |
|
| 64 |
void definite_pointer_chain() { |
| 65 |
MyObj* p; |
| 66 |
MyObj* q; |
| 67 |
{ |
| 68 |
MyObj s; |
| 69 |
p = &s; // expected-warning {{does not live long enough}} |
| 70 |
q = p; |
| 71 |
} // expected-note {{destroyed here}} |
| 72 |
(void)*q; // expected-note {{later used here}} |
| 73 |
} |
| 74 |
|
| 75 |
void definite_propagation_gsl() { |
| 76 |
View v1, v2; |
| 77 |
{ |
| 78 |
MyObj s; |
| 79 |
v1 = s; // expected-warning {{object whose reference is captured does not live long enough}} |
| 80 |
v2 = v1; |
| 81 |
} // expected-note {{destroyed here}} |
| 82 |
v2.use(); // expected-note {{later used here}} |
| 83 |
} |
| 84 |
|
| 85 |
void definite_multiple_uses_one_warning() { |
| 86 |
MyObj* p; |
| 87 |
{ |
| 88 |
MyObj s; |
| 89 |
p = &s; // expected-warning {{does not live long enough}} |
| 90 |
} // expected-note {{destroyed here}} |
| 91 |
(void)*p; // expected-note {{later used here}} |
| 92 |
// No second warning for the same loan. |
| 93 |
p->id = 1; |
| 94 |
MyObj* q = p; |
| 95 |
(void)*q; |
| 96 |
} |
| 97 |
|
| 98 |
void definite_multiple_pointers() { |
| 99 |
MyObj *p, *q, *r; |
| 100 |
{ |
| 101 |
MyObj s; |
| 102 |
p = &s; // expected-warning {{does not live long enough}} |
| 103 |
q = &s; // expected-warning {{does not live long enough}} |
| 104 |
r = &s; // expected-warning {{does not live long enough}} |
| 105 |
} // expected-note 3 {{destroyed here}} |
| 106 |
(void)*p; // expected-note {{later used here}} |
| 107 |
(void)*q; // expected-note {{later used here}} |
| 108 |
(void)*r; // expected-note {{later used here}} |
| 109 |
} |
| 110 |
|
| 111 |
void definite_single_pointer_multiple_loans(bool cond) { |
| 112 |
MyObj *p; |
| 113 |
if (cond){ |
| 114 |
MyObj s; |
| 115 |
p = &s; // expected-warning {{does not live long enough}} |
| 116 |
} // expected-note {{destroyed here}} |
| 117 |
else { |
| 118 |
MyObj t; |
| 119 |
p = &t; // expected-warning {{does not live long enough}} |
| 120 |
} // expected-note {{destroyed here}} |
| 121 |
(void)*p; // expected-note 2 {{later used here}} |
| 122 |
} |
| 123 |
|
| 124 |
void definite_single_pointer_multiple_loans_gsl(bool cond) { |
| 125 |
View v; |
| 126 |
if (cond){ |
| 127 |
MyObj s; |
| 128 |
v = s; // expected-warning {{object whose reference is captured does not live long enough}} |
| 129 |
} // expected-note {{destroyed here}} |
| 130 |
else { |
| 131 |
MyObj t; |
| 132 |
v = t; // expected-warning {{object whose reference is captured does not live long enough}} |
| 133 |
} // expected-note {{destroyed here}} |
| 134 |
v.use(); // expected-note 2 {{later used here}} |
| 135 |
} |
| 136 |
|
| 137 |
void definite_if_branch(bool cond) { |
| 138 |
MyObj safe; |
| 139 |
MyObj* p = &safe; |
| 140 |
if (cond) { |
| 141 |
MyObj temp; |
| 142 |
p = &temp; // expected-warning {{object whose reference is captured does not live long enough}} |
| 143 |
} // expected-note {{destroyed here}} |
| 144 |
(void)*p; // expected-note {{later used here}} |
| 145 |
} |
| 146 |
|
| 147 |
void potential_if_branch(bool cond) { |
| 148 |
MyObj safe; |
| 149 |
MyObj* p = &safe; |
| 150 |
if (cond) { |
| 151 |
MyObj temp; |
| 152 |
p = &temp; // expected-warning {{object whose reference is captured may not live long enough}} |
| 153 |
} // expected-note {{destroyed here}} |
| 154 |
if (!cond) |
| 155 |
(void)*p; // expected-note {{later used here}} |
| 156 |
else |
| 157 |
p = &safe; |
| 158 |
} |
| 159 |
|
| 160 |
void definite_if_branch_gsl(bool cond) { |
| 161 |
MyObj safe; |
| 162 |
View v = safe; |
| 163 |
if (cond) { |
| 164 |
MyObj temp; |
| 165 |
v = temp; // expected-warning {{object whose reference is captured does not live long enough}} |
| 166 |
} // expected-note {{destroyed here}} |
| 167 |
v.use(); // expected-note {{later used here}} |
| 168 |
} |
| 169 |
|
| 170 |
void definite_potential_together(bool cond) { |
| 171 |
MyObj safe; |
| 172 |
MyObj* p_maybe = &safe; |
| 173 |
MyObj* p_definite = nullptr; |
| 174 |
|
| 175 |
{ |
| 176 |
MyObj s; |
| 177 |
if (cond) |
| 178 |
p_definite = &s; // expected-warning {{does not live long enough}} |
| 179 |
if (cond) |
| 180 |
p_maybe = &s; // expected-warning {{may not live long enough}} |
| 181 |
} // expected-note 2 {{destroyed here}} |
| 182 |
(void)*p_definite; // expected-note {{later used here}} |
| 183 |
if (!cond) |
| 184 |
(void)*p_maybe; // expected-note {{later used here}} |
| 185 |
} |
| 186 |
|
| 187 |
void definite_overrides_potential(bool cond) { |
| 188 |
MyObj safe; |
| 189 |
MyObj* p; |
| 190 |
MyObj* q; |
| 191 |
{ |
| 192 |
MyObj s; |
| 193 |
q = &s; // expected-warning {{does not live long enough}} |
| 194 |
p = q; |
| 195 |
} // expected-note {{destroyed here}} |
| 196 |
|
| 197 |
if (cond) { |
| 198 |
// 'q' is conditionally "rescued". 'p' is not. |
| 199 |
q = &safe; |
| 200 |
} |
| 201 |
|
| 202 |
// The use of 'p' is a definite error because it was never rescued. |
| 203 |
(void)*q; |
| 204 |
(void)*p; // expected-note {{later used here}} |
| 205 |
(void)*q; |
| 206 |
} |
| 207 |
|
| 208 |
void potential_due_to_conditional_killing(bool cond) { |
| 209 |
MyObj safe; |
| 210 |
MyObj* q; |
| 211 |
{ |
| 212 |
MyObj s; |
| 213 |
q = &s; // expected-warning {{may not live long enough}} |
| 214 |
} // expected-note {{destroyed here}} |
| 215 |
if (cond) { |
| 216 |
// 'q' is conditionally "rescued". 'p' is not. |
| 217 |
q = &safe; |
| 218 |
} |
| 219 |
(void)*q; // expected-note {{later used here}} |
| 220 |
} |
| 221 |
|
| 222 |
void potential_for_loop_use_after_loop_body(MyObj safe) { |
| 223 |
MyObj* p = &safe; |
| 224 |
for (int i = 0; i < 1; ++i) { |
| 225 |
MyObj s; |
| 226 |
p = &s; // expected-warning {{may not live long enough}} |
| 227 |
} // expected-note {{destroyed here}} |
| 228 |
(void)*p; // expected-note {{later used here}} |
| 229 |
} |
| 230 |
|
| 231 |
void safe_for_loop_gsl() { |
| 232 |
MyObj safe; |
| 233 |
View v = safe; |
| 234 |
for (int i = 0; i < 1; ++i) { |
| 235 |
MyObj s; |
| 236 |
v = s; |
| 237 |
v.use(); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
void potential_for_loop_gsl() { |
| 242 |
MyObj safe; |
| 243 |
View v = safe; |
| 244 |
for (int i = 0; i < 1; ++i) { |
| 245 |
MyObj s; |
| 246 |
v = s; // expected-warning {{object whose reference is captured may not live long enough}} |
| 247 |
} // expected-note {{destroyed here}} |
| 248 |
v.use(); // expected-note {{later used here}} |
| 249 |
} |
| 250 |
|
| 251 |
void potential_for_loop_use_before_loop_body(MyObj safe) { |
| 252 |
MyObj* p = &safe; |
| 253 |
// Prefer the earlier use for diagnsotics. |
| 254 |
for (int i = 0; i < 1; ++i) { |
| 255 |
(void)*p; // expected-note {{later used here}} |
| 256 |
MyObj s; |
| 257 |
p = &s; // expected-warning {{does not live long enough}} |
| 258 |
} // expected-note {{destroyed here}} |
| 259 |
(void)*p; |
| 260 |
} |
| 261 |
|
| 262 |
void definite_loop_with_break(bool cond) { |
| 263 |
MyObj safe; |
| 264 |
MyObj* p = &safe; |
| 265 |
for (int i = 0; i < 10; ++i) { |
| 266 |
if (cond) { |
| 267 |
MyObj temp; |
| 268 |
p = &temp; // expected-warning {{does not live long enough}} |
| 269 |
break; // expected-note {{destroyed here}} |
| 270 |
} |
| 271 |
} |
| 272 |
(void)*p; // expected-note {{later used here}} |
| 273 |
} |
| 274 |
|
| 275 |
void definite_loop_with_break_gsl(bool cond) { |
| 276 |
MyObj safe; |
| 277 |
View v = safe; |
| 278 |
for (int i = 0; i < 10; ++i) { |
| 279 |
if (cond) { |
| 280 |
MyObj temp; |
| 281 |
v = temp; // expected-warning {{object whose reference is captured does not live long enough}} |
| 282 |
break; // expected-note {{destroyed here}} |
| 283 |
} |
| 284 |
} |
| 285 |
v.use(); // expected-note {{later used here}} |
| 286 |
} |
| 287 |
|
| 288 |
void potential_multiple_expiry_of_same_loan(bool cond) { |
| 289 |
// Choose the last expiry location for the loan (e.g., through scope-ends and break statements). |
| 290 |
MyObj safe; |
| 291 |
MyObj* p = &safe; |
| 292 |
for (int i = 0; i < 10; ++i) { |
| 293 |
MyObj unsafe; |
| 294 |
if (cond) { |
| 295 |
p = &unsafe; // expected-warning {{does not live long enough}} |
| 296 |
break; // expected-note {{destroyed here}} |
| 297 |
} |
| 298 |
} |
| 299 |
(void)*p; // expected-note {{later used here}} |
| 300 |
|
| 301 |
p = &safe; |
| 302 |
for (int i = 0; i < 10; ++i) { |
| 303 |
MyObj unsafe; |
| 304 |
if (cond) { |
| 305 |
p = &unsafe; // expected-warning {{does not live long enough}} |
| 306 |
if (cond) |
| 307 |
break; // expected-note {{destroyed here}} |
| 308 |
} |
| 309 |
} |
| 310 |
(void)*p; // expected-note {{later used here}} |
| 311 |
|
| 312 |
p = &safe; |
| 313 |
for (int i = 0; i < 10; ++i) { |
| 314 |
if (cond) { |
| 315 |
MyObj unsafe2; |
| 316 |
p = &unsafe2; // expected-warning {{does not live long enough}} |
| 317 |
break; // expected-note {{destroyed here}} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
// TODO: This can be argued to be a "maybe" warning. This is because |
| 322 |
// we only check for confidence of liveness and not the confidence of |
| 323 |
// the loan contained in an origin. To deal with this, we can introduce |
| 324 |
// a confidence in loan propagation analysis as well like liveness. |
| 325 |
(void)*p; // expected-note {{later used here}} |
| 326 |
|
| 327 |
p = &safe; |
| 328 |
for (int i = 0; i < 10; ++i) { |
| 329 |
MyObj unsafe; |
| 330 |
if (cond) |
| 331 |
p = &unsafe; // expected-warning {{does not live long enough}} |
| 332 |
if (cond) |
| 333 |
break; // expected-note {{destroyed here}} |
| 334 |
} |
| 335 |
(void)*p; // expected-note {{later used here}} |
| 336 |
} |
| 337 |
|
| 338 |
void potential_switch(int mode) { |
| 339 |
MyObj safe; |
| 340 |
MyObj* p = &safe; |
| 341 |
switch (mode) { |
| 342 |
case 1: { |
| 343 |
MyObj temp; |
| 344 |
p = &temp; // expected-warning {{object whose reference is captured may not live long enough}} |
| 345 |
break; // expected-note {{destroyed here}} |
| 346 |
} |
| 347 |
case 2: { |
| 348 |
p = &safe; // This path is okay. |
| 349 |
break; |
| 350 |
} |
| 351 |
} |
| 352 |
if (mode == 2) |
| 353 |
(void)*p; // expected-note {{later used here}} |
| 354 |
} |
| 355 |
|
| 356 |
void definite_switch(int mode) { |
| 357 |
MyObj safe; |
| 358 |
MyObj* p = &safe; |
| 359 |
// A use domintates all the loan expires --> all definite error. |
| 360 |
switch (mode) { |
| 361 |
case 1: { |
| 362 |
MyObj temp1; |
| 363 |
p = &temp1; // expected-warning {{does not live long enough}} |
| 364 |
break; // expected-note {{destroyed here}} |
| 365 |
} |
| 366 |
case 2: { |
| 367 |
MyObj temp2; |
| 368 |
p = &temp2; // expected-warning {{does not live long enough}} |
| 369 |
break; // expected-note {{destroyed here}} |
| 370 |
} |
| 371 |
default: { |
| 372 |
MyObj temp2; |
| 373 |
p = &temp2; // expected-warning {{does not live long enough}} |
| 374 |
break; // expected-note {{destroyed here}} |
| 375 |
} |
| 376 |
} |
| 377 |
(void)*p; // expected-note 3 {{later used here}} |
| 378 |
} |
| 379 |
|
| 380 |
void definite_switch_gsl(int mode) { |
| 381 |
View v; |
| 382 |
switch (mode) { |
| 383 |
case 1: { |
| 384 |
MyObj temp1; |
| 385 |
v = temp1; // expected-warning {{object whose reference is captured does not live long enough}} |
| 386 |
break; // expected-note {{destroyed here}} |
| 387 |
} |
| 388 |
case 2: { |
| 389 |
MyObj temp2; |
| 390 |
v = temp2; // expected-warning {{object whose reference is captured does not live long enough}} |
| 391 |
break; // expected-note {{destroyed here}} |
| 392 |
} |
| 393 |
default: { |
| 394 |
MyObj temp3; |
| 395 |
v = temp3; // expected-warning {{object whose reference is captured does not live long enough}} |
| 396 |
break; // expected-note {{destroyed here}} |
| 397 |
} |
| 398 |
} |
| 399 |
v.use(); // expected-note 3 {{later used here}} |
| 400 |
} |
| 401 |
|
| 402 |
void loan_from_previous_iteration(MyObj safe, bool condition) { |
| 403 |
MyObj* p = &safe; |
| 404 |
MyObj* q = &safe; |
| 405 |
|
| 406 |
while (condition) { |
| 407 |
MyObj x; |
| 408 |
p = &x; // expected-warning {{may not live long enough}} |
| 409 |
|
| 410 |
if (condition) |
| 411 |
q = p; |
| 412 |
(void)*p; |
| 413 |
(void)*q; // expected-note {{later used here}} |
| 414 |
} // expected-note {{destroyed here}} |
| 415 |
} |
| 416 |
|
| 417 |
void trivial_int_uaf() { |
| 418 |
int * a; |
| 419 |
{ |
| 420 |
int b = 1; |
| 421 |
a = &b; // expected-warning {{object whose reference is captured does not live long enough}} |
| 422 |
} // expected-note {{destroyed here}} |
| 423 |
(void)*a; // expected-note {{later used here}} |
| 424 |
} |
| 425 |
|
| 426 |
void trivial_class_uaf() { |
| 427 |
TriviallyDestructedClass* ptr; |
| 428 |
{ |
| 429 |
TriviallyDestructedClass s; |
| 430 |
ptr = &s; // expected-warning {{object whose reference is captured does not live long enough}} |
| 431 |
} // expected-note {{destroyed here}} |
| 432 |
(void)ptr; // expected-note {{later used here}} |
| 433 |
} |
| 434 |
|
| 435 |
void small_scope_reference_var_no_error() { |
| 436 |
MyObj safe; |
| 437 |
View view; |
| 438 |
{ |
| 439 |
const MyObj& ref = safe; |
| 440 |
view = ref; |
| 441 |
} |
| 442 |
view.use(); |
| 443 |
} |
| 444 |
|
| 445 |
//===----------------------------------------------------------------------===// |
| 446 |
// Basic Definite Use-After-Return (Return-Stack-Address) (-W...permissive) |
| 447 |
// These are cases where the pointer is guaranteed to be dangling at the use site. |
| 448 |
//===----------------------------------------------------------------------===// |
| 449 |
|
| 450 |
MyObj* simple_return_stack_address() { |
| 451 |
MyObj s; |
| 452 |
MyObj* p = &s; // expected-warning {{address of stack memory is returned later}} |
| 453 |
return p; // expected-note {{returned here}} |
| 454 |
} |
| 455 |
|
| 456 |
MyObj* direct_return() { |
| 457 |
MyObj s; |
| 458 |
return &s; // expected-warning {{address of stack memory is returned later}} |
| 459 |
// expected-note@-1 {{returned here}} |
| 460 |
} |
| 461 |
|
| 462 |
const MyObj& return_reference_to_param_no_error(const MyObj& in) { |
| 463 |
return in; |
| 464 |
} |
| 465 |
|
| 466 |
const MyObj& return_reference_to_param_via_ref_no_error(const MyObj& in) { |
| 467 |
const MyObj& ref = in; |
| 468 |
return ref; |
| 469 |
} |
| 470 |
|
| 471 |
const MyObj* getPointer(); |
| 472 |
const MyObj& return_reference_to_param_via_pointer_no_error() { |
| 473 |
const MyObj& ref = *getPointer(); |
| 474 |
return ref; |
| 475 |
} |
| 476 |
|
| 477 |
const MyObj* conditional_assign_unconditional_return(const MyObj& safe, bool c) { |
| 478 |
MyObj s; |
| 479 |
const MyObj* p = &safe; |
| 480 |
if (c) { |
| 481 |
p = &s; // expected-warning {{address of stack memory is returned later}} |
| 482 |
} |
| 483 |
return p; // expected-note {{returned here}} |
| 484 |
} |
| 485 |
|
| 486 |
View conditional_assign_both_branches(const MyObj& safe, bool c) { |
| 487 |
MyObj s; |
| 488 |
View p; |
| 489 |
if (c) { |
| 490 |
p = s; // expected-warning {{address of stack memory is returned later}} |
| 491 |
} |
| 492 |
else { |
| 493 |
p = safe; |
| 494 |
} |
| 495 |
return p; // expected-note {{returned here}} |
| 496 |
|
| 497 |
} |
| 498 |
|
| 499 |
View reassign_safe_to_local(const MyObj& safe) { |
| 500 |
MyObj local; |
| 501 |
View p = safe; |
| 502 |
p = local; // expected-warning {{address of stack memory is returned later}} |
| 503 |
return p; // expected-note {{returned here}} |
| 504 |
} |
| 505 |
|
| 506 |
View pointer_chain_to_local() { |
| 507 |
MyObj local; |
| 508 |
View p1 = local; // expected-warning {{address of stack memory is returned later}} |
| 509 |
View p2 = p1; |
| 510 |
return p2; // expected-note {{returned here}} |
| 511 |
} |
| 512 |
|
| 513 |
View multiple_assign_multiple_return(const MyObj& safe, bool c1, bool c2) { |
| 514 |
MyObj local1; |
| 515 |
MyObj local2; |
| 516 |
View p; |
| 517 |
if (c1) { |
| 518 |
p = local1; // expected-warning {{address of stack memory is returned later}} |
| 519 |
return p; // expected-note {{returned here}} |
| 520 |
} |
| 521 |
else if (c2) { |
| 522 |
p = local2; // expected-warning {{address of stack memory is returned later}} |
| 523 |
return p; // expected-note {{returned here}} |
| 524 |
} |
| 525 |
p = safe; |
| 526 |
return p; |
| 527 |
} |
| 528 |
|
| 529 |
View multiple_assign_single_return(const MyObj& safe, bool c1, bool c2) { |
| 530 |
MyObj local1; |
| 531 |
MyObj local2; |
| 532 |
View p; |
| 533 |
if (c1) { |
| 534 |
p = local1; // expected-warning {{address of stack memory is returned later}} |
| 535 |
} |
| 536 |
else if (c2) { |
| 537 |
p = local2; // expected-warning {{address of stack memory is returned later}} |
| 538 |
} |
| 539 |
else { |
| 540 |
p = safe; |
| 541 |
} |
| 542 |
return p; // expected-note 2 {{returned here}} |
| 543 |
} |
| 544 |
|
| 545 |
View direct_return_of_local() { |
| 546 |
MyObj stack; |
| 547 |
return stack; // expected-warning {{address of stack memory is returned later}} |
| 548 |
// expected-note@-1 {{returned here}} |
| 549 |
} |
| 550 |
|
| 551 |
MyObj& reference_return_of_local() { |
| 552 |
MyObj stack; |
| 553 |
return stack; // expected-warning {{address of stack memory is returned later}} |
| 554 |
// expected-note@-1 {{returned here}} |
| 555 |
} |
| 556 |
|
| 557 |
int* trivial_int_uar() { |
| 558 |
int *a; |
| 559 |
int b = 1; |
| 560 |
a = &b; // expected-warning {{address of stack memory is returned later}} |
| 561 |
return a; // expected-note {{returned here}} |
| 562 |
} |
| 563 |
|
| 564 |
TriviallyDestructedClass* trivial_class_uar () { |
| 565 |
TriviallyDestructedClass *ptr; |
| 566 |
TriviallyDestructedClass s; |
| 567 |
ptr = &s; // expected-warning {{address of stack memory is returned later}} |
| 568 |
return ptr; // expected-note {{returned here}} |
| 569 |
} |
| 570 |
|
| 571 |
const int& return_parameter(int a) { |
| 572 |
return a; // expected-warning {{address of stack memory is returned later}} |
| 573 |
// expected-note@-1 {{returned here}} |
| 574 |
} |
| 575 |
|
| 576 |
int* return_pointer_to_parameter(int a) { |
| 577 |
return &a; // expected-warning {{address of stack memory is returned later}} |
| 578 |
// expected-note@-1 {{returned here}} |
| 579 |
} |
| 580 |
|
| 581 |
const int& return_reference_to_parameter(int a) { |
| 582 |
const int &b = a; // expected-warning {{address of stack memory is returned later}} |
| 583 |
return b; // expected-note {{returned here}} |
| 584 |
} |
| 585 |
int return_reference_to_parameter_no_error(int a) { |
| 586 |
const int &b = a; |
| 587 |
return b; |
| 588 |
} |
| 589 |
|
| 590 |
const int& reference_via_conditional(int a, int b, bool cond) { |
| 591 |
const int &c = (cond ? ((a)) : (b)); // expected-warning 2 {{address of stack memory is returned later}} |
| 592 |
return c; // expected-note 2 {{returned here}} |
| 593 |
} |
| 594 |
const int* return_pointer_to_parameter_via_reference(int a, int b, bool cond) { |
| 595 |
const int &c = cond ? a : b; // expected-warning 2 {{address of stack memory is returned later}} |
| 596 |
const int* d = &c; |
| 597 |
return d; // expected-note 2 {{returned here}} |
| 598 |
} |
| 599 |
|
| 600 |
const int& return_pointer_to_parameter_via_reference_1(int a) { |
| 601 |
const int* d = &a; // expected-warning {{address of stack memory is returned later}} |
| 602 |
return *d; // expected-note {{returned here}} |
| 603 |
} |
| 604 |
|
| 605 |
const int& get_ref_to_local() { |
| 606 |
int a = 42; |
| 607 |
return a; // expected-warning {{address of stack memory is returned later}} |
| 608 |
// expected-note@-1 {{returned here}} |
| 609 |
} |
| 610 |
|
| 611 |
void test_view_pointer() { |
| 612 |
View* vp; |
| 613 |
{ |
| 614 |
View v; |
| 615 |
vp = &v; // expected-warning {{object whose reference is captured does not live long enough}} |
| 616 |
} // expected-note {{destroyed here}} |
| 617 |
vp->use(); // expected-note {{later used here}} |
| 618 |
} |
| 619 |
|
| 620 |
void test_view_double_pointer() { |
| 621 |
View** vpp; |
| 622 |
{ |
| 623 |
View* vp = nullptr; |
| 624 |
vpp = &vp; // expected-warning {{object whose reference is captured does not live long enough}} |
| 625 |
} // expected-note {{destroyed here}} |
| 626 |
(**vpp).use(); // expected-note {{later used here}} |
| 627 |
} |
| 628 |
|
| 629 |
struct PtrHolder { |
| 630 |
int* ptr; |
| 631 |
int* const& getRef() const [[clang::lifetimebound]] { return ptr; } |
| 632 |
}; |
| 633 |
|
| 634 |
int* const& test_ref_to_ptr() { |
| 635 |
PtrHolder a; |
| 636 |
int *const &ref = a.getRef(); // expected-warning {{address of stack memory is returned later}} |
| 637 |
return ref; // expected-note {{returned here}} |
| 638 |
} |
| 639 |
int* const test_ref_to_ptr_no_error() { |
| 640 |
PtrHolder a; |
| 641 |
int *const &ref = a.getRef(); |
| 642 |
return ref; |
| 643 |
} |
| 644 |
|
| 645 |
int** return_inner_ptr_addr(int*** ppp [[clang::lifetimebound]]); |
| 646 |
void test_lifetimebound_multi_level() { |
| 647 |
int** result; |
| 648 |
{ |
| 649 |
int* p = nullptr; |
| 650 |
int** pp = &p; |
| 651 |
int*** ppp = &pp; // expected-warning {{object whose reference is captured does not live long enough}} |
| 652 |
result = return_inner_ptr_addr(ppp); |
| 653 |
} // expected-note {{destroyed here}} |
| 654 |
(void)**result; // expected-note {{used here}} |
| 655 |
} |
| 656 |
|
| 657 |
// FIXME: Assignment does not track the dereference of a pointer. |
| 658 |
void test_assign_through_double_ptr() { |
| 659 |
int a = 1, b = 2; |
| 660 |
int* p = &a; |
| 661 |
int** pp = &p; |
| 662 |
{ |
| 663 |
int c = 3; |
| 664 |
*pp = &c; |
| 665 |
} |
| 666 |
(void)**pp; |
| 667 |
} |
| 668 |
|
| 669 |
int** test_ternary_double_ptr(bool cond) { |
| 670 |
int a = 1, b = 2; |
| 671 |
int* pa = &a; // expected-warning {{address of stack memory is returned later}} |
| 672 |
int* pb = &b; // expected-warning {{address of stack memory is returned later}} |
| 673 |
int** result = cond ? &pa : &pb; // expected-warning 2 {{address of stack memory is returned later}} |
| 674 |
return result; // expected-note 4 {{returned here}} |
| 675 |
} |
| 676 |
//===----------------------------------------------------------------------===// |
| 677 |
// Use-After-Scope & Use-After-Return (Return-Stack-Address) Combined |
| 678 |
// These are cases where the diagnostic kind is determined by location |
| 679 |
//===----------------------------------------------------------------------===// |
| 680 |
|
| 681 |
MyObj* uaf_before_uar() { |
| 682 |
MyObj* p; |
| 683 |
{ |
| 684 |
MyObj local_obj; |
| 685 |
p = &local_obj; // expected-warning {{object whose reference is captured does not live long enough}} |
| 686 |
} // expected-note {{destroyed here}} |
| 687 |
return p; // expected-note {{later used here}} |
| 688 |
} |
| 689 |
|
| 690 |
View uar_before_uaf(const MyObj& safe, bool c) { |
| 691 |
View p; |
| 692 |
{ |
| 693 |
MyObj local_obj; |
| 694 |
p = local_obj; // expected-warning {{address of stack memory is returned later}} |
| 695 |
if (c) { |
| 696 |
return p; // expected-note {{returned here}} |
| 697 |
} |
| 698 |
} |
| 699 |
p.use(); |
| 700 |
p = safe; |
| 701 |
return p; |
| 702 |
} |
| 703 |
|
| 704 |
//===----------------------------------------------------------------------===// |
| 705 |
// No-Error Cases |
| 706 |
//===----------------------------------------------------------------------===// |
| 707 |
void no_error_if_dangle_then_rescue() { |
| 708 |
MyObj safe; |
| 709 |
MyObj* p; |
| 710 |
{ |
| 711 |
MyObj temp; |
| 712 |
p = &temp; // p is temporarily dangling. |
| 713 |
} |
| 714 |
p = &safe; // p is "rescued" before use. |
| 715 |
(void)*p; // This is safe. |
| 716 |
} |
| 717 |
|
| 718 |
void no_error_if_dangle_then_rescue_gsl() { |
| 719 |
MyObj safe; |
| 720 |
View v; |
| 721 |
{ |
| 722 |
MyObj temp; |
| 723 |
v = temp; // 'v' is temporarily dangling. |
| 724 |
} |
| 725 |
v = safe; // 'v' is "rescued" before use by reassigning to a valid object. |
| 726 |
v.use(); // This is safe. |
| 727 |
} |
| 728 |
|
| 729 |
void no_error_loan_from_current_iteration(bool cond) { |
| 730 |
// See https://github.com/llvm/llvm-project/issues/156959. |
| 731 |
MyObj b; |
| 732 |
while (cond) { |
| 733 |
MyObj a; |
| 734 |
View p = b; |
| 735 |
if (cond) { |
| 736 |
p = a; |
| 737 |
} |
| 738 |
(void)p; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
View safe_return(const MyObj& safe) { |
| 743 |
MyObj local; |
| 744 |
View p = local; |
| 745 |
p = safe; // p has been reassigned |
| 746 |
return p; // This is safe |
| 747 |
} |
| 748 |
|
| 749 |
//===----------------------------------------------------------------------===// |
| 750 |
// Lifetimebound Attribute Tests |
| 751 |
//===----------------------------------------------------------------------===// |
| 752 |
|
| 753 |
View Identity(View v [[clang::lifetimebound]]); |
| 754 |
const MyObj& IdentityRef(const MyObj& obj [[clang::lifetimebound]]); |
| 755 |
MyObj* Identity(MyObj* v [[clang::lifetimebound]]); |
| 756 |
View Choose(bool cond, View a [[clang::lifetimebound]], View b [[clang::lifetimebound]]); |
| 757 |
MyObj* GetPointer(const MyObj& obj [[clang::lifetimebound]]); |
| 758 |
|
| 759 |
void lifetimebound_simple_function() { |
| 760 |
View v; |
| 761 |
{ |
| 762 |
MyObj obj; |
| 763 |
v = Identity(obj); // expected-warning {{object whose reference is captured does not live long enough}} |
| 764 |
} // expected-note {{destroyed here}} |
| 765 |
v.use(); // expected-note {{later used here}} |
| 766 |
} |
| 767 |
|
| 768 |
void lifetimebound_multiple_args_definite() { |
| 769 |
View v; |
| 770 |
{ |
| 771 |
MyObj obj1, obj2; |
| 772 |
v = Choose(true, |
| 773 |
obj1, // expected-warning {{object whose reference is captured does not live long enough}} |
| 774 |
obj2); // expected-warning {{object whose reference is captured does not live long enough}} |
| 775 |
} // expected-note 2 {{destroyed here}} |
| 776 |
v.use(); // expected-note 2 {{later used here}} |
| 777 |
} |
| 778 |
|
| 779 |
void lifetimebound_multiple_args_potential(bool cond) { |
| 780 |
MyObj safe; |
| 781 |
View v = safe; |
| 782 |
{ |
| 783 |
MyObj obj1; |
| 784 |
if (cond) { |
| 785 |
MyObj obj2; |
| 786 |
v = Choose(true, |
| 787 |
obj1, // expected-warning {{object whose reference is captured does not live long enough}} |
| 788 |
obj2); // expected-warning {{object whose reference is captured does not live long enough}} |
| 789 |
} // expected-note {{destroyed here}} |
| 790 |
} // expected-note {{destroyed here}} |
| 791 |
v.use(); // expected-note 2 {{later used here}} |
| 792 |
} |
| 793 |
|
| 794 |
View SelectFirst(View a [[clang::lifetimebound]], View b); |
| 795 |
void lifetimebound_mixed_args() { |
| 796 |
View v; |
| 797 |
{ |
| 798 |
MyObj obj1, obj2; |
| 799 |
v = SelectFirst(obj1, // expected-warning {{object whose reference is captured does not live long enough}} |
| 800 |
obj2); |
| 801 |
} // expected-note {{destroyed here}} |
| 802 |
v.use(); // expected-note {{later used here}} |
| 803 |
} |
| 804 |
|
| 805 |
struct LifetimeBoundMember { |
| 806 |
LifetimeBoundMember(); |
| 807 |
View get() const [[clang::lifetimebound]]; |
| 808 |
operator View() const [[clang::lifetimebound]]; |
| 809 |
}; |
| 810 |
|
| 811 |
void lifetimebound_member_function() { |
| 812 |
View v; |
| 813 |
{ |
| 814 |
MyObj obj; |
| 815 |
v = obj.getView(); // expected-warning {{object whose reference is captured does not live long enough}} |
| 816 |
} // expected-note {{destroyed here}} |
| 817 |
v.use(); // expected-note {{later used here}} |
| 818 |
} |
| 819 |
|
| 820 |
struct LifetimeBoundConversionView { |
| 821 |
LifetimeBoundConversionView(); |
| 822 |
~LifetimeBoundConversionView(); |
| 823 |
operator View() const [[clang::lifetimebound]]; |
| 824 |
}; |
| 825 |
|
| 826 |
void lifetimebound_conversion_operator() { |
| 827 |
View v; |
| 828 |
{ |
| 829 |
LifetimeBoundConversionView obj; |
| 830 |
v = obj; // expected-warning {{object whose reference is captured does not live long enough}} |
| 831 |
} // expected-note {{destroyed here}} |
| 832 |
v.use(); // expected-note {{later used here}} |
| 833 |
} |
| 834 |
|
| 835 |
void lifetimebound_chained_calls() { |
| 836 |
View v; |
| 837 |
{ |
| 838 |
MyObj obj; |
| 839 |
v = Identity(Identity(Identity(obj))); // expected-warning {{object whose reference is captured does not live long enough}} |
| 840 |
} // expected-note {{destroyed here}} |
| 841 |
v.use(); // expected-note {{later used here}} |
| 842 |
} |
| 843 |
|
| 844 |
void lifetimebound_with_pointers() { |
| 845 |
MyObj* ptr; |
| 846 |
{ |
| 847 |
MyObj obj; |
| 848 |
ptr = GetPointer(obj); // expected-warning {{object whose reference is captured does not live long enough}} |
| 849 |
} // expected-note {{destroyed here}} |
| 850 |
(void)*ptr; // expected-note {{later used here}} |
| 851 |
} |
| 852 |
|
| 853 |
void lifetimebound_no_error_safe_usage() { |
| 854 |
MyObj obj; |
| 855 |
View v1 = Identity(obj); // No warning - obj lives long enough |
| 856 |
View v2 = Choose(true, v1, Identity(obj)); // No warning - all args are safe |
| 857 |
v2.use(); // Safe usage |
| 858 |
} |
| 859 |
|
| 860 |
void lifetimebound_partial_safety(bool cond) { |
| 861 |
MyObj safe_obj; |
| 862 |
View v = safe_obj; |
| 863 |
|
| 864 |
if (cond) { |
| 865 |
MyObj temp_obj; |
| 866 |
v = Choose(true, |
| 867 |
safe_obj, |
| 868 |
temp_obj); // expected-warning {{object whose reference is captured does not live long enough}} |
| 869 |
} // expected-note {{destroyed here}} |
| 870 |
v.use(); // expected-note {{later used here}} |
| 871 |
} |
| 872 |
|
| 873 |
const MyObj& GetObject(View v [[clang::lifetimebound]]); |
| 874 |
void lifetimebound_return_reference() { |
| 875 |
View v; |
| 876 |
const MyObj* ptr; |
| 877 |
{ |
| 878 |
MyObj obj; |
| 879 |
View temp_v = obj; // expected-warning {{object whose reference is captured does not live long enough}} |
| 880 |
const MyObj& ref = GetObject(temp_v); |
| 881 |
ptr = &ref; |
| 882 |
} // expected-note {{destroyed here}} |
| 883 |
(void)*ptr; // expected-note {{later used here}} |
| 884 |
} |
| 885 |
|
| 886 |
// FIXME: No warning for non gsl::Pointer types. Origin tracking is only supported for pointer types. |
| 887 |
struct LifetimeBoundCtor { |
| 888 |
LifetimeBoundCtor(); |
| 889 |
LifetimeBoundCtor(const MyObj& obj [[clang::lifetimebound]]); |
| 890 |
}; |
| 891 |
|
| 892 |
void lifetimebound_ctor() { |
| 893 |
LifetimeBoundCtor v; |
| 894 |
{ |
| 895 |
MyObj obj; |
| 896 |
v = obj; |
| 897 |
} |
| 898 |
(void)v; |
| 899 |
} |
| 900 |
|
| 901 |
View lifetimebound_return_of_local() { |
| 902 |
MyObj stack; |
| 903 |
return Identity(stack); // expected-warning {{address of stack memory is returned later}} |
| 904 |
// expected-note@-1 {{returned here}} |
| 905 |
} |
| 906 |
|
| 907 |
const MyObj& lifetimebound_return_ref_to_local() { |
| 908 |
MyObj stack; |
| 909 |
return IdentityRef(stack); // expected-warning {{address of stack memory is returned later}} |
| 910 |
// expected-note@-1 {{returned here}} |
| 911 |
} |
| 912 |
|
| 913 |
View lifetimebound_return_by_value_param(MyObj stack_param) { |
| 914 |
return Identity(stack_param); // expected-warning {{address of stack memory is returned later}} |
| 915 |
// expected-note@-1 {{returned here}} |
| 916 |
} |
| 917 |
|
| 918 |
View lifetimebound_return_by_value_multiple_param(int cond, MyObj a, MyObj b, MyObj c) { |
| 919 |
if (cond == 1) |
| 920 |
return Identity(a); // expected-warning {{address of stack memory is returned later}} |
| 921 |
// expected-note@-1 {{returned here}} |
| 922 |
if (cond == 2) |
| 923 |
return Identity(b); // expected-warning {{address of stack memory is returned later}} |
| 924 |
// expected-note@-1 {{returned here}} |
| 925 |
return Identity(c); // expected-warning {{address of stack memory is returned later}} |
| 926 |
// expected-note@-1 {{returned here}} |
| 927 |
} |
| 928 |
|
| 929 |
template<class T> |
| 930 |
View lifetimebound_return_by_value_param_template(T t) { |
| 931 |
return Identity(t); // expected-warning {{address of stack memory is returned later}} |
| 932 |
// expected-note@-1 {{returned here}} |
| 933 |
} |
| 934 |
void use_lifetimebound_return_by_value_param_template() { |
| 935 |
lifetimebound_return_by_value_param_template(MyObj{}); // expected-note {{in instantiation of}} |
| 936 |
} |
| 937 |
|
| 938 |
void lambda_uar_param() { |
| 939 |
auto lambda = [](MyObj stack_param) { |
| 940 |
return Identity(stack_param); // expected-warning {{address of stack memory is returned later}} |
| 941 |
// expected-note@-1 {{returned here}} |
| 942 |
}; |
| 943 |
lambda(MyObj{}); |
| 944 |
} |
| 945 |
|
| 946 |
// FIXME: This should be detected. We see correct destructors but origin flow breaks somewhere. |
| 947 |
namespace VariadicTemplatedParamsUAR { |
| 948 |
|
| 949 |
template<typename... Args> |
| 950 |
View Max(Args... args [[clang::lifetimebound]]); |
| 951 |
|
| 952 |
template<typename... Args> |
| 953 |
View lifetimebound_return_of_variadic_param(Args... args) { |
| 954 |
return Max(args...); |
| 955 |
} |
| 956 |
void test_variadic() { |
| 957 |
lifetimebound_return_of_variadic_param(MyObj{1}, MyObj{2}, MyObj{3}); |
| 958 |
} |
| 959 |
} // namespace VariadicTemplatedParamsUAR |
| 960 |
|
| 961 |
// FIXME: Fails to diagnose UAF when a reference to a by-value param escapes via an out-param. |
| 962 |
void uaf_from_by_value_param_failing(MyObj param, View* out_p) { |
| 963 |
*out_p = Identity(param); |
| 964 |
} |
| 965 |
|
| 966 |
// Conditional operator. |
| 967 |
void conditional_operator_one_unsafe_branch(bool cond) { |
| 968 |
MyObj safe; |
| 969 |
MyObj* p = &safe; |
| 970 |
{ |
| 971 |
MyObj temp; |
| 972 |
p = cond ? &temp // expected-warning {{object whose reference is captured may not live long enough}} |
| 973 |
: &safe; |
| 974 |
} // expected-note {{destroyed here}} |
| 975 |
|
| 976 |
// This is not a use-after-free for any value of `cond` but the analysis |
| 977 |
// cannot reason this and marks the above as a false positive. This |
| 978 |
// ensures safety regardless of cond's value. |
| 979 |
if (cond) |
| 980 |
p = &safe; |
| 981 |
(void)*p; // expected-note {{later used here}} |
| 982 |
} |
| 983 |
|
| 984 |
void conditional_operator_two_unsafe_branches(bool cond) { |
| 985 |
MyObj* p; |
| 986 |
{ |
| 987 |
MyObj a, b; |
| 988 |
p = cond ? &a // expected-warning {{object whose reference is captured does not live long enough}} |
| 989 |
: &b; // expected-warning {{object whose reference is captured does not live long enough}} |
| 990 |
} // expected-note 2 {{destroyed here}} |
| 991 |
(void)*p; // expected-note 2 {{later used here}} |
| 992 |
} |
| 993 |
|
| 994 |
void conditional_operator_nested(bool cond) { |
| 995 |
MyObj* p; |
| 996 |
{ |
| 997 |
MyObj a, b, c, d; |
| 998 |
p = cond ? cond ? &a // expected-warning {{object whose reference is captured does not live long enough}}. |
| 999 |
: &b // expected-warning {{object whose reference is captured does not live long enough}}. |
| 1000 |
: cond ? &c // expected-warning {{object whose reference is captured does not live long enough}}. |
| 1001 |
: &d; // expected-warning {{object whose reference is captured does not live long enough}}. |
| 1002 |
} // expected-note 4 {{destroyed here}} |
| 1003 |
(void)*p; // expected-note 4 {{later used here}} |
| 1004 |
} |
| 1005 |
|
| 1006 |
void conditional_operator_lifetimebound(bool cond) { |
| 1007 |
MyObj* p; |
| 1008 |
{ |
| 1009 |
MyObj a, b; |
| 1010 |
p = Identity(cond ? &a // expected-warning {{object whose reference is captured does not live long enough}} |
| 1011 |
: &b); // expected-warning {{object whose reference is captured does not live long enough}} |
| 1012 |
} // expected-note 2 {{destroyed here}} |
| 1013 |
(void)*p; // expected-note 2 {{later used here}} |
| 1014 |
} |
| 1015 |
|
| 1016 |
void conditional_operator_lifetimebound_nested(bool cond) { |
| 1017 |
MyObj* p; |
| 1018 |
{ |
| 1019 |
MyObj a, b; |
| 1020 |
p = Identity(cond ? Identity(&a) // expected-warning {{object whose reference is captured does not live long enough}} |
| 1021 |
: Identity(&b)); // expected-warning {{object whose reference is captured does not live long enough}} |
| 1022 |
} // expected-note 2 {{destroyed here}} |
| 1023 |
(void)*p; // expected-note 2 {{later used here}} |
| 1024 |
} |
| 1025 |
|
| 1026 |
void conditional_operator_lifetimebound_nested_deep(bool cond) { |
| 1027 |
MyObj* p; |
| 1028 |
{ |
| 1029 |
MyObj a, b, c, d; |
| 1030 |
p = Identity(cond ? Identity(cond ? &a // expected-warning {{object whose reference is captured does not live long enough}} |
| 1031 |
: &b) // expected-warning {{object whose reference is captured does not live long enough}} |
| 1032 |
: Identity(cond ? &c // expected-warning {{object whose reference is captured does not live long enough}} |
| 1033 |
: &d)); // expected-warning {{object whose reference is captured does not live long enough}} |
| 1034 |
} // expected-note 4 {{destroyed here}} |
| 1035 |
(void)*p; // expected-note 4 {{later used here}} |
| 1036 |
} |
| 1037 |
|
| 1038 |
void parentheses(bool cond) { |
| 1039 |
MyObj* p; |
| 1040 |
{ |
| 1041 |
MyObj a; |
| 1042 |
p = &((((a)))); // expected-warning {{object whose reference is captured does not live long enough}} |
| 1043 |
} // expected-note {{destroyed here}} |
| 1044 |
(void)*p; // expected-note {{later used here}} |
| 1045 |
|
| 1046 |
{ |
| 1047 |
MyObj a; |
| 1048 |
p = ((GetPointer((a)))); // expected-warning {{object whose reference is captured does not live long enough}} |
| 1049 |
} // expected-note {{destroyed here}} |
| 1050 |
(void)*p; // expected-note {{later used here}} |
| 1051 |
|
| 1052 |
{ |
| 1053 |
MyObj a, b, c, d; |
| 1054 |
p = &(cond ? (cond ? a // expected-warning {{object whose reference is captured does not live long enough}}. |
| 1055 |
: b) // expected-warning {{object whose reference is captured does not live long enough}}. |
| 1056 |
: (cond ? c // expected-warning {{object whose reference is captured does not live long enough}}. |
| 1057 |
: d)); // expected-warning {{object whose reference is captured does not live long enough}}. |
| 1058 |
} // expected-note 4 {{destroyed here}} |
| 1059 |
(void)*p; // expected-note 4 {{later used here}} |
| 1060 |
|
| 1061 |
{ |
| 1062 |
MyObj a, b, c, d; |
| 1063 |
p = ((cond ? (((cond ? &a : &b))) // expected-warning 2 {{object whose reference is captured does not live long enough}}. |
| 1064 |
: &(((cond ? c : d))))); // expected-warning 2 {{object whose reference is captured does not live long enough}}. |
| 1065 |
} // expected-note 4 {{destroyed here}} |
| 1066 |
(void)*p; // expected-note 4 {{later used here}} |
| 1067 |
} |
| 1068 |
|
| 1069 |
namespace GH162834 { |
| 1070 |
// https://github.com/llvm/llvm-project/issues/162834 |
| 1071 |
template <class T> |
| 1072 |
struct StatusOr { |
| 1073 |
~StatusOr() {} |
| 1074 |
const T& value() const& [[clang::lifetimebound]] { return data; } |
| 1075 |
|
| 1076 |
private: |
| 1077 |
T data; |
| 1078 |
}; |
| 1079 |
|
| 1080 |
StatusOr<View> getViewOr(); |
| 1081 |
StatusOr<MyObj> getStringOr(); |
| 1082 |
StatusOr<MyObj*> getPointerOr(); |
| 1083 |
|
| 1084 |
void foo() { |
| 1085 |
View view; |
| 1086 |
{ |
| 1087 |
StatusOr<View> view_or = getViewOr(); |
| 1088 |
view = view_or.value(); |
| 1089 |
} |
| 1090 |
(void)view; |
| 1091 |
} |
| 1092 |
|
| 1093 |
void bar() { |
| 1094 |
MyObj* pointer; |
| 1095 |
{ |
| 1096 |
StatusOr<MyObj*> pointer_or = getPointerOr(); |
| 1097 |
pointer = pointer_or.value(); |
| 1098 |
} |
| 1099 |
(void)*pointer; |
| 1100 |
} |
| 1101 |
|
| 1102 |
void foobar() { |
| 1103 |
View view; |
| 1104 |
{ |
| 1105 |
StatusOr<MyObj> string_or = getStringOr(); |
| 1106 |
view = string_or. // expected-warning {{object whose reference is captured does not live long enough}} |
| 1107 |
value(); |
| 1108 |
} // expected-note {{destroyed here}} |
| 1109 |
(void)view; // expected-note {{later used here}} |
| 1110 |
} |
| 1111 |
} // namespace GH162834 |
| 1112 |
|
| 1113 |
namespace RangeBasedForLoop { |
| 1114 |
struct MyObjStorage { |
| 1115 |
MyObj objs[1]; |
| 1116 |
MyObjStorage() {} |
| 1117 |
~MyObjStorage() {} |
| 1118 |
const MyObj *begin() const [[clang::lifetimebound]] { return objs; } |
| 1119 |
const MyObj *end() const { return objs + 1; } |
| 1120 |
}; |
| 1121 |
|
| 1122 |
void range_based_for_use_after_scope() { |
| 1123 |
View v; |
| 1124 |
{ |
| 1125 |
MyObjStorage s; |
| 1126 |
for (const MyObj &o : s) { // expected-warning {{object whose reference is captured does not live long enough}} |
| 1127 |
v = o; |
| 1128 |
} |
| 1129 |
} // expected-note {{destroyed here}} |
| 1130 |
v.use(); // expected-note {{later used here}} |
| 1131 |
} |
| 1132 |
|
| 1133 |
View range_based_for_use_after_return() { |
| 1134 |
MyObjStorage s; |
| 1135 |
for (const MyObj &o : s) { // expected-warning {{address of stack memory is returned later}} |
| 1136 |
return o; // expected-note {{returned here}} |
| 1137 |
} |
| 1138 |
return *s.begin(); // expected-warning {{address of stack memory is returned later}} |
| 1139 |
// expected-note@-1 {{returned here}} |
| 1140 |
} |
| 1141 |
|
| 1142 |
void range_based_for_not_reference() { |
| 1143 |
View v; |
| 1144 |
{ |
| 1145 |
MyObjStorage s; |
| 1146 |
for (MyObj o : s) { // expected-note {{destroyed here}} |
| 1147 |
v = o; // expected-warning {{object whose reference is captured may not live long enough}} |
| 1148 |
} |
| 1149 |
} |
| 1150 |
v.use(); // expected-note {{later used here}} |
| 1151 |
} |
| 1152 |
|
| 1153 |
void range_based_for_no_error() { |
| 1154 |
View v; |
| 1155 |
MyObjStorage s; |
| 1156 |
for (const MyObj &o : s) { |
| 1157 |
v = o; |
| 1158 |
} |
| 1159 |
v.use(); |
| 1160 |
} |
| 1161 |
|
| 1162 |
} // namespace RangeBaseForLoop |
| 1163 |
|
| 1164 |
namespace UserDefinedDereference { |
| 1165 |
// Test user-defined dereference operators with lifetimebound |
| 1166 |
template<typename T> |
| 1167 |
struct SmartPtr { |
| 1168 |
T* ptr; |
| 1169 |
SmartPtr() {} |
| 1170 |
SmartPtr(T* p) : ptr(p) {} |
| 1171 |
T& operator*() const [[clang::lifetimebound]] { return *ptr; } |
| 1172 |
T* operator->() const [[clang::lifetimebound]] { return ptr; } |
| 1173 |
}; |
| 1174 |
|
| 1175 |
void test_user_defined_deref_uaf() { |
| 1176 |
MyObj* p; |
| 1177 |
{ |
| 1178 |
MyObj obj; |
| 1179 |
SmartPtr<MyObj> smart_ptr(&obj); |
| 1180 |
p = &(*smart_ptr); // expected-warning {{object whose reference is captured does not live long enough}} |
| 1181 |
} // expected-note {{destroyed here}} |
| 1182 |
(void)*p; // expected-note {{later used here}} |
| 1183 |
} |
| 1184 |
|
| 1185 |
MyObj& test_user_defined_deref_uar() { |
| 1186 |
MyObj obj; |
| 1187 |
SmartPtr<MyObj> smart_ptr(&obj); |
| 1188 |
return *smart_ptr; // expected-warning {{address of stack memory is returned later}} |
| 1189 |
// expected-note@-1 {{returned here}} |
| 1190 |
} |
| 1191 |
|
| 1192 |
void test_user_defined_deref_with_view() { |
| 1193 |
View v; |
| 1194 |
{ |
| 1195 |
MyObj obj; |
| 1196 |
SmartPtr<MyObj> smart_ptr(&obj); |
| 1197 |
v = *smart_ptr; // expected-warning {{object whose reference is captured does not live long enough}} |
| 1198 |
} // expected-note {{destroyed here}} |
| 1199 |
v.use(); // expected-note {{later used here}} |
| 1200 |
} |
| 1201 |
|
| 1202 |
void test_user_defined_deref_arrow() { |
| 1203 |
MyObj* p; |
| 1204 |
{ |
| 1205 |
MyObj obj; |
| 1206 |
SmartPtr<MyObj> smart_ptr(&obj); |
| 1207 |
p = smart_ptr.operator->(); // expected-warning {{object whose reference is captured does not live long enough}} |
| 1208 |
} // expected-note {{destroyed here}} |
| 1209 |
(void)*p; // expected-note {{later used here}} |
| 1210 |
} |
| 1211 |
|
| 1212 |
void test_user_defined_deref_chained() { |
| 1213 |
MyObj* p; |
| 1214 |
{ |
| 1215 |
MyObj obj; |
| 1216 |
SmartPtr<SmartPtr<MyObj>> double_ptr; |
| 1217 |
p = &(**double_ptr); // expected-warning {{object whose reference is captured does not live long enough}} |
| 1218 |
} // expected-note {{destroyed here}} |
| 1219 |
(void)*p; // expected-note {{later used here}} |
| 1220 |
} |
| 1221 |
|
| 1222 |
} // namespace UserDefinedDereference |
| 1223 |
|
| 1224 |
namespace structured_binding { |
| 1225 |
struct Pair { |
| 1226 |
MyObj a; |
| 1227 |
MyObj b; |
| 1228 |
Pair() {} |
| 1229 |
~Pair() {} |
| 1230 |
}; |
| 1231 |
|
| 1232 |
// FIXME: Detect this. |
| 1233 |
void structured_binding_use_after_scope() { |
| 1234 |
View v; |
| 1235 |
{ |
| 1236 |
Pair p; |
| 1237 |
auto &[a_ref, b_ref] = p; |
| 1238 |
v = a_ref; |
| 1239 |
} |
| 1240 |
v.use(); |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
namespace MaxFnLifetimeBound { |
| 1245 |
|
| 1246 |
template<class T> |
| 1247 |
T&& MaxT(T&& a [[clang::lifetimebound]], T&& b [[clang::lifetimebound]]); |
| 1248 |
|
| 1249 |
const MyObj& call_max_with_obj() { |
| 1250 |
MyObj oa, ob; |
| 1251 |
return MaxT(oa, // expected-warning {{address of stack memory is returned later}} |
| 1252 |
// expected-note@-1 2 {{returned here}} |
| 1253 |
ob); // expected-warning {{address of stack memory is returned later}} |
| 1254 |
|
| 1255 |
} |
| 1256 |
|
| 1257 |
MyObj* call_max_with_obj_error() { |
| 1258 |
MyObj oa, ob; |
| 1259 |
return &MaxT(oa, // expected-warning {{address of stack memory is returned later}} |
| 1260 |
// expected-note@-1 2 {{returned here}} |
| 1261 |
ob); // expected-warning {{address of stack memory is returned later}} |
| 1262 |
} |
| 1263 |
|
| 1264 |
const MyObj* call_max_with_ref_obj_error() { |
| 1265 |
MyObj oa, ob; |
| 1266 |
const MyObj& refa = oa; // expected-warning {{address of stack memory is returned later}} |
| 1267 |
const MyObj& refb = ob; // expected-warning {{address of stack memory is returned later}} |
| 1268 |
return &MaxT(refa, refb); // expected-note 2 {{returned here}} |
| 1269 |
} |
| 1270 |
const MyObj& call_max_with_ref_obj_return_ref_error() { |
| 1271 |
MyObj oa, ob; |
| 1272 |
const MyObj& refa = oa; // expected-warning {{address of stack memory is returned later}} |
| 1273 |
const MyObj& refb = ob; // expected-warning {{address of stack memory is returned later}} |
| 1274 |
return MaxT(refa, refb); // expected-note 2 {{returned here}} |
| 1275 |
} |
| 1276 |
|
| 1277 |
MyObj call_max_with_obj_no_error() { |
| 1278 |
MyObj oa, ob; |
| 1279 |
return MaxT(oa, ob); |
| 1280 |
} |
| 1281 |
|
| 1282 |
const MyObj& call_max_with_ref_obj_no_error(const MyObj& a, const MyObj& b) { |
| 1283 |
return MaxT(a, b); |
| 1284 |
} |
| 1285 |
|
| 1286 |
const View& call_max_with_view_with_error() { |
| 1287 |
View va, vb; |
| 1288 |
return MaxT(va, // expected-warning {{address of stack memory is returned later}} |
| 1289 |
// expected-note@-1 2 {{returned here}} |
| 1290 |
vb); // expected-warning {{address of stack memory is returned later}} |
| 1291 |
} |
| 1292 |
|
| 1293 |
struct [[gsl::Pointer]] NonTrivialPointer { ~NonTrivialPointer(); }; |
| 1294 |
|
| 1295 |
const NonTrivialPointer& call_max_with_non_trivial_view_with_error() { |
| 1296 |
NonTrivialPointer va, vb; |
| 1297 |
return MaxT(va, // expected-warning {{address of stack memory is returned later}} |
| 1298 |
// expected-note@-1 2 {{returned here}} |
| 1299 |
vb); // expected-warning {{address of stack memory is returned later}} |
| 1300 |
} |
| 1301 |
|
| 1302 |
namespace MultiPointerTypes { |
| 1303 |
int** return_2p() { |
| 1304 |
int a = 1; |
| 1305 |
int* b = &a; // expected-warning {{address of stack memory is returned later}} |
| 1306 |
int** c = &b; // expected-warning {{address of stack memory is returned later}} |
| 1307 |
return c; // expected-note 2 {{returned here}} |
| 1308 |
} |
| 1309 |
|
| 1310 |
int** return_2p_one_is_safe(int& a) { |
| 1311 |
int* b = &a; |
| 1312 |
int** c = &b; // expected-warning {{address of stack memory is returned later}} |
| 1313 |
return c; // expected-note {{returned here}} |
| 1314 |
} |
| 1315 |
|
| 1316 |
int*** return_3p() { |
| 1317 |
int a = 1; |
| 1318 |
int* b = &a; // expected-warning {{address of stack memory is returned later}} |
| 1319 |
int** c = &b; // expected-warning {{address of stack memory is returned later}} |
| 1320 |
int*** d = &c; // expected-warning {{address of stack memory is returned later}} |
| 1321 |
return d; // expected-note 3 {{returned here}} |
| 1322 |
} |
| 1323 |
|
| 1324 |
View** return_view_p() { |
| 1325 |
MyObj a; |
| 1326 |
View b = a; // expected-warning {{address of stack memory is returned later}} |
| 1327 |
View* c = &b; // expected-warning {{address of stack memory is returned later}} |
| 1328 |
View** d = &c; // expected-warning {{address of stack memory is returned later}} |
| 1329 |
return d; // expected-note 3 {{returned here}} |
| 1330 |
} |
| 1331 |
|
| 1332 |
} // namespace MultiPointerTypes |
| 1333 |
|
| 1334 |
View call_max_with_view_without_error() { |
| 1335 |
View va, vb; |
| 1336 |
return MaxT(va, vb); |
| 1337 |
} |
| 1338 |
|
| 1339 |
} // namespace StdMaxStyleLifetimeBound |
| 1340 |
|
| 1341 |
namespace CppCoverage { |
| 1342 |
|
| 1343 |
int getInt(); |
| 1344 |
|
| 1345 |
void ReferenceParam(unsigned Value, unsigned &Ref) { |
| 1346 |
Value = getInt(); |
| 1347 |
Ref = getInt(); |
| 1348 |
} |
| 1349 |
|
| 1350 |
inline void normalize(int &exponent, int &mantissa) { |
| 1351 |
const int shift = 1; |
| 1352 |
exponent -= shift; |
| 1353 |
mantissa <<= shift; |
| 1354 |
} |
| 1355 |
|
| 1356 |
void add(int c, MyObj* node) { |
| 1357 |
MyObj* arr[10]; |
| 1358 |
arr[4] = node; |
| 1359 |
} |
| 1360 |
} // namespace CppCoverage |
| 1361 |
|
| 1362 |
// Implicit this annotations with redecls. |
| 1363 |
namespace GH172013 { |
| 1364 |
// https://github.com/llvm/llvm-project/issues/62072 |
| 1365 |
// https://github.com/llvm/llvm-project/issues/172013 |
| 1366 |
struct S { |
| 1367 |
View x() const [[clang::lifetimebound]]; |
| 1368 |
MyObj i; |
| 1369 |
}; |
| 1370 |
|
| 1371 |
View S::x() const { return i; } |
| 1372 |
|
| 1373 |
void bar() { |
| 1374 |
View x; |
| 1375 |
{ |
| 1376 |
S s; |
| 1377 |
x = s.x(); // expected-warning {{object whose reference is captured does not live long enough}} |
| 1378 |
View y = S().x(); // FIXME: Handle temporaries. |
| 1379 |
} // expected-note {{destroyed here}} |
| 1380 |
(void)x; // expected-note {{used here}} |
| 1381 |
} |
| 1382 |
} |