commit 0a00ab9eb0d8eda4f63dd42d03fd9d4f6f28ca11
parent 97c4af12ae1de078939da954b3a6cf04402c4284
Author: MTRNord <mtrnord1@gmail.com>
Date: Wed, 7 Aug 2024 17:57:39 +0200
make fuzz targets build standalone and prepare wasm crosscompile
Diffstat:
8 files changed, 114 insertions(+), 21 deletions(-)
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
@@ -52,18 +52,6 @@ jobs:
if: runner.os == 'Linux' && matrix.build.cc == 'clang-18'
run: sudo apt-get install clang-18 libomp-18-dev lld-18 llvm-18
- - name: Build and install Botan
- run: |
- git clone --depth 1 https://github.com/randombit/botan.git
- pushd botan
- ./configure.py --prefix="${{ github.workspace }}/botan-install"
- make -j$(nproc)
- make install
- popd
- echo "PKG_CONFIG_PATH=${{ github.workspace }}/botan-install/lib/pkgconfig:$PKG_CONFIG_PATH" >> $GITHUB_ENV
- echo "CPPFLAGS=-I${{ github.workspace }}/botan-install/include $CPPFLAGS" >> $GITHUB_ENV
- echo "LDFLAGS=-L${{ github.workspace }}/botan-install/lib $LDFLAGS" >> $GITHUB_ENV
-
- name: Prepare macOS environment
if: runner.os == 'macOS'
@@ -90,6 +78,18 @@ jobs:
echo "LD_LIBRARY_PATH=$LLVM_PREFIX/lib:`dirname $ASAN_DSO`" >> $GITHUB_ENV
echo "$LLVM_PREFIX/bin" >> $GITHUB_PATH
+ - name: Build and install Botan
+ run: |
+ git clone --depth 1 https://github.com/randombit/botan.git
+ pushd botan
+ ./configure.py --prefix="${{ github.workspace }}/botan-install"
+ make -j$(nproc)
+ make install
+ popd
+ echo "PKG_CONFIG_PATH=${{ github.workspace }}/botan-install/lib/pkgconfig:$PKG_CONFIG_PATH" >> $GITHUB_ENV
+ echo "CPPFLAGS=-I${{ github.workspace }}/botan-install/include $CPPFLAGS" >> $GITHUB_ENV
+ echo "LDFLAGS=-L${{ github.workspace }}/botan-install/lib $LDFLAGS" >> $GITHUB_ENV
+
- name: Configure spank-olm
run: |
meson setup build \
diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml
@@ -15,6 +15,8 @@ jobs:
style: file
version: 18 # Ubuntu 24.04 provides clang-format-18
lines-changed-only: true
+ # ignore bundled files
+ ignore: 'fuzz/StandaloneFuzzTargetMain.c'
- name: Fail fast
continue-on-error: true # TODO: remove this line in the future
diff --git a/.gitignore b/.gitignore
@@ -23,4 +23,6 @@ compile_commands.json
.idea
-fuzz/corpuses/sign
-\ No newline at end of file
+fuzz/corpuses/sign
+
+build-wasm
+\ No newline at end of file
diff --git a/fuzz/StandaloneFuzzTargetMain.c b/fuzz/StandaloneFuzzTargetMain.c
@@ -0,0 +1,46 @@
+/*===- StandaloneFuzzTargetMain.c - standalone main() for fuzz targets. ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// This main() function can be linked to a fuzz target (i.e. a library
+// that exports LLVMFuzzerTestOneInput() and possibly LLVMFuzzerInitialize())
+// instead of libFuzzer. This main() function will not perform any fuzzing
+// but will simply feed all input files one by one to the fuzz target.
+//
+// Use this file to provide reproducers for bugs when linking against libFuzzer
+// or other fuzzing engine is undesirable.
+//===----------------------------------------------------------------------===*/
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+extern int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size);
+extern int LLVMFuzzerInitialize(int *argc, char ***argv);
+int main(int argc, char **argv) {
+ const char *progname;
+ if ((progname = strrchr(argv[0], '/')))
+ progname++;
+ else
+ progname = argv[0];
+ fprintf(stderr, "%s: running %d inputs\n", progname, argc - 1);
+ LLVMFuzzerInitialize(&argc, &argv);
+ for (int i = 1; i < argc; i++) {
+ fprintf(stderr, "Running: %s\n", argv[i]);
+ FILE *f = fopen(argv[i], "r+");
+ assert(f);
+ fseek(f, 0, SEEK_END);
+ long len = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ unsigned char *buf = (unsigned char*)malloc(len);
+ size_t n_read = fread(buf, 1, len, f);
+ fclose(f);
+ assert(n_read == len);
+ LLVMFuzzerTestOneInput(buf, len);
+ free(buf);
+ fprintf(stderr, "Done: %s: (%zd bytes)\n", argv[i], n_read);
+ }
+}
+\ No newline at end of file
diff --git a/fuzz/meson.build b/fuzz/meson.build
@@ -5,7 +5,12 @@ if get_option('fuzzer_ldflags') != ''
fuzz_ldflags += [get_option('fuzzer_ldflags')]
endif
-if fuzzing_engine == 'libfuzzer'
+if fuzzing_engine == 'none'
+ standalone_engine = static_library('standalone_engine',
+ 'StandaloneFuzzTargetMain.c'
+ )
+ fuzz_deps += declare_dependency(link_with : standalone_engine)
+elif fuzzing_engine == 'libfuzzer'
fuzz_ldflags += ['-fsanitize=fuzzer']
endif
diff --git a/fuzz/olm_sign_fuzzer.cpp b/fuzz/olm_sign_fuzzer.cpp
@@ -5,6 +5,13 @@
#include "account.hpp"
#include "botan/pubkey.h"
+// Just needed for compiling reasons
+extern "C" int
+LLVMFuzzerInitialize(int* argc, char*** argv)
+{
+ return 0;
+}
+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size)
{
if (Size == 0)
diff --git a/meson.build b/meson.build
@@ -1,10 +1,15 @@
-project('spank-olm', 'cpp',
+project('spank-olm', 'c', 'cpp',
version : '1.0.0',
- default_options : ['warning_level=3',
- 'cpp_std=c++23',
- 'b_lto=true',
- 'b_thinlto_cache=true',
- 'warning_level=3', ])
+ default_options : [
+ 'c_std=c11',
+ 'cpp_std=c++23',
+ 'b_lto=true',
+ 'b_thinlto_cache=true',
+ 'warning_level=3',
+ # do a release (optimised) build by default
+ 'buildtype=release',
+ # turn off asserts etc. in release mode
+ 'b_ndebug=if-release'])
# libFuzzer related things
fuzzing_engine = get_option('fuzzing_engine')
@@ -16,6 +21,15 @@ if fuzzing_engine == 'libfuzzer'
add_project_arguments(cc.first_supported_argument(fuzzer_args), language : ['cpp', 'c'])
endif
+# Check if we are building for WASM
+is_wasm = host_machine.system() == 'emscripten'
+
+# Add specific arguments for WASM
+if is_wasm
+ add_project_arguments('-s', 'WASM=1', '-s', 'MODULARIZE=1', '-s', 'EXPORT_NAME="createModule"', '-flto', language : 'cpp')
+ add_project_link_arguments('-s', 'WASM=1', '-s', 'MODULARIZE=1', '-s', 'EXPORT_NAME="createModule"', '-flto', language : 'cpp')
+endif
+
# Cmake doesnt work with meson, so we need to require pkg-config
botan_dep = dependency('botan-3', version : '>=3.6.0', required : true, method : 'pkg-config')
diff --git a/wasm-cross-file.txt b/wasm-cross-file.txt
@@ -0,0 +1,15 @@
+[binaries]
+c = 'emcc'
+cpp = 'em++'
+ar = 'emar'
+strip = 'llvm-strip'
+pkgconfig = 'em-pkg-config'
+
+[host_machine]
+system = 'emscripten'
+cpu_family = 'wasm'
+cpu = 'wasm32'
+endian = 'little'
+
+[properties]
+needs_exe_wrapper = true
+\ No newline at end of file