ppc64: Arguments to iselInt128Expr_to_32x4 should be initialized.
[valgrind.git] / memcheck / tests / leak_cpp_interior.cpp
blob56c53709d1e0463cf4a2d7aff7c94133eb12388c
1 #define _GLIBCXX_USE_CXX11_ABI 0
2 #define __STDC_FORMAT_MACROS
3 #include <inttypes.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <stdint.h>
7 #include <stdlib.h>
8 #include <string>
9 #include <sstream>
10 #include "../memcheck.h"
11 // Derived from test provided by Timur Iskhodzhanov (bug 280271)
13 class MyClass
15 char m1;
16 int m2;
17 public:
18 ~MyClass()
19 { fprintf(stderr, "destruct MyClass\n");
23 // Two hierarchies using MI, one with no fields,
24 // the other one with some data.
25 struct Ae
27 virtual ~Ae()
28 { fprintf(stderr, "destruct Ae\n");
31 struct Be
33 virtual ~Be()
34 { fprintf(stderr, "destruct Be\n");
37 struct Ce : public Ae, public Be
39 virtual ~Ce()
40 { fprintf(stderr, "destruct Ce\n");
44 struct A
46 char a;
47 A()
48 { a = 'a';
50 virtual ~A()
51 { fprintf(stderr, "destruct A\n");
54 struct B
56 char b;
57 B()
58 { b = 'b';
60 virtual ~B()
61 { fprintf(stderr, "destruct B\n");
64 struct C : public A, public B
66 char c;
67 C()
68 { c = 'c';
70 virtual ~C()
71 { fprintf(stderr, "destruct C\n");
75 void* wrap64_malloc(int size)
77 uint64_t *p = (uint64_t*)malloc(size + 8);
78 *p = size;
79 ++p;
80 return p;
83 void wrap64_free(void *p)
85 uint64_t *p2 = (uint64_t*)p;
86 if (p2 == NULL)
87 return;
88 --p2;
89 free(p2);
92 std::string str;
93 std::string str2;
94 MyClass *ptr;
95 MyClass *ptr2;
96 Be *ptrBCe;
97 Ae *ptrACe;
98 B *ptrBC;
99 A *ptrAC;
100 void* ptr64;
102 char who_points_at_cmd[100];
103 char who_points_at_cmd_brackets[100]; /* Same but with brackets. */
105 void doit(void)
107 str = "Valgrind"; // interior ptr.
108 str2 = str;
109 ptr = new MyClass[3]; // interior ptr.
110 ptr64 = wrap64_malloc(23);
112 // prepare the who_points_at cmd we will run.
113 // Do it here to avoid having ptr or its exterior ptr kept in a register.
114 sprintf(who_points_at_cmd, "who_points_at %#" PRIxPTR " 20",
115 (uintptr_t) (char*)ptr - sizeof(void*));
116 sprintf(who_points_at_cmd_brackets, "who_points_at %#" PRIxPTR "[20]",
117 (uintptr_t) (char*)ptr - sizeof(void*));
119 ptr2 = new MyClass[0]; // "interior but exterior ptr".
120 // ptr2 points after the chunk, is wrongly considered by memcheck as definitely leaked.
122 ptrBCe = new Ce; // interior ptr.
123 ptrACe = new Ce; // not an interior pointer.
124 ptrBC = new C; // interior ptr.
125 ptrAC = new C; // not an interior pointer.
128 str2 += " rocks (str2)\n"; // interior ptr.
132 int main() {
134 doit();
135 (void) VALGRIND_MONITOR_COMMAND("v.set log_output");
137 fprintf(stderr, "VALGRIND_DO_LEAK_CHECK\n");
138 VALGRIND_DO_LEAK_CHECK; // All possible leaks should be detected, giving only reachable data.
140 // Check individually each heuristic
141 fprintf(stderr, "leak_check summary heuristics multipleinheritance\n");
142 (void) VALGRIND_MONITOR_COMMAND("leak_check summary heuristics multipleinheritance");
143 fprintf(stderr, "leak_check summary any heuristics newarray\n");
144 (void) VALGRIND_MONITOR_COMMAND("leak_check summary heuristics newarray");
145 fprintf(stderr, "leak_check summary heuristics length64\n");
146 (void) VALGRIND_MONITOR_COMMAND("leak_check summary heuristics length64");
147 fprintf(stderr, "leak_check summary heuristics stdstring\n");
148 (void) VALGRIND_MONITOR_COMMAND("leak_check summary heuristics stdstring");
150 // check all and none
151 fprintf(stderr, "leak_check summary heuristics multipleinheritance,newarray,stdstring,length64\n");
152 (void) VALGRIND_MONITOR_COMMAND("leak_check summary heuristics multipleinheritance,newarray,stdstring,length64");
153 fprintf(stderr, "leak_check summary heuristics all\n");
154 (void) VALGRIND_MONITOR_COMMAND("leak_check summary heuristics all");
155 fprintf(stderr, "leak_check summary heuristics none\n");
156 (void) VALGRIND_MONITOR_COMMAND("leak_check summary heuristics none");
158 // Test the who_points_at when the block is pointed to with an interior ptr.
159 (void) VALGRIND_MONITOR_COMMAND(who_points_at_cmd);
160 // Same but with the bracket syntax.
161 (void) VALGRIND_MONITOR_COMMAND(who_points_at_cmd_brackets);
163 delete [] ptr;
164 delete [] ptr2;
165 delete ptrBCe;
166 delete ptrACe;
167 delete ptrBC;
168 delete ptrAC;
169 wrap64_free(ptr64);
170 fprintf(stderr, "Finished!\n");
171 return 0;