1.0.8.11: Fix typo in comment.
[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 #include "interr.h"                     /* for declaration of lose */
26
27
28 static void
29 ensure_space(lispobj *start, unsigned long size)
30 {
31     if (os_validate((os_vm_address_t)start,(os_vm_size_t)size)==NULL) {
32         fprintf(stderr,
33                 "ensure_space: failed to validate %ld bytes at 0x%08lx\n",
34                 size,
35                 (unsigned long)start);
36         fprintf(stderr,
37                 "(hint: Try \"ulimit -a\"; maybe you should increase memory limits.)\n");
38         exit(1);
39     }
40 }
41
42 os_vm_address_t undefined_alien_address = 0;
43
44 static void
45 ensure_undefined_alien(void) {
46     os_vm_address_t start = os_validate(NULL, os_vm_page_size);
47     if (start) {
48         os_protect(start, os_vm_page_size, OS_VM_PROT_NONE);
49         undefined_alien_address = start;
50     } else {
51         lose("could not allocate guard page for undefined alien\n");
52     }
53 }
54
55 void
56 validate(void)
57 {
58 #ifdef PRINTNOISE
59     printf("validating memory ...");
60     fflush(stdout);
61 #endif
62
63     ensure_space( (lispobj *)READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE);
64     ensure_space( (lispobj *)STATIC_SPACE_START   , STATIC_SPACE_SIZE);
65 #ifdef LISP_FEATURE_GENCGC
66     ensure_space( (lispobj *)DYNAMIC_SPACE_START  , dynamic_space_size);
67 #else
68     ensure_space( (lispobj *)DYNAMIC_0_SPACE_START, dynamic_space_size);
69     ensure_space( (lispobj *)DYNAMIC_1_SPACE_START, dynamic_space_size);
70 #endif
71
72 #ifdef LISP_FEATURE_LINKAGE_TABLE
73     ensure_space( (lispobj *)LINKAGE_TABLE_SPACE_START, LINKAGE_TABLE_SPACE_SIZE);
74 #endif
75
76 #ifdef LISP_FEATURE_OS_PROVIDES_DLOPEN
77     ensure_undefined_alien();
78 #endif
79
80 #ifdef PRINTNOISE
81     printf(" done.\n");
82 #endif
83 }
84
85 void
86 protect_control_stack_guard_page(int protect_p) {
87     struct thread *th = arch_os_get_current_thread();
88     os_protect(CONTROL_STACK_GUARD_PAGE(th),
89                os_vm_page_size,protect_p ?
90                (OS_VM_PROT_READ|OS_VM_PROT_EXECUTE) : OS_VM_PROT_ALL);
91 }
92
93 void
94 protect_control_stack_return_guard_page(int protect_p) {
95     struct thread *th = arch_os_get_current_thread();
96     os_protect(CONTROL_STACK_RETURN_GUARD_PAGE(th),
97                os_vm_page_size,protect_p ?
98                (OS_VM_PROT_READ|OS_VM_PROT_EXECUTE) : OS_VM_PROT_ALL);
99 }
100
101 /* these OOAO violations are here because with mach exception handlers
102  * we need to protect the stack guard pages from the mach exception
103  * handlers which run on a different thread, so we take a thread
104  * argument here. Too bad we don't have keywords args in C. */
105 void
106 protect_control_stack_guard_page_thread(int protect_p, struct thread *th) {
107     os_protect(CONTROL_STACK_GUARD_PAGE(th),
108                os_vm_page_size,protect_p ?
109                (OS_VM_PROT_READ|OS_VM_PROT_EXECUTE) : OS_VM_PROT_ALL);
110 }
111
112 void
113 protect_control_stack_return_guard_page_thread(int protect_p, struct thread* th) {
114     os_protect(CONTROL_STACK_RETURN_GUARD_PAGE(th),
115                os_vm_page_size,protect_p ?
116                (OS_VM_PROT_READ|OS_VM_PROT_EXECUTE) : OS_VM_PROT_ALL);
117 }