1.0.1.1:
[sbcl.git] / src / runtime / validate.c
1 /*
2  * memory validation
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
9  * This software is derived from the CMU CL system, which was
10  * written at Carnegie Mellon University and released into the
11  * public domain. The software is in the public domain and is
12  * provided with absolutely no warranty. See the COPYING and CREDITS
13  * files for more information.
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include "sbcl.h"
20 #include "runtime.h"
21 #include "os.h"
22 #include "globals.h"
23 #include "interr.h"
24 #include "validate.h"
25
26 static void
27 ensure_space(lispobj *start, unsigned long size)
28 {
29     if (os_validate((os_vm_address_t)start,(os_vm_size_t)size)==NULL) {
30         fprintf(stderr,
31                 "ensure_space: failed to validate %ld bytes at 0x%08lx\n",
32                 size,
33                 (unsigned long)start);
34         fprintf(stderr,
35                 "(hint: Try \"ulimit -a\"; maybe you should increase memory limits.)\n");
36         exit(1);
37     }
38 }
39
40 os_vm_address_t undefined_alien_address = 0;
41
42 static void
43 ensure_undefined_alien(void) {
44     os_vm_address_t start = os_validate(NULL, os_vm_page_size);
45     if (start) {
46         os_protect(start, os_vm_page_size, OS_VM_PROT_NONE);
47         undefined_alien_address = start;
48     } else {
49         lose("could not allocate guard page for undefined alien\n");
50     }
51 }
52
53 void
54 validate(void)
55 {
56 #ifdef PRINTNOISE
57     printf("validating memory ...");
58     fflush(stdout);
59 #endif
60
61     ensure_space( (lispobj *)READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE);
62     ensure_space( (lispobj *)STATIC_SPACE_START   , STATIC_SPACE_SIZE);
63 #ifdef LISP_FEATURE_GENCGC
64     ensure_space( (lispobj *)DYNAMIC_SPACE_START  , dynamic_space_size);
65 #else
66     ensure_space( (lispobj *)DYNAMIC_0_SPACE_START, dynamic_space_size);
67     ensure_space( (lispobj *)DYNAMIC_1_SPACE_START, dynamic_space_size);
68 #endif
69
70 #ifdef LISP_FEATURE_LINKAGE_TABLE
71     ensure_space( (lispobj *)LINKAGE_TABLE_SPACE_START, LINKAGE_TABLE_SPACE_SIZE);
72 #endif
73
74 #ifdef LISP_FEATURE_OS_PROVIDES_DLOPEN
75     ensure_undefined_alien();
76 #endif
77
78 #ifdef PRINTNOISE
79     printf(" done.\n");
80 #endif
81 }
82
83 void
84 protect_control_stack_guard_page(int protect_p) {
85     struct thread *th = arch_os_get_current_thread();
86     os_protect(CONTROL_STACK_GUARD_PAGE(th),
87                os_vm_page_size,protect_p ?
88                (OS_VM_PROT_READ|OS_VM_PROT_EXECUTE) : OS_VM_PROT_ALL);
89 }
90
91 void
92 protect_control_stack_return_guard_page(int protect_p) {
93     struct thread *th = arch_os_get_current_thread();
94     os_protect(CONTROL_STACK_RETURN_GUARD_PAGE(th),
95                os_vm_page_size,protect_p ?
96                (OS_VM_PROT_READ|OS_VM_PROT_EXECUTE) : OS_VM_PROT_ALL);
97 }
98
99 /* these OOAO violations are here because with mach exception handlers
100  * we need to protect the stack guard pages from the mach exception
101  * handlers which run on a different thread, so we take a thread
102  * argument here. Too bad we don't have keywords args in C. */
103 void
104 protect_control_stack_guard_page_thread(int protect_p, struct thread *th) {
105     os_protect(CONTROL_STACK_GUARD_PAGE(th),
106                os_vm_page_size,protect_p ?
107                (OS_VM_PROT_READ|OS_VM_PROT_EXECUTE) : OS_VM_PROT_ALL);
108 }
109
110 void
111 protect_control_stack_return_guard_page_thread(int protect_p, struct thread* th) {
112     os_protect(CONTROL_STACK_RETURN_GUARD_PAGE(th),
113                os_vm_page_size,protect_p ?
114                (OS_VM_PROT_READ|OS_VM_PROT_EXECUTE) : OS_VM_PROT_ALL);
115 }