1*fd525a9cSAndroid Build Coastguard Worker // Copyright 2017 Google Inc. All rights reserved.
2*fd525a9cSAndroid Build Coastguard Worker //
3*fd525a9cSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*fd525a9cSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*fd525a9cSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*fd525a9cSAndroid Build Coastguard Worker //
7*fd525a9cSAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
8*fd525a9cSAndroid Build Coastguard Worker //
9*fd525a9cSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*fd525a9cSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*fd525a9cSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*fd525a9cSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*fd525a9cSAndroid Build Coastguard Worker // limitations under the License.
14*fd525a9cSAndroid Build Coastguard Worker
15*fd525a9cSAndroid Build Coastguard Worker #include "libxml/parser.h"
16*fd525a9cSAndroid Build Coastguard Worker #include "libxml/xmlsave.h"
17*fd525a9cSAndroid Build Coastguard Worker
18*fd525a9cSAndroid Build Coastguard Worker #include "examples/xml/xml.pb.h"
19*fd525a9cSAndroid Build Coastguard Worker #include "examples/xml/xml_writer.h"
20*fd525a9cSAndroid Build Coastguard Worker #include "src/libfuzzer/libfuzzer_macro.h"
21*fd525a9cSAndroid Build Coastguard Worker
22*fd525a9cSAndroid Build Coastguard Worker namespace {
23*fd525a9cSAndroid Build Coastguard Worker protobuf_mutator::protobuf::LogSilencer log_silincer;
ignore(void * ctx,const char * msg,...)24*fd525a9cSAndroid Build Coastguard Worker void ignore(void* ctx, const char* msg, ...) {}
25*fd525a9cSAndroid Build Coastguard Worker
26*fd525a9cSAndroid Build Coastguard Worker template <class T, class D>
MakeUnique(T * obj,D del)27*fd525a9cSAndroid Build Coastguard Worker std::unique_ptr<T, D> MakeUnique(T* obj, D del) {
28*fd525a9cSAndroid Build Coastguard Worker return {obj, del};
29*fd525a9cSAndroid Build Coastguard Worker }
30*fd525a9cSAndroid Build Coastguard Worker }
31*fd525a9cSAndroid Build Coastguard Worker
DEFINE_PROTO_FUZZER(const protobuf_mutator::xml::Input & message)32*fd525a9cSAndroid Build Coastguard Worker DEFINE_PROTO_FUZZER(const protobuf_mutator::xml::Input& message) {
33*fd525a9cSAndroid Build Coastguard Worker std::string xml = MessageToXml(message.document());
34*fd525a9cSAndroid Build Coastguard Worker int options = message.options();
35*fd525a9cSAndroid Build Coastguard Worker
36*fd525a9cSAndroid Build Coastguard Worker // Network requests are too slow.
37*fd525a9cSAndroid Build Coastguard Worker options |= XML_PARSE_NONET;
38*fd525a9cSAndroid Build Coastguard Worker // These flags can cause network or file access and hangs.
39*fd525a9cSAndroid Build Coastguard Worker options &= ~(XML_PARSE_NOENT | XML_PARSE_HUGE | XML_PARSE_DTDVALID |
40*fd525a9cSAndroid Build Coastguard Worker XML_PARSE_DTDLOAD | XML_PARSE_DTDATTR);
41*fd525a9cSAndroid Build Coastguard Worker
42*fd525a9cSAndroid Build Coastguard Worker xmlSetGenericErrorFunc(nullptr, &ignore);
43*fd525a9cSAndroid Build Coastguard Worker
44*fd525a9cSAndroid Build Coastguard Worker if (auto doc =
45*fd525a9cSAndroid Build Coastguard Worker MakeUnique(xmlReadMemory(xml.c_str(), static_cast<int>(xml.size()),
46*fd525a9cSAndroid Build Coastguard Worker "", nullptr, options),
47*fd525a9cSAndroid Build Coastguard Worker &xmlFreeDoc)) {
48*fd525a9cSAndroid Build Coastguard Worker auto buf = MakeUnique(xmlBufferCreate(), &xmlBufferFree);
49*fd525a9cSAndroid Build Coastguard Worker auto ctxt =
50*fd525a9cSAndroid Build Coastguard Worker MakeUnique(xmlSaveToBuffer(buf.get(), nullptr, 0), &xmlSaveClose);
51*fd525a9cSAndroid Build Coastguard Worker xmlSaveDoc(ctxt.get(), doc.get());
52*fd525a9cSAndroid Build Coastguard Worker }
53*fd525a9cSAndroid Build Coastguard Worker }
54