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

CMake build, and Fuzzing via afl #151

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8

[*.{c,h}]
indent_style = tab
indent_size = 4
trim_trailing_whitespace = true

62 changes: 62 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
cmake_minimum_required(VERSION 3.0)

project(nanosvg LANGUAGES C VERSION "0.0.0")

set(NANOSVG_HEADERS "src/nanosvg.h" "src/nanosvgrast.h")

add_library(nanosvg INTERFACE)

target_include_directories(
nanosvg
INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>"
)

# Build examples
add_subdirectory(example)

# Introduce variables:
# * CMAKE_INSTALL_INCLUDEDIR
include(GNUInstallDirs)

set(GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated")
set(VERSION_CONFIG "${GENERATED_DIR}/${PROJECT_NAME}ConfigVersion.cmake")
set(PROJECT_CONFIG "${GENERATED_DIR}/${PROJECT_NAME}Config.cmake")

set(CONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

set(TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
set(NAMESPACE "${PROJECT_NAME}::")

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${VERSION_CONFIG}" COMPATIBILITY ExactVersion
)

configure_package_config_file(
"cmake/Config.cmake.in"
"${PROJECT_CONFIG}"
INSTALL_DESTINATION "${CONFIG_INSTALL_DIR}"
)

install(
TARGETS nanosvg
EXPORT "${TARGETS_EXPORT_NAME}"
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

install(
FILES ${NANOSVG_HEADERS}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
)

install(
FILES "${PROJECT_CONFIG}" "${VERSION_CONFIG}"
DESTINATION "${CONFIG_INSTALL_DIR}"
)

install(
EXPORT "${TARGETS_EXPORT_NAME}"
NAMESPACE "${NAMESPACE}"
DESTINATION "${CONFIG_INSTALL_DIR}"
)
16 changes: 16 additions & 0 deletions cmake/Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2016, Ruslan Baratov
#
# Licensed under the MIT License (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# http://opensource.org/licenses/MIT
#
# 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.

@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
check_required_components("@PROJECT_NAME@")
58 changes: 58 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

option (BUILD_FUZZ "Build binaries with tooling for fuzzing." OFF)

include_directories(${CMAKE_SOURCE_DIR}/src)
include_directories(${CMAKE_SOURCE_DIR}/example)

find_package(PkgConfig REQUIRED)
#pkg_search_module(GLFW REQUIRED glfw3)
#
add_compile_options(-O2 -Wall)
find_package(OpenGL REQUIRED)
find_package(glfw3 3.3 REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS})

add_executable(dump dump.c)
add_executable(example1 example1.c)
add_executable(example2 example2.c)

target_link_libraries(dump m )
target_link_libraries(example1 m ${OPENGL_LIBRARIES} glfw)
target_link_libraries(example2 m ${OPENGL_LIBRARIES} glfw)

#
# if fuzzing is enabled
#
if (BUILD_FUZZ)
include (ExternalProject)
#
# Afl
#
ExternalProject_Add (aflX
PREFIX aflX
URL http://lcamtuf.coredump.cx/afl/releases/afl-2.52b.tgz
CONFIGURE_COMMAND ""
BUILD_COMMAND "make"
BUILD_IN_SOURCE 1
INSTALL_COMMAND ""
LOG_DOWNLOAD ON
)
ExternalProject_Get_Property (aflX source_dir)
set (AFL_DIR "${source_dir}")
message ("American Fuzzy Lop in ${AFL_DIR}")
#set (CMAKE_C_COMPILER ${AFL_DIR}/afl-gcc)
#set (CMAKE_CXX_COMPILER ${AFL_DIR}/afl-g++)
set (CMAKE_C_COMPILER ${AFL_DIR}/afl-clang)
set (CMAKE_CXX_COMPILER ${AFL_DIR}/afl-clang++)

add_dependencies (example1 aflX)
add_dependencies (example2 aflX)

add_custom_target (fuzz
COMMAND mkdir -p fuzz-data
COMMAND cp nano.svg fuzz-data/
COMMAND ${AFL_DIR}/afl-fuzz -i fuzz-data -o fuzz-out -x fuzz.dict $<TARGET_FILE:example2> @@ ''
)
add_dependencies (fuzz example1)

endif (BUILD_FUZZ)
65 changes: 65 additions & 0 deletions example/dump.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// Copyright (c) 2017 Michael Tesch [email protected]
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//

#include <stdio.h>
#include <string.h>
#include <float.h>

#define NANOSVG_IMPLEMENTATION
#include "nanosvg.h"

NSVGimage* g_image = NULL;

int main(int argc, char *argv[])
{
for (int arg = 1; arg < argc; arg++) {
const char* filename = argv[arg];

g_image = nsvgParseFromFile(filename, "px", 96.0f);
if (g_image == NULL) {
printf("Could not open SVG image '%s'.\n", filename);
return -1;
}

printf("%s:\n", filename);
printf("size: %f x %f.\n", g_image->width, g_image->height);

#if 0
for (NSVGgroup* group = g_image->groups; group != NULL; group = group->next) {
printf("group: %s parent:%s\n", group->id, group->parent ? group->parent->id : "-");
}
#endif

for (NSVGshape* shape = g_image->shapes; shape != NULL; shape = shape->next) {
printf("shape: '%s' visible:%d\n", shape->id, 0 != (shape->flags & NSVG_FLAGS_VISIBLE));
#if 0
if (shape->group)
printf(" : group '%s'\n", shape->group->id);
#endif
for (NSVGpath* path = shape->paths; path != NULL; path = path->next) {
//drawPath(path->pts, path->npts, path->closed, px * 1.5f);
printf(" npts: %d [f:%d] [%f %f %f %f]\n", path->npts, 0,
path->bounds[0], path->bounds[1], path->bounds[2], path->bounds[3]);
}
}

nsvgDelete(g_image);
}

return 0;
}
14 changes: 10 additions & 4 deletions example/example1.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <GLFW/glfw3.h>

#define NANOSVG_IMPLEMENTATION
#define NANOSVG_ALL_COLOR_KEYWORDS
#include "nanosvg.h"

NSVGimage* g_image = NULL;
Expand Down Expand Up @@ -215,7 +216,7 @@ void resizecb(GLFWwindow* window, int width, int height)
drawframe(window);
}

int main()
int main(int argc, char *argv[])
{
GLFWwindow* window;
const GLFWvidmode* mode;
Expand All @@ -224,7 +225,7 @@ int main()
return -1;

mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
window = glfwCreateWindow(mode->width - 40, mode->height - 80, "Nano SVG", NULL, NULL);
window = glfwCreateWindow(mode->width - 40, mode->height - 80, "Nano SVG", NULL, NULL);
if (!window)
{
printf("Could not open window\n");
Expand All @@ -238,13 +239,18 @@ int main()
glEnable(GL_LINE_SMOOTH);


g_image = nsvgParseFromFile("../example/nano.svg", "px", 96.0f);
const char* filename = "../example/nano.svg";
if (argc > 1)
filename = argv[1];
g_image = nsvgParseFromFile(filename, "px", 96.0f);
if (g_image == NULL) {
printf("Could not open SVG image.\n");
printf("Could not open SVG image '%s'.\n", filename);
glfwTerminate();
return -1;
}

printf("size: %f x %f.\n", g_image->width, g_image->height);

while (!glfwWindowShouldClose(window))
{
drawframe(window);
Expand Down
15 changes: 12 additions & 3 deletions example/example2.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,24 @@
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define NANOSVG_IMPLEMENTATION
#define NANOSVG_ALL_COLOR_KEYWORDS
#include "nanosvg.h"
#define NANOSVGRAST_IMPLEMENTATION
#include "nanosvgrast.h"

int main()
int main(int argc, char *argv[])
{
NSVGimage *image = NULL;
NSVGrasterizer *rast = NULL;
unsigned char* img = NULL;
int w, h;
const char* filename = "../example/23.svg";
const char* out_filename = "svg.png";

if (argc > 1)
filename = argv[1];
if (argc > 2)
out_filename = argv[2];

printf("parsing %s\n", filename);
image = nsvgParseFromFile(filename, "px", 96.0f);
Expand All @@ -58,8 +65,10 @@ int main()
printf("rasterizing image %d x %d\n", w, h);
nsvgRasterize(rast, image, 0,0,1, img, w, h, w*4);

printf("writing svg.png\n");
stbi_write_png("svg.png", w, h, 4, img, w*4);
if (out_filename[0]) {
printf("writing %s\n", out_filename);
stbi_write_png(out_filename, w, h, 4, img, w*4);
}

error:
nsvgDeleteRasterizer(rast);
Expand Down
77 changes: 77 additions & 0 deletions example/fuzz.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#
# AFL dictionary for svg parsers
#

tag_a="<a>"
tag_animate="<animate>"
tag_animateMotion="<animateMotion>"
tag_animateTransform="<animateTransform>"
tag_circle="<circle>"
tag_clipPath="<clipPath>"
tag_color_profile="<color-profile>"
tag_defs="<defs>"
tag_desc="<desc>"
tag_discard="<discard>"
tag_ellipse="<ellipse>"
tag_feBlend="<feBlend>"
tag_feColorMatrix="<feColorMatrix>"
tag_feComponentTransfer="<feComponentTransfer>"
tag_feComposite="<feComposite>"
tag_feConvolveMatrix="<feConvolveMatrix>"
tag_feDiffuseLighting="<feDiffuseLighting>"
tag_feDisplacementMap="<feDisplacementMap>"
tag_feDistantLight="<feDistantLight>"
tag_feDropShadow="<feDropShadow>"
tag_feFlood="<feFlood>"
tag_feFuncA="<feFuncA>"
tag_feFuncB="<feFuncB>"
tag_feFuncG="<feFuncG>"
tag_feFuncR="<feFuncR>"
tag_feGaussianBlur="<feGaussianBlur>"
tag_feImage="<feImage>"
tag_feMerge="<feMerge>"
tag_feMergeNode="<feMergeNode>"
tag_feMorphology="<feMorphology>"
tag_feOffset="<feOffset>"
tag_fePointLight="<fePointLight>"
tag_feSpecularLighting="<feSpecularLighting>"
tag_feSpotLight="<feSpotLight>"
tag_feTile="<feTile>"
tag_feTurbulence="<feTurbulence>"
tag_filter="<filter>"
tag_foreignObject="<foreignObject>"
tag_g="<g>"
tag_hatch="<hatch>"
tag_hatchpath="<hatchpath>"
tag_image="<image>"
tag_line="<line>"
tag_linearGradient="<linearGradient>"
tag_marker="<marker>"
tag_mask="<mask>"
tag_mesh="<mesh>"
tag_meshgradient="<meshgradient>"
tag_meshpatch="<meshpatch>"
tag_meshrow="<meshrow>"
tag_metadata="<metadata>"
tag_mpath="<mpath>"
tag_path="<path>"
tag_pattern="<pattern>"
tag_polygon="<polygon>"
tag_polyline="<polyline>"
tag_radialGradient="<radialGradient>"
tag_rect="<rect>"
tag_script="<script>"
tag_set="<set>"
tag_solidcolor="<solidcolor>"
tag_stop="<stop>"
tag_style="<style>"
tag_svg="<svg>"
tag_switch="<switch>"
tag_symbol="<symbol>"
tag_text="<text>"
tag_textPath="<textPath>"
tag_title="<title>"
tag_tspan="<tspan>"
tag_unknown="<unknown>"
tag_use="<use>"
tag_view="<view>"
Loading