• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
21 
22 #include <libxml/parser.h>
23 #include <libxml/tree.h>
24 #include "abigail_reader.h"
25 #include "error.h"
26 #include "graph.h"
27 
DoNothing(void *,const char *,...)28 static void DoNothing(void*, const char*, ...) {}
29 
LLVMFuzzerTestOneInput(char * data,size_t size)30 extern "C" int LLVMFuzzerTestOneInput(char* data, size_t size) {
31   xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
32   // Suppress libxml error messages.
33   xmlSetGenericErrorFunc(ctxt, (xmlGenericErrorFunc) DoNothing);
34   xmlDocPtr doc = xmlCtxtReadMemory(
35       ctxt, data, size, nullptr, nullptr,
36       XML_PARSE_NOERROR | XML_PARSE_NONET | XML_PARSE_NOWARNING);
37   xmlFreeParserCtxt(ctxt);
38 
39   // Bail out if the doc XML is invalid.
40   if (!doc) {
41     return 0;
42   }
43 
44   xmlNodePtr root = xmlDocGetRootElement(doc);
45   if (root) {
46     try {
47       stg::Graph graph;
48       stg::abixml::Abigail(graph).ProcessRoot(root);
49     } catch (const stg::Exception&) {
50       // Pass as this is us catching invalid XML properly.
51     }
52   }
53 
54   xmlFreeDoc(doc);
55 
56   return 0;
57 }
58