reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
// RUN: %clangxx -std=c++1z -faligned-allocation -O0 %s -o %t && %run %t
// RUN: %clangxx -std=c++1z -faligned-allocation -fsized-deallocation -O0 %s -o %t && %run %t

// ubsan does not intercept new/delete.
// UNSUPPORTED: ubsan

// Check that all new/delete variants are defined and work with supported
// sanitizers. Sanitizer-specific failure modes tests are supposed to go to
// the particular sanitizier's test suites.

#include <cstddef>

// Define all new/delete to do not depend on the version provided by the
// platform. The implementation is provided by the sanitizer anyway.

namespace std {
struct nothrow_t {};
static const nothrow_t nothrow;
enum class align_val_t : size_t {};
}  // namespace std

void *operator new(size_t);
void *operator new[](size_t);
void *operator new(size_t, std::nothrow_t const&);
void *operator new[](size_t, std::nothrow_t const&);
void *operator new(size_t, std::align_val_t);
void *operator new[](size_t, std::align_val_t);
void *operator new(size_t, std::align_val_t, std::nothrow_t const&);
void *operator new[](size_t, std::align_val_t, std::nothrow_t const&);

void operator delete(void*) throw();
void operator delete[](void*) throw();
void operator delete(void*, std::nothrow_t const&);
void operator delete[](void*, std::nothrow_t const&);
void operator delete(void*, size_t) throw();
void operator delete[](void*, size_t) throw();
void operator delete(void*, std::align_val_t) throw();
void operator delete[](void*, std::align_val_t) throw();
void operator delete(void*, std::align_val_t, std::nothrow_t const&);
void operator delete[](void*, std::align_val_t, std::nothrow_t const&);
void operator delete(void*, size_t, std::align_val_t) throw();
void operator delete[](void*, size_t, std::align_val_t) throw();


template<typename T>
inline T* break_optimization(T *arg) {
  __asm__ __volatile__("" : : "r" (arg) : "memory");
  return arg;
}


struct S12 { int a, b, c; };
struct alignas(128) S12_128 { int a, b, c; };
struct alignas(256) S12_256 { int a, b, c; };
struct alignas(512) S1024_512 { char a[1024]; };
struct alignas(1024) S1024_1024 { char a[1024]; };


int main(int argc, char **argv) {
  delete break_optimization(new S12);
  operator delete(break_optimization(new S12), std::nothrow);
  delete [] break_optimization(new S12[100]);
  operator delete[](break_optimization(new S12[100]), std::nothrow);

  delete break_optimization(new S12_128);
  operator delete(break_optimization(new S12_128),
                  std::align_val_t(alignof(S12_128)));
  operator delete(break_optimization(new S12_128),
                  std::align_val_t(alignof(S12_128)), std::nothrow);
  operator delete(break_optimization(new S12_128), sizeof(S12_128),
                  std::align_val_t(alignof(S12_128)));

  delete [] break_optimization(new S12_128[100]);
  operator delete[](break_optimization(new S12_128[100]),
                    std::align_val_t(alignof(S12_128)));
  operator delete[](break_optimization(new S12_128[100]),
                    std::align_val_t(alignof(S12_128)), std::nothrow);
  operator delete[](break_optimization(new S12_128[100]), sizeof(S12_128[100]),
                    std::align_val_t(alignof(S12_128)));
}