| 1 |
// Copyright 2022 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
|
| 18 |
#include <algorithm> |
| 19 |
#include <cstdint> |
| 20 |
#include <string> |
| 21 |
#include <vector> |
| 22 |
|
| 23 |
#include "absl/strings/str_cat.h" |
| 24 |
#include "absl/strings/str_join.h" |
| 25 |
#include "absl/types/optional.h" |
| 26 |
|
| 27 |
#include "src/core/ext/transport/chttp2/transport/decode_huff.h" |
| 28 |
#include "src/core/ext/transport/chttp2/transport/huffsyms.h" |
| 29 |
|
| 30 |
bool squelch = true; |
| 31 |
bool leak_check = true; |
| 32 |
|
| 33 |
absl::optional<std::vector<uint8_t>> DecodeHuffSlow(const uint8_t* begin, |
| 34 |
const uint8_t* end) { |
| 35 |
uint64_t bits = 0; |
| 36 |
size_t bits_left = 0u; |
| 37 |
std::vector<uint8_t> out; |
| 38 |
while (true) { |
| 39 |
while (begin != end && bits_left < 30) { |
| 40 |
bits <<= 8; |
| 41 |
bits |= begin[0]; |
| 42 |
++begin; |
| 43 |
bits_left += 8; |
| 44 |
} |
| 45 |
if (bits_left < 5) break; |
| 46 |
bool found = false; |
| 47 |
for (int i = 0; i < GRPC_CHTTP2_NUM_HUFFSYMS; i++) { |
| 48 |
const auto& sym = grpc_chttp2_huffsyms[i]; |
| 49 |
if (sym.length > bits_left) continue; |
| 50 |
if (((bits >> (bits_left - sym.length)) & ((1 << sym.length) - 1)) == |
| 51 |
sym.bits) { |
| 52 |
found = true; |
| 53 |
bits_left -= sym.length; |
| 54 |
if (i == 256) { |
| 55 |
return out; |
| 56 |
} |
| 57 |
out.push_back(i); |
| 58 |
break; |
| 59 |
} |
| 60 |
} |
| 61 |
if (!found) { |
| 62 |
break; |
| 63 |
} |
| 64 |
} |
| 65 |
while (bits_left > 0) { |
| 66 |
if ((bits & 1) == 0) { |
| 67 |
return absl::nullopt; |
| 68 |
} |
| 69 |
bits >>= 1; |
| 70 |
bits_left--; |
| 71 |
} |
| 72 |
return out; |
| 73 |
} |
| 74 |
|
| 75 |
std::string ToString(absl::optional<std::vector<uint8_t>> s) { |
| 76 |
if (s == absl::nullopt) return "nullopt"; |
| 77 |
return absl::StrCat("{", absl::StrJoin(*s, ","), "}"); |
| 78 |
} |
| 79 |
|
| 80 |
absl::optional<std::vector<uint8_t>> DecodeHuffFast(const uint8_t* begin, |
| 81 |
const uint8_t* end) { |
| 82 |
std::vector<uint8_t> v; |
| 83 |
auto f = [&](uint8_t x) { v.push_back(x); }; |
| 84 |
if (!grpc_core::HuffDecoder<decltype(f)>(f, begin, end).Run()) { |
| 85 |
return absl::nullopt; |
| 86 |
} |
| 87 |
return v; |
| 88 |
} |
| 89 |
|
| 90 |
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 91 |
auto slow = DecodeHuffSlow(data, data + size); |
| 92 |
auto fast = DecodeHuffFast(data, data + size); |
| 93 |
if (slow != fast) { |
| 94 |
fprintf(stderr, "MISMATCH:\ninpt: %s\nslow: %s\nfast: %s\n", |
| 95 |
ToString(std::vector<uint8_t>(data, data + size)).c_str(), |
| 96 |
ToString(slow).c_str(), ToString(fast).c_str()); |
| 97 |
abort(); |
| 98 |
} |
| 99 |
return 0; |
| 100 |
} |