collapse the various sockaddr/setsockopt files into per-proto files.
[trinity.git] / random-page.c
blobda5c803915ed6d09fd189965334445a89dea69b1
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;
13 void **ptr;
15 for (i = 0; i < page_size; ) {
16 ptr = (void*)&page[i];
17 switch (rand() % 4) {
18 case 0:
19 i += sizeof(unsigned int);
20 if (i > page_size)
21 return;
22 *(unsigned int *)ptr = rand32();
23 break;
24 case 1:
25 i += sizeof(unsigned long);
26 if (i > page_size)
27 return;
28 *(unsigned long *)ptr = rand64();
29 break;
30 case 2:
31 i += sizeof(void *);
32 if (i > page_size)
33 return;
34 *ptr = get_address();
35 break;
36 case 3:
37 i += sizeof(unsigned int);
38 if (i > page_size)
39 return;
40 *(unsigned int *)ptr = rand() % page_size;
41 break;
42 default:
43 BUG("unreachable!\n");
44 return;
49 void generate_random_page(char *page)
51 unsigned int i;
52 unsigned int p = 0;
54 switch (rand() % 8) {
55 /* return a page of complete trash */
56 case 0: /* bytes */
57 for (i = 0; i < page_size; )
58 page[i++] = (unsigned char)rand();
59 return;
61 case 1: /* words */
62 for (i = 0; i < (page_size / 2); ) {
63 page[i++] = 0;
64 page[i++] = (unsigned char)rand();
66 return;
68 case 2: /* ints */
69 for (i = 0; i < (page_size / 4); ) {
70 page[i++] = 0;
71 page[i++] = 0;
72 page[i++] = 0;
73 page[i++] = (unsigned char)rand();
75 return;
77 /* return a page that looks kinda like a struct */
78 case 3: fabricate_onepage_struct(page);
79 return;
81 /* return a page of unicode nonsense. */
82 case 4: gen_unicode_page(page);
83 return;
85 /* page of 0's and 1's. */
86 case 5:
87 for (i = 0; i < page_size; )
88 page[i++] = (unsigned char)rand_bool();
89 return;
91 /* page full of format strings. */
92 case 6:
93 for (i = 0; i < page_size; ) {
94 page[i++] = '%';
95 switch (rand_bool()) {
96 case 0: page[i++] = 'd';
97 break;
98 case 1: page[i++] = 's';
99 break;
100 default: break;
103 page_size = getpagesize(); // Hack for clang 3.3 false positive.
104 page[rand() % page_size] = 0;
105 return;
107 /* ascii representation of a random number */
108 case 7:
109 switch (rand() % 3) {
110 case 0:
111 switch (rand() % 3) {
112 case 0: p = sprintf(page_rand, "%lu", (unsigned long) rand64());
113 break;
114 case 1: p = sprintf(page_rand, "%ld", (unsigned long) rand64());
115 break;
116 case 2: p = sprintf(page_rand, "%lx", (unsigned long) rand64());
117 break;
118 default: break;
120 break;
122 case 1:
123 switch (rand() % 3) {
124 case 0: p = sprintf(page_rand, "%u", (unsigned int) rand64());
125 break;
126 case 1: p = sprintf(page_rand, "%d", (int) rand64());
127 break;
128 case 2: p = sprintf(page_rand, "%x", (int) rand64());
129 break;
130 default: break;
132 break;
134 case 2:
135 switch (rand() % 3) {
136 case 0: p = sprintf(page_rand, "%u", (unsigned char) rand64());
137 break;
138 case 1: p = sprintf(page_rand, "%d", (char) rand64());
139 break;
140 case 2: p = sprintf(page_rand, "%x", (char) rand64());
141 break;
142 default: break;
144 break;
146 default: break;
149 page_rand[p] = 0;
151 break;
153 default:
154 BUG("unreachable!\n");
155 return;