Debian Codegraph @

src/dex-stack.c

raw symbols SCIP
1 /* dex-stack.c
2 *
3 * Copyright 2022 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include "config.h"
22
23 #include <glib.h>
24
25 #ifdef G_OS_UNIX
26 # include <errno.h>
27 # include <sys/mman.h>
28 #endif
29
30 #include "dex-platform.h"
31 #include "dex-stack-private.h"
32
33 #define DEFAULT_STACK_SIZE (MAX (4096*32, dex_get_min_stack_size()))
34 #define DEFAULT_MIN_POOL_SIZE 4
35 #define DEFAULT_MAX_POOL_SIZE 16
36
37 DexStackPool *
38 dex_stack_pool_new (gsize stack_size,
39 int min_pool_size,
40 int max_pool_size)
41 {
42 DexStackPool *stack_pool;
43
44 if (stack_size == 0)
45 stack_size = DEFAULT_STACK_SIZE;
46
47 if (min_pool_size < 0)
48 min_pool_size = DEFAULT_MIN_POOL_SIZE;
49
50 if (max_pool_size < 0)
51 max_pool_size = DEFAULT_MAX_POOL_SIZE;
52
53 g_return_val_if_fail (min_pool_size <= max_pool_size, NULL);
54
55 stack_pool = g_new0 (DexStackPool, 1);
56 stack_pool->min_pool_size = min_pool_size;
57 stack_pool->max_pool_size = max_pool_size;
58 stack_pool->stack_size = stack_size;
59 g_mutex_init (&stack_pool->mutex);
60
61 for (guint i = 0; i < stack_pool->min_pool_size; i++)
62 {
63 DexStack *stack = dex_stack_new (stack_size);
64 g_queue_push_head_link (&stack_pool->stacks, &stack->link);
65 }
66
67 return stack_pool;
68 }
69
70 void
71 dex_stack_pool_free (DexStackPool *stack_pool)
72 {
73 g_return_if_fail (stack_pool != NULL);
74
75 while (stack_pool->stacks.length > 0)
76 {
77 DexStack *stack = g_queue_pop_head_link (&stack_pool->stacks)->data;
78 dex_stack_free (stack);
79 }
80
81 g_mutex_clear (&stack_pool->mutex);
82 g_free (stack_pool);
83 }
84
85 DexStack *
86 dex_stack_new (gsize size)
87 {
88 gsize page_size = dex_get_page_size ();
89 DexStack *stack;
90 #ifdef G_OS_UNIX
91 gpointer map;
92 gpointer guard;
93 int flags = 0;
94 #endif
95
96 if (size < dex_get_min_stack_size ())
97 size = DEFAULT_STACK_SIZE;
98
99 /* Round up to next full page size */
100 if ((size & (page_size-1)) != 0)
101 size = (size + page_size) & ~(page_size-1);
102
103 /* Make sure the stack size we're about to allocate is reasonable */
104 g_assert_cmpuint (size, >=, page_size);
105 g_assert_cmpuint (size, <, G_MAXUINT32);
106
107 #ifdef G_OS_UNIX
108 flags = MAP_PRIVATE | MAP_ANONYMOUS;
109 #if defined(__OpenBSD__)
110 flags |= MAP_STACK;
111 #endif
112
113 /* mmap() the stack with an extra page for our guard page */
114 map = mmap (NULL, size + page_size, PROT_READ|PROT_WRITE, flags, -1, 0);
115
116 if (MAP_FAILED == map)
117 {
118 int errsv = errno;
119 g_error ("Failed to allocate stack: %s", g_strerror (errsv));
120 }
121
122 #ifdef __IA64__
123 /* Itanium has a "register stack", see
124 * itanium-software-runtime-architecture-guide.pdf for details.
125 */
126 guard = (gpointer)((gintptr)map + ((size / 2) & ~page_size));
127 #elif G_HAVE_GROWING_STACK
128 guard = (gpointer)((gintptr)map + size);
129 #else
130 guard = map;
131 #endif
132
133 /* Setup guard page to fault */
134 #if HAVE_MPROTECT
135 if (mprotect (guard, page_size, PROT_NONE) != 0)
136 {
137 int errsv = errno;
138 g_error ("Failed to protect stack guard page: %s",
139 g_strerror (errsv));
140 }
141 #endif
142 #endif
143
144 stack = g_new0 (DexStack, 1);
145 stack->link.data = stack;
146 stack->size = size;
147
148 #ifdef G_OS_UNIX
149 stack->base = map;
150 stack->guard = guard;
151
152 if (map == guard)
153 stack->ptr = (gpointer)((gintptr)map + page_size);
154 else
155 stack->ptr = map;
156 #endif
157
158 return stack;
159 }
160
161 void
162 dex_stack_free (DexStack *stack)
163 {
164 #ifdef G_OS_UNIX
165 guint page_size = dex_get_page_size ();
166
167 g_assert (stack->link.data == stack);
168 g_assert (stack->link.prev == NULL);
169 g_assert (stack->link.next == NULL);
170
171 if (stack->base != MAP_FAILED)
172 munmap (stack->base, stack->size + page_size);
173 stack->base = MAP_FAILED;
174 stack->guard = MAP_FAILED;
175
176 stack->size = 0;
177 stack->link.data = NULL;
178
179 g_free (stack);
180 #endif
181 }
182
183 void
184 dex_stack_mark_unused (DexStack *stack)
185 {
186 g_assert (stack != NULL);
187 g_assert (stack->link.data == stack);
188
189 #ifdef HAVE_MADVISE
190 madvise (stack->ptr, stack->size, MADV_DONTNEED);
191 #endif
192 }