• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
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 <fuzzer/FuzzedDataProvider.h>
16 
17 #include <cstdint>
18 #include <cstdlib>
19 #include <string>
20 
21 #include "tensorflow/core/framework/attr_value.pb.h"
22 #include "tensorflow/core/framework/attr_value_util.h"
23 
24 // This is a fuzzer for tensorflow::ParseAttrValue.
25 
26 namespace {
27 using tensorflow::StringPiece;
28 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)29 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
30   // ParseAttrValue converts text protos into the types of attr_value.proto,
31   // which are string, int, float, bool, DataType, TensorShapeProto,
32   // TensorProto, NameAttrList, and list of any previously mentioned data type.
33 
34   // This fuzzer tests the ParseAttrValue's ability to not crash.
35   FuzzedDataProvider fuzzed_data(data, size);
36   tensorflow::AttrValue out;
37 
38   std::string type = fuzzed_data.PickValueInArray(
39       {"string", "int", "float", "bool", "type", "shape", "tensor",
40        "list(string)", "list(int)", "list(float)", "list(bool)", "list(type)",
41        "list(shape)", "list(tensor)", "list(list(string))", "list(list(int))",
42        "list(list(float))", "list(list(bool))", "list(list(type))",
43        "list(list(shape))", "list(list(tensor))",
44        // Invalid values
45        "invalid", "123"});
46 
47   std::string text_string = fuzzed_data.ConsumeRemainingBytesAsString();
48   tensorflow::ParseAttrValue(type, text_string, &out);
49 
50   return 0;
51 }
52 
53 }  // namespace
54