xref: /aosp_15_r20/external/stg/fuzz/proto_reader_fuzzer.cc (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright 2022 Google LLC
5 //
6 // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7 // "License"); you may not use this file except in compliance with the
8 // License.  You may obtain a copy of the License at
9 //
10 //     https://llvm.org/LICENSE.txt
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 // Author: Matthias Maennich
19 
20 #include <cstddef>
21 #include <sstream>
22 #include <string_view>
23 
24 #include "error.h"
25 #include "graph.h"
26 #include "proto_reader.h"
27 #include "runtime.h"
28 
LLVMFuzzerTestOneInput(const char * data,size_t size)29 extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
30   try {
31     std::ostringstream os;
32     stg::Runtime runtime(os, false);
33     stg::Graph graph;
34     stg::proto::ReadFromString(runtime, graph, std::string_view(data, size));
35   } catch (const stg::Exception&) {
36     // Pass as this is us catching invalid proto properly.
37   }
38   return 0;
39 }
40