util: Fix memory leaks in unit test.
[mesa.git] / src / util / u_debug_stack_test.cpp
1 /*
2 * Copyright © 2020 Google, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include <stdio.h>
25 #include <gtest/gtest.h>
26
27 #include "util/u_debug_stack.h"
28
29 static void __attribute__((noinline))
30 func_a(void)
31 {
32 struct debug_stack_frame backtrace[16];
33
34 fprintf(stderr, "--- backtrace from func_a:\n");
35 debug_backtrace_capture(backtrace, 0, 16);
36 debug_backtrace_dump(backtrace, 16);
37 }
38
39 static void __attribute__((noinline))
40 func_b(void)
41 {
42 struct debug_stack_frame backtrace[16];
43
44 func_a();
45
46 fprintf(stderr, "--- backtrace from func_b:\n");
47 debug_backtrace_capture(backtrace, 0, 16);
48 debug_backtrace_dump(backtrace, 16);
49 }
50
51 static void __attribute__((noinline))
52 func_c(struct debug_stack_frame *frames)
53 {
54 debug_backtrace_capture(frames, 0, 16);
55 }
56
57 TEST(u_debug_stack_test, basics)
58 {
59 struct debug_stack_frame backtrace[16];
60 struct debug_stack_frame stored_backtrace[16];
61
62 func_c(stored_backtrace);
63
64 fprintf(stderr, "--- backtrace from main to stderr:\n");
65 debug_backtrace_capture(backtrace, 0, 16);
66 debug_backtrace_print(stderr, backtrace, 16);
67
68 fprintf(stderr, "--- backtrace from main again to debug_printf:\n");
69 debug_backtrace_capture(backtrace, 0, 16);
70 debug_backtrace_dump(backtrace, 16);
71
72 func_a();
73
74 func_b();
75
76 fprintf(stderr, "--- stored backtrace from start of main:\n");
77 debug_backtrace_dump(stored_backtrace, 16);
78 }
79
80 #if _POSIX_C_SOURCE >= 200809L
81
82 TEST(u_debug_stack_test, capture_not_overwritten)
83 {
84 struct debug_stack_frame backtrace1[16], backtrace2[16];
85
86 FILE *fp;
87 char *bt1, *bt2;
88 size_t size;
89
90 /* Old android implementation uses one global capture per thread. Test that
91 * we can store multiple captures and that they decode to different
92 * backtraces.
93 */
94
95 func_c(backtrace1);
96 debug_backtrace_capture(backtrace2, 0, 16);
97
98 fp = open_memstream(&bt1, &size);
99 debug_backtrace_print(fp, backtrace1, 16);
100 fclose(fp);
101
102 fp = open_memstream(&bt2, &size);
103 debug_backtrace_print(fp, backtrace2, 16);
104 fclose(fp);
105
106 if (size > 0) {
107 EXPECT_STRNE(bt1, bt2);
108 }
109
110 free(bt1);
111 free(bt2);
112 }
113
114 #endif