• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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 
20 #include "tensorflow/core/platform/status.h"
21 
22 // This is a fuzzer for `tensorflow::StatusGroup`. Since `Status` is used almost
23 // everywhere, we need to ensure that the common functionality is safe. We don't
24 // expect many crashes from this fuzzer
25 
26 namespace {
27 
BuildRandomErrorCode(uint32_t code)28 tensorflow::error::Code BuildRandomErrorCode(uint32_t code) {
29   // We cannot build a `Status` with error_code of 0 and a message, so force
30   // error code to be non-zero.
31   if (code == 0) {
32     return tensorflow::error::UNKNOWN;
33   }
34 
35   return static_cast<tensorflow::error::Code>(code);
36 }
37 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)38 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
39   const std::string error_message = "ERROR";
40   tensorflow::StatusGroup sg;
41   FuzzedDataProvider fuzzed_data(data, size);
42 
43   while (fuzzed_data.remaining_bytes() > 0) {
44     uint32_t code = fuzzed_data.ConsumeIntegral<uint32_t>();
45     tensorflow::error::Code error_code = BuildRandomErrorCode(code);
46     bool is_derived = fuzzed_data.ConsumeBool();
47 
48     tensorflow::Status s = tensorflow::Status(error_code, error_message);
49 
50     if (is_derived) {
51       tensorflow::Status derived_s = tensorflow::StatusGroup::MakeDerived(s);
52       sg.Update(derived_s);
53     } else {
54       sg.Update(s);
55     }
56   }
57 
58   // Ignore warnings that these values are unused
59   sg.as_summary_status().IgnoreError();
60   sg.as_concatenated_status().IgnoreError();
61   sg.AttachLogMessages();
62 
63   return 0;
64 }
65 
66 }  // namespace
67