-
Notifications
You must be signed in to change notification settings - Fork 1
/
random-page.c
92 lines (81 loc) · 1.71 KB
/
random-page.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
#include <stdio.h>
#include <stdlib.h>
#include "trinity.h" // page_size
#include "sanitise.h" // interesting_*
#include "log.h" // For BUG
static void fabricate_onepage_struct(char *page)
{
unsigned int i;
void **ptr;
for (i = 0; i < page_size; ) {
ptr = (void*)&page[i];
switch (rand() % 4) {
case 0:
i += sizeof(unsigned int);
if (i > page_size)
return;
*(unsigned int *)ptr = get_interesting_32bit_value();
break;
case 1:
i += sizeof(unsigned long);
if (i > page_size)
return;
*(unsigned long *)ptr = get_interesting_value();
break;
case 2:
i += sizeof(void *);
if (i > page_size)
return;
*ptr = get_address();
break;
case 3:
i += sizeof(unsigned int);
if (i > page_size)
return;
*(unsigned int *)ptr = rand() % page_size;
break;
default:
BUG("unreachable!\n");
return;
}
}
}
void generate_random_page(char *page)
{
unsigned int i;
switch (rand() % 6) {
/* return a page of complete trash */
case 0: /* bytes */
for (i = 0; i < page_size; )
page[i++] = (unsigned char)rand();
return;
case 1: /* words */
for (i = 0; i < (page_size / 2); ) {
page[i++] = 0;
page[i++] = (unsigned char)rand();
}
return;
case 2: /* ints */
for (i = 0; i < (page_size / 4); ) {
page[i++] = 0;
page[i++] = 0;
page[i++] = 0;
page[i++] = (unsigned char)rand();
}
return;
/* return a page that looks kinda like a struct */
case 3: fabricate_onepage_struct(page);
return;
/* return a page of unicode nonsense. */
case 4: gen_unicode_page(page);
return;
/* page of 0's and 1's. */
case 5:
for (i = 0; i < page_size; )
page[i++] = (unsigned char)rand() % 2;
return;
default:
BUG("unreachable!\n");
return;
}
}