Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tinyxml2] Update tinyxml2 fuzzer #11985

Merged
merged 7 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions projects/tinyxml2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ RUN apt-get update && apt-get install -y make autoconf automake libtool pkg-conf
RUN git clone --depth 1 https://github.com/leethomason/tinyxml2
WORKDIR tinyxml2

COPY build.sh $SRC/
COPY xmltest.* \
xml.dict $SRC/
COPY build.sh *.cpp *.dict *.options $SRC/
7 changes: 5 additions & 2 deletions projects/tinyxml2/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ sed -i 's/CXXFLAGS =/#CXXFLAGS/g' Makefile
make -j$(nproc) clean
make -j$(nproc) all

$CXX $CXXFLAGS -std=c++11 -Iinclude/ $SRC/xmltest.cpp -o $OUT/xmltest \
$LIB_FUZZING_ENGINE $SRC/tinyxml2/libtinyxml2.a
fuzz_harness=$(ls -d "$SRC"/*.cpp)
for h in $fuzz_harness; do
$CXX $CXXFLAGS -std=c++11 -Iinclude/ "$h" \
-o "$OUT/$(basename "$h")" $LIB_FUZZING_ENGINE $SRC/tinyxml2/libtinyxml2.a
done

cp $SRC/*.dict $SRC/*.options $OUT/
2 changes: 2 additions & 0 deletions projects/tinyxml2/project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: c++
primary_contact: "[email protected]"
auto_ccs:
- "[email protected]"
- "[email protected]"
sanitizers:
- address
- memory
Expand All @@ -13,4 +14,5 @@ fuzzing_engines:
- afl
- honggfuzz
- libfuzzer
- centipede

61 changes: 28 additions & 33 deletions projects/tinyxml2/xmltest.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
#if defined( _MSC_VER )
#if !defined( _CRT_SECURE_NO_WARNINGS )
#define _CRT_SECURE_NO_WARNINGS // This test file is not intended to be secure.
#endif
#endif

#include "tinyxml2/tinyxml2.h"
#include <string>
#include <stddef.h>
#include <stdint.h>

#if defined( _MSC_VER ) || defined (WIN32)
#include <crtdbg.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
_CrtMemState startMemState;
_CrtMemState endMemState;
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif

using namespace tinyxml2;
using namespace std;

// Entry point for LibFuzzer.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
std::string data_string(reinterpret_cast<const char*>(data), size);
XMLDocument doc;
doc.Parse( data_string.c_str() );

return 0;
}
/* Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "tinyxml2/tinyxml2.h"

#include <string>
#include <cstdint>

using namespace tinyxml2;
using namespace std;

// Entry point for LibFuzzer.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
std::string data_string(reinterpret_cast<const char*>(data), size);
XMLDocument doc;
doc.Parse( data_string.c_str() );

return 0;
}
38 changes: 38 additions & 0 deletions projects/tinyxml2/xmltest2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "tinyxml2/tinyxml2.h"

#include <string>
#include <cstdio>
#include <cstdint>
#include <cstdlib>

#include <unistd.h>

using namespace tinyxml2;
using namespace std;

// Entry point for LibFuzzer.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
char pathname[256];
sprintf(pathname, "/dev/shm/fuzz-%d", getpid());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this path always exist? /dev/shm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. Would it be better to modify it like this?

sprintf(filename, "/tmp/libfuzzer.%d", getpid());
FILE *fp = fopen(filename, "wb");

FILE *fp = fopen(pathname, "wb");
fwrite(data, size, 1, fp);
fclose(fp);

XMLDocument doc;
doc.LoadFile(pathname);

unlink(pathname);
return 0;
}
2 changes: 2 additions & 0 deletions projects/tinyxml2/xmltest2.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[libfuzzer]
dict = xml.dict
Loading