1#!/bin/bash -eu
2# Copyright 2016 Google Inc.
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
18# Build SwiftShader
19pushd third_party/externals/swiftshader/
20export SWIFTSHADER_INCLUDE_PATH=$PWD/include
21# SwiftShader already has a build/ directory, use something else
22rm -rf build_swiftshader
23mkdir build_swiftshader
24
25cd build_swiftshader
26if [ $SANITIZER == "address" ]; then
27  CMAKE_SANITIZER="SWIFTSHADER_ASAN"
28elif [ $SANITIZER == "memory" ]; then
29  CMAKE_SANITIZER="SWIFTSHADER_MSAN"
30  # oss-fuzz will patch the rpath for this after compilation and linking,
31  # so we only need to set this to appease the Swiftshader build rules check.
32  export SWIFTSHADER_MSAN_INSTRUMENTED_LIBCXX_PATH="/does/not/matter"
33elif [ $SANITIZER == "undefined" ]; then
34  # The current SwiftShader build needs -fno-sanitize=vptr, but it cannot be
35  # specified here since -fsanitize=undefined will always come after any
36  # user specified flags passed to cmake. SwiftShader does not need to be
37  # built with the undefined sanitizer in order to fuzz Skia, so don't.
38  CMAKE_SANITIZER="SWIFTSHADER_UBSAN_DISABLED"
39elif [ $SANITIZER == "coverage" ]; then
40  CMAKE_SANITIZER="SWIFTSHADER_EMIT_COVERAGE"
41elif [ $SANITIZER == "thread" ]; then
42  CMAKE_SANITIZER="SWIFTSHADER_UBSAN_DISABLED"
43else
44  exit 1
45fi
46CFLAGS= CXXFLAGS="-stdlib=libc++" cmake .. -GNinja -DCMAKE_MAKE_PROGRAM="$SRC/depot_tools/ninja" -D$CMAKE_SANITIZER=1
47
48$SRC/depot_tools/ninja libGLESv2 libEGL
49mv libGLESv2.so libEGL.so $OUT
50export SWIFTSHADER_LIB_PATH=$OUT
51
52popd
53# These are any clang warnings we need to silence.
54DISABLE="-Wno-zero-as-null-pointer-constant -Wno-unused-template
55         -Wno-cast-qual"
56# Disable UBSan vptr since target built with -fno-rtti.
57export CFLAGS="$CFLAGS $DISABLE -I$SWIFTSHADER_INCLUDE_PATH -DGR_EGL_TRY_GLES3_THEN_GLES2 -fno-sanitize=vptr"
58export CXXFLAGS="$CXXFLAGS $DISABLE -I$SWIFTSHADER_INCLUDE_PATH -DGR_EGL_TRY_GLES3_THEN_GLES2 -fno-sanitize=vptr"
59export LDFLAGS="$LIB_FUZZING_ENGINE $CXXFLAGS -L$SWIFTSHADER_LIB_PATH"
60
61# This splits a space separated list into a quoted, comma separated list for gn.
62export CFLAGS_ARR=`echo $CFLAGS | sed -e "s/\s/\",\"/g"`
63export CXXFLAGS_ARR=`echo $CXXFLAGS | sed -e "s/\s/\",\"/g"`
64export LDFLAGS_ARR=`echo $LDFLAGS | sed -e "s/\s/\",\"/g"`
65
66$SRC/skia/bin/fetch-gn
67
68set +u
69LIMITED_LINK_POOL="link_pool_depth=1"
70if [ "$CIFUZZ" = "true" ]; then
71  echo "Not restricting linking because on CIFuzz"
72  LIMITED_LINK_POOL=""
73fi
74set -u
75
76# Even though GPU is "enabled" for all these builds, none really
77# uses the gpu except for api_mock_gpu_canvas
78$SRC/skia/bin/gn gen out/Fuzz\
79    --args='cc="'$CC'"
80      cxx="'$CXX'"
81      '$LIMITED_LINK_POOL'
82      is_debug=false
83      extra_cflags_c=["'"$CFLAGS_ARR"'"]
84      extra_cflags_cc=["'"$CXXFLAGS_ARR"'"]
85      extra_ldflags=["'"$LDFLAGS_ARR"'"]
86      skia_build_fuzzers=true
87      skia_enable_fontmgr_custom_directory=false
88      skia_enable_fontmgr_custom_embedded=false
89      skia_enable_fontmgr_custom_empty=true
90      skia_enable_gpu=true
91      skia_enable_skottie=true
92      skia_use_egl=true
93      skia_use_fontconfig=false
94      skia_use_freetype=true
95      skia_use_system_freetype2=false
96      skia_use_wuffs=true
97      skia_use_libfuzzer_defaults=false'
98
99$SRC/depot_tools/ninja -C out/Fuzz \
100  android_codec \
101  animated_image_decode \
102  api_create_ddl \
103  api_ddl_threading \
104  api_draw_functions \
105  api_gradients \
106  api_image_filter \
107  api_mock_gpu_canvas \
108  api_null_canvas \
109  api_path_measure \
110  api_pathop \
111  api_polyutils \
112  api_raster_n32_canvas \
113  api_regionop \
114  api_skparagraph \
115  api_svg_canvas \
116  image_decode \
117  image_decode_incremental \
118  image_filter_deserialize \
119  jpeg_encoder \
120  path_deserialize \
121  png_encoder \
122  region_deserialize \
123  region_set_path \
124  skdescriptor_deserialize \
125  skjson \
126  skottie_json \
127  skp \
128  skruntimeeffect \
129  sksl2glsl \
130  sksl2metal \
131  sksl2pipeline \
132  sksl2spirv \
133  svg_dom \
134  textblob_deserialize \
135  webp_encoder
136
137rm -rf $OUT/data
138mkdir $OUT/data
139
140mv out/Fuzz/region_deserialize $OUT/region_deserialize
141
142mv out/Fuzz/region_set_path $OUT/region_set_path
143mv ../skia_data/region_set_path_seed_corpus.zip $OUT/region_set_path_seed_corpus.zip
144
145mv out/Fuzz/textblob_deserialize $OUT/textblob_deserialize
146mv ../skia_data/textblob_deserialize_seed_corpus.zip $OUT/textblob_deserialize_seed_corpus.zip
147
148mv out/Fuzz/path_deserialize $OUT/path_deserialize
149mv ../skia_data/path_deserialize_seed_corpus.zip $OUT/path_deserialize_seed_corpus.zip
150
151mv out/Fuzz/animated_image_decode $OUT/animated_image_decode
152mv ../skia_data/animated_image_decode_seed_corpus.zip $OUT/animated_image_decode_seed_corpus.zip
153
154# Only create the width version of image_filter_deserialize if building with
155# libfuzzer, since it depends on a libfuzzer specific flag.
156if [ "$FUZZING_ENGINE" == "libfuzzer" ]
157then
158  # Use the same binary as image_filter_deserialize.
159  cp out/Fuzz/image_filter_deserialize $OUT/image_filter_deserialize_width
160  mv ../skia_data/image_filter_deserialize_width.options $OUT/image_filter_deserialize_width.options
161  # Use the same seed corpus as image_filter_deserialize.
162  cp ../skia_data/image_filter_deserialize_seed_corpus.zip $OUT/image_filter_deserialize_width_seed_corpus.zip
163fi
164
165mv out/Fuzz/image_filter_deserialize $OUT/image_filter_deserialize
166mv ../skia_data/image_filter_deserialize_seed_corpus.zip $OUT/image_filter_deserialize_seed_corpus.zip
167
168mv out/Fuzz/api_draw_functions $OUT/api_draw_functions
169mv ../skia_data/api_draw_functions_seed_corpus.zip $OUT/api_draw_functions_seed_corpus.zip
170
171mv out/Fuzz/api_gradients $OUT/api_gradients
172mv ../skia_data/api_gradients_seed_corpus.zip $OUT/api_gradients_seed_corpus.zip
173
174mv out/Fuzz/api_path_measure $OUT/api_path_measure
175mv ../skia_data/api_path_measure_seed_corpus.zip $OUT/api_path_measure_seed_corpus.zip
176
177mv out/Fuzz/api_pathop $OUT/api_pathop
178mv ../skia_data/api_pathop_seed_corpus.zip $OUT/api_pathop_seed_corpus.zip
179
180# These 3 use the same corpus.
181mv out/Fuzz/png_encoder $OUT/png_encoder
182cp ../skia_data/encoder_seed_corpus.zip $OUT/png_encoder_seed_corpus.zip
183
184mv out/Fuzz/jpeg_encoder $OUT/jpeg_encoder
185cp ../skia_data/encoder_seed_corpus.zip $OUT/jpeg_encoder_seed_corpus.zip
186
187mv out/Fuzz/webp_encoder $OUT/webp_encoder
188mv ../skia_data/encoder_seed_corpus.zip $OUT/webp_encoder_seed_corpus.zip
189
190mv out/Fuzz/skottie_json $OUT/skottie_json
191mv ../skia_data/skottie_json_seed_corpus.zip $OUT/skottie_json_seed_corpus.zip
192
193mv out/Fuzz/skjson $OUT/skjson
194mv ../skia_data/json.dict $OUT/skjson.dict
195mv ../skia_data/skjson_seed_corpus.zip $OUT/skjson_seed_corpus.zip
196
197# These 4 use the same canvas_seed_corpus.
198mv out/Fuzz/api_mock_gpu_canvas $OUT/api_mock_gpu_canvas
199cp ../skia_data/canvas_seed_corpus.zip $OUT/api_mock_gpu_canvas_seed_corpus.zip
200
201mv out/Fuzz/api_raster_n32_canvas $OUT/api_raster_n32_canvas
202cp ../skia_data/canvas_seed_corpus.zip $OUT/api_raster_n32_canvas_seed_corpus.zip
203
204mv out/Fuzz/api_svg_canvas $OUT/api_svg_canvas
205cp ../skia_data/canvas_seed_corpus.zip $OUT/api_svg_canvas_seed_corpus.zip
206
207mv out/Fuzz/api_null_canvas $OUT/api_null_canvas
208mv ../skia_data/canvas_seed_corpus.zip $OUT/api_null_canvas_seed_corpus.zip
209
210mv out/Fuzz/api_image_filter $OUT/api_image_filter
211mv ../skia_data/api_image_filter_seed_corpus.zip $OUT/api_image_filter_seed_corpus.zip
212
213mv out/Fuzz/api_polyutils $OUT/api_polyutils
214mv ../skia_data/api_polyutils_seed_corpus.zip $OUT/api_polyutils_seed_corpus.zip
215
216# These 3 use the same corpus.
217mv out/Fuzz/image_decode $OUT/image_decode
218cp ../skia_data/image_decode_seed_corpus.zip $OUT/image_decode_seed_corpus.zip
219
220mv out/Fuzz/android_codec $OUT/android_codec
221cp ../skia_data/image_decode_seed_corpus.zip $OUT/android_codec_seed_corpus.zip.
222
223mv out/Fuzz/image_decode_incremental $OUT/image_decode_incremental
224mv ../skia_data/image_decode_seed_corpus.zip $OUT/image_decode_incremental_seed_corpus.zip
225
226# These 4 use the same sksl_seed_corpus.
227mv out/Fuzz/sksl2glsl $OUT/sksl2glsl
228cp ../skia_data/sksl_seed_corpus.zip $OUT/sksl2glsl_seed_corpus.zip
229
230mv out/Fuzz/sksl2spirv $OUT/sksl2spirv
231cp ../skia_data/sksl_seed_corpus.zip $OUT/sksl2spirv_seed_corpus.zip
232
233mv out/Fuzz/sksl2metal $OUT/sksl2metal
234cp ../skia_data/sksl_seed_corpus.zip $OUT/sksl2metal_seed_corpus.zip
235
236mv out/Fuzz/sksl2pipeline $OUT/sksl2pipeline
237mv ../skia_data/sksl_seed_corpus.zip $OUT/sksl2pipeline_seed_corpus.zip
238
239mv out/Fuzz/skdescriptor_deserialize $OUT/skdescriptor_deserialize
240
241mv out/Fuzz/svg_dom $OUT/svg_dom
242mv ../skia_data/svg_dom_seed_corpus.zip $OUT/svg_dom_seed_corpus.zip
243
244
245mv out/Fuzz/skruntimeeffect $OUT/skruntimeeffect
246mv ../skia_data/sksl_with_256_padding_seed_corpus.zip $OUT/skruntimeeffect_seed_corpus.zip
247
248mv out/Fuzz/api_create_ddl $OUT/api_create_ddl
249
250mv out/Fuzz/api_ddl_threading $OUT/api_ddl_threading
251
252mv out/Fuzz/skp $OUT/skp
253mv ../skia_data/skp_seed_corpus.zip $OUT/skp_seed_corpus.zip
254
255mv out/Fuzz/api_skparagraph $OUT/api_skparagraph
256
257mv out/Fuzz/api_regionop $OUT/api_regionop
258