Logo AND Algorithmique Numérique Distribuée

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