mapi: Avoid Data Execution Prevention on windows.
[mesa.git] / src / mapi / mapi / u_execmem.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file glapi_execmem.c
28 *
29 * Function for allocating executable memory for dispatch stubs.
30 *
31 * Copied from main/execmem.c and simplified for dispatch stubs.
32 */
33
34
35 #include "u_compiler.h"
36 #include "u_thread.h"
37 #include "u_execmem.h"
38
39
40 #if defined(__linux__) || defined(__OpenBSD__) || defined(_NetBSD__) || defined(__sun)
41
42 #include <unistd.h>
43 #include <sys/mman.h>
44
45 #ifdef MESA_SELINUX
46 #include <selinux/selinux.h>
47 #endif
48
49
50 #ifndef MAP_ANONYMOUS
51 #define MAP_ANONYMOUS MAP_ANON
52 #endif
53
54
55 #define EXEC_MAP_SIZE (4*1024)
56
57 u_mutex_declare_static(exec_mutex);
58
59 static unsigned int head = 0;
60
61 static unsigned char *exec_mem = NULL;
62
63
64 /*
65 * Dispatch stubs are of fixed size and never freed. Thus, we do not need to
66 * overlay a heap, we just mmap a page and manage through an index.
67 */
68
69 static int
70 init_map(void)
71 {
72 #ifdef MESA_SELINUX
73 if (is_selinux_enabled()) {
74 if (!security_get_boolean_active("allow_execmem") ||
75 !security_get_boolean_pending("allow_execmem"))
76 return 0;
77 }
78 #endif
79
80 if (!exec_mem)
81 exec_mem = mmap(NULL, EXEC_MAP_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE,
82 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
83
84 return (exec_mem != MAP_FAILED);
85 }
86
87
88 void *
89 u_execmem_alloc(unsigned int size)
90 {
91 void *addr = NULL;
92
93 u_mutex_lock(exec_mutex);
94
95 if (!init_map())
96 goto bail;
97
98 /* free space check, assumes no integer overflow */
99 if (head + size > EXEC_MAP_SIZE)
100 goto bail;
101
102 /* allocation, assumes proper addr and size alignement */
103 addr = exec_mem + head;
104 head += size;
105
106 bail:
107 u_mutex_unlock(exec_mutex);
108
109 return addr;
110 }
111
112
113 #elif defined(_WIN32)
114
115 #include <windows.h>
116
117
118 /*
119 * Avoid Data Execution Prevention.
120 */
121
122 void *
123 u_execmem_alloc(unsigned int size)
124 {
125 return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
126 }
127
128
129 #else
130
131 void *
132 u_execmem_alloc(unsigned int size)
133 {
134 return malloc(size);
135 }
136
137
138 #endif