runtime: improve error reporting when given an exe with a wrong magic number

The error message includes both the expected and the encountered
magic numbers.
master
Sébastien Hinderer 2018-05-15 15:15:06 +02:00
parent 20c4303837
commit 2209bda71b
3 changed files with 22 additions and 7 deletions

View File

@ -46,15 +46,17 @@ struct section_descriptor {
uint32_t len; /* Length of data in bytes */
};
#define EXEC_MAGIC_LENGTH 12
/* Structure of the trailer. */
struct exec_trailer {
uint32_t num_sections; /* Number of sections */
char magic[12]; /* The magic number */
uint32_t num_sections; /* Number of sections */
char magic[EXEC_MAGIC_LENGTH]; /* The magic number */
struct section_descriptor * section; /* Not part of file */
};
#define TRAILER_SIZE (4+12)
#define TRAILER_SIZE (4+EXEC_MAGIC_LENGTH)
/* Magic number for this release */

View File

@ -37,7 +37,7 @@ CAMLextern value caml_startup_code_exn(
int pooling,
char_os **argv);
enum { FILE_NOT_FOUND = -1, BAD_BYTECODE = -2 };
enum { FILE_NOT_FOUND = -1, BAD_BYTECODE = -2, WRONG_MAGIC = -3 };
extern int caml_attempt_open(char_os **name, struct exec_trailer *trail,
int do_open_script);

View File

@ -66,6 +66,8 @@
#define SEEK_END 2
#endif
static char magicstr[EXEC_MAGIC_LENGTH+1];
/* Read the trailer of a bytecode file */
static void fixup_endianness_trailer(uint32_t * p)
@ -82,10 +84,13 @@ static int read_trailer(int fd, struct exec_trailer *trail)
if (read(fd, (char *) trail, TRAILER_SIZE) < TRAILER_SIZE)
return BAD_BYTECODE;
fixup_endianness_trailer(&trail->num_sections);
if (strncmp(trail->magic, EXEC_MAGIC, 12) == 0)
if (strncmp(trail->magic, EXEC_MAGIC, sizeof(trail->magic)) == 0)
return 0;
else
return BAD_BYTECODE;
else {
memcpy(magicstr, trail->magic, EXEC_MAGIC_LENGTH);
magicstr[EXEC_MAGIC_LENGTH] = 0;
return WRONG_MAGIC;
}
}
int caml_attempt_open(char_os **name, struct exec_trailer *trail,
@ -370,6 +375,14 @@ CAMLexport void caml_main(char_os **argv)
"the file '%s' is not a bytecode executable file",
caml_stat_strdup_of_os(exe_name));
break;
case WRONG_MAGIC:
caml_fatal_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) */