/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  | 15.4k  | #  define TESSERACT_FUZZER_WIDTH 100  | 
11  |  | #endif  | 
12  |  |  | 
13  |  | #ifndef TESSERACT_FUZZER_HEIGHT  | 
14  | 15.4k  | #  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  | 15.4k  |   BitReader(const uint8_t *data, size_t size) : data(data), size(size), shift(0) {} | 
25  |  |  | 
26  | 1.09G  |   int Read(void) { | 
27  | 1.09G  |     if (size == 0) { | 
28  | 945M  |       return 0;  | 
29  | 945M  |     }  | 
30  |  |  | 
31  | 144M  |     const int ret = ((*data) >> shift) & 1;  | 
32  |  |  | 
33  | 144M  |     shift++;  | 
34  | 144M  |     if (shift >= 8) { | 
35  | 18.1M  |       shift = 0;  | 
36  | 18.1M  |       data++;  | 
37  | 18.1M  |       size--;  | 
38  | 18.1M  |     }  | 
39  |  |  | 
40  | 144M  |     return ret;  | 
41  | 1.09G  |   }  | 
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  | 15.4k  | static PIX *createPix(BitReader &BR, const size_t width, const size_t height) { | 
71  | 15.4k  |   Pix *pix = pixCreate(width, height, 1);  | 
72  |  |  | 
73  | 15.4k  |   if (pix == nullptr) { | 
74  | 0  |     printf("pix creation failed\n"); | 
75  | 0  |     abort();  | 
76  | 0  |   }  | 
77  |  |  | 
78  | 4.74M  |   for (size_t i = 0; i < width; i++) { | 
79  | 1.09G  |     for (size_t j = 0; j < height; j++) { | 
80  | 1.09G  |       pixSetPixel(pix, i, j, BR.Read());  | 
81  | 1.09G  |     }  | 
82  | 4.73M  |   }  | 
83  |  |  | 
84  | 15.4k  |   return pix;  | 
85  | 15.4k  | }  | 
86  |  |  | 
87  | 30.9k  | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | 
88  | 30.9k  |   BitReader BR(data, size);  | 
89  |  |  | 
90  | 30.9k  |   auto pix = createPix(BR, TESSERACT_FUZZER_WIDTH, TESSERACT_FUZZER_HEIGHT);  | 
91  |  |  | 
92  | 30.9k  |   api->SetImage(pix);  | 
93  |  |  | 
94  | 30.9k  |   char *outText = api->GetUTF8Text();  | 
95  |  |  | 
96  | 30.9k  |   pixDestroy(&pix);  | 
97  | 30.9k  |   delete[] outText;  | 
98  |  |  | 
99  | 30.9k  |   return 0;  | 
100  | 30.9k  | } Line  | Count  | Source  |  87  | 15.4k  | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |  88  | 15.4k  |   BitReader BR(data, size);  |  89  |  |  |  90  | 15.4k  |   auto pix = createPix(BR, TESSERACT_FUZZER_WIDTH, TESSERACT_FUZZER_HEIGHT);  |  91  |  |  |  92  | 15.4k  |   api->SetImage(pix);  |  93  |  |  |  94  | 15.4k  |   char *outText = api->GetUTF8Text();  |  95  |  |  |  96  | 15.4k  |   pixDestroy(&pix);  |  97  | 15.4k  |   delete[] outText;  |  98  |  |  |  99  | 15.4k  |   return 0;  |  100  | 15.4k  | }  |  
 Line  | Count  | Source  |  87  | 15.4k  | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |  88  | 15.4k  |   BitReader BR(data, size);  |  89  |  |  |  90  | 15.4k  |   auto pix = createPix(BR, TESSERACT_FUZZER_WIDTH, TESSERACT_FUZZER_HEIGHT);  |  91  |  |  |  92  | 15.4k  |   api->SetImage(pix);  |  93  |  |  |  94  | 15.4k  |   char *outText = api->GetUTF8Text();  |  95  |  |  |  96  | 15.4k  |   pixDestroy(&pix);  |  97  | 15.4k  |   delete[] outText;  |  98  |  |  |  99  | 15.4k  |   return 0;  |  100  | 15.4k  | }  |  
  |