Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
23 changes: 23 additions & 0 deletions projects/hyperscan/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2019 Google Inc.
#
# 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.
#
################################################################################

FROM gcr.io/oss-fuzz-base/base-builder
MAINTAINER [email protected]
RUN apt-get update && apt-get install -y cmake make libboost-dev python ragel
RUN git clone --branch develop --depth 1 https://github.com/intel/hyperscan.git hyperscan
WORKDIR $SRC
COPY build.sh $SRC/
COPY fuzz_*.c $SRC/
28 changes: 28 additions & 0 deletions projects/hyperscan/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash -eu
# Copyright 2019 Google Inc.
#
# 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.
#
################################################################################

# build project
cd hyperscan
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Debug -DOPTIMISE=OFF ..
make

for target in `ls ../../fuzz*.c | cut -d/ -f3 | sed 's/.c//'`; do
$CC $CFLAGS -I ../src -c ../../"$target".c -o "$target".o
$CXX $CXXFLAGS "$target".o -o $OUT/"$target" $LIB_FUZZING_ENGINE lib/libhs.a
done
23 changes: 23 additions & 0 deletions projects/hyperscan/fuzz_deserialize_database.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "hs.h"

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

FILE * logfile = NULL;


int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
if (logfile == NULL) {
logfile = fopen("/dev/null", "wb");
}

hs_database_t *database;
hs_error_t err = hs_deserialize_database(Data, Size, &database);
if (err != HS_SUCCESS) {
fprintf(logfile, "ERROR\n");
return 0;
}
hs_free_database(database);
return 0;
}
71 changes: 71 additions & 0 deletions projects/hyperscan/fuzz_simplegrep.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "hs.h"

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

FILE * logfile = NULL;
hs_scratch_t *scratch = NULL;
void * scrachAllocArea = NULL;
size_t scrachAllocSize = 0;

static int eventHandler(unsigned int id, unsigned long long from,
unsigned long long to, unsigned int flags, void *ctx) {
fprintf(logfile, "Match for pattern \"%s\" at offset %llu\n", (char *)ctx, to);
return 0;
}

static void * fuzz_scratch_alloc(size_t size) {
if (size > scrachAllocSize) {
scrachAllocArea = realloc(scrachAllocArea, size);
scrachAllocSize = size;
}
return scrachAllocArea;
}

static void fuzz_nofree(void * area) {
fprintf(logfile, "free\n");
return;
}

int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

if (logfile == NULL) {
//use custom allocator to avoir reallocating at each run
//hs_set_scratch_allocator(fuzz_scratch_alloc, fuzz_nofree);
logfile = fopen("/dev/null", "wb");
}
//decompose into needle and haystack
const uint8_t * haystack = memchr(Data, 0, Size);
if (haystack == NULL) {
return 0;
}
haystack++;
Size -= (haystack - Data);

hs_database_t *database;
hs_compile_error_t *compile_err;
if (hs_compile(Data, HS_FLAG_DOTALL, HS_MODE_BLOCK, NULL, &database,
&compile_err) != HS_SUCCESS) {
fprintf(logfile, "ERROR: Unable to compile pattern \"%s\": %s\n",
Data, compile_err->message);
hs_free_compile_error(compile_err);
return 0;
}

if (hs_alloc_scratch(database, &scratch) != HS_SUCCESS) {
fprintf(logfile, "ERROR: Unable to allocate scratch space. Exiting.\n");
hs_free_database(database);
return 0;
}

if (hs_scan(database, haystack, Size, 0, scratch, eventHandler,
Data) != HS_SUCCESS) {
fprintf(logfile, "ERROR: Unable to scan input buffer. Exiting.\n");
hs_free_database(database);
return 0;
}

hs_free_database(database);
return 0;
}
4 changes: 4 additions & 0 deletions projects/hyperscan/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
homepage: "https://www.hyperscan.io"
primary_contact: "[email protected]"
auto_ccs :
- "[email protected]"