-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvma.c
361 lines (308 loc) · 8.56 KB
/
vma.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
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
/*
* Copyright: Sima Alexandru (312CA) 2023
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "io.h"
#include "list.h"
#include "mem_alloc.h"
#include "mem_io.h"
#include "mem_prot.h"
#include "utils.h"
#include "vma.h"
/*
* Verifica daca blocul `next` incepe inainte sa se termine blocul `prev`.
*/
static inline int check_overlap(block_t *prev, block_t *next);
/*
* Daca blocurile `prev` si `next` sunt adiacente, se
* muta miniblocurile in `prev` si se elibereaza `next`.
*
* Returneaza blocul in care se afla nodurile din `next` dupa operatie.
*/
static block_t *merge_adjacent_blocks(block_t *prev, block_t *next);
/*
* Returneaza ultimul nod cu adresa de start
* mai mica decat `address` (daca exista).
*/
static list_t *get_prev_block(list_t *list, const uint64_t address);
arena_t *alloc_arena(const uint64_t size)
{
arena_t *arena = malloc(sizeof(arena_t));
if (!arena)
return NULL;
arena->arena_size = size;
arena->alloc_list = NULL;
arena->has_error = 0;
return arena;
}
void dealloc_arena(arena_t *arena)
{
if (!arena)
return;
clear_list(arena->alloc_list, free_block_data);
free(arena);
}
void alloc_block(arena_t *arena, const uint64_t address, const uint64_t size)
{
block_t *new_block = init_block(address, size);
if (!new_block)
return;
if (!arena->alloc_list) {
list_t *new = encapsulate(new_block);
if (!new) {
arena->has_error = 1;
free(new_block);
return;
}
new->next = arena->alloc_list;
arena->alloc_list = new;
return;
}
list_t *const prev = get_prev_block(arena->alloc_list, address);
list_t *const next = prev ? prev->next : arena->alloc_list;
block_t *prev_block = prev ? prev->data : NULL;
block_t *next_block = next ? next->data : NULL;
// Blocul se suprapune cu un altul.
if (check_overlap(prev_block, new_block) ||
check_overlap(new_block, next_block)) {
print_err(INVALID_ALLOC_BLOCK);
free_block_data(new_block);
return;
}
prev_block = merge_adjacent_blocks(prev_block, new_block);
next_block = merge_adjacent_blocks(prev_block, next_block);
// Blocul s-a unit cu urmatorul,
// se elibereaza nodul urmator.
if (prev_block == next_block) {
if (next) {
remove_item(&arena->alloc_list, next);
free(next);
}
}
// Blocul nu s-a unit cu anteriorul,
// se aloca un bloc nou dupa acesta.
if (prev_block == new_block) {
list_t *new = encapsulate(new_block);
if (!new) {
arena->has_error = 1;
return;
}
insert_after(&arena->alloc_list, prev, new);
}
}
void free_block(arena_t *arena, const uint64_t address)
{
list_t *block_list = access_block(arena, address);
if (!block_list) {
print_err(INVALID_ADDRESS_FREE);
return;
}
block_t *block = block_list->data;
list_t *iter = block->miniblock_list;
while (iter) {
miniblock_t *miniblock = iter->data;
if (address != miniblock->start_address) {
iter = iter->next;
continue;
}
remove_item(&block->miniblock_list, iter);
// Blocul este gol: se sterge.
if (!block->miniblock_list) {
remove_item(&arena->alloc_list, block_list);
free_miniblock_data(miniblock);
free_block_data(block);
free(block_list);
free(iter);
return;
}
// Miniblocul are vecini in ambele parti, asa ca
// miniblocurile incepand cu urmatorul sunt mutate in alt bloc.
if (iter->prev && iter->next) {
block_t *new_block = malloc(sizeof(block_t));
if (!new_block) {
arena->has_error = 1;
return;
}
miniblock_t *next = iter->next->data;
uint64_t block_end_address = block->start_address + block->size;
// Vechiul bloc se va termina inaintea miniblocului sters,
// asa ca lungimea lui va fi distanta adreselor lor.
block->size = miniblock->start_address - block->start_address;
new_block->miniblock_list = iter->next;
new_block->start_address = next->start_address;
new_block->size = block_end_address - new_block->start_address;
iter->next->prev = NULL;
iter->prev->next = NULL;
list_t *new_node = encapsulate(new_block);
if (!new_block) {
free(new_block);
arena->has_error = 1;
return;
}
insert_after(&arena->alloc_list, block_list, new_node);
} else {
// Nodul este la marginea unui bloc, asa ca doar se scade
// dimensiunea acestuia si, eventual, se modifica capul listei de
// noduri.
if (!iter->prev) {
miniblock_t *next = iter->next->data;
block->start_address = next->start_address;
}
block->size -= miniblock->size;
}
free_miniblock_data(miniblock);
free(iter);
return;
}
print_err(INVALID_ADDRESS_FREE);
}
void read(arena_t *arena, uint64_t address, uint64_t size)
{
list_t *iter = access_miniblock(arena, address);
if (!iter) {
print_err(INVALID_ADDRESS_READ);
return;
}
char *buffer = malloc(sizeof(char) * size);
if (!buffer) {
arena->has_error = 1;
return;
}
miniblock_t *miniblock = iter->data;
iter = iter->next;
if (!check_perm(miniblock, PROT_READ)) {
print_err(INVALID_PERMISSIONS_READ);
free(buffer);
return;
}
uint64_t offset = address - miniblock->start_address;
uint64_t batch = min(size, miniblock->size - offset);
uint64_t bytes_read = batch;
memcpy(buffer, miniblock->rw_buffer + offset, batch);
while (iter && bytes_read != size) {
miniblock = iter->data;
iter = iter->next;
if (!check_perm(miniblock, PROT_READ)) {
print_err(INVALID_PERMISSIONS_READ);
free(buffer);
return;
}
batch = min(size - bytes_read, miniblock->size);
memcpy(buffer + bytes_read, miniblock->rw_buffer, batch);
bytes_read += batch;
}
if (bytes_read != size)
printf("Warning: size was bigger than the block size. Reading %lu "
"characters.\n",
bytes_read);
uint64_t out_len = max_len(buffer, bytes_read);
fwrite(buffer, sizeof(char), out_len, stdout);
free(buffer);
puts("");
}
void write(arena_t *arena, const uint64_t address, const uint64_t size,
char *data)
{
list_t *iter = access_miniblock(arena, address);
if (!iter) {
print_err(INVALID_ADDRESS_WRITE);
return;
}
miniblock_t *miniblock = iter->data;
iter = iter->next;
if (!check_perm(miniblock, PROT_WRITE)) {
print_err(INVALID_PERMISSIONS_WRITE);
return;
}
uint64_t offset = address - miniblock->start_address;
uint64_t batch = min(size, miniblock->size - offset);
uint64_t bytes_written = batch;
memcpy(miniblock->rw_buffer + offset, data, batch);
while (iter && bytes_written != size) {
if (!check_perm(miniblock, PROT_WRITE)) {
print_err(INVALID_PERMISSIONS_WRITE);
return;
}
miniblock = iter->data;
iter = iter->next;
batch = min(size - bytes_written, miniblock->size);
memcpy(miniblock->rw_buffer, data + bytes_written, batch);
bytes_written += batch;
}
if (bytes_written != size)
printf("Warning: size was bigger than the block size. Writing %lu "
"characters.\n",
bytes_written);
}
void pmap(const arena_t *arena)
{
uint64_t no_blocks = 0;
uint64_t no_miniblocks = 0;
uint64_t free_memory = arena->arena_size;
list_t *block_iter = arena->alloc_list;
while (block_iter) {
block_t *block = block_iter->data;
free_memory -= block->size;
list_t *miniblock_iter = block->miniblock_list;
while (miniblock_iter) {
++no_miniblocks;
miniblock_iter = miniblock_iter->next;
}
++no_blocks;
block_iter = block_iter->next;
}
printf("Total memory: 0x%lX bytes\n", arena->arena_size);
printf("Free memory: 0x%lX bytes\n", free_memory);
printf("Number of allocated blocks: %lu\n", no_blocks);
printf("Number of allocated miniblocks: %lu\n", no_miniblocks);
uint64_t block_index = 1;
apply_func(arena->alloc_list, print_block, &block_index);
}
void mprotect(arena_t *arena, uint64_t address, char *permission)
{
uint8_t perm = parse_perm_str(permission);
list_t *miniblock_node = access_miniblock_start(arena, address);
if (!miniblock_node) {
print_err(INVALID_ADDRESS_MPROTECT);
return;
}
miniblock_t *miniblock = miniblock_node->data;
miniblock->perm = perm;
}
static inline int check_overlap(block_t *prev, block_t *next)
{
if (!prev || !next)
return 0;
return next->start_address < prev->start_address + prev->size;
}
static block_t *merge_adjacent_blocks(block_t *prev, block_t *next)
{
if (!prev)
return next;
if (!next)
return prev;
if (next->start_address == prev->start_address + prev->size) {
merge_lists(prev->miniblock_list, next->miniblock_list);
prev->size += next->size;
free(next);
return prev;
}
return next;
}
static list_t *get_prev_block(list_t *list, const uint64_t address)
{
block_t *data = list->data;
if (address < data->start_address)
return NULL;
list_t *next;
while ((next = list->next)) {
data = next->data;
if (address < data->start_address)
return list;
list = next;
}
return list;
}