2 * Copyright (C) 2010 Red Hat, Inc.
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
22 /* We test for errors in optimize-only definitions in gmem.h */
25 #pragma GCC optimize (1)
31 static gsize a
= G_MAXSIZE
/ 10 + 10;
35 #define MEM_OVERFLOW_TEST(name, code) \
37 mem_overflow_ ## name (void) \
45 MEM_OVERFLOW_TEST (malloc_n_a_a
, p
= g_malloc_n (a
, a
))
46 MEM_OVERFLOW_TEST (malloc_n_a_b
, p
= g_malloc_n (a
, b
))
47 MEM_OVERFLOW_TEST (malloc_n_b_a
, p
= g_malloc_n (b
, a
))
48 MEM_OVERFLOW_TEST (malloc_n_b_b
, p
= g_malloc_n (b
, b
))
50 MEM_OVERFLOW_TEST (malloc0_n_a_a
, p
= g_malloc0_n (a
, a
))
51 MEM_OVERFLOW_TEST (malloc0_n_a_b
, p
= g_malloc0_n (a
, b
))
52 MEM_OVERFLOW_TEST (malloc0_n_b_a
, p
= g_malloc0_n (b
, a
))
53 MEM_OVERFLOW_TEST (malloc0_n_b_b
, p
= g_malloc0_n (b
, b
))
55 MEM_OVERFLOW_TEST (realloc_n_a_a
, p
= g_malloc (1); p
= g_realloc_n (p
, a
, a
))
56 MEM_OVERFLOW_TEST (realloc_n_a_b
, p
= g_malloc (1); p
= g_realloc_n (p
, a
, b
))
57 MEM_OVERFLOW_TEST (realloc_n_b_a
, p
= g_malloc (1); p
= g_realloc_n (p
, b
, a
))
58 MEM_OVERFLOW_TEST (realloc_n_b_b
, p
= g_malloc (1); p
= g_realloc_n (p
, b
, b
))
60 MEM_OVERFLOW_TEST (new_a
, p
= g_new (X
, a
))
61 MEM_OVERFLOW_TEST (new_b
, p
= g_new (X
, b
))
63 MEM_OVERFLOW_TEST (new0_a
, p
= g_new0 (X
, a
))
64 MEM_OVERFLOW_TEST (new0_b
, p
= g_new0 (X
, b
))
66 MEM_OVERFLOW_TEST (renew_a
, p
= g_malloc (1); p
= g_renew (X
, p
, a
))
67 MEM_OVERFLOW_TEST (renew_b
, p
= g_malloc (1); p
= g_renew (X
, p
, b
))
70 mem_overflow_malloc_0 (void)
79 mem_overflow_realloc_0 (void)
94 /* "FAIL" here apparently means "fail to overflow"... */
95 #define CHECK_PASS(P) p = (P); g_assert (p == NULL);
96 #define CHECK_FAIL(P) p = (P); g_assert (p != NULL);
98 CHECK_PASS (g_try_malloc_n (a
, a
));
99 CHECK_PASS (g_try_malloc_n (a
, b
));
100 CHECK_PASS (g_try_malloc_n (b
, a
));
101 CHECK_FAIL (g_try_malloc_n (b
, b
));
104 CHECK_PASS (g_try_malloc0_n (a
, a
));
105 CHECK_PASS (g_try_malloc0_n (a
, b
));
106 CHECK_PASS (g_try_malloc0_n (b
, a
));
107 CHECK_FAIL (g_try_malloc0_n (b
, b
));
111 CHECK_PASS (g_try_realloc_n (q
, a
, a
));
112 CHECK_PASS (g_try_realloc_n (q
, a
, b
));
113 CHECK_PASS (g_try_realloc_n (q
, b
, a
));
114 CHECK_FAIL (g_try_realloc_n (q
, b
, b
));
117 CHECK_PASS (g_try_new (X
, a
));
118 CHECK_FAIL (g_try_new (X
, b
));
121 CHECK_PASS (g_try_new0 (X
, a
));
122 CHECK_FAIL (g_try_new0 (X
, b
));
125 q
= g_try_malloc (1);
126 CHECK_PASS (g_try_renew (X
, q
, a
));
127 CHECK_FAIL (g_try_renew (X
, q
, b
));
130 #define CHECK_SUBPROCESS_FAIL(name) do { \
131 if (g_test_undefined ()) \
133 g_test_trap_subprocess ("/mem/overflow/subprocess/" #name, 0, 0); \
134 g_test_trap_assert_failed(); \
138 #define CHECK_SUBPROCESS_PASS(name) do { \
139 if (g_test_undefined ()) \
141 g_test_trap_subprocess ("/mem/overflow/subprocess/" #name, 0, 0); \
142 g_test_trap_assert_passed(); \
146 CHECK_SUBPROCESS_FAIL (malloc_n_a_a
);
147 CHECK_SUBPROCESS_FAIL (malloc_n_a_b
);
148 CHECK_SUBPROCESS_FAIL (malloc_n_b_a
);
149 CHECK_SUBPROCESS_PASS (malloc_n_b_b
);
151 CHECK_SUBPROCESS_FAIL (malloc0_n_a_a
);
152 CHECK_SUBPROCESS_FAIL (malloc0_n_a_b
);
153 CHECK_SUBPROCESS_FAIL (malloc0_n_b_a
);
154 CHECK_SUBPROCESS_PASS (malloc0_n_b_b
);
156 CHECK_SUBPROCESS_FAIL (realloc_n_a_a
);
157 CHECK_SUBPROCESS_FAIL (realloc_n_a_b
);
158 CHECK_SUBPROCESS_FAIL (realloc_n_b_a
);
159 CHECK_SUBPROCESS_PASS (realloc_n_b_b
);
161 CHECK_SUBPROCESS_FAIL (new_a
);
162 CHECK_SUBPROCESS_PASS (new_b
);
164 CHECK_SUBPROCESS_FAIL (new0_a
);
165 CHECK_SUBPROCESS_PASS (new0_b
);
167 CHECK_SUBPROCESS_FAIL (renew_a
);
168 CHECK_SUBPROCESS_PASS (renew_b
);
170 CHECK_SUBPROCESS_PASS (malloc_0
);
171 CHECK_SUBPROCESS_PASS (realloc_0
);
180 empty_alloc_subprocess (void)
184 empty
= g_new0 (Empty
, 1);
185 g_assert (empty
== NULL
);
192 g_test_bug ("615379");
194 g_assert_cmpint (sizeof (Empty
), ==, 0);
196 g_test_trap_subprocess ("/mem/empty-alloc/subprocess", 0, 0);
197 g_test_trap_assert_passed ();
205 g_test_init (&argc
, &argv
, NULL
);
207 g_test_bug_base ("http://bugzilla.gnome.org/");
209 g_test_add_func ("/mem/overflow", mem_overflow
);
210 g_test_add_func ("/mem/overflow/subprocess/malloc_n_a_a", mem_overflow_malloc_n_a_a
);
211 g_test_add_func ("/mem/overflow/subprocess/malloc_n_a_b", mem_overflow_malloc_n_a_b
);
212 g_test_add_func ("/mem/overflow/subprocess/malloc_n_b_a", mem_overflow_malloc_n_b_a
);
213 g_test_add_func ("/mem/overflow/subprocess/malloc_n_b_b", mem_overflow_malloc_n_b_b
);
214 g_test_add_func ("/mem/overflow/subprocess/malloc0_n_a_a", mem_overflow_malloc0_n_a_a
);
215 g_test_add_func ("/mem/overflow/subprocess/malloc0_n_a_b", mem_overflow_malloc0_n_a_b
);
216 g_test_add_func ("/mem/overflow/subprocess/malloc0_n_b_a", mem_overflow_malloc0_n_b_a
);
217 g_test_add_func ("/mem/overflow/subprocess/malloc0_n_b_b", mem_overflow_malloc0_n_b_b
);
218 g_test_add_func ("/mem/overflow/subprocess/realloc_n_a_a", mem_overflow_realloc_n_a_a
);
219 g_test_add_func ("/mem/overflow/subprocess/realloc_n_a_b", mem_overflow_realloc_n_a_b
);
220 g_test_add_func ("/mem/overflow/subprocess/realloc_n_b_a", mem_overflow_realloc_n_b_a
);
221 g_test_add_func ("/mem/overflow/subprocess/realloc_n_b_b", mem_overflow_realloc_n_b_b
);
222 g_test_add_func ("/mem/overflow/subprocess/new_a", mem_overflow_new_a
);
223 g_test_add_func ("/mem/overflow/subprocess/new_b", mem_overflow_new_b
);
224 g_test_add_func ("/mem/overflow/subprocess/new0_a", mem_overflow_new0_a
);
225 g_test_add_func ("/mem/overflow/subprocess/new0_b", mem_overflow_new0_b
);
226 g_test_add_func ("/mem/overflow/subprocess/renew_a", mem_overflow_renew_a
);
227 g_test_add_func ("/mem/overflow/subprocess/renew_b", mem_overflow_renew_b
);
228 g_test_add_func ("/mem/overflow/subprocess/malloc_0", mem_overflow_malloc_0
);
229 g_test_add_func ("/mem/overflow/subprocess/realloc_0", mem_overflow_realloc_0
);
232 g_test_add_func ("/mem/empty-alloc", empty_alloc
);
233 g_test_add_func ("/mem/empty-alloc/subprocess", empty_alloc_subprocess
);