Skip to content

Commit

Permalink
Merge pull request #14 from tesch1/fix-fuzz2
Browse files Browse the repository at this point in the history
Fix fuzz2
  • Loading branch information
marcel-goldschen-ohm authored Nov 9, 2018
2 parents 0c8ddcc + 92bd10b commit bd31fe6
Show file tree
Hide file tree
Showing 6 changed files with 431 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ addons:

env:
- EIGEN_VERSION=3.2.10
- EIGEN_VERSION=3.3.3
- EIGEN_VERSION=3.3.4

before_install:
- ${CXX} --version;
Expand Down
240 changes: 186 additions & 54 deletions EigenLab.h

Large diffs are not rendered by default.

52 changes: 51 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ cmake_minimum_required (VERSION 2.8)

project (EigenLab CXX)

set (EIGEN_VERSION "3.3.3" CACHE STRING "Select Eigen Version.")
option (BUILD_FUZZ "Build binaries with tooling for fuzzing." OFF)
set (EIGEN_VERSION "3.3.4" CACHE STRING "Select Eigen Version.")

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Expand All @@ -31,12 +32,61 @@ set (EIGEN3_INCLUDE_DIRS "${source_dir}")
include_directories ("${PROJECT_SOURCE_DIR}/..")
include_directories ("${EIGEN3_INCLUDE_DIRS}")

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-int-in-bool-context") # eigen 3.3.4
endif ()
add_definitions (-DDEBUG)
add_definitions (-D_GLIBCXX_DEBUG)
add_executable (Test Test.cpp)
add_dependencies (Test eigenX)

#
# The command-line EigenLab
#
add_executable (CmdLine CmdLine.cpp)
add_dependencies (CmdLine eigenX)

#
# The command-line EigenLab
#
add_executable (EigenLab EigenLab.cpp)
add_dependencies (EigenLab eigenX)


enable_testing ()
add_test (basic Test)

#add_executable (tmp tmp.cpp)

#
# Change the compiler to use
#
if (BUILD_FUZZ)
#
# 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 (EigenLab aflX)

add_custom_target (fuzz
COMMAND ${AFL_DIR}/afl-fuzz -i fuzz-data -o fuzz-out -x fuzz.dict $<TARGET_FILE:EigenLab> -f @@
)
add_dependencies (fuzz EigenLab)

endif (BUILD_FUZZ)
136 changes: 136 additions & 0 deletions tests/EigenLab.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// --*- Mode: C++; c-basic-offset:4; indent-tabs-mode:t; tab-width:4 -*--
// EigenLab
// Version: 1.0.0
// Author: Michael Tesch
// Email: [email protected]
// Copyright (c) 2018 by Michael Tesch
// Licence: MIT
//----------------------------------------
#include <regex>
#include <getopt.h>

int eigenlab_debug = 0;
long int eigenlab_maxmatrix = 16*1024*1024; // 16 MB
#define EIGENLAB_DEBUG eigenlab_debug
#define EIGENLAB_MAXMATRIX eigenlab_maxmatrix
#include "EigenLab.h"

//
// Simple command-line program to evaluate expressions using EigenLab.h
//

typedef EigenLab::ParserXd ParserType;


// Evaluate a single expression
bool eval(ParserType & parser, const std::string & expr)
{
try {
size_t pos = 0;
while (pos < expr.size()) {
size_t end = expr.find_first_of("|", pos);
if (end == std::string::npos)
end = expr.size();
std::cout << parser.eval(expr.substr(pos, end-pos)).matrix() << "\n";
pos = end + 1;
}
}
catch (std::exception & ex) {
std::cerr << "err:" << ex.what() << "\n";
}
return true;
}

void usage()
{
std::cerr << "usage:\n"
" -e,--expr <expression> | parse the given expression\n"
" -f,--file <filename> | parse each line of filename as expression\n"
" -l,--limit <bytes> | limit matrices to this many bytes\n"
" -h,--help | print this help message and exit\n"
" -v,--verbose | be more verbose\n";
exit(-1);
}

int main(int argc, char *argv[])
{
ParserType parser;

while (1) {
int option_index = 0;
static struct option long_options[] = {
{"expr", required_argument, 0, 'e' },
{"file", required_argument, 0, 'f' },
{"limit", required_argument, 0, 'l' },
{"help", no_argument, 0, 'h' },
{"verbose", no_argument, 0, 'v' },
{0, 0, 0, 0 }
};

int c = getopt_long(argc, argv, "e:f:hv", long_options, &option_index);
if (c == -1)
break;

switch (c) {
case 0:
printf("option %s", long_options[option_index].name);
if (optarg)
printf(" with arg %s", optarg);
printf("\n");
break;

case 'f': {
printf("expressions from file '%s':\n", optarg);
if (!strcmp(optarg, "-"))
strcpy(optarg, "/dev/stdin");
if (FILE * fp = fopen(optarg, "r")) {
char * line = NULL;
size_t len = 0;
ssize_t nread;
while ((nread = getline(&line, &len, fp)) != -1) {
printf(">> %s", line);
eval(parser, line);
}

free(line);
fclose(fp);
}
else
perror("fopen");
}
break;

case 'e':
printf("expression '%s'\n", optarg);
eval(parser, optarg);
break;

case 'l':
eigenlab_maxmatrix = atol(optarg);
printf("max matrix bytes '%s' = %ld\n", optarg, eigenlab_maxmatrix);
break;

case 'v':
eigenlab_debug++;
break;

case 'h':
case '?':
usage();
break;

default:
printf("?? getopt returned character code 0%o ??\n", c);
}
}

if (optind < argc) {
printf("Error, unrecognized command line arguments: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
return -1;
}

return 0;
}
1 change: 1 addition & 0 deletions tests/fuzz-data/add
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1+2
56 changes: 56 additions & 0 deletions tests/fuzz.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#
# AFL dictionary for EigenLab
# ---------------------------
#
# Contains function keywords and syntax building blocks.
#

"+"
"-"
"*"
"/"
"^"
"("
")"
"["
"]"
"="
".+"
".-"
".*"
"./"
".^"
"abs"
"sqrt"
"exp"
"log"
"log10"
"sin"
"cos"
"tan"
"asin"
"acos"
"trace"
"norm"
"size"
"min"
"max"
"absmax"
"cwiseMin"
"cwiseMax"
"mean"
"sum"
"prod"
"transpose"
"conjugate"
"adjoint"
"zeros"
"ones"
"eye"
"e"
"E"
"."
","
";"
"1"
":"

0 comments on commit bd31fe6

Please sign in to comment.