#!/bin/bash -eu
# Copyright 2016 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.
#
################################################################################

mkdir -p /logs && chmod o+w /logs

pushd $SRC/h2o
sed 's/int fuzz_without_main(/int main(/' -i /src/h2o/src/main.c
cmake -DBUILD_FUZZER=ON -DOSS_FUZZ=ON -DOPENSSL_USE_STATIC_LIBS=TRUE .
sed 's/-O2/-fsanitize=fuzzer-no-link -O2/' -i /src/h2o/CMakeLists.txt
make
sed 's/int main(/int fuzz_without_main(/' -i /src/h2o/src/main.c
make src/main.o

clang++ -fsanitize=fuzzer $LIB_FUZZING_ENGINE -O1 -fno-omit-frame-pointer -gline-tables-only -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -stdlib=libc++ -fno-omit-frame-pointer -O2 -g -Wall -Wno-unused-value -Wno-unused-function -O1 -fno-omit-frame-pointer -gline-tables-only -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -DH2O_ROOT="/usr/local" -DH2O_CONFIG_PATH="etc/h2o.conf" -rdynamic /src/fuzz-diff.cc CMakeFiles/h2o.dir/deps/cloexec/cloexec.c.o CMakeFiles/h2o.dir/deps/libgkc/gkc.c.o CMakeFiles/h2o.dir/deps/libyrmcds/close.c.o CMakeFiles/h2o.dir/deps/libyrmcds/connect.c.o CMakeFiles/h2o.dir/deps/libyrmcds/recv.c.o CMakeFiles/h2o.dir/deps/libyrmcds/send.c.o CMakeFiles/h2o.dir/deps/libyrmcds/send_text.c.o CMakeFiles/h2o.dir/deps/libyrmcds/socket.c.o CMakeFiles/h2o.dir/deps/libyrmcds/strerror.c.o CMakeFiles/h2o.dir/deps/libyrmcds/text_mode.c.o CMakeFiles/h2o.dir/deps/picohttpparser/picohttpparser.c.o CMakeFiles/h2o.dir/lib/common/cache.c.o CMakeFiles/h2o.dir/lib/common/file.c.o CMakeFiles/h2o.dir/lib/common/filecache.c.o CMakeFiles/h2o.dir/lib/common/hostinfo.c.o CMakeFiles/h2o.dir/lib/common/http1client.c.o CMakeFiles/h2o.dir/lib/common/memcached.c.o CMakeFiles/h2o.dir/lib/common/memory.c.o CMakeFiles/h2o.dir/lib/common/multithread.c.o CMakeFiles/h2o.dir/lib/common/serverutil.c.o CMakeFiles/h2o.dir/lib/common/socket.c.o CMakeFiles/h2o.dir/lib/common/socketpool.c.o CMakeFiles/h2o.dir/lib/common/string.c.o CMakeFiles/h2o.dir/lib/common/time.c.o CMakeFiles/h2o.dir/lib/common/timeout.c.o CMakeFiles/h2o.dir/lib/common/url.c.o CMakeFiles/h2o.dir/lib/core/config.c.o CMakeFiles/h2o.dir/lib/core/configurator.c.o CMakeFiles/h2o.dir/lib/core/context.c.o CMakeFiles/h2o.dir/lib/core/headers.c.o CMakeFiles/h2o.dir/lib/core/logconf.c.o CMakeFiles/h2o.dir/lib/core/proxy.c.o CMakeFiles/h2o.dir/lib/core/request.c.o CMakeFiles/h2o.dir/lib/core/token.c.o CMakeFiles/h2o.dir/lib/core/util.c.o CMakeFiles/h2o.dir/lib/handler/access_log.c.o CMakeFiles/h2o.dir/lib/handler/chunked.c.o CMakeFiles/h2o.dir/lib/handler/compress.c.o CMakeFiles/h2o.dir/lib/handler/compress/gzip.c.o CMakeFiles/h2o.dir/lib/handler/errordoc.c.o CMakeFiles/h2o.dir/lib/handler/expires.c.o CMakeFiles/h2o.dir/lib/handler/fastcgi.c.o CMakeFiles/h2o.dir/lib/handler/file.c.o CMakeFiles/h2o.dir/lib/handler/headers.c.o CMakeFiles/h2o.dir/lib/handler/mimemap.c.o CMakeFiles/h2o.dir/lib/handler/proxy.c.o CMakeFiles/h2o.dir/lib/handler/redirect.c.o CMakeFiles/h2o.dir/lib/handler/reproxy.c.o CMakeFiles/h2o.dir/lib/handler/throttle_resp.c.o CMakeFiles/h2o.dir/lib/handler/status.c.o CMakeFiles/h2o.dir/lib/handler/headers_util.c.o CMakeFiles/h2o.dir/lib/handler/status/events.c.o CMakeFiles/h2o.dir/lib/handler/status/requests.c.o CMakeFiles/h2o.dir/lib/handler/http2_debug_state.c.o CMakeFiles/h2o.dir/lib/handler/status/durations.c.o CMakeFiles/h2o.dir/lib/handler/configurator/access_log.c.o CMakeFiles/h2o.dir/lib/handler/configurator/compress.c.o CMakeFiles/h2o.dir/lib/handler/configurator/errordoc.c.o CMakeFiles/h2o.dir/lib/handler/configurator/expires.c.o CMakeFiles/h2o.dir/lib/handler/configurator/fastcgi.c.o CMakeFiles/h2o.dir/lib/handler/configurator/file.c.o CMakeFiles/h2o.dir/lib/handler/configurator/headers.c.o CMakeFiles/h2o.dir/lib/handler/configurator/proxy.c.o CMakeFiles/h2o.dir/lib/handler/configurator/redirect.c.o CMakeFiles/h2o.dir/lib/handler/configurator/reproxy.c.o CMakeFiles/h2o.dir/lib/handler/configurator/throttle_resp.c.o CMakeFiles/h2o.dir/lib/handler/configurator/status.c.o CMakeFiles/h2o.dir/lib/handler/configurator/http2_debug_state.c.o CMakeFiles/h2o.dir/lib/handler/configurator/headers_util.c.o CMakeFiles/h2o.dir/lib/http1.c.o CMakeFiles/h2o.dir/lib/tunnel.c.o CMakeFiles/h2o.dir/lib/http2/cache_digests.c.o CMakeFiles/h2o.dir/lib/http2/casper.c.o CMakeFiles/h2o.dir/lib/http2/connection.c.o CMakeFiles/h2o.dir/lib/http2/frame.c.o CMakeFiles/h2o.dir/lib/http2/hpack.c.o CMakeFiles/h2o.dir/lib/http2/scheduler.c.o CMakeFiles/h2o.dir/lib/http2/stream.c.o CMakeFiles/h2o.dir/lib/http2/http2_debug_state.c.o CMakeFiles/h2o.dir/deps/yaml/src/api.c.o CMakeFiles/h2o.dir/deps/yaml/src/dumper.c.o CMakeFiles/h2o.dir/deps/yaml/src/emitter.c.o CMakeFiles/h2o.dir/deps/yaml/src/loader.c.o CMakeFiles/h2o.dir/deps/yaml/src/parser.c.o CMakeFiles/h2o.dir/deps/yaml/src/reader.c.o CMakeFiles/h2o.dir/deps/yaml/src/scanner.c.o CMakeFiles/h2o.dir/deps/yaml/src/writer.c.o CMakeFiles/h2o.dir/deps/brotli/enc/backward_references.cc.o CMakeFiles/h2o.dir/deps/brotli/enc/block_splitter.cc.o CMakeFiles/h2o.dir/deps/brotli/enc/brotli_bit_stream.cc.o CMakeFiles/h2o.dir/deps/brotli/enc/compress_fragment.cc.o CMakeFiles/h2o.dir/deps/brotli/enc/compress_fragment_two_pass.cc.o CMakeFiles/h2o.dir/deps/brotli/enc/dictionary.cc.o CMakeFiles/h2o.dir/deps/brotli/enc/encode.cc.o CMakeFiles/h2o.dir/deps/brotli/enc/entropy_encode.cc.o CMakeFiles/h2o.dir/deps/brotli/enc/histogram.cc.o CMakeFiles/h2o.dir/deps/brotli/enc/literal_cost.cc.o CMakeFiles/h2o.dir/deps/brotli/enc/metablock.cc.o CMakeFiles/h2o.dir/deps/brotli/enc/static_dict.cc.o CMakeFiles/h2o.dir/deps/brotli/enc/streams.cc.o CMakeFiles/h2o.dir/deps/brotli/enc/utf8_util.cc.o CMakeFiles/h2o.dir/lib/handler/compress/brotli.cc.o CMakeFiles/h2o.dir/deps/neverbleed/neverbleed.c.o CMakeFiles/h2o.dir/src/main.c.o CMakeFiles/h2o.dir/src/ssl.c.o CMakeFiles/h2o.dir/deps/picotls/deps/micro-ecc/uECC.c.o CMakeFiles/h2o.dir/deps/picotls/deps/cifra/src/aes.c.o CMakeFiles/h2o.dir/deps/picotls/deps/cifra/src/blockwise.c.o CMakeFiles/h2o.dir/deps/picotls/deps/cifra/src/chacha20.c.o CMakeFiles/h2o.dir/deps/picotls/deps/cifra/src/chash.c.o CMakeFiles/h2o.dir/deps/picotls/deps/cifra/src/curve25519.c.o CMakeFiles/h2o.dir/deps/picotls/deps/cifra/src/drbg.c.o CMakeFiles/h2o.dir/deps/picotls/deps/cifra/src/hmac.c.o CMakeFiles/h2o.dir/deps/picotls/deps/cifra/src/gcm.c.o CMakeFiles/h2o.dir/deps/picotls/deps/cifra/src/gf128.c.o CMakeFiles/h2o.dir/deps/picotls/deps/cifra/src/modes.c.o CMakeFiles/h2o.dir/deps/picotls/deps/cifra/src/poly1305.c.o CMakeFiles/h2o.dir/deps/picotls/deps/cifra/src/sha256.c.o CMakeFiles/h2o.dir/deps/picotls/deps/cifra/src/sha512.c.o CMakeFiles/h2o.dir/deps/picotls/lib/picotls.c.o CMakeFiles/h2o.dir/deps/picotls/lib/cifra.c.o CMakeFiles/h2o.dir/deps/picotls/lib/uecc.c.o CMakeFiles/h2o.dir/deps/picotls/lib/openssl.c.o -o /out/fuzz-diff  -Wl,-Bstatic -lssl -lcrypto -Wl,-Bdynamic -lpthread -ldl -lz -lpthread -ldl -lz

popd
