Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
18fdca9d4f4770576a1b123adcacc4f7be484ea8
[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 #define BUFFSIZE 204800
21 #define TESTSIZE 100
22 #define size_of_block(i) (((i % 50)+1)* 100)
23
24 static void check_block(const void* s, int c, int n)
25 {
26   const unsigned char* p = static_cast<const unsigned char*>(s);
27   unsigned char b        = static_cast<unsigned char>(c);
28   for (int i = 0; i < n; i++)
29     if (p[i] != b)
30       xbt_die("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   void *pointers[TESTSIZE];
37   xbt_init(&argc,argv);
38
39   XBT_INFO("Allocating a new heap");
40   unsigned long mask = ~((unsigned long)xbt_pagesize - 1);
41   void *addr = (void*)(((unsigned long)sbrk(0) + BUFFSIZE) & mask);
42   heapA = xbt_mheap_new(-1, addr);
43   if (heapA == NULL) {
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 catched)");
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(xbt_ex& e) {
80       gotit = true;
81     }
82     if (not gotit)
83       xbt_die("FAIL: A double-free went undetected (for size:%d)",size_of_block(i));
84   }
85
86   XBT_INFO("free again all blocks (to really check that double free are correctly catched)");
87   for (i = 0; i < TESTSIZE; i++) {
88     bool gotit = false;
89     try {
90       mfree(heapA, pointers[i]);
91     } catch(xbt_ex& e) {
92       gotit = true;
93     }
94     if (not gotit)
95       xbt_die("FAIL: A double-free went undetected (for size:%d)",size_of_block(i));
96   }
97
98   XBT_INFO("Let's try different codepaths for mrealloc");
99   for (i = 0; i < TESTSIZE; i++) {
100     const std::vector<std::pair<int, int>> requests = {
101         {size_of_block(i) / 2, 0x77}, {size_of_block(i) * 2, 0xaa}, {1, 0xc0}, {0, 0}};
102     pointers[i] = nullptr;
103     for (unsigned k = 0; k < requests.size(); ++k) {
104       size        = requests[k].first;
105       pointers[i] = mrealloc(heapA, pointers[i], size);
106       if (k > 0)
107         check_block(pointers[i], requests[k - 1].second, 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 }