1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// The fuzzer takes as input a buffer of bytes. The buffer is read in as:
// <angle>, <x_center>, <y_center>, and the remaining bytes will be read
// in as a <pix>. The image is then rotated by angle around the center. All
// inputs should not result in undefined behavior.
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include "leptonica/allheaders.h"
// Set to true only for debugging; always false for production
static const bool DebugOutput = false;
namespace {
// Reads the front bytes of a data buffer containing `size` bytes as an int16_t,
// and advances the buffer forward [if there is sufficient capacity]. If there
// is insufficient capacity, this returns 0 and does not modify size or data.
int16_t ReadInt16(const uint8_t** data, size_t* size) {
int16_t result = 0;
if (*size >= sizeof(result)) {
memcpy(&result, *data, sizeof(result));
*data += sizeof(result);
*size -= sizeof(result);
}
return result;
}
} // namespace
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
const int16_t angle = ReadInt16(&data, &size);
const int16_t x_center = ReadInt16(&data, &size);
const int16_t y_center = ReadInt16(&data, &size);
// Check for pnm format; this can cause timeouts.
// The format checker requires at least 12 bytes.
if (size < 12) return EXIT_SUCCESS;
int format;
findFileFormatBuffer(data, &format);
if (format == IFF_PNM) return EXIT_SUCCESS;
Pix* pix = pixReadMem(reinterpret_cast<const unsigned char*>(data), size);
if (pix == nullptr) {
return EXIT_SUCCESS;
}
// Never in production
if (DebugOutput) {
L_INFO("w = %d, h = %d, d = %d\n", "fuzzer",
pixGetWidth(pix), pixGetHeight(pix), pixGetDepth(pix));
}
constexpr float deg2rad = M_PI / 180.;
Pix* pix_rotated = pixRotateShear(pix, x_center, y_center, deg2rad * angle,
L_BRING_IN_WHITE);
if (pix_rotated) {
pixDestroy(&pix_rotated);
}
pixDestroy(&pix);
return EXIT_SUCCESS;
}
|