/src/libwebp/tests/fuzzer/dec_fuzzer.cc
Line | Count | Source |
1 | | // Copyright 2024 Google Inc. |
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 | | //////////////////////////////////////////////////////////////////////////////// |
16 | | |
17 | | #include <cstdint> |
18 | | #include <cstdio> |
19 | | #include <cstdlib> |
20 | | #include <cstring> |
21 | | #include <string_view> |
22 | | |
23 | | #include "./nalloc.h" |
24 | | #include "src/webp/decode.h" |
25 | | #include "tests/fuzzer/fuzz_utils.h" |
26 | | |
27 | | namespace { |
28 | | |
29 | | void DecodeWebP(std::string_view arbitrary_bytes, |
30 | 3.76k | const fuzz_utils::WebPDecoderOptionsCpp& decoder_options) { |
31 | 3.76k | WebPDecoderConfig decoder_config; |
32 | 3.76k | if (!WebPInitDecoderConfig(&decoder_config)) { |
33 | 0 | fprintf(stderr, "WebPInitDecoderConfig failed.\n"); |
34 | 0 | std::abort(); |
35 | 0 | } |
36 | 3.76k | nalloc_init(nullptr); |
37 | 3.76k | nalloc_start(reinterpret_cast<const uint8_t*>(arbitrary_bytes.data()), |
38 | 3.76k | arbitrary_bytes.size()); |
39 | 3.76k | std::memcpy(&decoder_config.options, &decoder_options, |
40 | 3.76k | sizeof(decoder_options)); |
41 | 3.76k | const VP8StatusCode status = |
42 | 3.76k | WebPDecode(reinterpret_cast<const uint8_t*>(arbitrary_bytes.data()), |
43 | 3.76k | arbitrary_bytes.size(), &decoder_config); |
44 | 3.76k | WebPFreeDecBuffer(&decoder_config.output); |
45 | | // The decoding may fail (because the fuzzed input can be anything) but not |
46 | | // for these reasons. |
47 | 3.76k | if (status == VP8_STATUS_SUSPENDED || status == VP8_STATUS_USER_ABORT) { |
48 | 0 | std::abort(); |
49 | 0 | } |
50 | 3.76k | nalloc_end(); |
51 | 3.76k | } |
52 | | |
53 | | FUZZ_TEST(WebPSuite, DecodeWebP) |
54 | | .WithDomains(fuzztest::String().WithMaxSize(fuzz_utils::kMaxWebPFileSize + |
55 | | 1), |
56 | | fuzz_utils::ArbitraryValidWebPDecoderOptions()); |
57 | | |
58 | | } // namespace |