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