-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
64 lines (59 loc) · 1.65 KB
/
main.c
File metadata and controls
64 lines (59 loc) · 1.65 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "vma.h"
#define DIE(assertion, call_description) \
do { \
if (assertion) { \
fprintf(stderr, "(%s, %d): ", \
__FILE__, __LINE__); \
perror(call_description); \
exit(errno); \
} \
} while (0)
#define MAX_STRING_SIZE 64
int main(void)
{
arena_t *arena = NULL;
uint64_t size, address;
int8_t *data = NULL;
int8_t *permission[MAX_STRING_SIZE];
while (1) {
char command[MAX_STRING_SIZE];
scanf("%s", command);
if (strcmp(command, "ALLOC_ARENA") == 0) {
scanf("%ld", &size);
arena = alloc_arena(size);
} else if (strcmp(command, "DEALLOC_ARENA") == 0) {
dealloc_arena(arena);
break;
} else if (strcmp(command, "ALLOC_BLOCK") == 0) {
scanf("%ld%ld", &address, &size);
alloc_block(arena, address, size);
} else if (strcmp(command, "FREE_BLOCK") == 0) {
scanf("%ld", &address);
free_block(arena, address);
} else if (strcmp(command, "READ") == 0) {
scanf("%ld%ld", &address, &size);
read(arena, address, size);
} else if (strcmp(command, "WRITE") == 0) {
scanf("%ld%ld", &address, &size);
scanf("%*c");
data = (int8_t *)calloc(sizeof(int8_t), size);
DIE(!data, "data malloc");
fread(data, sizeof(int8_t), size, stdin);
write(arena, address, size, data);
free(data);
} else if (strcmp(command, "PMAP") == 0) {
pmap(arena);
} else if (strcmp(command, "MPROTECT") == 0) {
scanf("%ld", &address);
fgets((char *)permission, MAX_STRING_SIZE, stdin);
mprotect(arena, address, permission);
} else {
printf("Invalid command. Please try again.\n");
}
}
return 0;
}