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