Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / teshsuite / xbt / mmalloc / mmalloc_test.cpp
1 /* Copyright (c) 2012-2022. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/Exception.hpp"
7 #include "xbt.h"
8 #include "xbt/mmalloc.h"
9
10 #include <array>
11 #include <cassert>
12 #include <cstdio>
13 #include <cstdlib>
14 #include <cstring>
15 #include <fcntl.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <vector>
19
20 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"this test");
21
22 constexpr int BUFFSIZE = 204800;
23 constexpr int TESTSIZE = 100;
24
25 #define size_of_block(i) ((((i) % 50) + 1) * 100)
26
27 static void check_block(const unsigned char* p, unsigned char b, int n)
28 {
29   for (int i = 0; i < n; i++)
30     xbt_assert(p[i] == b, "value mismatch: %p[%d] = %#hhx, expected %#hhx", p, i, p[i], b);
31 }
32
33 int main(int argc, char**argv)
34 {
35   xbt_mheap_t heapA = nullptr;
36   std::array<void*, TESTSIZE> pointers;
37   xbt_init(&argc,argv);
38
39   XBT_INFO("Allocating a new heap");
40   unsigned long mask = ~((unsigned long)xbt_pagesize - 1);
41   auto* addr         = reinterpret_cast<void*>(((unsigned long)sbrk(0) + BUFFSIZE) & mask);
42   heapA              = xbt_mheap_new(addr, 0);
43   if (heapA == nullptr) {
44     perror("attach 1 failed");
45     fprintf(stderr, "bye\n");
46     exit(1);
47   }
48
49   XBT_INFO("HeapA allocated");
50
51   int i;
52   int size;
53   for (i = 0; i < TESTSIZE; i++) {
54     size = size_of_block(i);
55     pointers[i] = mmalloc(heapA, size);
56     XBT_INFO("%d bytes allocated with offset %zx", size, (size_t)((char*)pointers[i] - (char*)heapA));
57   }
58   XBT_INFO("All blocks were correctly allocated. Free every second block");
59   for (i = 0; i < TESTSIZE; i+=2) {
60     mfree(heapA, pointers[i]);
61   }
62   XBT_INFO("Memset every second block to zero (yeah, they are not currently allocated :)");
63   for (i = 0; i < TESTSIZE; i+=2) {
64     size = size_of_block(i);
65     memset(pointers[i],0, size);
66   }
67   XBT_INFO("Re-allocate every second block");
68   for (i = 0; i < TESTSIZE; i+=2) {
69     size = size_of_block(i);
70     pointers[i] = mmalloc(heapA, size);
71   }
72
73   XBT_INFO("free all blocks (each one twice, to check that double free are correctly caught)");
74   for (i = 0; i < TESTSIZE; i++) {
75     bool gotit = false;
76     mfree(heapA, pointers[i]);
77     try {
78       mfree(heapA, pointers[i]);
79     } catch (const simgrid::Exception&) {
80       gotit = true;
81     }
82     xbt_assert(gotit, "FAIL: A double-free went undetected (for size:%d)", size_of_block(i));
83   }
84
85   XBT_INFO("free again all blocks (to really check that double free are correctly caught)");
86   for (i = 0; i < TESTSIZE; i++) {
87     bool gotit = false;
88     try {
89       mfree(heapA, pointers[i]);
90     } catch (const simgrid::Exception&) {
91       gotit = true;
92     }
93     xbt_assert(gotit, "FAIL: A double-free went undetected (for size:%d)", size_of_block(i));
94   }
95
96   XBT_INFO("Let's try different codepaths for mrealloc");
97   for (i = 0; i < TESTSIZE; i++) {
98     const std::vector<std::pair<int, unsigned char>> requests = {
99         {size_of_block(i) / 2, 0x77}, {size_of_block(i) * 2, 0xaa}, {1, 0xc0}, {0, 0}};
100     pointers[i] = nullptr;
101     for (unsigned k = 0; k < requests.size(); ++k) {
102       size        = requests[k].first;
103       pointers[i] = mrealloc(heapA, pointers[i], size);
104       if (k > 0)
105         check_block(static_cast<unsigned char*>(pointers[i]), requests[k - 1].second,
106                     std::min(size, requests[k - 1].first));
107       if (size > 0)
108         memset(pointers[i], requests[k].second, size);
109     }
110   }
111
112   XBT_INFO("Damnit, I cannot break mmalloc this time. That's SO disappointing.");
113   return 0;
114 }