Fix the build on Windows
[sbcl.git] / src / runtime / save.c
1 /*
2  * This software is part of the SBCL system. See the README file for
3  * more information.
4  *
5  * This software is derived from the CMU CL system, which was
6  * written at Carnegie Mellon University and released into the
7  * public domain. The software is in the public domain and is
8  * provided with absolutely no warranty. See the COPYING and CREDITS
9  * files for more information.
10  */
11
12 #ifndef LISP_FEATURE_WIN32
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #endif
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <signal.h>
20 #include <sys/file.h>
21
22 #include "sbcl.h"
23 #include "runtime.h"
24 #include "os.h"
25 #include "core.h"
26 #include "globals.h"
27 #include "save.h"
28 #include "dynbind.h"
29 #include "lispregs.h"
30 #include "validate.h"
31 #include "gc-internal.h"
32 #include "thread.h"
33
34 #include "genesis/static-symbols.h"
35 #include "genesis/symbol.h"
36
37 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
38 #include "genesis/lutex.h"
39 #endif
40
41 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
42 # include <zlib.h>
43 #endif
44
45
46 /* write_runtime_options uses a simple serialization scheme that
47  * consists of one word of magic, one word indicating whether options
48  * are actually saved, and one word per struct field. */
49 static void
50 write_runtime_options(FILE *file, struct runtime_options *options)
51 {
52     size_t optarray[RUNTIME_OPTIONS_WORDS];
53
54     memset(&optarray, 0, sizeof(optarray));
55     optarray[0] = RUNTIME_OPTIONS_MAGIC;
56
57     if (options != NULL) {
58         /* optarray[1] is a flag indicating that options are present */
59         optarray[1] = 1;
60         optarray[2] = options->dynamic_space_size;
61         optarray[3] = options->thread_control_stack_size;
62     }
63
64     if (RUNTIME_OPTIONS_WORDS !=
65         fwrite(optarray, sizeof(size_t), RUNTIME_OPTIONS_WORDS, file)) {
66         perror("Error writing runtime options to file");
67     }
68 }
69
70 static void
71 write_lispobj(lispobj obj, FILE *file)
72 {
73     if (1 != fwrite(&obj, sizeof(lispobj), 1, file)) {
74         perror("Error writing to file");
75     }
76 }
77
78 static void
79 write_bytes_to_file(FILE * file, char *addr, long bytes, int compression)
80 {
81     if (compression == COMPRESSION_LEVEL_NONE) {
82         while (bytes > 0) {
83             long count = fwrite(addr, 1, bytes, file);
84             if (count > 0) {
85                 bytes -= count;
86                 addr += count;
87             }
88             else {
89                 perror("error writing to save file");
90                 bytes = 0;
91             }
92         }
93 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
94     } else if ((compression >= -1) && (compression <= 9)) {
95 # define ZLIB_BUFFER_SIZE (1u<<16)
96         z_stream stream;
97         unsigned char buf[ZLIB_BUFFER_SIZE];
98         unsigned char * written, * end;
99         long total_written = 0;
100         int ret;
101         stream.zalloc = NULL;
102         stream.zfree = NULL;
103         stream.opaque = NULL;
104         stream.avail_in = bytes;
105         stream.next_in  = (void*)addr;
106         ret = deflateInit(&stream, compression);
107         if (ret != Z_OK)
108             lose("deflateInit: %i\n", ret);
109         do {
110             stream.avail_out = sizeof(buf);
111             stream.next_out = buf;
112             ret = deflate(&stream, Z_FINISH);
113             if (ret < 0) lose("zlib deflate error: %i... exiting\n", ret);
114             written = buf;
115             end     = buf+sizeof(buf)-stream.avail_out;
116             total_written += end - written;
117             while (written < end) {
118                 long count = fwrite(written, 1, end-written, file);
119                 if (count > 0) {
120                     written += count;
121                 } else {
122                     lose("unable to write to core file\n");
123                 }
124             }
125         } while (stream.avail_out == 0);
126         deflateEnd(&stream);
127         printf("compressed %lu bytes into %lu at level %i\n",
128                bytes, total_written, compression);
129 # undef ZLIB_BUFFER_SIZE
130 #endif
131     } else {
132 #ifdef LISP_FEATURE_SB_CORE_COMPRESSION
133         lose("Unknown core compression level %i, exiting\n", compression);
134 #else
135         lose("zlib-compressed core support not built in this runtime\n");
136 #endif
137     }
138
139     fflush(file);
140 };
141
142
143 static long
144 write_and_compress_bytes(FILE *file, char *addr, long bytes, os_vm_offset_t file_offset,
145                          int compression)
146 {
147     long here, data;
148
149     bytes = (bytes+os_vm_page_size-1)&~(os_vm_page_size-1);
150
151 #ifdef LISP_FEATURE_WIN32
152     long count;
153     /* touch every single page in the space to force it to be mapped. */
154     for (count = 0; count < bytes; count += 0x1000) {
155         volatile int temp = addr[count];
156     }
157 #endif
158
159     fflush(file);
160     here = ftell(file);
161     fseek(file, 0, SEEK_END);
162     data = (ftell(file)+os_vm_page_size-1)&~(os_vm_page_size-1);
163     fseek(file, data, SEEK_SET);
164     write_bytes_to_file(file, addr, bytes, compression);
165     fseek(file, here, SEEK_SET);
166     return ((data - file_offset) / os_vm_page_size) - 1;
167 }
168
169 static long
170 write_bytes(FILE *file, char *addr, long bytes, os_vm_offset_t file_offset)
171 {
172     return write_and_compress_bytes(file, addr, bytes, file_offset,
173                                     COMPRESSION_LEVEL_NONE);
174 }
175
176 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
177 /* saving lutexes in the core */
178 static void **lutex_addresses;
179 static long n_lutexes = 0;
180 static long max_lutexes = 0;
181
182 static long
183 default_scan_action(lispobj *obj)
184 {
185     return (sizetab[widetag_of(*obj)])(obj);
186 }
187
188 static long
189 lutex_scan_action(lispobj *obj)
190 {
191     /* note the address of the lutex */
192     if(n_lutexes >= max_lutexes) {
193         max_lutexes *= 2;
194         lutex_addresses = realloc(lutex_addresses, max_lutexes * sizeof(void *));
195         gc_assert(lutex_addresses);
196     }
197
198     lutex_addresses[n_lutexes++] = obj;
199
200     return (*sizetab[widetag_of(*obj)])(obj);
201 }
202
203 typedef long (*scan_table[256])(lispobj *obj);
204
205 static void
206 scan_objects(lispobj *start, long n_words, scan_table table)
207 {
208     lispobj *end = start + n_words;
209     lispobj *object_ptr;
210     long n_words_scanned;
211     for (object_ptr = start;
212          object_ptr < end;
213          object_ptr += n_words_scanned) {
214         lispobj obj = *object_ptr;
215
216         n_words_scanned = (table[widetag_of(obj)])(object_ptr);
217     }
218 }
219
220 static void
221 scan_for_lutexes(lispobj *addr, long n_words)
222 {
223     static int initialized = 0;
224     static scan_table lutex_scan_table;
225
226     if (!initialized) {
227         int i;
228
229         /* allocate a little space to get started */
230         lutex_addresses = malloc(16*sizeof(void *));
231         gc_assert(lutex_addresses);
232         max_lutexes = 16;
233
234         /* initialize the mapping table */
235         for(i = 0; i < ((sizeof lutex_scan_table)/(sizeof lutex_scan_table[0])); ++i) {
236             lutex_scan_table[i] = default_scan_action;
237         }
238
239         lutex_scan_table[LUTEX_WIDETAG] = lutex_scan_action;
240
241         initialized = 1;
242     }
243
244     /* do the scan */
245     scan_objects(addr, n_words, lutex_scan_table);
246 }
247 #endif
248
249 static void
250 output_space(FILE *file, int id, lispobj *addr, lispobj *end,
251              os_vm_offset_t file_offset,
252              int core_compression_level)
253 {
254     size_t words, bytes, data, compressed_flag;
255     static char *names[] = {NULL, "dynamic", "static", "read-only"};
256
257     compressed_flag
258             = ((core_compression_level != COMPRESSION_LEVEL_NONE)
259                ? DEFLATED_CORE_SPACE_ID_FLAG : 0);
260
261     write_lispobj(id | compressed_flag, file);
262     words = end - addr;
263     write_lispobj(words, file);
264
265     bytes = words * sizeof(lispobj);
266
267 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
268     printf("scanning space for lutexes...\n");
269     scan_for_lutexes((void *)addr, words);
270 #endif
271
272     printf("writing %lu bytes from the %s space at 0x%08lx\n",
273            (unsigned long)bytes, names[id], (unsigned long)addr);
274
275     data = write_and_compress_bytes(file, (char *)addr, bytes, file_offset,
276                                     core_compression_level);
277
278     write_lispobj(data, file);
279     write_lispobj((long)addr / os_vm_page_size, file);
280     write_lispobj((bytes + os_vm_page_size - 1) / os_vm_page_size, file);
281 }
282
283 FILE *
284 open_core_for_saving(char *filename)
285 {
286     /* Open the output file. We don't actually need the file yet, but
287      * the fopen() might fail for some reason, and we want to detect
288      * that and back out before we do anything irreversible. */
289     unlink(filename);
290     return fopen(filename, "wb");
291 }
292
293 boolean
294 save_to_filehandle(FILE *file, char *filename, lispobj init_function,
295                    boolean make_executable,
296                    boolean save_runtime_options,
297                    int core_compression_level)
298 {
299     struct thread *th;
300     os_vm_offset_t core_start_pos;
301
302     /* Smash the enclosing state. (Once we do this, there's no good
303      * way to go back, which is a sufficient reason that this ends up
304      * being SAVE-LISP-AND-DIE instead of SAVE-LISP-AND-GO-ON). */
305     printf("[undoing binding stack and other enclosing state... ");
306     fflush(stdout);
307     for_each_thread(th) {       /* XXX really? */
308         unbind_to_here((lispobj *)th->binding_stack_start,th);
309         SetSymbolValue(CURRENT_CATCH_BLOCK, 0,th);
310         SetSymbolValue(CURRENT_UNWIND_PROTECT_BLOCK, 0,th);
311     }
312     printf("done]\n");
313     fflush(stdout);
314
315     /* (Now we can actually start copying ourselves into the output file.) */
316
317     printf("[saving current Lisp image into %s:\n", filename);
318     fflush(stdout);
319
320     core_start_pos = ftell(file);
321     write_lispobj(CORE_MAGIC, file);
322
323     write_lispobj(VERSION_CORE_ENTRY_TYPE_CODE, file);
324     write_lispobj(3, file);
325     write_lispobj(SBCL_CORE_VERSION_INTEGER, file);
326
327     write_lispobj(BUILD_ID_CORE_ENTRY_TYPE_CODE, file);
328     write_lispobj(/* (We're writing the word count of the entry here, and the 2
329           * term is one word for the leading BUILD_ID_CORE_ENTRY_TYPE_CODE
330           * word and one word where we store the count itself.) */
331          2 + strlen((const char *)build_id),
332          file);
333     {
334         unsigned char *p;
335         for (p = (unsigned char *)build_id; *p; ++p)
336             write_lispobj(*p, file);
337     }
338
339     write_lispobj(NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE, file);
340     write_lispobj(/* (word count = 3 spaces described by 5 words each, plus the
341           * entry type code, plus this count itself) */
342          (5*3)+2, file);
343     output_space(file,
344                  READ_ONLY_CORE_SPACE_ID,
345                  (lispobj *)READ_ONLY_SPACE_START,
346                  (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0),
347                  core_start_pos,
348                  core_compression_level);
349     output_space(file,
350                  STATIC_CORE_SPACE_ID,
351                  (lispobj *)STATIC_SPACE_START,
352                  (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0),
353                  core_start_pos,
354                  core_compression_level);
355 #ifdef LISP_FEATURE_GENCGC
356     /* Flush the current_region, updating the tables. */
357     gc_alloc_update_all_page_tables();
358     update_dynamic_space_free_pointer();
359 #endif
360 #ifdef reg_ALLOC
361 #ifdef LISP_FEATURE_GENCGC
362     output_space(file,
363                  DYNAMIC_CORE_SPACE_ID,
364                  (lispobj *)DYNAMIC_SPACE_START,
365                  dynamic_space_free_pointer,
366                  core_start_pos,
367                  core_compression_level);
368 #else
369     output_space(file,
370                  DYNAMIC_CORE_SPACE_ID,
371                  (lispobj *)current_dynamic_space,
372                  dynamic_space_free_pointer,
373                  core_start_pos,
374                  core_compression_level);
375 #endif
376 #else
377     output_space(file,
378                  DYNAMIC_CORE_SPACE_ID,
379                  (lispobj *)DYNAMIC_SPACE_START,
380                  (lispobj *)SymbolValue(ALLOCATION_POINTER,0),
381                  core_start_pos,
382                  core_compression_level);
383 #endif
384
385     write_lispobj(INITIAL_FUN_CORE_ENTRY_TYPE_CODE, file);
386     write_lispobj(3, file);
387     write_lispobj(init_function, file);
388
389 #ifdef LISP_FEATURE_GENCGC
390     {
391         size_t size = (last_free_page*sizeof(long)+os_vm_page_size-1)
392             &~(os_vm_page_size-1);
393         unsigned long *data = calloc(size, 1);
394         if (data) {
395             unsigned long word;
396             long offset;
397             int i;
398             for (i = 0; i < last_free_page; i++) {
399                 /* Thanks to alignment requirements, the two low bits
400                  * are always zero, so we can use them to store the
401                  * allocation type -- region is always closed, so only
402                  * the two low bits of allocation flags matter. */
403                 word = page_table[i].region_start_offset;
404                 gc_assert((word & 0x03) == 0);
405                 data[i] = word | (0x03 & page_table[i].allocated);
406             }
407             write_lispobj(PAGE_TABLE_CORE_ENTRY_TYPE_CODE, file);
408             write_lispobj(4, file);
409             write_lispobj(size, file);
410             offset = write_bytes(file, (char *)data, size, core_start_pos);
411             write_lispobj(offset, file);
412         }
413     }
414 #endif
415
416 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_SB_LUTEX)
417     if(n_lutexes > 0) {
418         long offset;
419         printf("writing %ld lutexes to the core...\n", n_lutexes);
420         write_lispobj(LUTEX_TABLE_CORE_ENTRY_TYPE_CODE, file);
421         /* word count of the entry */
422         write_lispobj(4, file);
423         /* indicate how many lutexes we saved */
424         write_lispobj(n_lutexes, file);
425         /* save the lutexes */
426         offset = write_bytes(file, (char *) lutex_addresses,
427                              n_lutexes * sizeof(*lutex_addresses),
428                              core_start_pos);
429
430         write_lispobj(offset, file);
431     }
432 #endif
433
434     write_lispobj(END_CORE_ENTRY_TYPE_CODE, file);
435
436     /* Write a trailing header, ignored when parsing the core normally.
437      * This is used to locate the start of the core when the runtime is
438      * prepended to it. */
439     fseek(file, 0, SEEK_END);
440
441     /* If NULL runtime options are passed to write_runtime_options,
442      * command-line processing is performed as normal in the SBCL
443      * executable. Otherwise, the saved runtime options are used and
444      * all command-line arguments are available to Lisp in
445      * SB-EXT:*POSIX-ARGV*. */
446     write_runtime_options(file,
447                           (save_runtime_options ? runtime_options : NULL));
448
449     if (1 != fwrite(&core_start_pos, sizeof(os_vm_offset_t), 1, file)) {
450         perror("Error writing core starting position to file");
451         fclose(file);
452     } else {
453         write_lispobj(CORE_MAGIC, file);
454         fclose(file);
455     }
456
457 #ifndef LISP_FEATURE_WIN32
458     if (make_executable)
459         chmod (filename, 0755);
460 #endif
461
462     printf("done]\n");
463     exit(0);
464 }
465
466 /* Check if the build_id for the current runtime is present in a
467  * buffer. */
468 int
469 check_runtime_build_id(void *buf, size_t size)
470 {
471     size_t idlen;
472     char *pos;
473
474     idlen = strlen(build_id) - 1;
475     while ((pos = memchr(buf, build_id[0], size)) != NULL) {
476         size -= (pos + 1) - (char *)buf;
477         buf = (pos + 1);
478         if (idlen <= size && memcmp(buf, build_id + 1, idlen) == 0)
479             return 1;
480     }
481
482     return 0;
483 }
484
485 /* Slurp the executable portion of the runtime into a malloced buffer
486  * and return it.  Places the size in bytes of the runtime into
487  * 'size_out'.  Returns NULL if the runtime cannot be loaded from
488  * 'runtime_path'. */
489 void *
490 load_runtime(char *runtime_path, size_t *size_out)
491 {
492     void *buf = NULL;
493     FILE *input = NULL;
494     size_t size, count;
495     os_vm_offset_t core_offset;
496
497     core_offset = search_for_embedded_core (runtime_path);
498     if ((input = fopen(runtime_path, "rb")) == NULL) {
499         fprintf(stderr, "Unable to open runtime: %s\n", runtime_path);
500         goto lose;
501     }
502
503     fseek(input, 0, SEEK_END);
504     size = (size_t) ftell(input);
505     fseek(input, 0, SEEK_SET);
506
507     if (core_offset != -1 && size > core_offset)
508         size = core_offset;
509
510     buf = successful_malloc(size);
511     if ((count = fread(buf, 1, size, input)) != size) {
512         fprintf(stderr, "Premature EOF while reading runtime.\n");
513         goto lose;
514     }
515
516     if (!check_runtime_build_id(buf, size)) {
517         fprintf(stderr, "Failed to locate current build_id in runtime: %s\n",
518             runtime_path);
519         goto lose;
520     }
521
522     fclose(input);
523     *size_out = size;
524     return buf;
525
526 lose:
527     if (input != NULL)
528         fclose(input);
529     if (buf != NULL)
530         free(buf);
531     return NULL;
532 }
533
534 boolean
535 save_runtime_to_filehandle(FILE *output, void *runtime, size_t runtime_size)
536 {
537     size_t padding;
538     void *padbytes;
539
540     if (runtime_size != fwrite(runtime, 1, runtime_size, output)) {
541         perror("Error saving runtime");
542         return 0;
543     }
544
545     padding = (os_vm_page_size - (runtime_size % os_vm_page_size)) & ~os_vm_page_size;
546     if (padding > 0) {
547         padbytes = successful_malloc(padding);
548         memset(padbytes, 0, padding);
549         if (padding != fwrite(padbytes, 1, padding, output)) {
550             perror("Error saving runtime");
551             free(padbytes);
552             return 0;
553         }
554         free(padbytes);
555     }
556
557     return 1;
558 }
559
560 FILE *
561 prepare_to_save(char *filename, boolean prepend_runtime, void **runtime_bytes,
562                 size_t *runtime_size)
563 {
564     FILE *file;
565     char *runtime_path;
566
567     if (prepend_runtime) {
568         runtime_path = os_get_runtime_executable_path(0);
569
570         if (runtime_path == NULL && saved_runtime_path == NULL) {
571             fprintf(stderr, "Unable to get default runtime path.\n");
572             return NULL;
573         }
574
575         if (runtime_path == NULL)
576             *runtime_bytes = load_runtime(saved_runtime_path, runtime_size);
577         else {
578             *runtime_bytes = load_runtime(runtime_path, runtime_size);
579             free(runtime_path);
580         }
581
582         if (*runtime_bytes == NULL)
583             return 0;
584     }
585
586     file = open_core_for_saving(filename);
587     if (file == NULL) {
588         free(*runtime_bytes);
589         perror(filename);
590         return NULL;
591     }
592
593     return file;
594 }
595
596 boolean
597 save(char *filename, lispobj init_function, boolean prepend_runtime,
598      boolean save_runtime_options, boolean compressed, int compression_level)
599 {
600     FILE *file;
601     void *runtime_bytes = NULL;
602     size_t runtime_size;
603
604     file = prepare_to_save(filename, prepend_runtime, &runtime_bytes, &runtime_size);
605     if (file == NULL)
606         return 1;
607
608     if (prepend_runtime)
609         save_runtime_to_filehandle(file, runtime_bytes, runtime_size);
610
611     return save_to_filehandle(file, filename, init_function, prepend_runtime,
612                               save_runtime_options,
613                               compressed ? compressed : COMPRESSION_LEVEL_NONE);
614 }