Debian Codegraph @

contrib/fuzz/mpatch.cc

raw
1 /*
2 * mpatch.cc - fuzzer harness for mpatch.c
3 *
4 * Copyright 2018, Google Inc.
5 *
6 * This software may be used and distributed according to the terms of
7 * the GNU General Public License, incorporated herein by reference.
8 */
9 #include <iostream>
10 #include <memory>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <vector>
14
15 #include "fuzzutil.h"
16
17 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
18 {
19 return 0;
20 }
21
22 // To avoid having too many OOMs from the fuzzer infrastructure, we'll
23 // skip patch application if the resulting fulltext would be bigger
24 // than 10MiB.
25 #define MAX_OUTPUT_SIZE 10485760
26
27 extern "C" {
28 #include "bitmanipulation.h"
29 #include "mpatch.h"
30
31 struct mpatchbin {
32 std::unique_ptr<char[]> data;
33 size_t len;
34 };
35
36 static mpatch_flist *getitem(void *vbins, ssize_t pos)
37 {
38 std::vector<mpatchbin> *bins = (std::vector<mpatchbin> *)vbins;
39 const mpatchbin &bin = bins->at(pos + 1);
40 struct mpatch_flist *res;
41 LOG(2) << "mpatch_decode " << bin.len << std::endl;
42 if (mpatch_decode(bin.data.get(), bin.len, &res) < 0)
43 return NULL;
44 return res;
45 }
46
47 // input format:
48 // u8 number of inputs
49 // one u16 for each input, its length
50 // the inputs
51 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
52 {
53 if (!Size) {
54 return 0;
55 }
56 // First byte of data is how many texts we expect, first text
57 // being the base the rest being the deltas.
58 ssize_t numtexts = Data[0];
59 if (numtexts < 2) {
60 // No point if we don't have at least a base text and a delta...
61 return 0;
62 }
63 // Each text will be described by a byte for how long it
64 // should be, so give up if we don't have enough.
65 if ((Size - 1) < (numtexts * 2)) {
66 return 0;
67 }
68 size_t consumed = 1 + (numtexts * 2);
69 LOG(2) << "input contains " << Size << std::endl;
70 LOG(2) << numtexts << " texts, consuming " << consumed << std::endl;
71 std::vector<mpatchbin> bins;
72 bins.reserve(numtexts);
73 for (int i = 0; i < numtexts; ++i) {
74 mpatchbin bin;
75 size_t nthsize = getbeuint16((char *)Data + 1 + (2 * i));
76 LOG(2) << "text " << i << " is " << nthsize << std::endl;
77 char *start = (char *)Data + consumed;
78 consumed += nthsize;
79 if (consumed > Size) {
80 LOG(2) << "ran out of data, consumed " << consumed
81 << " of " << Size << std::endl;
82 return 0;
83 }
84 bin.len = nthsize;
85 bin.data.reset(new char[nthsize]);
86 memcpy(bin.data.get(), start, nthsize);
87 bins.push_back(std::move(bin));
88 }
89 LOG(2) << "mpatch_flist" << std::endl;
90 struct mpatch_flist *patch =
91 mpatch_fold(&bins, getitem, 0, numtexts - 1);
92 if (!patch) {
93 return 0;
94 }
95 LOG(2) << "mpatch_calcsize" << std::endl;
96 ssize_t outlen = mpatch_calcsize(bins[0].len, patch);
97 LOG(2) << "outlen " << outlen << std::endl;
98 if (outlen < 0 || outlen > MAX_OUTPUT_SIZE) {
99 goto cleanup;
100 }
101 {
102 char *dest = (char *)malloc(outlen);
103 LOG(2) << "expecting " << outlen << " total bytes at "
104 << (void *)dest << std::endl;
105 mpatch_apply(dest, bins[0].data.get(), bins[0].len, patch);
106 free(dest);
107 LOG(1) << "applied a complete patch" << std::endl;
108 }
109 cleanup:
110 mpatch_lfree(patch);
111 return 0;
112 }
113
114 } // extern "C"