1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright 2021-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
22 #include <libxml/parser.h>
23 #include <libxml/tree.h>
24 #include <libxml/xmlerror.h>
25 #include "abigail_reader.h"
26 #include "error.h"
27 #include "graph.h"
28
29 namespace {
30
DoNothing(void *,const char *,...)31 void DoNothing(void*, const char*, ...) {}
32
33 } // namespace
34
LLVMFuzzerTestOneInput(const char * data,size_t size)35 extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
36 xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
37 // Suppress libxml error messages.
38 xmlSetGenericErrorFunc(ctxt, (xmlGenericErrorFunc) DoNothing);
39 xmlDocPtr document = xmlCtxtReadMemory(
40 ctxt, data, size, nullptr, nullptr,
41 XML_PARSE_NOERROR | XML_PARSE_NONET | XML_PARSE_NOWARNING);
42 xmlFreeParserCtxt(ctxt);
43
44 // Bail out if the document XML is invalid.
45 if (document == nullptr) {
46 return 0;
47 }
48
49 try {
50 stg::Graph graph;
51 stg::abixml::ProcessDocument(graph, document);
52 } catch (const stg::Exception&) {
53 // Pass as this is us catching invalid XML properly.
54 }
55
56 xmlFreeDoc(document);
57
58 return 0;
59 }
60