blob: 5dbbf069de8fa423ee4a7261e99ad9f24693988d [file] [log] [blame]
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +00001/*
2 *
3 * honggfuzz - buffer mangling routines
4 * -----------------------------------------
5 *
6 * Author:
7 * Robert Swiecki <swiecki@google.com>
8 *
9 * Copyright 2010-2015 by Google Inc. All Rights Reserved.
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License. You may obtain
13 * a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
20 * implied. See the License for the specific language governing
21 * permissions and limitations under the License.
22 *
23 */
24
25#include "common.h"
26#include "mangle.h"
27
28#include <inttypes.h>
robert.swiecki@gmail.com173f5192015-02-23 00:06:47 +000029#include <math.h>
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +000030#include <stdlib.h>
31#include <string.h>
robert.swiecki@gmail.come7680522015-02-22 22:22:37 +000032#include <sys/mman.h>
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +000033#include <unistd.h>
34
35#include "log.h"
36#include "util.h"
37
Robert Swieckie6389e22015-11-25 15:13:38 +010038static inline void mangle_Overwrite(uint8_t * dst, const uint8_t * src, size_t dstSz, size_t off,
39 size_t sz)
robert.swiecki@gmail.com1c555c72015-02-22 16:25:54 +000040{
41 size_t maxToCopy = dstSz - off;
42 if (sz > maxToCopy) {
43 sz = maxToCopy;
44 }
45
robert.swiecki@gmail.com50e1f3b2015-02-23 17:32:11 +000046 memcpy(&dst[off], src, sz);
robert.swiecki@gmail.com1c555c72015-02-22 16:25:54 +000047}
48
Anestis Bechtsoudis2ff92d12015-12-20 15:33:20 +020049static void mangle_Byte(honggfuzz_t * hfuzz UNUSED, uint8_t * buf, size_t bufSz UNUSED, size_t off)
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +000050{
51 buf[off] = (uint8_t) util_rndGet(0, UINT8_MAX);
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +000052}
53
Anestis Bechtsoudis2ff92d12015-12-20 15:33:20 +020054static void mangle_Bytes(honggfuzz_t * hfuzz UNUSED, uint8_t * buf, size_t bufSz, size_t off)
robert.swiecki@gmail.com457ca142015-02-22 14:59:34 +000055{
56 uint32_t val = (uint32_t) util_rndGet(0, UINT32_MAX);
57
robert.swiecki@gmail.com12e75062015-02-22 15:01:21 +000058 /* Overwrite with random 2,3,4-byte values */
59 size_t toCopy = util_rndGet(2, 4);
robert.swiecki@gmail.combcdeaea2015-02-23 17:36:54 +000060 mangle_Overwrite(buf, (uint8_t *) & val, bufSz, off, toCopy);
robert.swiecki@gmail.com457ca142015-02-22 14:59:34 +000061}
62
Anestis Bechtsoudis2ff92d12015-12-20 15:33:20 +020063static void mangle_Bit(honggfuzz_t * hfuzz UNUSED, uint8_t * buf, size_t bufSz UNUSED, size_t off)
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +000064{
65 buf[off] ^= ((uint8_t) 1 << util_rndGet(0, 7));
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +000066}
67
Anestis Bechtsoudis2ff92d12015-12-20 15:33:20 +020068static void mangle_Dictionary(honggfuzz_t * hfuzz UNUSED, uint8_t * buf, size_t bufSz, size_t off)
robert.swiecki@gmail.com4f1124f2015-04-21 17:12:22 +000069{
70 if (hfuzz->dictionaryCnt == 0) {
tlogic@gmail.com7b6d7ee2015-04-23 21:30:12 +000071 mangle_Bit(hfuzz, buf, bufSz, off);
72 return;
robert.swiecki@gmail.com4f1124f2015-04-21 17:12:22 +000073 }
74
75 uint64_t choice = util_rndGet(0, hfuzz->dictionaryCnt - 1);
76 mangle_Overwrite(buf, (uint8_t *) hfuzz->dictionary[choice], bufSz, off,
77 strlen(hfuzz->dictionary[choice]));
robert.swiecki@gmail.com4f1124f2015-04-21 17:12:22 +000078}
79
Anestis Bechtsoudis2ff92d12015-12-20 15:33:20 +020080static void mangle_Magic(honggfuzz_t * hfuzz UNUSED, uint8_t * buf, size_t bufSz, size_t off)
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +000081{
robert.swiecki@gmail.com4be26672015-03-05 03:36:50 +000082 /* *INDENT-OFF* */
robert.swiecki@gmail.com3d928f12015-04-15 14:43:21 +000083 static const struct {
Anestis Bechtsoudis4799a9d2015-12-20 15:44:26 +020084 const uint8_t val[8];
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +000085 const size_t size;
Anestis Bechtsoudis4799a9d2015-12-20 15:44:26 +020086 } mangleMagicVals[] = {
Anestis Bechtsoudis51c9bb12015-08-26 14:46:21 +030087 /* 1B - No endianness */
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +000088 { "\x00\x00\x00\x00\x00\x00\x00\x00", 1},
89 { "\x01\x00\x00\x00\x00\x00\x00\x00", 1},
robert.swiecki@gmail.combe554fb2015-02-23 00:31:53 +000090 { "\x02\x00\x00\x00\x00\x00\x00\x00", 1},
91 { "\x03\x00\x00\x00\x00\x00\x00\x00", 1},
92 { "\x04\x00\x00\x00\x00\x00\x00\x00", 1},
robert.swiecki@gmail.com03e70392015-03-01 03:53:11 +000093 { "\x7E\x00\x00\x00\x00\x00\x00\x00", 1},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +000094 { "\x7F\x00\x00\x00\x00\x00\x00\x00", 1},
95 { "\x80\x00\x00\x00\x00\x00\x00\x00", 1},
robert.swiecki@gmail.com395278b2015-02-28 22:48:53 +000096 { "\x81\x00\x00\x00\x00\x00\x00\x00", 1},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +000097 { "\xFF\x00\x00\x00\x00\x00\x00\x00", 1},
98 /* 2B - NE */
99 { "\x00\x00\x00\x00\x00\x00\x00\x00", 2},
robert.swiecki@gmail.com1ba76192015-02-22 15:45:49 +0000100 { "\x01\x01\x00\x00\x00\x00\x00\x00", 2},
101 { "\x80\x80\x00\x00\x00\x00\x00\x00", 2},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000102 { "\xFF\xFF\x00\x00\x00\x00\x00\x00", 2},
103 /* 2B - BE */
104 { "\x00\x01\x00\x00\x00\x00\x00\x00", 2},
robert.swiecki@gmail.combe554fb2015-02-23 00:31:53 +0000105 { "\x00\x02\x00\x00\x00\x00\x00\x00", 2},
106 { "\x00\x03\x00\x00\x00\x00\x00\x00", 2},
107 { "\x00\x04\x00\x00\x00\x00\x00\x00", 2},
robert.swiecki@gmail.com03e70392015-03-01 03:53:11 +0000108 { "\x7E\xFF\x00\x00\x00\x00\x00\x00", 2},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000109 { "\x7F\xFF\x00\x00\x00\x00\x00\x00", 2},
110 { "\x80\x00\x00\x00\x00\x00\x00\x00", 2},
robert.swiecki@gmail.com395278b2015-02-28 22:48:53 +0000111 { "\x80\x01\x00\x00\x00\x00\x00\x00", 2},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000112 /* 2B - LE */
robert.swiecki@gmail.combe554fb2015-02-23 00:31:53 +0000113 { "\x01\x00\x00\x00\x00\x00\x00\x00", 2},
114 { "\x02\x00\x00\x00\x00\x00\x00\x00", 2},
115 { "\x03\x00\x00\x00\x00\x00\x00\x00", 2},
116 { "\x04\x00\x00\x00\x00\x00\x00\x00", 2},
robert.swiecki@gmail.com03e70392015-03-01 03:53:11 +0000117 { "\xFF\x7E\x00\x00\x00\x00\x00\x00", 2},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000118 { "\xFF\x7F\x00\x00\x00\x00\x00\x00", 2},
119 { "\x00\x80\x00\x00\x00\x00\x00\x00", 2},
robert.swiecki@gmail.com395278b2015-02-28 22:48:53 +0000120 { "\x01\x80\x00\x00\x00\x00\x00\x00", 2},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000121 /* 4B - NE */
122 { "\x00\x00\x00\x00\x00\x00\x00\x00", 4},
robert.swiecki@gmail.com1ba76192015-02-22 15:45:49 +0000123 { "\x01\x01\x01\x01\x00\x00\x00\x00", 4},
124 { "\x80\x80\x80\x80\x00\x00\x00\x00", 4},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000125 { "\xFF\xFF\xFF\xFF\x00\x00\x00\x00", 4},
126 /* 4B - BE */
127 { "\x00\x00\x00\x01\x00\x00\x00\x00", 4},
robert.swiecki@gmail.combe554fb2015-02-23 00:31:53 +0000128 { "\x00\x00\x00\x02\x00\x00\x00\x00", 4},
129 { "\x00\x00\x00\x03\x00\x00\x00\x00", 4},
130 { "\x00\x00\x00\x04\x00\x00\x00\x00", 4},
robert.swiecki@gmail.com03e70392015-03-01 03:53:11 +0000131 { "\x7E\xFF\xFF\xFF\x00\x00\x00\x00", 4},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000132 { "\x7F\xFF\xFF\xFF\x00\x00\x00\x00", 4},
133 { "\x80\x00\x00\x00\x00\x00\x00\x00", 4},
robert.swiecki@gmail.com395278b2015-02-28 22:48:53 +0000134 { "\x80\x00\x00\x01\x00\x00\x00\x00", 4},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000135 /* 4B - LE */
136 { "\x01\x00\x00\x00\x00\x00\x00\x00", 4},
robert.swiecki@gmail.combe554fb2015-02-23 00:31:53 +0000137 { "\x02\x00\x00\x00\x00\x00\x00\x00", 4},
138 { "\x03\x00\x00\x00\x00\x00\x00\x00", 4},
139 { "\x04\x00\x00\x00\x00\x00\x00\x00", 4},
robert.swiecki@gmail.com03e70392015-03-01 03:53:11 +0000140 { "\xFF\xFF\xFF\x7E\x00\x00\x00\x00", 4},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000141 { "\xFF\xFF\xFF\x7F\x00\x00\x00\x00", 4},
142 { "\x00\x00\x00\x80\x00\x00\x00\x00", 4},
robert.swiecki@gmail.com395278b2015-02-28 22:48:53 +0000143 { "\x01\x00\x00\x80\x00\x00\x00\x00", 4},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000144 /* 8B - NE */
145 { "\x00\x00\x00\x00\x00\x00\x00\x00", 8},
robert.swiecki@gmail.com1ba76192015-02-22 15:45:49 +0000146 { "\x01\x01\x01\x01\x01\x01\x01\x01", 8},
147 { "\x80\x80\x80\x80\x80\x80\x80\x80", 8},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000148 { "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 8},
149 /* 8B - BE */
150 { "\x00\x00\x00\x00\x00\x00\x00\x01", 8},
robert.swiecki@gmail.combe554fb2015-02-23 00:31:53 +0000151 { "\x00\x00\x00\x00\x00\x00\x00\x02", 8},
152 { "\x00\x00\x00\x00\x00\x00\x00\x03", 8},
153 { "\x00\x00\x00\x00\x00\x00\x00\x04", 8},
robert.swiecki@gmail.com03e70392015-03-01 03:53:11 +0000154 { "\x7E\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 8},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000155 { "\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 8},
156 { "\x80\x00\x00\x00\x00\x00\x00\x00", 8},
robert.swiecki@gmail.com395278b2015-02-28 22:48:53 +0000157 { "\x80\x00\x00\x00\x00\x00\x00\x01", 8},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000158 /* 8B - LE */
159 { "\x01\x00\x00\x00\x00\x00\x00\x00", 8},
robert.swiecki@gmail.combe554fb2015-02-23 00:31:53 +0000160 { "\x02\x00\x00\x00\x00\x00\x00\x00", 8},
161 { "\x03\x00\x00\x00\x00\x00\x00\x00", 8},
162 { "\x04\x00\x00\x00\x00\x00\x00\x00", 8},
robert.swiecki@gmail.com03e70392015-03-01 03:53:11 +0000163 { "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7E", 8},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000164 { "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F", 8},
165 { "\x00\x00\x00\x00\x00\x00\x00\x80", 8},
robert.swiecki@gmail.com395278b2015-02-28 22:48:53 +0000166 { "\x01\x00\x00\x00\x00\x00\x00\x80", 8},
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000167 };
robert.swiecki@gmail.com4be26672015-03-05 03:36:50 +0000168 /* *INDENT-ON* */
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000169
170 uint64_t choice = util_rndGet(0, ARRAYSIZE(mangleMagicVals) - 1);
robert.swiecki@gmail.combcdeaea2015-02-23 17:36:54 +0000171 mangle_Overwrite(buf, mangleMagicVals[choice].val, bufSz, off, mangleMagicVals[choice].size);
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000172}
173
Anestis Bechtsoudis2ff92d12015-12-20 15:33:20 +0200174static void mangle_MemSet(honggfuzz_t * hfuzz UNUSED, uint8_t * buf, size_t bufSz, size_t off)
robert.swiecki@gmail.com89cc38c2015-02-23 02:52:08 +0000175{
176 uint64_t sz = util_rndGet(1, bufSz - off);
177 int val = (int)util_rndGet(0, UINT8_MAX);
178
179 memset(&buf[off], val, sz);
180}
181
Anestis Bechtsoudis2ff92d12015-12-20 15:33:20 +0200182static void mangle_MemMove(honggfuzz_t * hfuzz UNUSED, uint8_t * buf, size_t bufSz, size_t off)
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000183{
robert.swiecki@gmail.comb7779612015-02-22 14:51:17 +0000184 uint64_t mangleTo = util_rndGet(0, bufSz - 1);
robert.swiecki@gmail.com8c3e0f22015-02-22 16:32:16 +0000185 uint64_t mangleSzTo = bufSz - mangleTo;
robert.swiecki@gmail.com0a7eabe2015-02-22 14:47:45 +0000186
robert.swiecki@gmail.come8555c22015-02-22 14:49:22 +0000187 uint64_t mangleSzFrom = util_rndGet(1, bufSz - off);
robert.swiecki@gmail.com0a7eabe2015-02-22 14:47:45 +0000188 uint64_t mangleSz = mangleSzFrom < mangleSzTo ? mangleSzFrom : mangleSzTo;
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000189
190 memmove(&buf[mangleTo], &buf[off], mangleSz);
191}
192
Anestis Bechtsoudis2ff92d12015-12-20 15:33:20 +0200193static void mangle_Random(honggfuzz_t * hfuzz UNUSED, uint8_t * buf, size_t bufSz, size_t off)
robert.swiecki@gmail.com89cc38c2015-02-23 02:52:08 +0000194{
195 uint64_t sz = util_rndGet(1, bufSz - off);
196 util_rndBuf(&buf[off], sz);
197}
198
Anestis Bechtsoudis2ff92d12015-12-20 15:33:20 +0200199static void mangle_AddSub(honggfuzz_t * hfuzz UNUSED, uint8_t * buf, size_t bufSz, size_t off)
robert.swiecki@gmail.com549ff182015-02-28 22:38:00 +0000200{
201 /* 1,2,4 */
202 uint64_t varLen = 1ULL << util_rndGet(0, 2);
203 if ((bufSz - off) < varLen) {
robert.swiecki@gmail.com57197642015-03-01 15:39:30 +0000204 varLen = 1;
robert.swiecki@gmail.com549ff182015-02-28 22:38:00 +0000205 }
206
207 int delta = (int)util_rndGet(0, 64);
208 delta -= 32;
209
210 switch (varLen) {
211 case 1:
robert.swiecki@gmail.com62e34ae2015-03-05 03:39:32 +0000212 {
213 buf[off] += delta;
214 return;
215 break;
216 }
robert.swiecki@gmail.com549ff182015-02-28 22:38:00 +0000217 case 2:
robert.swiecki@gmail.com62e34ae2015-03-05 03:39:32 +0000218 {
219 uint16_t val = *((uint16_t *) & buf[off]);
220 if (util_rndGet(0, 1) == 0) {
221 /* BE */
222 val = util_ToFromBE16(val);
223 val += delta;
224 val = util_ToFromBE16(val);
225 } else {
226 /* LE */
227 val = util_ToFromLE16(val);
228 val += delta;
229 val = util_ToFromLE16(val);
230 }
231 mangle_Overwrite(buf, (uint8_t *) & val, bufSz, off, varLen);
232 return;
233 break;
robert.swiecki@gmail.com549ff182015-02-28 22:38:00 +0000234 }
235 case 4:
robert.swiecki@gmail.com62e34ae2015-03-05 03:39:32 +0000236 {
237 uint32_t val = *((uint32_t *) & buf[off]);
238 if (util_rndGet(0, 1) == 0) {
239 /* BE */
240 val = util_ToFromBE32(val);
241 val += delta;
242 val = util_ToFromBE32(val);
243 } else {
244 /* LE */
245 val = util_ToFromLE32(val);
246 val += delta;
247 val = util_ToFromLE32(val);
248 }
249 mangle_Overwrite(buf, (uint8_t *) & val, bufSz, off, varLen);
250 return;
251 break;
robert.swiecki@gmail.com549ff182015-02-28 22:38:00 +0000252 }
253 default:
robert.swiecki@gmail.com62e34ae2015-03-05 03:39:32 +0000254 {
Anestis Bechtsoudis07e14ce2015-12-30 14:15:42 +0200255 LOG_F("Unknown variable length size: %" PRIu64, varLen);
robert.swiecki@gmail.com62e34ae2015-03-05 03:39:32 +0000256 break;
257 }
robert.swiecki@gmail.com549ff182015-02-28 22:38:00 +0000258 }
259}
260
Anestis Bechtsoudis2ff92d12015-12-20 15:33:20 +0200261static void mangle_IncByte(honggfuzz_t * hfuzz UNUSED, uint8_t * buf, size_t bufSz UNUSED,
262 size_t off)
robert.swiecki@gmail.com78973ed2015-03-01 03:57:18 +0000263{
264 buf[off] += (uint8_t) 1UL;
robert.swiecki@gmail.com78973ed2015-03-01 03:57:18 +0000265}
266
Anestis Bechtsoudis2ff92d12015-12-20 15:33:20 +0200267static void mangle_DecByte(honggfuzz_t * hfuzz UNUSED, uint8_t * buf, size_t bufSz UNUSED,
268 size_t off)
robert.swiecki@gmail.com78973ed2015-03-01 03:57:18 +0000269{
270 buf[off] -= (uint8_t) 1UL;
robert.swiecki@gmail.com78973ed2015-03-01 03:57:18 +0000271}
272
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000273void mangle_mangleContent(honggfuzz_t * hfuzz, uint8_t * buf, size_t bufSz)
274{
robert.swiecki@gmail.com4be26672015-03-05 03:36:50 +0000275 /* *INDENT-OFF* */
Robert Swieckie6389e22015-11-25 15:13:38 +0100276 static void (*const mangleFuncs[]) (honggfuzz_t * hfuzz, uint8_t * buf, size_t bufSz, size_t off) = {
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000277 mangle_Byte,
robert.swiecki@gmail.com17ee6762015-02-22 15:19:31 +0000278 mangle_Byte,
279 mangle_Byte,
280 mangle_Byte,
281 mangle_Byte,
282 mangle_Byte,
283 mangle_Byte,
284 mangle_Byte,
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000285 mangle_Bit,
robert.swiecki@gmail.com17ee6762015-02-22 15:19:31 +0000286 mangle_Bit,
287 mangle_Bit,
288 mangle_Bit,
289 mangle_Bit,
290 mangle_Bit,
291 mangle_Bit,
292 mangle_Bit,
293 mangle_Bytes,
294 mangle_Bytes,
295 mangle_Magic,
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000296 mangle_Magic,
robert.swiecki@gmail.com87e72752015-03-03 16:15:59 +0000297 mangle_IncByte,
298 mangle_IncByte,
299 mangle_DecByte,
300 mangle_DecByte,
301 mangle_AddSub,
302 mangle_AddSub,
robert.swiecki@gmail.com4f1124f2015-04-21 17:12:22 +0000303 mangle_Dictionary,
304 mangle_Dictionary,
robert.swiecki@gmail.com89cc38c2015-02-23 02:52:08 +0000305 mangle_MemMove,
306 mangle_MemSet,
robert.swiecki@gmail.com50e1f3b2015-02-23 17:32:11 +0000307 mangle_Random,
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000308 };
robert.swiecki@gmail.com4be26672015-03-05 03:36:50 +0000309 /* *INDENT-ON* */
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000310
311 /*
312 * Minimal number of changes is 1
313 */
314 uint64_t changesCnt = bufSz * hfuzz->flipRate;
315 if (changesCnt == 0ULL) {
316 changesCnt = 1;
317 }
318 changesCnt = util_rndGet(1, changesCnt);
319
320 for (uint64_t x = 0; x < changesCnt; x++) {
robert.swiecki@gmail.com0a7eabe2015-02-22 14:47:45 +0000321 size_t offset = util_rndGet(0, bufSz - 1);
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000322 uint64_t choice = util_rndGet(0, ARRAYSIZE(mangleFuncs) - 1);
robert.swiecki@gmail.com4f1124f2015-04-21 17:12:22 +0000323 mangleFuncs[choice] (hfuzz, buf, bufSz, offset);
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000324 }
325}
326
robert.swiecki@gmail.comf891cad2015-02-25 12:21:04 +0000327static double mangle_ExpDist(void)
robert.swiecki@gmail.com61b02a52015-02-23 01:14:18 +0000328{
robert.swiecki@gmail.comf891cad2015-02-25 12:21:04 +0000329 double rnd = (double)util_rndGet(1, UINT32_MAX) / (double)(UINT32_MAX);
robert.swiecki@gmail.comedbca382015-02-26 14:21:19 +0000330 return pow(rnd, 4.0L);
robert.swiecki@gmail.com61b02a52015-02-23 01:14:18 +0000331}
332
robert.swiecki@gmail.com173f5192015-02-23 00:06:47 +0000333/* Gauss-like distribution */
robert.swiecki@gmail.comace40862015-03-08 07:09:56 +0000334bool mangle_Resize(honggfuzz_t * hfuzz, uint8_t * buf, size_t * bufSz)
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000335{
robert.swiecki@gmail.com060a9dd2015-02-28 06:37:27 +0000336 const uint64_t chance_one_in_x = 5;
robert.swiecki@gmail.come7680522015-02-22 22:22:37 +0000337 if (util_rndGet(1, chance_one_in_x) != 1) {
338 return true;
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000339 }
robert.swiecki@gmail.comf891cad2015-02-25 12:21:04 +0000340 ssize_t newSz = *bufSz;
robert.swiecki@gmail.com1c246502015-02-25 13:47:27 +0000341 int delta = 0;
robert.swiecki@gmail.coma56173d2015-02-26 00:46:24 +0000342 unsigned int val = (unsigned int)util_rndGet(1, 64);
Anestis Bechtsoudisccdf28c2016-01-10 15:04:22 +0200343
344 /* *INDENT-OFF* */
robert.swiecki@gmail.com1c246502015-02-25 13:47:27 +0000345 switch (val) {
robert.swiecki@gmail.coma56173d2015-02-26 00:46:24 +0000346 case 1 ... 16:
robert.swiecki@gmail.com1c246502015-02-25 13:47:27 +0000347 delta = -val;
348 break;
robert.swiecki@gmail.coma56173d2015-02-26 00:46:24 +0000349 case 17 ... 32:
350 delta = val - 16;
robert.swiecki@gmail.com1c246502015-02-25 13:47:27 +0000351 break;
robert.swiecki@gmail.coma56173d2015-02-26 00:46:24 +0000352 case 33 ... 48:
robert.swiecki@gmail.com1c246502015-02-25 13:47:27 +0000353 delta += (int)(mangle_ExpDist() * (double)((hfuzz->maxFileSz - *bufSz)));
354 break;
robert.swiecki@gmail.coma56173d2015-02-26 00:46:24 +0000355 case 49 ... 64:
robert.swiecki@gmail.com1c246502015-02-25 13:47:27 +0000356 delta -= (int)(mangle_ExpDist() * (double)(*bufSz));
357 break;
358 default:
Robert Swieckic8c32db2015-10-09 18:06:22 +0200359 LOG_F("Random value out of scope %u", val);
robert.swiecki@gmail.com1c246502015-02-25 13:47:27 +0000360 break;
361 }
Anestis Bechtsoudisccdf28c2016-01-10 15:04:22 +0200362 /* *INDENT-ON* */
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000363
robert.swiecki@gmail.com1c246502015-02-25 13:47:27 +0000364 newSz += delta;
robert.swiecki@gmail.com173f5192015-02-23 00:06:47 +0000365
robert.swiecki@gmail.com59526032015-02-23 17:10:29 +0000366 if (newSz < 1) {
367 newSz = 1;
robert.swiecki@gmail.com173f5192015-02-23 00:06:47 +0000368 }
robert.swiecki@gmail.com1c246502015-02-25 13:47:27 +0000369 if (newSz > (ssize_t) hfuzz->maxFileSz) {
370 newSz = (ssize_t) hfuzz->maxFileSz;
robert.swiecki@gmail.com173f5192015-02-23 00:06:47 +0000371 }
372
robert.swiecki@gmail.comace40862015-03-08 07:09:56 +0000373 if ((size_t) newSz > *bufSz) {
374 util_rndBuf(&buf[*bufSz], newSz - *bufSz);
375 }
376
Robert Swieckic8c32db2015-10-09 18:06:22 +0200377 LOG_D("Current size: %zu, Maximal size: %zu, New Size: %zu, Delta: %d", *bufSz,
378 hfuzz->maxFileSz, newSz, delta);
robert.swiecki@gmail.com173f5192015-02-23 00:06:47 +0000379
robert.swiecki@gmail.com173f5192015-02-23 00:06:47 +0000380 *bufSz = (size_t) newSz;
robert.swiecki@gmail.come7680522015-02-22 22:22:37 +0000381 return true;
robert.swiecki@gmail.coma3e014e2015-02-22 14:33:46 +0000382}