MOS Source Code
Loading...
Searching...
No Matches
random_data.c
Go to the documentation of this file.
1#include "random_data.h"
2#include <string.h>
3#include <malloc_wrappers.h>
4#include <pb_encode.h>
5
6#ifndef LLVMFUZZER
7
8static uint32_t g_random_seed = 1234;
9
11{
12 g_random_seed = seed;
13}
14
19
20/* Uses xorshift64 here instead of rand() for both speed and
21 * reproducibility across platforms. */
29
30/* Get a random integer in range, with approximately flat distribution. */
31int rand_int(int min, int max)
32{
33 return rand_word() % (max + 1 - min) + min;
34}
35
37{
38 return rand_word() & 1;
39}
40
41/* Get a random byte, with skewed distribution.
42 * Important corner cases like 0xFF, 0x00 and 0xFE occur more
43 * often than other values. */
45{
46 uint32_t w = rand_word();
47 uint8_t b = w & 0xFF;
48 if (w & 0x100000)
49 b >>= (w >> 8) & 7;
50 if (w & 0x200000)
51 b <<= (w >> 12) & 7;
52 if (w & 0x400000)
53 b ^= 0xFF;
54 return b;
55}
56
57/* Get a random length, with skewed distribution.
58 * Favors the shorter lengths, but always at least 1. */
59size_t rand_len(size_t max)
60{
61 uint32_t w = rand_word();
62 size_t s;
63 if (w & 0x800000)
64 w &= 3;
65 else if (w & 0x400000)
66 w &= 15;
67 else if (w & 0x200000)
68 w &= 255;
69
70 s = (w % max);
71 if (s == 0)
72 s = 1;
73
74 return s;
75}
76
77/* Fills a buffer with random data with skewed distribution. */
78void rand_fill(uint8_t *buf, size_t count)
79{
80 for (; count > 0; count--)
81 {
82 *buf++ = rand_byte();
83 }
84}
85
86/* Fill with random protobuf-like data */
87size_t rand_fill_protobuf(uint8_t *buf, size_t min_bytes, size_t max_bytes, int min_tag)
88{
89 pb_ostream_t stream = pb_ostream_from_buffer(buf, max_bytes);
90
91 while(stream.bytes_written < min_bytes)
92 {
93 pb_wire_type_t wt = rand_int(0, 3);
94 if (wt == 3) wt = 5; /* Gap in values */
95
96 if (!pb_encode_tag(&stream, wt, rand_int(min_tag, min_tag + 512)))
97 break;
98
99 if (wt == PB_WT_VARINT)
100 {
101 uint64_t value;
102 rand_fill((uint8_t*)&value, sizeof(value));
103 pb_encode_varint(&stream, value);
104 }
105 else if (wt == PB_WT_64BIT)
106 {
107 uint64_t value;
108 rand_fill((uint8_t*)&value, sizeof(value));
109 pb_encode_fixed64(&stream, &value);
110 }
111 else if (wt == PB_WT_32BIT)
112 {
113 uint32_t value;
114 rand_fill((uint8_t*)&value, sizeof(value));
115 pb_encode_fixed32(&stream, &value);
116 }
117 else if (wt == PB_WT_STRING)
118 {
119 size_t len;
120 uint8_t *buf;
121
122 if (min_bytes > stream.bytes_written)
123 len = rand_len(min_bytes - stream.bytes_written);
124 else
125 len = 0;
126
127 buf = malloc(len);
128 pb_encode_varint(&stream, len);
129 rand_fill(buf, len);
130 pb_write(&stream, buf, len);
131 free(buf);
132 }
133 }
134
135 return stream.bytes_written;
136}
137
138/* Given a buffer of data, mess it up a bit */
139void rand_mess(uint8_t *buf, size_t count)
140{
141 int m = rand_int(0, 3);
142
143 if (m == 0)
144 {
145 /* Replace random substring */
146 int s = rand_int(0, count - 1);
147 int l = rand_len(count - s);
148 rand_fill(buf + s, l);
149 }
150 else if (m == 1)
151 {
152 /* Swap random bytes */
153 int a = rand_int(0, count - 1);
154 int b = rand_int(0, count - 1);
155 int x = buf[a];
156 buf[a] = buf[b];
157 buf[b] = x;
158 }
159 else if (m == 2)
160 {
161 /* Duplicate substring */
162 int s = rand_int(0, count - 2);
163 int l = rand_len((count - s) / 2);
164 memcpy(buf + s + l, buf + s, l);
165 }
166 else if (m == 3)
167 {
168 /* Add random protobuf noise */
169 int s = rand_int(0, count - 1);
170 int l = rand_len(count - s);
171 rand_fill_protobuf(buf + s, l, count - s, 1);
172 }
173}
174
175/* Append or prepend protobuf noise */
176void rand_protobuf_noise(uint8_t *buffer, size_t bufsize, size_t *msglen)
177{
178 int m = rand_int(0, 2);
179 size_t max_size = bufsize - 32 - *msglen;
180 if (m == 1)
181 {
182 /* Prepend */
183 uint8_t *tmp = malloc_with_check(bufsize);
184 size_t s = rand_fill_protobuf(tmp, rand_len(max_size), bufsize - *msglen, 1000);
185 memmove(buffer + s, buffer, *msglen);
186 memcpy(buffer, tmp, s);
187 free_with_check(tmp);
188 *msglen += s;
189 }
190 else if (m == 2)
191 {
192 /* Append */
193 size_t s = rand_fill_protobuf(buffer + *msglen, rand_len(max_size), bufsize - *msglen, 1000);
194 *msglen += s;
195 }
196}
197
198#endif
MOSAPI void * memmove(void *dest, const void *src, size_t n)
Definition mos_string.c:111
void * malloc_with_check(size_t size)
void free_with_check(void *mem)
pb_wire_type_t
Definition pb.h:420
@ PB_WT_VARINT
Definition pb.h:421
@ PB_WT_32BIT
Definition pb.h:424
@ PB_WT_STRING
Definition pb.h:423
@ PB_WT_64BIT
Definition pb.h:422
bool pb_encode_varint(pb_ostream_t *stream, uint64_t value)
Definition pb_encode.c:607
bool pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
Definition pb_encode.c:84
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
Definition pb_encode.c:63
bool pb_encode_fixed64(pb_ostream_t *stream, const void *value)
Definition pb_encode.c:654
bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
Definition pb_encode.c:675
bool pb_encode_fixed32(pb_ostream_t *stream, const void *value)
Definition pb_encode.c:637
static void * memcpy(void *s1, const void *s2, size_t n)
Definition pb_syshdr.h:90
unsigned int uint32_t
Definition pb_syshdr.h:24
unsigned long long uint64_t
Definition pb_syshdr.h:26
unsigned char uint8_t
Definition pb_syshdr.h:20
size_t rand_len(size_t max)
Definition random_data.c:59
void random_set_seed(uint32_t seed)
Definition random_data.c:10
uint32_t random_get_seed()
Definition random_data.c:15
uint8_t rand_byte()
Definition random_data.c:44
void rand_fill(uint8_t *buf, size_t count)
Definition random_data.c:78
size_t rand_fill_protobuf(uint8_t *buf, size_t min_bytes, size_t max_bytes, int min_tag)
Definition random_data.c:87
static uint32_t g_random_seed
Definition random_data.c:8
uint32_t rand_word()
Definition random_data.c:22
void rand_mess(uint8_t *buf, size_t count)
bool rand_bool()
Definition random_data.c:36
int rand_int(int min, int max)
Definition random_data.c:31
void rand_protobuf_noise(uint8_t *buffer, size_t bufsize, size_t *msglen)
static char buffer[2048]
Definition test_printf.c:7