brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · 695a31e Raw
45 lines · cpp
1// REQUIRES: gwp_asan2// RUN: %clangxx_gwp_asan %s -o %t -DTEST_MALLOC3// RUN: %expect_crash %run %t 2>&1 | FileCheck %s --check-prefix CHECK-MALLOC4 5// Check both C++98 and C.6// RUN: %clangxx_gwp_asan -std=c++98 %s -o %t -DTEST_FREE7// RUN: %expect_crash %run %t 2>&1 | FileCheck %s --check-prefix CHECK-FREE8// RUN: cp %s %t.c && %clang_gwp_asan %t.c -o %t -DTEST_FREE9// RUN: %expect_crash %run %t 2>&1 | FileCheck %s --check-prefix CHECK-FREE10 11// Ensure GWP-ASan stub implementation of realloc() in Scudo works to-spec. In12// particular, the behaviour regarding realloc of size zero is interesting, as13// it's defined as free().14 15#include <stdlib.h>16 17int main() {18#if defined(TEST_MALLOC)19  // realloc(nullptr, size) is equivalent to malloc(size).20  char *Ptr = reinterpret_cast<char *>(realloc(nullptr, 1));21  *Ptr = 0;22  // Trigger an INVALID_FREE to the right.23  free(Ptr + 1);24 25  // CHECK-MALLOC: GWP-ASan detected a memory error26  // CHECK-MALLOC: Invalid (Wild) Free at 0x{{[a-f0-9]+}} (1 byte to the right27  // CHECK-MALLOC-SAME: of a 1-byte allocation28#elif defined(TEST_FREE)29  char *Ptr = (char *) malloc(1);30  // realloc(ptr, 0) is equivalent to free(ptr) and must return nullptr. Note31  // that this is only the specification in C++98 and C.32  if (realloc(Ptr, 0) != NULL) {33 34  }35  // Trigger a USE_AFTER_FREE.36  *Ptr = 0;37 38  // CHECK-FREE: GWP-ASan detected a memory error39  // CHECK-FREE: Use After Free at 0x{{[a-f0-9]+}} (0 bytes into a 1-byte40  // CHECK-FREE-SAME: allocation41#endif42 43  return 0;44}45