merge clone variants
[trinity.git] / random-page.c
blob765e5a7958a82cdb89e1d40d38deac1c938b942a
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "arch.h" // page_size
5 #include "random.h"
6 #include "sanitise.h" // get_address
7 #include "maps.h"
8 #include "log.h" // For BUG
10 static void fabricate_onepage_struct(char *page)
12 unsigned int i;
14 for (i = 0; i < page_size; ) {
15 void **ptr;
17 ptr = (void*)&page[i];
18 switch (rand() % 4) {
19 case 0:
20 i += sizeof(unsigned int);
21 if (i > page_size)
22 return;
23 *(unsigned int *)ptr = rand32();
24 break;
25 case 1:
26 i += sizeof(unsigned long);
27 if (i > page_size)
28 return;
29 *(unsigned long *)ptr = rand64();
30 break;
31 case 2:
32 i += sizeof(void *);
33 if (i > page_size)
34 return;
35 *ptr = get_address();
36 break;
37 case 3:
38 i += sizeof(unsigned int);
39 if (i > page_size)
40 return;
41 *(unsigned int *)ptr = rand() % page_size;
42 break;
47 void generate_random_page(char *page)
49 unsigned int i;
50 unsigned int p = 0;
52 switch (rand() % 8) {
53 /* return a page of complete trash */
54 case 0: /* bytes */
55 for (i = 0; i < page_size; )
56 page[i++] = (unsigned char)rand();
57 return;
59 case 1: /* words */
60 for (i = 0; i < (page_size / 2); ) {
61 page[i++] = 0;
62 page[i++] = (unsigned char)rand();
64 return;
66 case 2: /* ints */
67 for (i = 0; i < (page_size / 4); ) {
68 page[i++] = 0;
69 page[i++] = 0;
70 page[i++] = 0;
71 page[i++] = (unsigned char)rand();
73 return;
75 /* return a page that looks kinda like a struct */
76 case 3: fabricate_onepage_struct(page);
77 return;
79 /* return a page of unicode nonsense. */
80 case 4: gen_unicode_page(page);
81 return;
83 /* page of 0's and 1's. */
84 case 5:
85 for (i = 0; i < page_size; )
86 page[i++] = (unsigned char)rand_bool();
87 return;
89 /* page full of format strings. */
90 case 6:
91 for (i = 0; i < page_size; ) {
92 page[i++] = '%';
93 switch (rand_bool()) {
94 case 0: page[i++] = 'd';
95 break;
96 case 1: page[i++] = 's';
97 break;
100 page_size = getpagesize(); // Hack for clang 3.3 false positive.
101 page[rand() % page_size] = 0;
102 return;
104 /* ascii representation of a random number */
105 case 7:
106 switch (rand() % 3) {
107 case 0:
108 switch (rand() % 3) {
109 case 0: p = sprintf(page_rand, "%lu", (unsigned long) rand64());
110 break;
111 case 1: p = sprintf(page_rand, "%ld", (unsigned long) rand64());
112 break;
113 case 2: p = sprintf(page_rand, "%lx", (unsigned long) rand64());
114 break;
116 break;
118 case 1:
119 switch (rand() % 3) {
120 case 0: p = sprintf(page_rand, "%u", (unsigned int) rand64());
121 break;
122 case 1: p = sprintf(page_rand, "%d", (int) rand64());
123 break;
124 case 2: p = sprintf(page_rand, "%x", (int) rand64());
125 break;
127 break;
129 case 2:
130 switch (rand() % 3) {
131 case 0: p = sprintf(page_rand, "%u", (unsigned char) rand64());
132 break;
133 case 1: p = sprintf(page_rand, "%d", (char) rand64());
134 break;
135 case 2: p = sprintf(page_rand, "%x", (char) rand64());
136 break;
138 break;
142 page_rand[p] = 0;
143 break;