File: test-wrong.c

package info (click to toggle)
mimalloc 3.1.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,264 kB
  • sloc: ansic: 15,699; cpp: 397; makefile: 21
file content (92 lines) | stat: -rw-r--r-- 1,995 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* ----------------------------------------------------------------------------
Copyright (c) 2018-2020, Microsoft Research, Daan Leijen
This is free software; you can redistribute it and/or modify it under the
terms of the MIT license. A copy of the license can be found in the file
"LICENSE" at the root of this distribution.
-----------------------------------------------------------------------------*/

/* test file for valgrind/asan support.

   VALGRIND:
   ----------
   Compile in an "out/debug" folder:

   > cd out/debug
   > cmake ../.. -DMI_TRACK_VALGRIND=1
   > make -j8

   and then compile this file as:

   > gcc -g -o test-wrong -I../../include ../../test/test-wrong.c libmimalloc-valgrind-debug.a -lpthread

   and test as:

   > valgrind ./test-wrong

   
   ASAN
   ----------
   Compile in an "out/debug" folder:

   > cd out/debug
   > cmake ../.. -DMI_TRACK_ASAN=1
   > make -j8

   and then compile this file as:

   > clang -g -o test-wrong -I../../include ../../test/test-wrong.c libmimalloc-asan-debug.a -lpthread -fsanitize=address -fsanitize-recover=address

   and test as:

   > ASAN_OPTIONS=verbosity=1:halt_on_error=0 ./test-wrong


*/
#include <stdio.h>
#include <stdlib.h>
#include "mimalloc.h"

#ifdef USE_STD_MALLOC
# define mi(x) x
#else
# define mi(x) mi_##x
#endif

int main(int argc, char** argv) {
  int* p = (int*)mi(malloc)(3*sizeof(int));

  int* r = (int*)mi_malloc_aligned(8,16);
  mi_free(r);

  // illegal byte wise read
  char* c = (char*)mi(malloc)(3);
  printf("invalid byte: over: %d, under: %d\n", c[4], c[-1]);
  mi(free)(c);

  // undefined access
  int* q = (int*)mi(malloc)(sizeof(int));
  printf("undefined: %d\n", *q);

  // illegal int read
  printf("invalid: over: %d, under: %d\n", q[1], q[-1]);

  *q = 42;

  // buffer overflow
  q[1] = 43;

  // buffer underflow
  q[-1] = 44;

  mi(free)(q);

  // double free
  mi(free)(q);

  // use after free
  printf("use-after-free: %d\n", *q);

  // leak p
  // mi_free(p)
  return 0;
}