lib/deadalloc/src/smoke/cxx.cpp

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 #include <cstdint>
 2 #include <cstring>
 3 #include <malloc.h>
 4 #include <new>
 5 
 6 extern "C" void *__libc_malloc(std::size_t);
 7 extern "C" void *__libc_calloc(std::size_t, std::size_t);
 8 extern "C" void __libc_free(void *);
 9 
10 int main() {
11   char *array = new char[64];
12   for (int i = 0; i < 64; ++i) array[i] = static_cast<char>(i);
13   for (int i = 0; i < 64; ++i) if (array[i] != static_cast<char>(i)) return 1;
14   delete[] array;
15 
16   void *scalar = ::operator new(37);
17   if (scalar == nullptr) return 2;
18   std::memset(scalar, 0x5a, 37);
19   ::operator delete(scalar);
20 
21   char *nothrow_array = new (std::nothrow) char[33];
22   if (nothrow_array == nullptr) return 3;
23   nothrow_array[0] = 7;
24   delete[] nothrow_array;
25 
26   void *aligned_scalar = ::operator new(57, std::align_val_t(64));
27   if ((reinterpret_cast<std::uintptr_t>(aligned_scalar) & 63) != 0) return 4;
28   ::operator delete(aligned_scalar, std::align_val_t(64));
29 
30   char *aligned_nothrow_array = new (std::align_val_t(128), std::nothrow) char[65];
31   if (aligned_nothrow_array == nullptr) return 5;
32   if ((reinterpret_cast<std::uintptr_t>(aligned_nothrow_array) & 127) != 0) return 6;
33   ::operator delete[](aligned_nothrow_array, std::align_val_t(128), std::nothrow);
34 
35   void *libc_scalar = __libc_malloc(48);
36   if (libc_scalar == nullptr) return 7;
37   if (malloc_usable_size(libc_scalar) == 0) return 8;
38   std::memset(libc_scalar, 0x33, 48);
39   __libc_free(libc_scalar);
40 
41   unsigned char *libc_zeroed = static_cast<unsigned char *>(__libc_calloc(5, 8));
42   if (libc_zeroed == nullptr) return 9;
43   if (malloc_usable_size(libc_zeroed) == 0) return 10;
44   for (int i = 0; i < 40; ++i) if (libc_zeroed[i] != 0) return 11;
45   __libc_free(libc_zeroed);
46   return 0;
47 }