Logo AND Algorithmique Numérique Distribuée

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