-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.c
82 lines (75 loc) · 1.94 KB
/
runtime.c
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
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#define BOOL_F 0x2F
#define BOOL_T 0x6F
#define FIXNUM_MASK 0x03
#define FIXNUM_TAG 0x00
#define FIXNUM_SHIFT 2
#define CHAR_MASK 0x0F
#define CHAR_SHIFT 8
#define NIL 0x3F
extern int scheme_entry();
typedef unsigned int ptr;
static void print_ptr(ptr x)
{
if((x & FIXNUM_MASK) == FIXNUM_TAG)
{
printf("%d", ((int)x) >> FIXNUM_SHIFT);
}
else if(x == BOOL_T)
{
printf("#t");
}
else if(x == BOOL_F)
{
printf("#f");
}
else if ((x & 0xFF) == CHAR_MASK)
{
printf("#\\%c", ((int)x) >> CHAR_SHIFT);
}
else if (x == NIL)
{
printf("()");
}
else
{
printf("#<unknown 0x%08x>", x);
}
printf("\n");
}
static char* allocate_protected_space(int size) {
int page = getpagesize();
int status;
int aligned_size = ((size + page - 1) / page) * page;
char* p = mmap(0, aligned_size + 2 * page,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE,
0, 0);
if (p == MAP_FAILED) { perror("map"); exit(1); }
status = mprotect(p, page, PROT_NONE);
if (status != 0) { perror("mprotect"); exit(status); }
status = mprotect(p + page + aligned_size, page, PROT_NONE);
if (status != 0) { perror("mprotect"); exit(status); }
return (p + page);
}
static void deallocate_protected_space(char* p, int size) {
int page = getpagesize();
int status;
int aligned_size = ((size + page - 1) / page) * page;
status = munmap(p - page, aligned_size + 2 * page);
if (status != 0) { perror("munmap"); exit(status); }
}
int main(int argc, char **argv)
{
int stack_size = (16 * 4096);
char* stack_top = allocate_protected_space(stack_size);
char* stack_base = stack_top + stack_size;
print_ptr(scheme_entry(stack_base));
deallocate_protected_space(stack_top, stack_size);
return 0;
}