1#!/bin/bash -eu 2# Copyright 2021 Google LLC 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16################################################################################ 17 18mkdir $SRC/ClickHouse/build 19cd $SRC/ClickHouse/build 20 21sed -i -e '/warnings.cmake)/d' $SRC/ClickHouse/CMakeLists.txt 22 23# It will be hard to maintain any compilation fails (if any) in two repositories. 24# Also ClickHouse won't compile without this. 25# It is very strange, because we have as many warnings as you could imagine. 26sed -i -e 's/add_warning(/no_warning(/g' $SRC/ClickHouse/CMakeLists.txt 27 28# ClickHouse uses libcxx from contrib. 29# Enabling this manually will cause duplicate symbols at linker stage. 30CXXFLAGS=${CXXFLAGS//-stdlib=libc++/} 31 32CLICKHOUSE_CMAKE_FLAGS=( 33 "-DCMAKE_CXX_COMPILER_LAUNCHER=/usr/bin/ccache" 34 "-DCMAKE_C_COMPILER=$CC" 35 "-DCMAKE_CXX_COMPILER=$CXX" 36 "-DCMAKE_BUILD_TYPE=RelWithDebInfo" 37 "-DLIB_FUZZING_ENGINE:STRING=$LIB_FUZZING_ENGINE" 38 "-DENABLE_EMBEDDED_COMPILER=0" 39 "-DENABLE_THINLTO=0" 40 "-DENABLE_TESTS=0" 41 "-DENABLE_EXAMPLES=0" 42 "-DENABLE_UTILS=0" 43 "-DENABLE_JEMALLOC=0" 44 "-DENABLE_FUZZING=1" 45 "-DENABLE_CLICKHOUSE_ODBC_BRIDGE=OFF" 46 "-DENABLE_LIBRARIES=0" 47 "-DENABLE_SSL=1" 48 "-DUSE_INTERNAL_SSL_LIBRARY=1" 49 "-DUSE_UNWIND=ON" 50 "-DGLIBC_COMPATIBILITY=OFF" 51) 52 53if [ "$SANITIZER" = "coverage" ]; then 54 cmake -G Ninja $SRC/ClickHouse ${CLICKHOUSE_CMAKE_FLAGS[@]} -DCMAKE_CXX_FLAGS="$CXXFLAGS" -DCMAKE_C_FLAGS="$CFLAGS" -DWITH_COVERAGE=1 55else 56 cmake -G Ninja $SRC/ClickHouse ${CLICKHOUSE_CMAKE_FLAGS[@]} -DCMAKE_CXX_FLAGS="$CXXFLAGS" -DCMAKE_C_FLAGS="$CFLAGS" -DWITH_COVERAGE=1 -DSANITIZE=$SANITIZER 57fi 58 59NUM_JOBS=$(($(nproc || grep -c ^processor /proc/cpuinfo) / 2)) 60 61TARGETS=$(find $SRC/ClickHouse/src -name '*_fuzzer.cpp' -execdir basename {} .cpp ';' | tr '\n' ' ') 62 63for FUZZER_TARGET in $TARGETS 64do 65 ninja -j $NUM_JOBS $FUZZER_TARGET 66 # Find this binary in build directory and strip it 67 TEMP=$(find $SRC/ClickHouse/build -name $FUZZER_TARGET) 68 strip --strip-unneeded $TEMP 69done 70 71# copy out fuzzer binaries 72find $SRC/ClickHouse/build -name '*_fuzzer' -exec cp -v '{}' $OUT ';' 73 74# copy out fuzzer options and dictionaries 75cp $SRC/ClickHouse/tests/fuzz/*.dict $OUT/ 76cp $SRC/ClickHouse/tests/fuzz/*.options $OUT/ 77 78# prepare corpus dirs 79mkdir $SRC/ClickHouse/tests/fuzz/lexer_fuzzer.in/ 80mkdir $SRC/ClickHouse/tests/fuzz/select_parser_fuzzer.in/ 81mkdir $SRC/ClickHouse/tests/fuzz/create_parser_fuzzer.in/ 82 83# prepare corpus 84cp $SRC/ClickHouse/tests/queries/0_stateless/*.sql $SRC/ClickHouse/tests/fuzz/lexer_fuzzer.in/ 85cp $SRC/ClickHouse/tests/queries/0_stateless/*.sql $SRC/ClickHouse/tests/fuzz/select_parser_fuzzer.in/ 86cp $SRC/ClickHouse/tests/queries/0_stateless/*.sql $SRC/ClickHouse/tests/fuzz/create_parser_fuzzer.in/ 87cp $SRC/ClickHouse/tests/queries/1_stateful/*.sql $SRC/ClickHouse/tests/fuzz/lexer_fuzzer.in/ 88cp $SRC/ClickHouse/tests/queries/1_stateful/*.sql $SRC/ClickHouse/tests/fuzz/select_parser_fuzzer.in/ 89cp $SRC/ClickHouse/tests/queries/1_stateful/*.sql $SRC/ClickHouse/tests/fuzz/create_parser_fuzzer.in/ 90 91# copy out corpus 92cd $SRC/ClickHouse/tests/fuzz 93for dir in *_fuzzer.in; do 94 fuzzer=$(basename $dir .in) 95 zip -rj "$OUT/${fuzzer}_seed_corpus.zip" "${dir}/" 96done 97 98# copy sources for code coverage if required 99if [ "$SANITIZER" = "coverage" ]; then 100 mkdir -p $OUT/src/ClickHouse/ 101 cp -rL --parents $SRC/ClickHouse/src $OUT 102 cp -rL --parents $SRC/ClickHouse/base $OUT 103 cp -rL --parents $SRC/ClickHouse/programs $OUT 104fi 105 106# Just check binaries size 107BINARIES_SIZE=$(find $SRC/ClickHouse/build -name '*_fuzzer' -exec du -sh '{}' ';') 108