fefa834b6c2a207cd4477ec93fa21aadcbd0be58
[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 "sbcl.h"
18
19 #ifndef LISP_FEATURE_WIN32
20 #ifdef LISP_FEATURE_LINUX
21 /* For madvise */
22 #define _BSD_SOURCE
23 #include <sys/mman.h>
24 #undef _BSD_SOURCE
25 #else
26 #include <sys/mman.h>
27 #endif
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/file.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38
39 #include "os.h"
40 #include "runtime.h"
41 #include "globals.h"
42 #include "core.h"
43 #include "arch.h"
44 #include "interr.h"
45 #include "thread.h"
46
47 #include "validate.h"
48 #include "gc-internal.h"
49 #include "runtime-options.h"
50
51 #include <errno.h>
52
53 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
54 # include <zlib.h>
55 #endif
56
57 unsigned char build_id[] =
58 #include "../../output/build-id.tmp"
59 ;
60
61 int
62 open_binary(char *filename, int mode)
63 {
64 #ifdef LISP_FEATURE_WIN32
65     mode |= O_BINARY;
66 #endif
67
68     return open(filename, mode);
69 }
70
71
72 static struct runtime_options *
73 read_runtime_options(int fd)
74 {
75     os_vm_size_t optarray[RUNTIME_OPTIONS_WORDS];
76     struct runtime_options *options = NULL;
77
78     if (read(fd, optarray, RUNTIME_OPTIONS_WORDS * sizeof(os_vm_size_t)) !=
79         RUNTIME_OPTIONS_WORDS * sizeof(size_t)) {
80         return NULL;
81     }
82
83     if ((RUNTIME_OPTIONS_MAGIC != optarray[0]) || (0 == optarray[1])) {
84         return NULL;
85     }
86
87     options = successful_malloc(sizeof(struct runtime_options));
88
89     options->dynamic_space_size = optarray[2];
90     options->thread_control_stack_size = optarray[3];
91
92     return options;
93 }
94
95 void
96 maybe_initialize_runtime_options(int fd)
97 {
98     struct runtime_options *new_runtime_options;
99     off_t end_offset = sizeof(lispobj) +
100         sizeof(os_vm_offset_t) +
101         (RUNTIME_OPTIONS_WORDS * sizeof(size_t));
102
103     lseek(fd, -end_offset, SEEK_END);
104
105     if ((new_runtime_options = read_runtime_options(fd))) {
106         runtime_options = new_runtime_options;
107     }
108 }
109
110 /* Search 'filename' for an embedded core.  An SBCL core has, at the
111  * end of the file, a trailer containing optional saved runtime
112  * options, the start of the core (an os_vm_offset_t), and a final
113  * signature word (the lispobj CORE_MAGIC).  If this trailer is found
114  * at the end of the file, the start of the core can be determined
115  * from the core size.
116  *
117  * If an embedded core is present, this returns the offset into the
118  * file to load the core from, or -1 if no core is present. */
119 os_vm_offset_t
120 search_for_embedded_core(char *filename)
121 {
122     lispobj header;
123     os_vm_offset_t lispobj_size = sizeof(lispobj);
124     os_vm_offset_t trailer_size = lispobj_size + sizeof(os_vm_offset_t);
125     os_vm_offset_t core_start, pos;
126     int fd = -1;
127
128     if ((fd = open_binary(filename, O_RDONLY)) < 0)
129         goto lose;
130
131     if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
132         goto lose;
133     if (header == CORE_MAGIC) {
134         /* This file is a real core, not an embedded core.  Return 0 to
135          * indicate where the core starts, and do not look for runtime
136          * options in this case. */
137         return 0;
138     }
139
140     if (lseek(fd, -lispobj_size, SEEK_END) < 0)
141         goto lose;
142     if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
143         goto lose;
144
145     if (header == CORE_MAGIC) {
146         if (lseek(fd, -trailer_size, SEEK_END) < 0)
147             goto lose;
148         if (read(fd, &core_start, sizeof(os_vm_offset_t)) < 0)
149             goto lose;
150
151         if (lseek(fd, core_start, SEEK_SET) < 0)
152             goto lose;
153         pos = lseek(fd, 0, SEEK_CUR);
154
155         if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
156             goto lose;
157
158         if (header != CORE_MAGIC)
159             goto lose;
160
161         maybe_initialize_runtime_options(fd);
162
163         close(fd);
164         return pos;
165     }
166
167 lose:
168     if (fd != -1)
169         close(fd);
170
171     return -1;
172 }
173
174 /* If more platforms doesn't support overlapping mmap rename this
175  * def to something like ifdef nommapoverlap */
176 /* currently hpux only */
177 #ifdef LISP_FEATURE_HPUX
178 os_vm_address_t copy_core_bytes(int fd, os_vm_offset_t offset,
179                                 os_vm_address_t addr, int len)
180 {
181   unsigned char buf[4096];
182   int c,x;
183   int old_fd = lseek(fd, 0, SEEK_CUR);
184
185   if(len & (4096-1)){
186     fprintf(stderr, "cant copy a slice of core because slice-length is not of page size(4096)\n");
187     exit(-1);
188   }
189   if(old_fd < 0){
190     fprintf(stderr, "cant perform lseek() on corefile\n");
191   }
192   lseek(fd, offset, SEEK_SET);
193   if(fd < 0){
194     fprintf(stderr, "cant perform lseek(%u,%lu,SEEK_SET) on corefile\n", fd, offset);
195   }
196   for(x = 0; x < len; x += 4096){
197     c = read(fd, buf, 4096);
198     if(c != 4096){
199       fprintf(stderr, "cant read memory area from corefile at position %lu, got %d\n", offset + x, c);
200       exit(-1);
201     }
202     memcpy(addr+x, buf, 4096);
203   }
204   os_flush_icache(addr, len);
205   return addr;
206 }
207 #endif
208
209 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
210 # define ZLIB_BUFFER_SIZE (1u<<16)
211 os_vm_address_t inflate_core_bytes(int fd, os_vm_offset_t offset,
212                                    os_vm_address_t addr, int len)
213 {
214     z_stream stream;
215     unsigned char buf[ZLIB_BUFFER_SIZE];
216     int ret;
217
218     if (-1 == lseek(fd, offset, SEEK_SET)) {
219         lose("Unable to lseek() on corefile\n");
220     }
221
222     stream.zalloc = NULL;
223     stream.zfree = NULL;
224     stream.opaque = NULL;
225     stream.avail_in = 0;
226     stream.next_in = buf;
227
228     ret = inflateInit(&stream);
229     if (ret != Z_OK)
230         lose("zlib error %i\n", ret);
231
232     stream.next_out  = (void*)addr;
233     stream.avail_out = len;
234     do {
235         ssize_t count = read(fd, buf, sizeof(buf));
236         if (count < 0)
237             lose("unable to read core file (errno = %i)\n", errno);
238         stream.next_in = buf;
239         stream.avail_in = count;
240         if (count == 0) break;
241         ret = inflate(&stream, Z_NO_FLUSH);
242         switch (ret) {
243         case Z_STREAM_END:
244             break;
245         case Z_OK:
246             if (stream.avail_out == 0)
247                 lose("Runaway gzipped core directory... aborting\n");
248             if (stream.avail_in > 0)
249                 lose("zlib inflate returned without fully"
250                      "using up input buffer... aborting\n");
251             break;
252         default:
253             lose("zlib inflate error: %i\n", ret);
254             break;
255         }
256     } while (ret != Z_STREAM_END);
257
258     if (stream.avail_out > 0) {
259         if (stream.avail_out >= os_vm_page_size)
260             fprintf(stderr, "Warning: gzipped core directory significantly"
261                     "shorter than expected (%lu bytes)", (unsigned long)stream.avail_out);
262         /* Is this needed? */
263         memset(stream.next_out, 0, stream.avail_out);
264     }
265
266     inflateEnd(&stream);
267     return addr;
268 }
269 # undef ZLIB_BUFFER_SIZE
270 #endif
271
272 int merge_core_pages = -1;
273
274 static void
275 process_directory(int fd, lispobj *ptr, int count, os_vm_offset_t file_offset)
276 {
277     struct ndir_entry *entry;
278     int compressed;
279
280     FSHOW((stderr, "/process_directory(..), count=%d\n", count));
281
282     for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
283         compressed = 0;
284         long id = entry->identifier;
285         if (id <= (MAX_CORE_SPACE_ID | DEFLATED_CORE_SPACE_ID_FLAG)) {
286             if (id & DEFLATED_CORE_SPACE_ID_FLAG)
287                 compressed = 1;
288             id &= ~(DEFLATED_CORE_SPACE_ID_FLAG);
289         }
290         long offset = os_vm_page_size * (1 + entry->data_page);
291         os_vm_address_t addr =
292             (os_vm_address_t) (os_vm_page_size * entry->address);
293         lispobj *free_pointer = (lispobj *) addr + entry->nwords;
294         unsigned long len = os_vm_page_size * entry->page_count;
295         if (len != 0) {
296             os_vm_address_t real_addr;
297             FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
298                    (long)len, (long)len, (unsigned long)addr));
299             if (compressed) {
300 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
301                 real_addr = inflate_core_bytes(fd, offset + file_offset, addr, len);
302 #else
303                 lose("This runtime was not built with zlib-compressed core support... aborting\n");
304 #endif
305             } else {
306 #ifdef LISP_FEATURE_HPUX
307                 real_addr = copy_core_bytes(fd, offset + file_offset, addr, len);
308 #else
309                 real_addr = os_map(fd, offset + file_offset, addr, len);
310 #endif
311             }
312             if (real_addr != addr) {
313                 lose("file mapped in wrong place! "
314                      "(0x%08x != 0x%08lx)\n",
315                      real_addr,
316                      addr);
317             }
318         }
319
320 #ifdef MADV_MERGEABLE
321         if ((merge_core_pages == 1)
322             || ((merge_core_pages == -1) && compressed)) {
323                 madvise(addr, len, MADV_MERGEABLE);
324         }
325 #endif
326
327         FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
328                id, (unsigned long)free_pointer));
329
330         switch (id) {
331         case DYNAMIC_CORE_SPACE_ID:
332             if (len > dynamic_space_size) {
333                 fprintf(stderr,
334                         "dynamic space too small for core: %ldKiB required, %ldKiB available.\n",
335                         len >> 10,
336                         (long)dynamic_space_size >> 10);
337                 exit(1);
338             }
339 #ifdef LISP_FEATURE_GENCGC
340             if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
341                 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
342                         (long)addr, (long)DYNAMIC_SPACE_START);
343                 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
344             }
345 #else
346             if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
347                 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
348                 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
349                         (long)addr,
350                         (long)DYNAMIC_0_SPACE_START,
351                         (long)DYNAMIC_1_SPACE_START);
352                 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
353             }
354 #endif
355 #if defined(ALLOCATION_POINTER)
356             SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
357 #else
358             dynamic_space_free_pointer = free_pointer;
359 #endif
360             /* For stop-and-copy GC, this will be whatever the GC was
361              * using at the time. With GENCGC, this will always be
362              * space 0. (We checked above that for GENCGC,
363              * addr==DYNAMIC_SPACE_START.) */
364             current_dynamic_space = (lispobj *)addr;
365             break;
366         case STATIC_CORE_SPACE_ID:
367             if (addr != (os_vm_address_t)STATIC_SPACE_START) {
368                 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
369                         (long)addr, (long)STATIC_SPACE_START);
370                 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
371             }
372             break;
373         case READ_ONLY_CORE_SPACE_ID:
374             if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
375                 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
376                         (long)addr, (long)READ_ONLY_SPACE_START);
377                 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
378             }
379             break;
380         default:
381             lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
382         }
383     }
384 }
385
386 lispobj
387 load_core_file(char *file, os_vm_offset_t file_offset)
388 {
389     void *header;
390     word_t val, *ptr;
391     os_vm_size_t len, remaining_len;
392     int fd = open_binary(file, O_RDONLY);
393     ssize_t count;
394     lispobj initial_function = NIL;
395
396     FSHOW((stderr, "/entering load_core_file(%s)\n", file));
397     if (fd < 0) {
398         fprintf(stderr, "could not open file \"%s\"\n", file);
399         perror("open");
400         exit(1);
401     }
402
403     lseek(fd, file_offset, SEEK_SET);
404     header = calloc(os_vm_page_size, 1);
405
406     count = read(fd, header, os_vm_page_size);
407     if (count < os_vm_page_size) {
408         lose("premature end of core file\n");
409     }
410     SHOW("successfully read first page of core");
411
412     ptr = header;
413     val = *ptr++;
414
415     if (val != CORE_MAGIC) {
416         lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
417              val,
418              CORE_MAGIC);
419     }
420     SHOW("found CORE_MAGIC");
421
422     while (val != END_CORE_ENTRY_TYPE_CODE) {
423         val = *ptr++;
424         len = *ptr++;
425         remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
426         FSHOW((stderr, "/val=0x%"WORD_FMTX", remaining_len=0x%"WORD_FMTX"\n",
427                val, remaining_len));
428
429         switch (val) {
430
431         case END_CORE_ENTRY_TYPE_CODE:
432             SHOW("END_CORE_ENTRY_TYPE_CODE case");
433             break;
434
435         case VERSION_CORE_ENTRY_TYPE_CODE:
436             SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
437             if (*ptr != SBCL_CORE_VERSION_INTEGER) {
438                 lose("core file version (%d) != runtime library version (%d)\n",
439                      *ptr,
440                      SBCL_CORE_VERSION_INTEGER);
441             }
442             break;
443
444         case BUILD_ID_CORE_ENTRY_TYPE_CODE:
445             SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
446             {
447                 os_vm_size_t i;
448
449                 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
450                 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
451                 if (remaining_len != strlen((const char *)build_id))
452                     goto losing_build_id;
453                 for (i = 0; i < remaining_len; ++i) {
454                     FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
455                            i, ptr[i], build_id[i]));
456                     if (ptr[i] != build_id[i])
457                         goto losing_build_id;
458                 }
459                 break;
460             losing_build_id:
461                 /* .core files are not binary-compatible between
462                  * builds because we can't easily detect whether the
463                  * sources were patched between the time the
464                  * dumping-the-.core runtime was built and the time
465                  * that the loading-the-.core runtime was built.
466                  *
467                  * (We could easily detect whether version.lisp-expr
468                  * was changed, but people experimenting with patches
469                  * don't necessarily update version.lisp-expr.) */
470
471                 lose("can't load .core for different runtime, sorry\n");
472             }
473
474         case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
475             SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
476             process_directory(fd,
477                               ptr,
478 #ifndef LISP_FEATURE_ALPHA
479                               remaining_len / (sizeof(struct ndir_entry) /
480                                                sizeof(long)),
481 #else
482                               remaining_len / (sizeof(struct ndir_entry) /
483                                                sizeof(u32)),
484 #endif
485                               file_offset);
486             break;
487
488         case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
489             SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
490             initial_function = (lispobj)*ptr;
491             break;
492
493 #ifdef LISP_FEATURE_GENCGC
494         case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
495         {
496             os_vm_size_t size = *ptr;
497             os_vm_size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
498             page_index_t offset = 0;
499             ssize_t bytes_read;
500             word_t data[4096];
501             word_t word;
502             lseek(fd, fdoffset + file_offset, SEEK_SET);
503             while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
504                     > 0)
505             {
506                 int i = 0;
507                 size -= bytes_read;
508                 while (bytes_read) {
509                     bytes_read -= sizeof(word_t);
510                     /* Ignore all zeroes. The size of the page table
511                      * core entry was rounded up to os_vm_page_size
512                      * during the save, and might now have more
513                      * elements than the page table.
514                      *
515                      * The low bits of each word are allocation flags.
516                      */
517                     if ((word=data[i])) {
518                         page_table[offset].region_start_offset = word & ~0x03;
519                         page_table[offset].allocated = word & 0x03;
520                     }
521                     i++;
522                     offset++;
523                 }
524             }
525
526             gencgc_partial_pickup = 1;
527             break;
528         }
529 #endif
530         default:
531             lose("unknown core file entry: 0x%"WORD_FMTX"\n", val);
532         }
533
534         ptr += remaining_len;
535         FSHOW((stderr, "/new ptr=0x%"WORD_FMTX"\n", ptr));
536     }
537     SHOW("about to free(header)");
538     free(header);
539     SHOW("returning from load_core_file(..)");
540     return initial_function;
541 }
542