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