1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
|
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Xavier Leroy and Damien Doligez, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
#define CAML_INTERNALS
/* Start-up code */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "caml/config.h"
#ifdef HAS_UNISTD
#include <unistd.h>
#endif
#ifdef _WIN32
#include <io.h>
#include <process.h>
#endif
#include "caml/alloc.h"
#include "caml/backtrace.h"
#include "caml/callback.h"
#include "caml/codefrag.h"
#include "caml/custom.h"
#include "caml/debugger.h"
#include "caml/domain_state.h"
#include "caml/dynlink.h"
#include "caml/runtime_events.h"
#include "caml/exec.h"
#include "caml/fail.h"
#include "caml/fiber.h"
#include "caml/fix_code.h"
#include "caml/gc_ctrl.h"
#include "caml/instrtrace.h"
#include "caml/interp.h"
#include "caml/intext.h"
#include "caml/io.h"
#include "caml/memory.h"
#include "caml/minor_gc.h"
#include "caml/misc.h"
#include "caml/mlvalues.h"
#include "caml/osdeps.h"
#include "caml/prims.h"
#include "caml/printexc.h"
#include "caml/reverse.h"
#include "caml/signals.h"
#include "caml/sys.h"
#include "caml/startup.h"
#include "caml/startup_aux.h"
#include "caml/version.h"
#include "build_config.h"
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifndef SEEK_END
#define SEEK_END 2
#endif
static char magicstr[EXEC_MAGIC_LENGTH+1];
/* Print the specified error message followed by an end-of-line and exit */
static void error(char *msg, ...)
{
va_list ap;
va_start(ap, msg);
vfprintf (stderr, msg, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(127);
}
/* Read the trailer of a bytecode file */
static void fixup_endianness_trailer(uint32_t * p)
{
#ifndef ARCH_BIG_ENDIAN
Reverse_32(p, p);
#endif
}
int caml_read_trailer(int fd, struct exec_trailer *trail)
{
if (lseek(fd, (long) -TRAILER_SIZE, SEEK_END) == -1)
return BAD_BYTECODE;
if (read(fd, (char *) trail, TRAILER_SIZE) < TRAILER_SIZE)
return BAD_BYTECODE;
fixup_endianness_trailer(&trail->num_sections);
memcpy(magicstr, trail->magic, EXEC_MAGIC_LENGTH);
magicstr[EXEC_MAGIC_LENGTH] = 0;
if (caml_params->print_magic) {
printf("%s\n", magicstr);
exit(0);
}
return
(strncmp(trail->magic, EXEC_MAGIC, sizeof(trail->magic)) == 0)
? 0 : WRONG_MAGIC;
}
enum caml_byte_program_mode caml_byte_program_mode = STANDARD;
int caml_attempt_open(char_os **name, struct exec_trailer *trail,
int do_open_script)
{
char_os * truename;
int fd;
int err;
char buf [2], * u8;
truename = caml_search_exe_in_path(*name);
u8 = caml_stat_strdup_of_os(truename);
caml_gc_message(0x100, "Opening bytecode executable %s\n", u8);
caml_stat_free(u8);
fd = open_os(truename, O_RDONLY | O_BINARY);
if (fd == -1) {
caml_stat_free(truename);
caml_gc_message(0x100, "Cannot open file\n");
if (errno == EMFILE)
return NO_FDS;
else
return FILE_NOT_FOUND;
}
if (!do_open_script) {
err = read (fd, buf, 2);
if (err < 2 || (buf [0] == '#' && buf [1] == '!')) {
close(fd);
caml_stat_free(truename);
caml_gc_message(0x100, "Rejected #! script\n");
return BAD_BYTECODE;
}
}
err = caml_read_trailer(fd, trail);
if (err != 0) {
close(fd);
caml_stat_free(truename);
caml_gc_message(0x100, "Not a bytecode executable\n");
return err;
}
*name = truename;
return fd;
}
/* Read the section descriptors */
void caml_read_section_descriptors(int fd, struct exec_trailer *trail)
{
int toc_size;
toc_size = trail->num_sections * 8;
trail->section = caml_stat_alloc(toc_size);
lseek(fd, - (long) (TRAILER_SIZE + toc_size), SEEK_END);
if (read(fd, (char *) trail->section, toc_size) != toc_size)
caml_fatal_error("cannot read section table");
/* Fixup endianness of lengths */
for (int i = 0; i < trail->num_sections; i++)
fixup_endianness_trailer(&(trail->section[i].len));
}
/* Position fd at the beginning of the section having the given name.
Return the length of the section data in bytes, or -1 if no section
found with that name. */
int32_t caml_seek_optional_section(int fd, struct exec_trailer *trail,
char *name)
{
long ofs;
ofs = TRAILER_SIZE + trail->num_sections * 8;
for (int i = trail->num_sections - 1; i >= 0; i--) {
ofs += trail->section[i].len;
if (strncmp(trail->section[i].name, name, 4) == 0) {
lseek(fd, -ofs, SEEK_END);
return trail->section[i].len;
}
}
return -1;
}
/* Position fd at the beginning of the section having the given name.
Return the length of the section data in bytes. */
int32_t caml_seek_section(int fd, struct exec_trailer *trail, char *name)
{
int32_t len = caml_seek_optional_section(fd, trail, name);
if (len == -1)
caml_fatal_error("section `%s' is missing", name);
return len;
}
/* Read and return the contents of the section having the given name.
Add a terminating 0. Return NULL if no such section. */
static char * read_section(int fd, struct exec_trailer *trail, char *name)
{
int32_t len;
char * data;
len = caml_seek_optional_section(fd, trail, name);
if (len == -1) return NULL;
data = caml_stat_alloc(len + 1);
if (read(fd, data, len) != len)
caml_fatal_error("error reading section %s", name);
data[len] = 0;
return data;
}
#ifdef _WIN32
static char_os * read_section_to_os(int fd, struct exec_trailer *trail,
char *name)
{
int32_t len, wlen;
char * data;
wchar_t * wdata;
len = caml_seek_optional_section(fd, trail, name);
if (len == -1) return NULL;
data = caml_stat_alloc(len + 1);
if (read(fd, data, len) != len)
caml_fatal_error("error reading section %s", name);
data[len] = 0;
wlen = caml_win32_multi_byte_to_wide_char(data, len, NULL, 0);
wdata = caml_stat_alloc((wlen + 1)*sizeof(wchar_t));
caml_win32_multi_byte_to_wide_char(data, len, wdata, wlen);
wdata[wlen] = 0;
caml_stat_free(data);
return wdata;
}
#else
#define read_section_to_os read_section
#endif
/* Invocation of ocamlrun: 4 cases.
1. runtime + bytecode
user types: ocamlrun [options] bytecode args...
arguments: ocamlrun [options] bytecode args...
2. bytecode script
user types: bytecode args...
2a (kernel 1) arguments: ocamlrun ./bytecode args...
2b (kernel 2) arguments: bytecode bytecode args...
3. concatenated runtime and bytecode
user types: composite args...
arguments: composite args...
Algorithm:
1- If argument 0 is a valid byte-code file that does not start with #!,
then we are in case 3 and we pass the same command line to the
OCaml program.
2- In all other cases, we parse the command line as:
(whatever) [options] bytecode args...
and we strip "(whatever) [options]" from the command line.
*/
static void do_print_help(void)
{
printf("%s\n",
"Usage: ocamlrun [<options>] [--] <executable> [<command-line>]\n"
"Options are:\n"
" -b Set runtime parameter b (detailed exception backtraces)\n"
" -config Print configuration values and exit\n"
" -events Trace debug events in bytecode interpreter (ignored \n"
" if not ocamlrund)\n"
" -I <dir> Add <dir> to the list of DLL search directories\n"
" -m Print the magic number of <executable> and exit\n"
" -M Print the magic number expected by this runtime and exit\n"
" -p Print the names of the primitives known to this runtime\n"
" -t Trace the execution of the bytecode interpreter (specify multiple\n"
" times to increase verbosity)\n"
" -v Set runtime parameter v=61 (GC event information)\n"
" -version Print version string and exit\n"
" -vnum Print short version number and exit\n"
" -help Display this list of options\n"
" --help Display this list of options");
}
/* Parse options on the command line */
static int parse_command_line(char_os **argv)
{
int i, len, parsed;
/* cast to make caml_params mutable; this assumes we are only called
by one thread at startup */
struct caml_params* params = (struct caml_params*)caml_params;
for(i = 1; argv[i] != NULL && argv[i][0] == '-'; i++) {
len = strlen_os(argv[i]);
parsed = 1;
if (len == 2) {
/* Single-letter options, e.g. -v */
switch(argv[i][1]) {
case '-':
return i + 1;
break;
case 't':
params->trace_level += 1; /* ignored unless DEBUG mode */
break;
case 'v':
atomic_store_relaxed(&caml_verb_gc, 0x001+0x004+0x008+0x010+0x020);
break;
case 'p':
for (int j = 0; caml_names_of_builtin_cprim[j] != NULL; j++)
printf("%s\n", caml_names_of_builtin_cprim[j]);
exit(0);
break;
case 'b':
params->backtrace_enabled = 1;
break;
case 'I':
if (argv[i + 1] != NULL) {
caml_ext_table_add(&caml_shared_libs_path, argv[i + 1]);
i++;
} else {
error("option '-I' needs an argument.");
}
break;
case 'm':
params->print_magic = 1;
break;
case 'M':
printf("%s\n", EXEC_MAGIC);
exit(0);
break;
default:
parsed = 0;
}
} else {
/* Named options, e.g. -version */
if (!strcmp_os(argv[i], T("-version"))) {
printf("%s\n", "The OCaml runtime, version " OCAML_VERSION_STRING);
exit(0);
} else if (!strcmp_os(argv[i], T("-vnum"))) {
printf("%s\n", OCAML_VERSION_STRING);
exit(0);
} else if (!strcmp_os(argv[i], T("-events"))) {
params->event_trace = 1; /* Ignored unless DEBUG mode */
} else if (!strcmp_os(argv[i], T("-help")) ||
!strcmp_os(argv[i], T("--help"))) {
do_print_help();
exit(0);
} else if (!strcmp_os(argv[i], T("-config"))) {
params->print_config = 1;
} else {
parsed = 0;
}
}
if (!parsed)
error("unknown option %s", caml_stat_strdup_of_os(argv[i]));
}
return i;
}
/* Print the configuration of the runtime to stdout; memory allocated is not
freed, since the runtime will terminate after calling this. */
static void do_print_config(void)
{
char_os * dir;
/* Print the runtime configuration */
printf("version: %s\n", OCAML_VERSION_STRING);
printf("standard_library_default: %s\n",
caml_stat_strdup_of_os(OCAML_STDLIB_DIR));
printf("standard_library: %s\n",
caml_stat_strdup_of_os(caml_get_stdlib_location()));
printf("int_size: %d\n", 8 * (int)sizeof(value));
printf("word_size: %d\n", 8 * (int)sizeof(value) - 1);
printf("os_type: %s\n", OCAML_OS_TYPE);
printf("host: %s\n", HOST);
printf("flat_float_array: %s\n",
#ifdef FLAT_FLOAT_ARRAY
"true");
#else
"false");
#endif
printf("supports_afl: %s\n",
#ifdef HAS_SYS_SHM_H
"true");
#else
"false");
#endif
printf("windows_unicode: %s\n",
#if defined(WINDOWS_UNICODE) && WINDOWS_UNICODE != 0
"true");
#else
"false");
#endif
printf("supports_shared_libraries: %s\n",
#ifdef SUPPORT_DYNAMIC_LINKING
"true");
#else
"false");
#endif
printf("no_naked_pointers: true\n");
printf("reserved header bits: %d\n", HEADER_RESERVED_BITS);
printf("exec_magic_number: %s\n", EXEC_MAGIC);
/* Parse ld.conf and print the effective search path */
puts("shared_libs_path:");
caml_parse_ld_conf();
for (int i = 0; i < caml_shared_libs_path.size; i++) {
dir = caml_shared_libs_path.contents[i];
if (dir[0] == 0)
#ifdef _WIN32
/* See caml_search_in_path in win32.c */
continue;
#else
dir = ".";
#endif
printf(" %s\n", caml_stat_strdup_of_os(dir));
}
}
#ifdef _WIN32
extern void caml_signal_thread(void * lpParam);
#endif
#ifdef _MSC_VER
/* PR 4887: avoid crash box of windows runtime on some system calls */
extern void caml_install_invalid_parameter_handler(void);
#endif
/* Main entry point when loading code from a file */
CAMLexport void caml_main(char_os **argv)
{
int fd, pos;
struct exec_trailer trail;
struct channel * chan;
value res;
char * req_prims;
char_os * shared_lib_path, * shared_libs;
char_os * exe_name, * proc_self_exe;
/* Determine options */
caml_parse_ocamlrunparam();
if (!caml_startup_aux(/* pooling */ caml_params->cleanup_on_exit))
return;
caml_init_codefrag();
caml_init_locale();
#ifdef _MSC_VER
caml_install_invalid_parameter_handler();
#endif
caml_init_custom_operations();
caml_init_os_params();
caml_ext_table_init(&caml_shared_libs_path, 8);
/* Determine position of bytecode file */
pos = 0;
/* First, try argv[0] (when ocamlrun is called by a bytecode program) */
exe_name = argv[0];
fd = caml_attempt_open(&exe_name, &trail, 0);
/* Little grasshopper wonders why we do that at all, since
"The current executable is ocamlrun itself, it's never a bytecode
program". Little grasshopper "ocamlc -custom" in mind should keep.
With -custom, we have an executable that is ocamlrun itself
concatenated with the bytecode. So, if the attempt with argv[0]
failed, it is worth trying again with executable_name. */
if (fd < 0 && (proc_self_exe = caml_executable_name()) != NULL) {
exe_name = proc_self_exe;
fd = caml_attempt_open(&exe_name, &trail, 0);
}
if (fd < 0) {
pos = parse_command_line(argv);
if (caml_params->print_config) {
do_print_config();
exit(0);
}
if (argv[pos] == 0) {
error("no bytecode file specified");
}
exe_name = argv[pos];
fd = caml_attempt_open(&exe_name, &trail, 1);
switch(fd) {
case FILE_NOT_FOUND:
error("cannot find file '%s'",
caml_stat_strdup_of_os(argv[pos]));
break;
case BAD_BYTECODE:
error(
"the file '%s' is not a bytecode executable file",
caml_stat_strdup_of_os(exe_name));
break;
case WRONG_MAGIC:
error(
"the file '%s' has not the right magic number: "\
"expected %s, got %s",
caml_stat_strdup_of_os(exe_name),
EXEC_MAGIC,
magicstr);
break;
}
}
/* Read the table of contents (section descriptors) */
caml_read_section_descriptors(fd, &trail);
/* Initialize the abstract machine */
caml_init_gc ();
/* bring up runtime_events after we've initialised the gc */
CAML_RUNTIME_EVENTS_INIT();
Caml_state->external_raise = NULL;
/* Setup signal handling */
caml_init_signals();
/* Initialize the interpreter */
caml_interprete(NULL, 0);
/* Initialize the debugger, if needed */
caml_debugger_init();
/* Load the code */
caml_code_size = caml_seek_section(fd, &trail, "CODE");
caml_load_code(fd, caml_code_size);
caml_init_debug_info();
/* Build the table of primitives */
shared_lib_path = read_section_to_os(fd, &trail, "DLPT");
shared_libs = read_section_to_os(fd, &trail, "DLLS");
req_prims = read_section(fd, &trail, "PRIM");
if (req_prims == NULL) caml_fatal_error("no PRIM section");
caml_build_primitive_table(shared_lib_path, shared_libs, req_prims);
caml_stat_free(shared_lib_path);
caml_stat_free(shared_libs);
caml_stat_free(req_prims);
/* Load the globals */
caml_seek_section(fd, &trail, "DATA");
chan = caml_open_descriptor_in(fd);
value global_data = caml_input_val(chan);
caml_modify_generational_global_root(&caml_global_data, global_data);
caml_close_channel(chan); /* this also closes fd */
caml_stat_free(trail.section);
/* Initialize system libraries */
caml_sys_init(exe_name, argv + pos);
/* Load debugging info, if b>=2 */
caml_load_main_debug_info();
/* ensure all globals are in major heap */
caml_minor_collection();
#ifdef _WIN32
/* Start a thread to handle signals */
if (caml_secure_getenv(T("CAMLSIGPIPE")))
_beginthread(caml_signal_thread, 4096, NULL);
#endif
/* Execute the program */
caml_debugger(PROGRAM_START, Val_unit);
res = caml_interprete(caml_start_code, caml_code_size);
if (Is_exception_result(res)) {
value exn = Extract_exception(res);
if (caml_debugger_in_use) {
Caml_state->current_stack->sp = &exn; /* The debugger needs the
exception value.*/
caml_debugger(UNCAUGHT_EXC, Val_unit);
}
caml_fatal_uncaught_exception(exn);
}
caml_terminate_signals();
}
/* Main entry point when code is linked in as initialized data */
CAMLexport value caml_startup_code_exn(
code_t code, asize_t code_size,
char *data, asize_t data_size,
char *section_table, asize_t section_table_size,
int pooling,
char_os **argv)
{
char_os * exe_name;
value res;
/* Determine options */
caml_parse_ocamlrunparam();
if (caml_params->cleanup_on_exit)
pooling = 1;
if (!caml_startup_aux(pooling))
return Val_unit;
caml_init_codefrag();
caml_init_locale();
#ifdef _MSC_VER
caml_install_invalid_parameter_handler();
#endif
caml_init_custom_operations();
caml_init_os_params();
caml_ext_table_init(&caml_shared_libs_path, 8);
/* Initialize the abstract machine */
caml_init_gc ();
/* runtime_events has to be brought up after the gc */
CAML_RUNTIME_EVENTS_INIT();
exe_name = caml_executable_name();
if (exe_name == NULL) exe_name = caml_search_exe_in_path(argv[0]);
Caml_state->external_raise = NULL;
/* Setup signal handling */
caml_init_signals();
/* Initialize the interpreter */
caml_interprete(NULL, 0);
/* Initialize the debugger, if needed */
caml_debugger_init();
/* Load the code */
caml_start_code = code;
caml_code_size = code_size;
caml_init_code_fragments();
caml_init_debug_info();
#ifdef THREADED_CODE
caml_thread_code(caml_start_code, code_size);
#endif
/* Use the builtin table of primitives */
caml_build_primitive_table_builtin();
/* Load the globals */
caml_modify_generational_global_root
(&caml_global_data, caml_input_value_from_block(data, data_size));
/* Initialize system libraries */
caml_sys_init(exe_name, argv);
/* Load debugging info, if b>=2 */
caml_load_main_debug_info();
/* ensure all globals are in major heap */
caml_minor_collection();
/* Record the sections (for caml_dynlink_get_bytecode_sections) */
caml_init_section_table(section_table, section_table_size);
/* Execute the program */
caml_debugger(PROGRAM_START, Val_unit);
res = caml_interprete(caml_start_code, caml_code_size);
caml_terminate_signals();
return res;
}
CAMLexport void caml_startup_code(
code_t code, asize_t code_size,
char *data, asize_t data_size,
char *section_table, asize_t section_table_size,
int pooling,
char_os **argv)
{
value res;
res = caml_startup_code_exn(code, code_size, data, data_size,
section_table, section_table_size,
pooling, argv);
if (Is_exception_result(res)) {
value exn = Extract_exception(res);
if (caml_debugger_in_use) {
Caml_state->current_stack->sp = &exn; /* The debugger needs the
exception value.*/
caml_debugger(UNCAUGHT_EXC, Val_unit);
}
caml_fatal_uncaught_exception(exn);
}
}
|