1.0.45.22: non-racy RUN-PROGRAM :PTY on OpenBSD
[sbcl.git] / src / runtime / coreparse.c
1 /*
2  * A saved SBCL system is a .core file; the code here helps us accept
3  * such a file as input.
4  */
5
6 /*
7  * This software is part of the SBCL system. See the README file for
8  * more information.
9  *
10  * This software is derived from the CMU CL system, which was
11  * written at Carnegie Mellon University and released into the
12  * public domain. The software is in the public domain and is
13  * provided with absolutely no warranty. See the COPYING and CREDITS
14  * files for more information.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/file.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25
26 #include "sbcl.h"
27 #include "os.h"
28 #include "runtime.h"
29 #include "globals.h"
30 #include "core.h"
31 #include "arch.h"
32 #include "interr.h"
33 #include "thread.h"
34
35 #include "validate.h"
36 #include "gc-internal.h"
37
38 /* lutex stuff */
39 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
40 #include "genesis/sap.h"
41 #include "pthread-lutex.h"
42 #endif
43
44
45 unsigned char build_id[] =
46 #include "../../output/build-id.tmp"
47 ;
48
49 int
50 open_binary(char *filename, int mode)
51 {
52 #ifdef LISP_FEATURE_WIN32
53     mode |= O_BINARY;
54 #endif
55
56     return open(filename, mode);
57 }
58
59
60 static struct runtime_options *
61 read_runtime_options(int fd)
62 {
63     size_t optarray[RUNTIME_OPTIONS_WORDS];
64     struct runtime_options *options = NULL;
65
66     if (read(fd, optarray, RUNTIME_OPTIONS_WORDS * sizeof(size_t)) !=
67         RUNTIME_OPTIONS_WORDS * sizeof(size_t)) {
68         return NULL;
69     }
70
71     if ((RUNTIME_OPTIONS_MAGIC != optarray[0]) || (0 == optarray[1])) {
72         return NULL;
73     }
74
75     options = successful_malloc(sizeof(struct runtime_options));
76
77     options->dynamic_space_size = optarray[2];
78     options->thread_control_stack_size = optarray[3];
79
80     return options;
81 }
82
83 void
84 maybe_initialize_runtime_options(int fd)
85 {
86     struct runtime_options *new_runtime_options;
87     off_t end_offset = sizeof(lispobj) +
88         sizeof(os_vm_offset_t) +
89         (RUNTIME_OPTIONS_WORDS * sizeof(size_t));
90
91     lseek(fd, -end_offset, SEEK_END);
92
93     if (new_runtime_options = read_runtime_options(fd)) {
94         runtime_options = new_runtime_options;
95     }
96 }
97
98 /* Search 'filename' for an embedded core.  An SBCL core has, at the
99  * end of the file, a trailer containing optional saved runtime
100  * options, the start of the core (an os_vm_offset_t), and a final
101  * signature word (the lispobj CORE_MAGIC).  If this trailer is found
102  * at the end of the file, the start of the core can be determined
103  * from the core size.
104  *
105  * If an embedded core is present, this returns the offset into the
106  * file to load the core from, or -1 if no core is present. */
107 os_vm_offset_t
108 search_for_embedded_core(char *filename)
109 {
110     lispobj header;
111     os_vm_offset_t lispobj_size = sizeof(lispobj);
112     os_vm_offset_t trailer_size = lispobj_size + sizeof(os_vm_offset_t);
113     os_vm_offset_t core_start, pos;
114     int fd = -1;
115
116     if ((fd = open_binary(filename, O_RDONLY)) < 0)
117         goto lose;
118     if (lseek(fd, -lispobj_size, SEEK_END) < 0)
119         goto lose;
120     if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
121         goto lose;
122
123     if (header == CORE_MAGIC) {
124         if (lseek(fd, -trailer_size, SEEK_END) < 0)
125             goto lose;
126         if (read(fd, &core_start, sizeof(os_vm_offset_t)) < 0)
127             goto lose;
128
129         if (lseek(fd, core_start, SEEK_SET) < 0)
130             goto lose;
131         pos = lseek(fd, 0, SEEK_CUR);
132
133         if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
134             goto lose;
135
136         if (header != CORE_MAGIC)
137             goto lose;
138
139         maybe_initialize_runtime_options(fd);
140
141         close(fd);
142         return pos;
143     }
144
145 lose:
146     if (fd != -1)
147         close(fd);
148
149     return -1;
150 }
151
152 /* If more platforms doesn't support overlapping mmap rename this
153  * def to something like ifdef nommapoverlap */
154 /* currently hpux only */
155 #ifdef LISP_FEATURE_HPUX
156 os_vm_address_t copy_core_bytes(int fd, os_vm_offset_t offset,
157                                 os_vm_address_t addr, int len)
158 {
159   unsigned char buf[4096];
160   int c,x;
161   int old_fd = lseek(fd, 0, SEEK_CUR);
162
163   if(len & (4096-1)){
164     fprintf(stderr, "cant copy a slice of core because slice-length is not of page size(4096)\n");
165     exit(-1);
166   }
167   if(old_fd < 0){
168     fprintf(stderr, "cant perform lseek() on corefile\n");
169   }
170   lseek(fd, offset, SEEK_SET);
171   if(fd < 0){
172     fprintf(stderr, "cant perform lseek(%u,%lu,SEEK_SET) on corefile\n", fd, offset);
173   }
174   for(x = 0; x < len; x += 4096){
175     c = read(fd, buf, 4096);
176     if(c != 4096){
177       fprintf(stderr, "cant read memory area from corefile at position %lu, got %d\n", offset + x, c);
178       exit(-1);
179     }
180     memcpy(addr+x, buf, 4096);
181   }
182   os_flush_icache(addr, len);
183   return addr;
184 }
185 #endif
186
187 static void
188 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
189 {
190     struct ndir_entry *entry;
191
192     FSHOW((stderr, "/process_directory(..), count=%d\n", count));
193
194     for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
195
196         long id = entry->identifier;
197         long offset = os_vm_page_size * (1 + entry->data_page);
198         os_vm_address_t addr =
199             (os_vm_address_t) (os_vm_page_size * entry->address);
200         lispobj *free_pointer = (lispobj *) addr + entry->nwords;
201         unsigned long len = os_vm_page_size * entry->page_count;
202         if (len != 0) {
203             os_vm_address_t real_addr;
204             FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
205                    (long)len, (long)len, (unsigned long)addr));
206 #ifdef LISP_FEATURE_HPUX
207             real_addr = copy_core_bytes(fd, offset + file_offset, addr, len);
208 #else
209             real_addr = os_map(fd, offset + file_offset, addr, len);
210 #endif
211             if (real_addr != addr) {
212                 lose("file mapped in wrong place! "
213                      "(0x%08x != 0x%08lx)\n",
214                      real_addr,
215                      addr);
216             }
217         }
218
219         FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
220                id, (unsigned long)free_pointer));
221
222         switch (id) {
223         case DYNAMIC_CORE_SPACE_ID:
224             if (len > dynamic_space_size) {
225                 fprintf(stderr,
226                         "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
227                         len >> 10,
228                         (long)dynamic_space_size >> 10);
229                 exit(1);
230             }
231 #ifdef LISP_FEATURE_GENCGC
232             if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
233                 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
234                         (long)addr, (long)DYNAMIC_SPACE_START);
235                 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
236             }
237 #else
238             if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
239                 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
240                 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
241                         (long)addr,
242                         (long)DYNAMIC_0_SPACE_START,
243                         (long)DYNAMIC_1_SPACE_START);
244                 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
245             }
246 #endif
247 #if defined(ALLOCATION_POINTER)
248             SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
249 #else
250             dynamic_space_free_pointer = free_pointer;
251 #endif
252             /* For stop-and-copy GC, this will be whatever the GC was
253              * using at the time. With GENCGC, this will always be
254              * space 0. (We checked above that for GENCGC,
255              * addr==DYNAMIC_SPACE_START.) */
256             current_dynamic_space = (lispobj *)addr;
257             break;
258         case STATIC_CORE_SPACE_ID:
259             if (addr != (os_vm_address_t)STATIC_SPACE_START) {
260                 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
261                         (long)addr, (long)STATIC_SPACE_START);
262                 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
263             }
264             break;
265         case READ_ONLY_CORE_SPACE_ID:
266             if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
267                 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
268                         (long)addr, (long)READ_ONLY_SPACE_START);
269                 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
270             }
271             break;
272         default:
273             lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
274         }
275     }
276 }
277
278 lispobj
279 load_core_file(char *file, os_vm_offset_t file_offset)
280 {
281     lispobj *header, val, len, *ptr, remaining_len;
282     int fd = open_binary(file, O_RDONLY);
283     unsigned int count;
284
285     lispobj initial_function = NIL;
286     FSHOW((stderr, "/entering load_core_file(%s)\n", file));
287     if (fd < 0) {
288         fprintf(stderr, "could not open file \"%s\"\n", file);
289         perror("open");
290         exit(1);
291     }
292
293     lseek(fd, file_offset, SEEK_SET);
294     header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
295
296     count = read(fd, header, os_vm_page_size);
297     if (count < os_vm_page_size) {
298         lose("premature end of core file\n");
299     }
300     SHOW("successfully read first page of core");
301
302     ptr = header;
303     val = *ptr++;
304
305     if (val != CORE_MAGIC) {
306         lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
307              val,
308              CORE_MAGIC);
309     }
310     SHOW("found CORE_MAGIC");
311
312     while (val != END_CORE_ENTRY_TYPE_CODE) {
313         val = *ptr++;
314         len = *ptr++;
315         remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
316         FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
317                (long)val, (long)remaining_len));
318
319         switch (val) {
320
321         case END_CORE_ENTRY_TYPE_CODE:
322             SHOW("END_CORE_ENTRY_TYPE_CODE case");
323             break;
324
325         case VERSION_CORE_ENTRY_TYPE_CODE:
326             SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
327             if (*ptr != SBCL_CORE_VERSION_INTEGER) {
328                 lose("core file version (%d) != runtime library version (%d)\n",
329                      *ptr,
330                      SBCL_CORE_VERSION_INTEGER);
331             }
332             break;
333
334         case BUILD_ID_CORE_ENTRY_TYPE_CODE:
335             SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
336             {
337                 unsigned int i;
338
339                 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
340                 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
341                 if (remaining_len != strlen((const char *)build_id))
342                     goto losing_build_id;
343                 for (i = 0; i < remaining_len; ++i) {
344                     FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
345                            i, ptr[i], build_id[i]));
346                     if (ptr[i] != build_id[i])
347                         goto losing_build_id;
348                 }
349                 break;
350             losing_build_id:
351                 /* .core files are not binary-compatible between
352                  * builds because we can't easily detect whether the
353                  * sources were patched between the time the
354                  * dumping-the-.core runtime was built and the time
355                  * that the loading-the-.core runtime was built.
356                  *
357                  * (We could easily detect whether version.lisp-expr
358                  * was changed, but people experimenting with patches
359                  * don't necessarily update version.lisp-expr.) */
360
361                 lose("can't load .core for different runtime, sorry\n");
362             }
363
364         case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
365             SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
366             process_directory(fd,
367                               ptr,
368 #ifndef LISP_FEATURE_ALPHA
369                               remaining_len / (sizeof(struct ndir_entry) /
370                                                sizeof(long)),
371 #else
372                               remaining_len / (sizeof(struct ndir_entry) /
373                                                sizeof(u32)),
374 #endif
375                               file_offset);
376             break;
377
378         case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
379             SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
380             initial_function = (lispobj)*ptr;
381             break;
382
383 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
384         case LUTEX_TABLE_CORE_ENTRY_TYPE_CODE:
385             SHOW("LUTEX_TABLE_CORE_ENTRY_TYPE_CODE case");
386             {
387                 size_t n_lutexes = *ptr;
388                 size_t fdoffset = (*(ptr + 1) + 1) * (os_vm_page_size);
389                 size_t data_length = n_lutexes * sizeof(struct sap *);
390                 struct lutex **lutexes_to_resurrect = malloc(data_length);
391                 long bytes_read;
392
393                 lseek(fd, fdoffset + file_offset, SEEK_SET);
394
395                 FSHOW((stderr, "attempting to read %ld lutexes from core\n", n_lutexes));
396                 bytes_read = read(fd, lutexes_to_resurrect, data_length);
397
398                 /* XXX */
399                 if (bytes_read != data_length) {
400                     lose("Could not read the lutex table");
401                 }
402                 else {
403                     int i;
404
405                     for (i=0; i<n_lutexes; ++i) {
406                         struct lutex *lutex = lutexes_to_resurrect[i];
407
408                         FSHOW((stderr, "re-init'ing lutex @ %p\n", lutex));
409                         lutex_init((tagged_lutex_t) lutex);
410                     }
411
412                     free(lutexes_to_resurrect);
413                 }
414                 break;
415             }
416 #endif
417
418 #ifdef LISP_FEATURE_GENCGC
419         case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
420         {
421             size_t size = *ptr;
422             size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
423             size_t offset = 0;
424             long bytes_read;
425             unsigned long data[4096];
426             unsigned long word;
427             lseek(fd, fdoffset + file_offset, SEEK_SET);
428             while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
429                     > 0)
430             {
431                 int i = 0;
432                 size -= bytes_read;
433                 while (bytes_read) {
434                     bytes_read -= sizeof(long);
435                     /* Ignore all zeroes. The size of the page table
436                      * core entry was rounded up to os_vm_page_size
437                      * during the save, and might now have more
438                      * elements than the page table.
439                      *
440                      * The low bits of each word are allocation flags.
441                      */
442                     if (word=data[i]) {
443                         page_table[offset].region_start_offset = word & ~0x03;
444                         page_table[offset].allocated = word & 0x03;
445                     }
446                     i++;
447                     offset++;
448                 }
449             }
450
451             gencgc_partial_pickup = 1;
452             break;
453         }
454 #endif
455         default:
456             lose("unknown core file entry: %ld\n", (long)val);
457         }
458
459         ptr += remaining_len;
460         FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
461     }
462     SHOW("about to free(header)");
463     free(header);
464     SHOW("returning from load_core_file(..)");
465     return initial_function;
466 }
467