Browse Source

Include the ABI version in the file format, and refuse to execute

binary files with ABI mismatches
master
Gavan Fantom 18 years ago
parent
commit
ec2b71d545
  1. 11
      src/include/code.h
  2. 22
      src/lsc/codegen.c
  3. 6
      src/lsi/abi.h
  4. 5
      src/lsi/vm.c

11
src/include/code.h

@ -132,10 +132,15 @@ typedef int stkentry;
* Structure of the file header:
*
* 2 "LC" (magic)
* 2 0 (version)
* 2 00:01 (version)
* 4 ptr (pointer to function table)
* 4 abiversion1
* 4 abiversion2
* 4 abiversion3
* 4 abiversion4
* 4 abiversion5
*
* ptr-4 code
* ptr-28 code
*
* 1 or more of:
* 4 length of block, or 0 to terminate
@ -150,5 +155,5 @@ typedef int stkentry;
#define MAGIC1 'L'
#define MAGIC2 'C'
#define VERSION1 0
#define VERSION2 0
#define VERSION2 1

22
src/lsc/codegen.c

@ -267,9 +267,11 @@ int lookup_function(ast *node, struct fn_desc *fn)
return 1;
}
int lookup_constant(ast *node, int *constant)
#define lookup_constant(node, constant) \
lookup_constant_string(node->info.string, constant)
int lookup_constant_string(char *constantname, int *constant)
{
char *constantname = node->info.string;
struct hashentry *hashptr;
if (constant_count < 0)
@ -1185,6 +1187,7 @@ void output_code(void)
int morepasses = 1;
int passno = 0;
int pc;
int abiver;
while (morepasses) {
ptr = instr_head;
@ -1259,9 +1262,22 @@ void output_code(void)
output_byte(VERSION1);
output_byte(VERSION2);
#define ABIVERSION(x) do { \
abiver = 0; \
if (!lookup_constant_string(x, &abiver)) \
printf("%s not defined\n", x); \
output_int(abiver); \
} while (0)
pc = (pc + 3) & ~3; /* Align table */
output_int(pc+8); /* Pointer to function table */
output_int(pc+28); /* Pointer to function table */
ABIVERSION("__abiversion1");
ABIVERSION("__abiversion2");
ABIVERSION("__abiversion3");
ABIVERSION("__abiversion4");
ABIVERSION("__abiversion5");
ptr = instr_head;
pc = 0;

6
src/lsi/abi.h

@ -2,6 +2,12 @@
typedef int (*vm_intfn)(void);
extern const long vm_abiversion1;
extern const long vm_abiversion2;
extern const long vm_abiversion3;
extern const long vm_abiversion4;
extern const long vm_abiversion5;
/*
* We must include here all prototypes for functions which can be
* included in the ABI function table

5
src/lsi/vm.c

@ -609,6 +609,11 @@ void vm_init_functions(void)
assert(vm_codearea[2] == VERSION1);
assert(vm_codearea[3] == VERSION2);
assert(GETINT(vm_codearea, 8) == vm_abiversion1);
assert(GETINT(vm_codearea, 12) == vm_abiversion2);
assert(GETINT(vm_codearea, 16) == vm_abiversion3);
assert(GETINT(vm_codearea, 20) == vm_abiversion4);
assert(GETINT(vm_codearea, 24) == vm_abiversion5);
/* Now, get the function table pointer */
t = GETINT(vm_codearea, 4);

Loading…
Cancel
Save