-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
298 lines (268 loc) · 8.61 KB
/
Copy pathmain.cpp
File metadata and controls
298 lines (268 loc) · 8.61 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
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
#include <string.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <cstdio>
#include <cinttypes>
#include "Heap.h"
#include "Colors.h"
#include <unistd.h>
#include <array>
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
/*
* Input heap size and program will alloc it
* then add the possibility to free random heap by indexes
*
* */
void clear(){
system("clear");
//printf("GDB");
}
void help(){
fprintf(stdout, "\n------------");
fprintf(stdout, "\n1. Allocate a new heap chunk");
fprintf(stdout, "\n2. Free an heap chunk");
fprintf(stdout, "\n3. Print chunk info");
fprintf(stdout, "\n4. Print heap layout");
fprintf(stdout, "\n------------\n");
}
void help_game(){
fprintf(stdout, "[-------------------");
fprintf(stdout, "\nCommands:");
fprintf(stdout, "\nmalloc <size>");
fprintf(stdout, "\nfree <chunk_number>");
fprintf(stdout, "\nshow <chunk_number>");
fprintf(stdout, "\n-------------------]\n");
}
int choose_index(uint limit){
//choose the index and make sure to not chose a non existing one
uint result = 9999;
while(result >= limit){
std::cout << "Index > ";
std::cin >> result;
}
return result;
}
void press(){
// Just continue the execution
std::cout << std::endl << " Press Any Key to continue .." << std::endl;
getchar();
}
bool is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
int manual_loop(){
// Alloc something and put something in it
int32_t heap_size = 0;
Heap heap_array[100];
uint64_t * heap_ptr[100];
int i = 0; // heap index
int command;
int index; // used to reference a heap chunk
// TODO: Maybe integer over here btw
clear();
void* temp_malloc; //used as temporary pointer for malloc
bool reused=false;
while(true){
// Infinite loop
help();
fprintf(stdout, "\n[?] Command > ");
scanf("%d", &command);
switch(command){
case 1:
clear();
fprintf(stdout, "\n[?] Heap size to allocate: ");
scanf("%d", &heap_size);
// I need to perform the malloc here.
// Because i need to update freed chunks
temp_malloc = malloc(heap_size);
// and now check that the chunk has not been already inserted
reused=false;
for(int j=0; j < i; j++){
if(heap_array[j].getMallocPointer() == temp_malloc){
heap_array[j].reallocated(heap_array, i);
reused=true;
}
}
if (!reused){
heap_array[i] = Heap(heap_size, temp_malloc, heap_array, i);
i++;
}
break;
case 2:
clear();
index = choose_index(i);
heap_array[index].freeChunk();
break;
case 3:
clear();
index = choose_index(i);
heap_array[index].showInfo(true);
break;
case 4:
// print heap layout starting from the first allocated to the last one
// print also the first and the last allocated heap address
clear();
printf("\n[*] First allocated chunk: 0x%x", heap_array[0].getMallocPointer());
printf("\n[*] Last allocated chunk: 0x%x", heap_array[i-1].getMallocPointer());
for(int j=0; j < i; j++){
heap_array[j].showInfo(false);
}
default:
fprintf(stdout, "\nCommand not found");
break;
}
}
}
int game_loop(){
setbuf(stdout, NULL);
clear();
std::cout << "Input your chunks. For example, if you want to play with 3 chunks of size 10,20 and 30 the just enter: 10 20 30" << std::endl;
std::cout << "Tell me chunk sizes > ";
std::string input_chunks;
std::cin.ignore();
std::getline (std::cin, input_chunks);
char * spl_chunks;
uint64_t chunks [100];
int i_heap; //heap allocated. TODO: change in this in other mode
Heap heap_array[100];
void* temp_malloc; //used as temporary pointer for malloc
spl_chunks = strtok((char *)input_chunks.c_str()," " );
int i=0;
while (spl_chunks != NULL)
{
if(i < (sizeof(chunks)/sizeof(uint64_t)))
chunks[i] = atoi(spl_chunks);
else
printf("\nLimit chunks reached (%x). Not putting %d",sizeof(chunks)/sizeof(uint64_t), atoi(spl_chunks));
//printf ("%s)\n",spl_chunks);
spl_chunks = strtok(NULL, " ");
i++;
}
uint64_t chunks_len = i;
std::cout << std::endl << "These will be your chunks: " << std::endl;
for(int i=0; i < chunks_len; i++){
printf("\n[%d] %d", i, chunks[i]);
}
press();
// now alloc what requested
for(int i=0; i < chunks_len; i++){
printf("\n[*] Allocating %d for chunk %d .. ", chunks[i], i);
temp_malloc = malloc(chunks[i]);
heap_array[i_heap] = Heap(chunks[i], temp_malloc, heap_array, i_heap);
i_heap++;
}
printf("Everything allocated, let's start !");
// now all chunks are allocated. need to start the game
// will be a while true with everytime show the heap and possibility to free/alloc new heaps
int index;
bool reused;
uint32_t heap_size = 0;
std::string cmd_delimiter = " ";
std::string cmd_first;
std::string cmd_second;
std::string cmd_temp;
int cmd=0;
while(true){
// CORE INFINITE LOOP
// everything initialized, can start with the core
// show help and chunks
clear();
help_game();
printf("\n[*] First allocated chunk: 0x%x", heap_array[0].getMallocPointer());
printf("\n[*] Last allocated chunk: 0x%x", heap_array[i_heap-1].getMallocPointer());
// show chunks info
for(int j=0; j < i_heap; j++){
heap_array[j].showInfo(false);
printf("\n");
}
std::cout << "Command > ";
std::string command;
std::getline(std::cin, command);
// get the cmd part
cmd_first = command.substr(0, command.find(cmd_delimiter));
cmd = 0;
if(cmd_first.compare("malloc") == 0)
cmd = 1;
if(cmd_first.compare("free") == 0)
cmd = 2;
if(cmd_first.compare("show") == 0)
cmd = 3;
// get the parameter part
if(command.find(cmd_delimiter) != -1){
// or it will cause a runtime exception
cmd_temp = command.substr(command.find(cmd_delimiter), command.size()+1);
cmd_second = cmd_temp.substr(0, command.find(cmd_delimiter));
}
else{
// missing the 2nd argument. 777 will throw bad syntax
if(cmd != 0)
cmd = 777;
}
//just a test
//heap_array[0].test(heap_array, i_heap);
//sleep(10);
switch(cmd){
case 1:
//alloc
heap_size = std::atoi(cmd_second.c_str());
//printf("[*] Allocating %d ", heap_size);
temp_malloc = malloc(heap_size);
// and now check that the chunk has not been already inserted
reused=false;
for(int j=0; j < i_heap; j++){
if(heap_array[j].getMallocPointer() == temp_malloc){
heap_array[j].reallocated(heap_array, i_heap);
reused=true;
}
}
if (!reused){
heap_array[i_heap] = Heap(heap_size, temp_malloc, heap_array, i_heap);
i_heap++;
}
break;
case 2:
// free
index = std::atoi(cmd_second.c_str());
if(index < i_heap)
heap_array[index].freeChunk(heap_array, i_heap);
else
printf("[-] Invalid chunk index .. ");
sleep(1);
break;
case 3:
// examine
index = std::atoi(cmd_second.c_str());
if(index < i_heap){
heap_array[index].showInfo(true);
press();
}
else {
printf("[-] Inalid chunk index .. ");
sleep(1);
}
break;
case 777:
printf("Bad syntax ..");
sleep(1);
break;
default:
printf("Command not found ..");
sleep(1);
break;
}
}
}
int main(){
clear();
printf("1 for manual mode\n2 for demo mode\n> ");
int choose;
scanf("%d", &choose);
if(choose == 1)
manual_loop();
else if(choose == 2)
game_loop();
}