0.9.2.18: various error &co reporting improvements and build tweaks
[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 "validate.h"
24
25 static void
26 ensure_space(lispobj *start, unsigned long size)
27 {
28     if (os_validate((os_vm_address_t)start,(os_vm_size_t)size)==NULL) {
29         fprintf(stderr,
30                 "ensure_space: failed to validate %ld bytes at 0x%08lx\n",
31                 size,
32                 (unsigned long)start);
33         fprintf(stderr,
34                 "(hint: Try \"ulimit -a\"; maybe you should increase memory limits.)\n");
35         exit(1);
36     }
37 }
38
39 os_vm_address_t undefined_alien_address = 0;
40
41 static void
42 ensure_undefined_alien(void) {
43     os_vm_address_t start = os_validate(NULL, os_vm_page_size);
44     if (start) {
45         os_protect(start, os_vm_page_size, OS_VM_PROT_NONE);
46         undefined_alien_address = start;
47     } else {
48         lose("could not allocate guard page for undefined alien");
49     }
50 }
51
52 void
53 validate(void)
54 {
55 #ifdef PRINTNOISE
56     printf("validating memory ...");
57     fflush(stdout);
58 #endif
59     
60     ensure_space( (lispobj *)READ_ONLY_SPACE_START, READ_ONLY_SPACE_SIZE);
61     ensure_space( (lispobj *)STATIC_SPACE_START   , STATIC_SPACE_SIZE);
62 #ifdef LISP_FEATURE_GENCGC
63     ensure_space( (lispobj *)DYNAMIC_SPACE_START  , DYNAMIC_SPACE_SIZE);
64 #else
65     ensure_space( (lispobj *)DYNAMIC_0_SPACE_START  , DYNAMIC_SPACE_SIZE);
66     ensure_space( (lispobj *)DYNAMIC_1_SPACE_START  , DYNAMIC_SPACE_SIZE);
67 #endif
68
69 #ifdef LISP_FEATURE_LINKAGE_TABLE
70     ensure_space( (lispobj *)LINKAGE_TABLE_SPACE_START, LINKAGE_TABLE_SPACE_SIZE);
71 #endif
72
73 #ifdef LISP_FEATURE_OS_PROVIDES_DLOPEN
74     ensure_undefined_alien();
75 #endif
76  
77 #ifdef PRINTNOISE
78     printf(" done.\n");
79 #endif
80 }
81
82 void 
83 protect_control_stack_guard_page(struct thread *th, int protect_p) {
84     os_protect(CONTROL_STACK_GUARD_PAGE(th),
85                os_vm_page_size,protect_p ?
86                (OS_VM_PROT_READ|OS_VM_PROT_EXECUTE) : OS_VM_PROT_ALL);
87 }
88
89 void 
90 protect_control_stack_return_guard_page(struct thread *th, int protect_p) {
91     os_protect(CONTROL_STACK_RETURN_GUARD_PAGE(th),
92                os_vm_page_size,protect_p ?
93                (OS_VM_PROT_READ|OS_VM_PROT_EXECUTE) : OS_VM_PROT_ALL);
94 }