Commit 94638256 authored by Sam Kerr's avatar Sam Kerr 🎙
Browse files

Add fuzz testing

parent 2093fbe0
......@@ -3,6 +3,7 @@ image: gcc
stages:
- build
- test
- fuzz
build_wizard:
stage: build
......@@ -85,3 +86,22 @@ create_project:
- cmake .
- make all
- cd ..
include:
- template: Coverage-Fuzzing.gitlab-ci.yml
fuzz_target:
extends: .fuzz_base
image: ubuntu:18.04
variables:
CI_SEED_CORPUS: './fuzz_tests/corpus'
script:
- apt-get update -qq && apt-get install -y -qq git make clang cmake
- apt-get install --yes cmake clang libasound2-dev libglu1-mesa-dev freeglut3-dev libglfw3-dev libgles2-mesa-dev xvfb
- export CC=`which clang`
- export CXX=`which clang++`
- export VERBOSE=1
- mkdir fuzz_tests/corpus && cp wizard/data/*.wav fuzz_tests/corpus
- mkdir -p build && cd build
- cmake ../fuzz_tests -DCMAKE_CXX_FLAGS='-D_GLIBCXX_USE_CXX11_ABI=0' -DCMAKE_BUILD_TYPE=AddressSanitizer && make all && cd ..
- ./gitlab-cov-fuzz run --regression=$REGRESSION -- ./build/fuzz_harness
......@@ -44,6 +44,7 @@ tl;drLegal: [https://www.tldrlegal.com/l/mit](https://www.tldrlegal.com/l/mit)
* Miniz 2.1.0 (01/06/2019) Copyright 2013-2014 RAD Game Tools and Valve Software, Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC ([https://github.com/richgel999/miniz](https://github.com/richgel999/miniz))
* OpenFBX (01/06/2019) by Mikulas Florek ([https://github.com/nem0/OpenFBX](https://github.com/nem0/OpenFBX))
* Collection of wait-free/lock-free queues (18/07/2020) Copyright (c) 2018 Vitaliy Manushkin ([https://gitlab.com/agrianius/mt_queue](https://gitlab.com/agrianius/mt_queue))
* [Coverage-guided fuzz testing](https://docs.gitlab.com/ee/user/application_security/coverage_fuzzing/#coverage-guided-fuzz-testing-ultimate) added by @stkerr at GitLab
#### ArcticOne font:
......
......@@ -31,8 +31,14 @@
#include <iostream>
#ifdef ARCTIC_NO_HARD_EXIT
#include <setjmp.h>
extern jmp_buf arctic_jmp_env;
#endif
namespace arctic {
void Fatal(const char *message, const char *message_postfix) {
size_t size = 1 +
strlen(message) +
......@@ -41,8 +47,15 @@ void Fatal(const char *message, const char *message_postfix) {
memset(full_message, 0, size);
snprintf(full_message, size, "%s%s", message,
(message_postfix ? message_postfix : ""));
#ifndef ARCTIC_NO_FATAL_MESSAGES
std::cerr << "Arctic Engine ERROR: " << full_message << std::endl;
#endif
#ifndef ARCTIC_NO_HARD_EXIT
exit(1);
#else
free(full_message);
longjmp(arctic_jmp_env, 1337);
#endif
}
void Check(bool condition, const char *error_message,
......
......@@ -241,7 +241,7 @@ void SetCursorVisible(bool/* is_enable*/) {
} // namespace arctic
#ifndef ARCTIC_NO_MAIN
int main(int argc, char **argv) {
arctic::SystemInfo system_info;
......@@ -262,5 +262,6 @@ int main(int argc, char **argv) {
return 0;
}
#endif // ARCTIC_NO_MAIN
#endif // ARCTIC_PLATFORM_PI_OPENGL_GLX
cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
################### Variables. ####################
# Change if you want modify path or other values. #
###################################################
set(CMAKE_MACOSX_BUNDLE 1)
# Define Release by default.
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
message(STATUS "Build type not specified: defaulting to release.")
endif(NOT CMAKE_BUILD_TYPE)
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}.")
set(PROJECT_NAME fuzz_harness)
# Output Variables
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
# Folders files
set(DATA_DIR .)
set(CPP_DIR_1 ../engine)
set(CPP_DIR_2 .)
set(HEADER_DIR_1 ../engine)
set(HEADER_DIR_2 .)
file(GLOB_RECURSE RES_SOURCES "${DATA_DIR}/data/*")
SET(CMAKE_CXX_COMPILER "/usr/bin/clang++")
set(CMAKE_CXX_STANDARD 14)
set(THREADS_PREFER_PTHREAD_FLAG ON)
############## Define Project. ###############
# ---- This the main options of project ---- #
##############################################
project(${PROJECT_NAME} CXX)
ENABLE_LANGUAGE(C)
IF (APPLE)
FIND_LIBRARY(AUDIOTOOLBOX AudioToolbox)
FIND_LIBRARY(COREAUDIO CoreAudio)
FIND_LIBRARY(COREFOUNDATION CoreFoundation)
FIND_LIBRARY(COCOA Cocoa)
FIND_LIBRARY(OPENGL OpenGL)
ELSE (APPLE)
find_package(ALSA REQUIRED)
find_library(EGL_LIBRARY NAMES EGL)
find_path(EGL_INCLUDE_DIR EGL/egl.h)
find_library(GLES_LIBRARY NAMES GLESv2)
find_path(GLES_INCLUDE_DIR GLES/gl.h)
IF (EGL_LIBRARY AND EGL_INCLUDE_DIR AND GLES_LIBRARY AND GLES_INCLUDE_DIR)
message(STATUS "GLES EGL mode")
set(EGL_MODE "EGL")
ELSE ()
message(STATUS "OPENGL GLX mode")
ENDIF()
IF (NOT EGL_MODE)
#only for opengl glx
set (OpenGL_GL_PREFERENCE "LEGACY")
find_package(OpenGL REQUIRED)
ENDIF (NOT EGL_MODE)
find_package(X11 REQUIRED)
find_package(Threads REQUIRED)
ENDIF (APPLE)
# Definition of Macros
#-D_DEBUG
IF (APPLE)
add_definitions(
-DGL_SILENCE_DEPRECATION
)
ELSE (APPLE)
IF (EGL_MODE)
#only for es egl
add_definitions(
-DPLATFORM_RPI
)
ELSE (EGL_MODE)
#only for opengl glx
add_definitions(
-DPLATFORM_LINUX
)
ENDIF (EGL_MODE)
add_definitions(
-DGLX
-DGL_GLEXT_PROTOTYPES
)
ENDIF (APPLE)
include_directories(${CMAKE_SOURCE_DIR}/..)
################# Flags ################
# Defines Flags for Windows and Linux. #
########################################
message(STATUS "CompilerId: ${CMAKE_CXX_COMPILER_ID}.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -O2")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang++" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_STATIC_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
endif()
IF (EGL_MODE)
#only for es egl
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lGLESv2 -lEGL")
ENDIF (EGL_MODE)
################ Files ################
# -- Add files to project. -- #
#######################################
IF (APPLE)
file(GLOB SRC_FILES
${CPP_DIR_1}/*.cpp
${CPP_DIR_1}/*.mm
${CPP_DIR_1}/*.c
${CPP_DIR_2}/*.cpp
${CPP_DIR_2}/*.c
${HEADER_DIR_1}/*.h
${HEADER_DIR_1}/*.hpp
${HEADER_DIR_2}/*.h
${HEADER_DIR_2}/*.hpp
)
ELSE (APPLE)
file(GLOB SRC_FILES
${CPP_DIR_1}/*.cpp
${CPP_DIR_1}/*.c
${CPP_DIR_2}/*.cpp
${CPP_DIR_2}/*.c
${HEADER_DIR_1}/*.h
${HEADER_DIR_1}/*.hpp
${HEADER_DIR_2}/*.h
${HEADER_DIR_2}/*.hpp
)
ENDIF (APPLE)
file(GLOB SRC_FILES_TO_REMOVE
${CPP_DIR_1}/arctic_platform_pi.cpp
${CPP_DIR_1}/byte_array.cpp
${HEADER_DIR_1}/byte_array.h
)
list(REMOVE_ITEM SRC_FILES ${SRC_FILES_TO_REMOVE})
macro(fuzzer name)
add_executable(${PROJECT_NAME} ${name}.cpp ${SRC_FILES} ${RES_SOURCES})
set_target_properties(${PROJECT_NAME}
PROPERTIES
COMPILE_FLAGS "-fsanitize=fuzzer -DARCTIC_NO_MAIN -DARCTIC_NO_HARD_EXIT -DARCTIC_NO_FATAL_MESSAGES"
LINK_FLAGS "-fsanitize=fuzzer")
target_include_directories(${PROJECT_NAME}
PRIVATE
../src)
endmacro()
fuzzer( fuzz_harness )
# Add executable to build.
SET_SOURCE_FILES_PROPERTIES(${RES_SOURCES} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/data)
IF (APPLE)
target_link_libraries(
${PROJECT_NAME}
${AUDIOTOOLBOX}
${COREAUDIO}
${COREFOUNDATION}
${COCOA}
${OPENGL}
)
ELSE (APPLE)
target_link_libraries(
${PROJECT_NAME}
${OPENGL_gl_LIBRARY}
${X11_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
${ALSA_LIBRARY}
# ${EGL_LIBRARY}
# ${GLES_LIBRARY}
)
ENDIF (APPLE)
#include <engine/easy.h>
#include <engine/easy_sound_instance.h>
#include <engine/arctic_types.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
jmp_buf arctic_jmp_env;
extern "C" int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size) {
void* fake_wav = calloc(size, 1);
memcpy((char*)fake_wav, data, size);
int val = setjmp(arctic_jmp_env);
if(val == 1337)
{
free(fake_wav);
return 0;
}
std::shared_ptr<arctic::easy::SoundInstance> result = arctic::easy::LoadWav((arctic::Ui8*)fake_wav, size);
free(fake_wav);
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment