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