Coverage Report

Created: 2024-02-28 06:46

/src/tesseract/unittest/fuzzers/fuzzer-api.cpp
Line
Count
Source (jump to first uncovered line)
1
#include <allheaders.h>
2
#include <tesseract/baseapi.h>
3
4
#include <libgen.h> // for dirname
5
#include <cstdio>   // for printf
6
#include <cstdlib>  // for std::getenv, std::setenv
7
#include <string>   // for std::string
8
9
#ifndef TESSERACT_FUZZER_WIDTH
10
16.6k
#  define TESSERACT_FUZZER_WIDTH 100
11
#endif
12
13
#ifndef TESSERACT_FUZZER_HEIGHT
14
16.6k
#  define TESSERACT_FUZZER_HEIGHT 100
15
#endif
16
17
class BitReader {
18
private:
19
  uint8_t const *data;
20
  size_t size;
21
  size_t shift;
22
23
public:
24
16.6k
  BitReader(const uint8_t *data, size_t size) : data(data), size(size), shift(0) {}
25
26
1.20G
  int Read(void) {
27
1.20G
    if (size == 0) {
28
1.04G
      return 0;
29
1.04G
    }
30
31
167M
    const int ret = ((*data) >> shift) & 1;
32
33
167M
    shift++;
34
167M
    if (shift >= 8) {
35
20.9M
      shift = 0;
36
20.9M
      data++;
37
20.9M
      size--;
38
20.9M
    }
39
40
167M
    return ret;
41
1.20G
  }
42
};
43
44
static tesseract::TessBaseAPI *api = nullptr;
45
46
4
extern "C" int LLVMFuzzerInitialize(int * /*pArgc*/, char ***pArgv) {
47
4
  if (std::getenv("TESSDATA_PREFIX") == nullptr) {
48
2
    std::string binary_path = *pArgv[0];
49
2
    const std::string filepath = dirname(&binary_path[0]);
50
51
2
    const std::string tessdata_path = filepath + "/" + "tessdata";
52
2
    if (setenv("TESSDATA_PREFIX", tessdata_path.c_str(), 1) != 0) {
53
0
      printf("Setenv failed\n");
54
0
      std::abort();
55
0
    }
56
2
  }
57
58
4
  api = new tesseract::TessBaseAPI();
59
4
  if (api->Init(nullptr, "eng") != 0) {
60
0
    printf("Cannot initialize API\n");
61
0
    abort();
62
0
  }
63
64
  /* Silence output */
65
4
  api->SetVariable("debug_file", "/dev/null");
66
67
4
  return 0;
68
4
}
69
70
16.6k
static PIX *createPix(BitReader &BR, const size_t width, const size_t height) {
71
16.6k
  Pix *pix = pixCreate(width, height, 1);
72
73
16.6k
  if (pix == nullptr) {
74
0
    printf("pix creation failed\n");
75
0
    abort();
76
0
  }
77
78
5.22M
  for (size_t i = 0; i < width; i++) {
79
1.21G
    for (size_t j = 0; j < height; j++) {
80
1.20G
      pixSetPixel(pix, i, j, BR.Read());
81
1.20G
    }
82
5.21M
  }
83
84
16.6k
  return pix;
85
16.6k
}
86
87
33.3k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
88
33.3k
  BitReader BR(data, size);
89
90
33.3k
  auto pix = createPix(BR, TESSERACT_FUZZER_WIDTH, TESSERACT_FUZZER_HEIGHT);
91
92
33.3k
  api->SetImage(pix);
93
94
33.3k
  char *outText = api->GetUTF8Text();
95
96
33.3k
  pixDestroy(&pix);
97
33.3k
  delete[] outText;
98
99
33.3k
  return 0;
100
33.3k
}
LLVMFuzzerTestOneInput
Line
Count
Source
87
16.6k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
88
16.6k
  BitReader BR(data, size);
89
90
16.6k
  auto pix = createPix(BR, TESSERACT_FUZZER_WIDTH, TESSERACT_FUZZER_HEIGHT);
91
92
16.6k
  api->SetImage(pix);
93
94
16.6k
  char *outText = api->GetUTF8Text();
95
96
16.6k
  pixDestroy(&pix);
97
16.6k
  delete[] outText;
98
99
16.6k
  return 0;
100
16.6k
}
LLVMFuzzerTestOneInput
Line
Count
Source
87
16.6k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
88
16.6k
  BitReader BR(data, size);
89
90
16.6k
  auto pix = createPix(BR, TESSERACT_FUZZER_WIDTH, TESSERACT_FUZZER_HEIGHT);
91
92
16.6k
  api->SetImage(pix);
93
94
16.6k
  char *outText = api->GetUTF8Text();
95
96
16.6k
  pixDestroy(&pix);
97
16.6k
  delete[] outText;
98
99
16.6k
  return 0;
100
16.6k
}