Coverage Report

Created: 2022-11-20 06:22

/src/exprtk_fuzzer.cpp
Line
Count
Source
1
// Copyright 2020 Google LLC
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 <cstdint>
16
#include <string>
17
18
#define exprtk_enable_range_runtime_checks
19
#include "exprtk.hpp"
20
21
22
template <typename T>
23
void run(const std::string& expression_string)
24
20.8k
{
25
20.8k
   typedef exprtk::symbol_table<T>    symbol_table_t;
26
20.8k
   typedef exprtk::expression<T>      expression_t;
27
20.8k
   typedef exprtk::parser<T>          parser_t;
28
20.8k
   typedef exprtk::loop_runtime_check loop_runtime_check_t;
29
30
20.8k
   T x = T(1.2345);
31
20.8k
   T y = T(2.2345);
32
20.8k
   T z = T(3.2345);
33
20.8k
   T w = T(4.2345);
34
35
20.8k
   symbol_table_t symbol_table;
36
20.8k
   symbol_table.add_variable("x",x);
37
20.8k
   symbol_table.add_variable("y",y);
38
20.8k
   symbol_table.add_variable("z",z);
39
20.8k
   symbol_table.add_variable("w",w);
40
20.8k
   symbol_table.add_constants();
41
42
20.8k
   expression_t expression;
43
20.8k
   expression.register_symbol_table(symbol_table);
44
45
20.8k
   loop_runtime_check_t loop_runtime_check;
46
20.8k
   loop_runtime_check.loop_set = loop_runtime_check_t::e_all_loops;
47
20.8k
   loop_runtime_check.max_loop_iterations = 100000;
48
49
20.8k
   parser_t parser;
50
51
20.8k
   parser.register_loop_runtime_check(loop_runtime_check);
52
53
20.8k
   if (parser.compile(expression_string, expression))
54
14.6k
   {
55
14.6k
      const std::size_t max_expression_size = 64 * 1024;
56
57
14.6k
      if (expression_string.size() <= max_expression_size)
58
14.3k
      {
59
14.3k
         try
60
14.3k
         {
61
14.3k
            expression.value();
62
14.3k
         }
63
14.3k
         catch (std::runtime_error& rte)
64
14.3k
         {}
65
66
14.3k
         parser.clear_loop_runtime_check();
67
14.3k
      }
68
14.6k
   }
69
20.8k
}
void run<double>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
24
10.4k
{
25
10.4k
   typedef exprtk::symbol_table<T>    symbol_table_t;
26
10.4k
   typedef exprtk::expression<T>      expression_t;
27
10.4k
   typedef exprtk::parser<T>          parser_t;
28
10.4k
   typedef exprtk::loop_runtime_check loop_runtime_check_t;
29
30
10.4k
   T x = T(1.2345);
31
10.4k
   T y = T(2.2345);
32
10.4k
   T z = T(3.2345);
33
10.4k
   T w = T(4.2345);
34
35
10.4k
   symbol_table_t symbol_table;
36
10.4k
   symbol_table.add_variable("x",x);
37
10.4k
   symbol_table.add_variable("y",y);
38
10.4k
   symbol_table.add_variable("z",z);
39
10.4k
   symbol_table.add_variable("w",w);
40
10.4k
   symbol_table.add_constants();
41
42
10.4k
   expression_t expression;
43
10.4k
   expression.register_symbol_table(symbol_table);
44
45
10.4k
   loop_runtime_check_t loop_runtime_check;
46
10.4k
   loop_runtime_check.loop_set = loop_runtime_check_t::e_all_loops;
47
10.4k
   loop_runtime_check.max_loop_iterations = 100000;
48
49
10.4k
   parser_t parser;
50
51
10.4k
   parser.register_loop_runtime_check(loop_runtime_check);
52
53
10.4k
   if (parser.compile(expression_string, expression))
54
7.36k
   {
55
7.36k
      const std::size_t max_expression_size = 64 * 1024;
56
57
7.36k
      if (expression_string.size() <= max_expression_size)
58
7.16k
      {
59
7.16k
         try
60
7.16k
         {
61
7.16k
            expression.value();
62
7.16k
         }
63
7.16k
         catch (std::runtime_error& rte)
64
7.16k
         {}
65
66
7.16k
         parser.clear_loop_runtime_check();
67
7.16k
      }
68
7.36k
   }
69
10.4k
}
void run<float>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
24
10.4k
{
25
10.4k
   typedef exprtk::symbol_table<T>    symbol_table_t;
26
10.4k
   typedef exprtk::expression<T>      expression_t;
27
10.4k
   typedef exprtk::parser<T>          parser_t;
28
10.4k
   typedef exprtk::loop_runtime_check loop_runtime_check_t;
29
30
10.4k
   T x = T(1.2345);
31
10.4k
   T y = T(2.2345);
32
10.4k
   T z = T(3.2345);
33
10.4k
   T w = T(4.2345);
34
35
10.4k
   symbol_table_t symbol_table;
36
10.4k
   symbol_table.add_variable("x",x);
37
10.4k
   symbol_table.add_variable("y",y);
38
10.4k
   symbol_table.add_variable("z",z);
39
10.4k
   symbol_table.add_variable("w",w);
40
10.4k
   symbol_table.add_constants();
41
42
10.4k
   expression_t expression;
43
10.4k
   expression.register_symbol_table(symbol_table);
44
45
10.4k
   loop_runtime_check_t loop_runtime_check;
46
10.4k
   loop_runtime_check.loop_set = loop_runtime_check_t::e_all_loops;
47
10.4k
   loop_runtime_check.max_loop_iterations = 100000;
48
49
10.4k
   parser_t parser;
50
51
10.4k
   parser.register_loop_runtime_check(loop_runtime_check);
52
53
10.4k
   if (parser.compile(expression_string, expression))
54
7.33k
   {
55
7.33k
      const std::size_t max_expression_size = 64 * 1024;
56
57
7.33k
      if (expression_string.size() <= max_expression_size)
58
7.14k
      {
59
7.14k
         try
60
7.14k
         {
61
7.14k
            expression.value();
62
7.14k
         }
63
7.14k
         catch (std::runtime_error& rte)
64
7.14k
         {}
65
66
7.14k
         parser.clear_loop_runtime_check();
67
7.14k
      }
68
7.33k
   }
69
10.4k
}
70
71
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
72
10.4k
{
73
10.4k
   const std::string expression(reinterpret_cast<const char*>(data), size);
74
75
10.4k
   run<double>(expression);
76
10.4k
   run<float> (expression);
77
78
10.4k
   return 0;
79
10.4k
}