blob: c780b6f970b44b7452f7f2bae391be9638cbaf1a [file] [log] [blame]
Mitch Phillipsda9a4632019-07-15 09:34:09 -07001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
Kris Alderf979ee32019-10-22 10:52:01 -070018 "encoding/json"
Mitch Phillips4de896e2019-08-28 16:04:36 -070019 "path/filepath"
Mitch Phillipse1ee1a12019-10-17 19:20:41 -070020 "sort"
Mitch Phillipsa0a5e192019-09-27 14:00:06 -070021 "strings"
Mitch Phillips4de896e2019-08-28 16:04:36 -070022
Victor Chang00c144f2021-02-09 12:30:33 +000023 "github.com/google/blueprint/proptools"
24
Mitch Phillipsda9a4632019-07-15 09:34:09 -070025 "android/soong/android"
26 "android/soong/cc/config"
27)
28
Kris Alderf979ee32019-10-22 10:52:01 -070029type FuzzConfig struct {
30 // Email address of people to CC on bugs or contact about this fuzz target.
31 Cc []string `json:"cc,omitempty"`
hamzeh3478a0d2019-12-16 16:25:50 -080032 // Specify whether to enable continuous fuzzing on devices. Defaults to true.
33 Fuzz_on_haiku_device *bool `json:"fuzz_on_haiku_device,omitempty"`
34 // Specify whether to enable continuous fuzzing on host. Defaults to true.
35 Fuzz_on_haiku_host *bool `json:"fuzz_on_haiku_host,omitempty"`
Kris Alderf979ee32019-10-22 10:52:01 -070036 // Component in Google's bug tracking system that bugs should be filed to.
37 Componentid *int64 `json:"componentid,omitempty"`
38 // Hotlists in Google's bug tracking system that bugs should be marked with.
39 Hotlists []string `json:"hotlists,omitempty"`
Kris Aldere051d0d2020-04-28 18:32:23 +000040 // Specify whether this fuzz target was submitted by a researcher. Defaults
41 // to false.
42 Researcher_submitted *bool `json:"researcher_submitted,omitempty"`
Kris Alder2598c9b2020-09-29 22:09:36 +000043 // Specify who should be acknowledged for CVEs in the Android Security
44 // Bulletin.
45 Acknowledgement []string `json:"acknowledgement,omitempty"`
Kris Alderc81f59f2021-01-07 20:57:23 +000046 // Additional options to be passed to libfuzzer when run in Haiku.
47 Libfuzzer_options []string `json:"libfuzzer_options,omitempty"`
48 // Additional options to be passed to HWASAN when running on-device in Haiku.
49 Hwasan_options []string `json:"hwasan_options,omitempty"`
50 // Additional options to be passed to HWASAN when running on host in Haiku.
51 Asan_options []string `json:"asan_options,omitempty"`
Kris Alderf979ee32019-10-22 10:52:01 -070052}
53
54func (f *FuzzConfig) String() string {
55 b, err := json.Marshal(f)
56 if err != nil {
57 panic(err)
58 }
59
60 return string(b)
61}
62
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -070063type FuzzProperties struct {
64 // Optional list of seed files to be installed to the fuzz target's output
65 // directory.
66 Corpus []string `android:"path"`
Tri Voad172d82019-11-27 13:45:45 -080067 // Optional list of data files to be installed to the fuzz target's output
68 // directory. Directory structure relative to the module is preserved.
69 Data []string `android:"path"`
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -070070 // Optional dictionary to be installed to the fuzz target's output directory.
71 Dictionary *string `android:"path"`
Kris Alderf979ee32019-10-22 10:52:01 -070072 // Config for running the target on fuzzing infrastructure.
73 Fuzz_config *FuzzConfig
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -070074}
75
Mitch Phillipsda9a4632019-07-15 09:34:09 -070076func init() {
77 android.RegisterModuleType("cc_fuzz", FuzzFactory)
Mitch Phillipsd3254b42019-09-24 13:03:28 -070078 android.RegisterSingletonType("cc_fuzz_packaging", fuzzPackagingFactory)
Mitch Phillipsda9a4632019-07-15 09:34:09 -070079}
80
81// cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at
82// $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on
83// your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree.
84func FuzzFactory() android.Module {
85 module := NewFuzz(android.HostAndDeviceSupported)
86 return module.Init()
87}
88
89func NewFuzzInstaller() *baseInstaller {
90 return NewBaseInstaller("fuzz", "fuzz", InstallInData)
91}
92
93type fuzzBinary struct {
94 *binaryDecorator
95 *baseCompiler
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -070096
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -070097 Properties FuzzProperties
98 dictionary android.Path
99 corpus android.Paths
100 corpusIntermediateDir android.Path
Kris Alderf979ee32019-10-22 10:52:01 -0700101 config android.Path
Tri Voad172d82019-11-27 13:45:45 -0800102 data android.Paths
103 dataIntermediateDir android.Path
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700104 installedSharedDeps []string
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700105}
106
107func (fuzz *fuzzBinary) linkerProps() []interface{} {
108 props := fuzz.binaryDecorator.linkerProps()
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700109 props = append(props, &fuzz.Properties)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700110 return props
111}
112
113func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) {
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700114 fuzz.binaryDecorator.linkerInit(ctx)
115}
116
117func (fuzz *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
118 deps.StaticLibs = append(deps.StaticLibs,
119 config.LibFuzzerRuntimeLibrary(ctx.toolchain()))
120 deps = fuzz.binaryDecorator.linkerDeps(ctx, deps)
121 return deps
122}
123
124func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
125 flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800126 // RunPaths on devices isn't instantiated by the base linker. `../lib` for
127 // installed fuzz targets (both host and device), and `./lib` for fuzz
128 // target packages.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700129 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800130 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700131 return flags
132}
133
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700134// This function performs a breadth-first search over the provided module's
135// dependencies using `visitDirectDeps` to enumerate all shared library
136// dependencies. We require breadth-first expansion, as otherwise we may
137// incorrectly use the core libraries (sanitizer runtimes, libc, libdl, etc.)
138// from a dependency. This may cause issues when dependencies have explicit
139// sanitizer tags, as we may get a dependency on an unsanitized libc, etc.
Colin Crossdc809f92019-11-20 15:58:32 -0800140func collectAllSharedDependencies(ctx android.SingletonContext, module android.Module) android.Paths {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700141 var fringe []android.Module
142
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700143 seen := make(map[string]bool)
Colin Crossdc809f92019-11-20 15:58:32 -0800144
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700145 // Enumerate the first level of dependencies, as we discard all non-library
146 // modules in the BFS loop below.
147 ctx.VisitDirectDeps(module, func(dep android.Module) {
Colin Crossdc809f92019-11-20 15:58:32 -0800148 if isValidSharedDependency(dep) {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800149 fringe = append(fringe, dep)
150 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700151 })
152
Colin Crossdc809f92019-11-20 15:58:32 -0800153 var sharedLibraries android.Paths
154
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700155 for i := 0; i < len(fringe); i++ {
156 module := fringe[i]
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700157 if seen[module.Name()] {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700158 continue
159 }
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700160 seen[module.Name()] = true
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700161
162 ccModule := module.(*Module)
Colin Crossdc809f92019-11-20 15:58:32 -0800163 sharedLibraries = append(sharedLibraries, ccModule.UnstrippedOutputFile())
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700164 ctx.VisitDirectDeps(module, func(dep android.Module) {
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700165 if isValidSharedDependency(dep) && !seen[dep.Name()] {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800166 fringe = append(fringe, dep)
167 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700168 })
169 }
Colin Crossdc809f92019-11-20 15:58:32 -0800170
171 return sharedLibraries
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700172}
173
174// This function takes a module and determines if it is a unique shared library
175// that should be installed in the fuzz target output directories. This function
176// returns true, unless:
Victor Chang00c144f2021-02-09 12:30:33 +0000177// - The module is not an installable shared library, or
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100178// - The module is a header, stub, or vendor-linked library, or
179// - The module is a prebuilt and its source is available, or
180// - The module is a versioned member of an SDK snapshot.
Colin Crossdc809f92019-11-20 15:58:32 -0800181func isValidSharedDependency(dependency android.Module) bool {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700182 // TODO(b/144090547): We should be parsing these modules using
183 // ModuleDependencyTag instead of the current brute-force checking.
184
Colin Cross31076b32020-10-23 17:22:06 -0700185 linkable, ok := dependency.(LinkableInterface)
186 if !ok || !linkable.CcLibraryInterface() {
187 // Discard non-linkables.
188 return false
189 }
190
191 if !linkable.Shared() {
192 // Discard static libs.
193 return false
194 }
195
196 if linkable.UseVndk() {
197 // Discard vendor linked libraries.
198 return false
199 }
200
201 if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800202 // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not
203 // be excluded on the basis of they're not CCLibrary()'s.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700204 return false
205 }
206
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800207 // We discarded module stubs libraries above, but the LLNDK prebuilts stubs
208 // libraries must be handled differently - by looking for the stubDecorator.
209 // Discard LLNDK prebuilts stubs as well.
210 if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary {
211 if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary {
212 return false
213 }
Victor Chang00c144f2021-02-09 12:30:33 +0000214 // Discard installable:false libraries because they are expected to be absent
215 // in runtime.
216 if !proptools.BoolDefault(ccLibrary.Properties.Installable, true) {
217 return false
218 }
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800219 }
220
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100221 // If the same library is present both as source and a prebuilt we must pick
222 // only one to avoid a conflict. Always prefer the source since the prebuilt
223 // probably won't be built with sanitizers enabled.
Paul Duffinf7c99f52021-04-28 10:41:21 +0100224 if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() {
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100225 return false
226 }
227
228 // Discard versioned members of SDK snapshots, because they will conflict with
229 // unversioned ones.
230 if sdkMember, ok := dependency.(android.SdkAware); ok && !sdkMember.ContainingSdk().Unversioned() {
231 return false
232 }
233
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700234 return true
235}
236
237func sharedLibraryInstallLocation(
238 libraryPath android.Path, isHost bool, archString string) string {
239 installLocation := "$(PRODUCT_OUT)/data"
240 if isHost {
241 installLocation = "$(HOST_OUT)"
242 }
243 installLocation = filepath.Join(
244 installLocation, "fuzz", archString, "lib", libraryPath.Base())
245 return installLocation
246}
247
Mitch Phillips0bf97132020-03-06 09:38:12 -0800248// Get the device-only shared library symbols install directory.
249func sharedLibrarySymbolsInstallLocation(libraryPath android.Path, archString string) string {
250 return filepath.Join("$(PRODUCT_OUT)/symbols/data/fuzz/", archString, "/lib/", libraryPath.Base())
251}
252
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700253func (fuzz *fuzzBinary) install(ctx ModuleContext, file android.Path) {
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700254 fuzz.binaryDecorator.baseInstaller.dir = filepath.Join(
255 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
256 fuzz.binaryDecorator.baseInstaller.dir64 = filepath.Join(
257 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700258 fuzz.binaryDecorator.baseInstaller.install(ctx, file)
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700259
260 fuzz.corpus = android.PathsForModuleSrc(ctx, fuzz.Properties.Corpus)
Colin Crossf1a035e2020-11-16 17:32:30 -0800261 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700262 intermediateDir := android.PathForModuleOut(ctx, "corpus")
263 for _, entry := range fuzz.corpus {
264 builder.Command().Text("cp").
265 Input(entry).
266 Output(intermediateDir.Join(ctx, entry.Base()))
267 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800268 builder.Build("copy_corpus", "copy corpus")
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700269 fuzz.corpusIntermediateDir = intermediateDir
270
Tri Voad172d82019-11-27 13:45:45 -0800271 fuzz.data = android.PathsForModuleSrc(ctx, fuzz.Properties.Data)
Colin Crossf1a035e2020-11-16 17:32:30 -0800272 builder = android.NewRuleBuilder(pctx, ctx)
Tri Voad172d82019-11-27 13:45:45 -0800273 intermediateDir = android.PathForModuleOut(ctx, "data")
274 for _, entry := range fuzz.data {
275 builder.Command().Text("cp").
276 Input(entry).
277 Output(intermediateDir.Join(ctx, entry.Rel()))
278 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800279 builder.Build("copy_data", "copy data")
Tri Voad172d82019-11-27 13:45:45 -0800280 fuzz.dataIntermediateDir = intermediateDir
281
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700282 if fuzz.Properties.Dictionary != nil {
283 fuzz.dictionary = android.PathForModuleSrc(ctx, *fuzz.Properties.Dictionary)
284 if fuzz.dictionary.Ext() != ".dict" {
285 ctx.PropertyErrorf("dictionary",
286 "Fuzzer dictionary %q does not have '.dict' extension",
287 fuzz.dictionary.String())
288 }
289 }
Kris Alderf979ee32019-10-22 10:52:01 -0700290
291 if fuzz.Properties.Fuzz_config != nil {
Kris Alderdb97af42019-10-30 10:17:04 -0700292 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
Colin Crosscf371cc2020-11-13 11:48:42 -0800293 android.WriteFileRule(ctx, configPath, fuzz.Properties.Fuzz_config.String())
Kris Alderf979ee32019-10-22 10:52:01 -0700294 fuzz.config = configPath
295 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700296
297 // Grab the list of required shared libraries.
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700298 seen := make(map[string]bool)
Colin Crossdc809f92019-11-20 15:58:32 -0800299 var sharedLibraries android.Paths
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700300 ctx.WalkDeps(func(child, parent android.Module) bool {
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700301 if seen[child.Name()] {
Colin Crossdc809f92019-11-20 15:58:32 -0800302 return false
303 }
Mitch Phillipsc0b442f2020-04-27 16:44:58 -0700304 seen[child.Name()] = true
Colin Crossdc809f92019-11-20 15:58:32 -0800305
306 if isValidSharedDependency(child) {
307 sharedLibraries = append(sharedLibraries, child.(*Module).UnstrippedOutputFile())
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700308 return true
309 }
310 return false
311 })
312
313 for _, lib := range sharedLibraries {
314 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
315 sharedLibraryInstallLocation(
316 lib, ctx.Host(), ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800317
318 // Also add the dependency on the shared library symbols dir.
319 if !ctx.Host() {
320 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
321 sharedLibrarySymbolsInstallLocation(lib, ctx.Arch().ArchType.String()))
322 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700323 }
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700324}
325
326func NewFuzz(hod android.HostOrDeviceSupported) *Module {
327 module, binary := NewBinary(hod)
328
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700329 binary.baseInstaller = NewFuzzInstaller()
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500330 module.sanitize.SetSanitizer(Fuzzer, true)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700331
332 fuzz := &fuzzBinary{
333 binaryDecorator: binary,
334 baseCompiler: NewBaseCompiler(),
335 }
336 module.compiler = fuzz
337 module.linker = fuzz
338 module.installer = fuzz
Colin Crosseec9b282019-07-18 16:20:52 -0700339
340 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
341 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Alex Light71123ec2019-07-24 13:34:19 -0700342 disableDarwinAndLinuxBionic := struct {
Colin Crosseec9b282019-07-18 16:20:52 -0700343 Target struct {
344 Darwin struct {
345 Enabled *bool
346 }
Alex Light71123ec2019-07-24 13:34:19 -0700347 Linux_bionic struct {
348 Enabled *bool
349 }
Colin Crosseec9b282019-07-18 16:20:52 -0700350 }
351 }{}
Alex Light71123ec2019-07-24 13:34:19 -0700352 disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false)
353 disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false)
354 ctx.AppendProperties(&disableDarwinAndLinuxBionic)
Colin Crosseec9b282019-07-18 16:20:52 -0700355 })
356
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700357 return module
358}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700359
360// Responsible for generating GNU Make rules that package fuzz targets into
361// their architecture & target/host specific zip file.
362type fuzzPackager struct {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700363 packages android.Paths
364 sharedLibInstallStrings []string
365 fuzzTargets map[string]bool
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700366}
367
368func fuzzPackagingFactory() android.Singleton {
369 return &fuzzPackager{}
370}
371
372type fileToZip struct {
373 SourceFilePath android.Path
374 DestinationPathPrefix string
375}
376
Colin Crossdc809f92019-11-20 15:58:32 -0800377type archOs struct {
378 hostOrTarget string
379 arch string
380 dir string
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700381}
382
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700383func (s *fuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
384 // Map between each architecture + host/device combination, and the files that
385 // need to be packaged (in the tuple of {source file, destination folder in
386 // archive}).
Colin Crossdc809f92019-11-20 15:58:32 -0800387 archDirs := make(map[archOs][]fileToZip)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700388
Colin Crossdc809f92019-11-20 15:58:32 -0800389 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
390 // multiple fuzzers that depend on the same shared library.
391 sharedLibraryInstalled := make(map[string]bool)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700392
393 // List of individual fuzz targets, so that 'make fuzz' also installs the targets
394 // to the correct output directories as well.
395 s.fuzzTargets = make(map[string]bool)
396
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700397 ctx.VisitAllModules(func(module android.Module) {
398 // Discard non-fuzz targets.
399 ccModule, ok := module.(*Module)
400 if !ok {
401 return
402 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700403
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700404 fuzzModule, ok := ccModule.compiler.(*fuzzBinary)
405 if !ok {
406 return
407 }
408
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700409 // Discard ramdisk + vendor_ramdisk + recovery modules, they're duplicates of
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800410 // fuzz targets we're going to package anyway.
411 if !ccModule.Enabled() || ccModule.Properties.PreventInstall ||
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700412 ccModule.InRamdisk() || ccModule.InVendorRamdisk() || ccModule.InRecovery() {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700413 return
414 }
415
Mitch Phillips6a9bf212019-12-05 07:36:11 -0800416 // Discard modules that are in an unavailable namespace.
417 if !ccModule.ExportedToMake() {
418 return
419 }
420
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700421 hostOrTargetString := "target"
422 if ccModule.Host() {
423 hostOrTargetString = "host"
424 }
425
426 archString := ccModule.Arch().ArchType.String()
427 archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
Colin Crossdc809f92019-11-20 15:58:32 -0800428 archOs := archOs{hostOrTarget: hostOrTargetString, arch: archString, dir: archDir.String()}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700429
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700430 // Grab the list of required shared libraries.
Colin Crossdc809f92019-11-20 15:58:32 -0800431 sharedLibraries := collectAllSharedDependencies(ctx, module)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700432
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800433 var files []fileToZip
Colin Crossf1a035e2020-11-16 17:32:30 -0800434 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800435
436 // Package the corpora into a zipfile.
437 if fuzzModule.corpus != nil {
438 corpusZip := archDir.Join(ctx, module.Name()+"_seed_corpus.zip")
Colin Crossf1a035e2020-11-16 17:32:30 -0800439 command := builder.Command().BuiltTool("soong_zip").
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800440 Flag("-j").
441 FlagWithOutput("-o ", corpusZip)
Colin Cross70c47412021-03-12 17:48:14 -0800442 rspFile := corpusZip.ReplaceExtension(ctx, "rsp")
443 command.FlagWithRspFileInputList("-r ", rspFile, fuzzModule.corpus)
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800444 files = append(files, fileToZip{corpusZip, ""})
445 }
446
Tri Voad172d82019-11-27 13:45:45 -0800447 // Package the data into a zipfile.
448 if fuzzModule.data != nil {
449 dataZip := archDir.Join(ctx, module.Name()+"_data.zip")
Colin Crossf1a035e2020-11-16 17:32:30 -0800450 command := builder.Command().BuiltTool("soong_zip").
Tri Voad172d82019-11-27 13:45:45 -0800451 FlagWithOutput("-o ", dataZip)
452 for _, f := range fuzzModule.data {
453 intermediateDir := strings.TrimSuffix(f.String(), f.Rel())
454 command.FlagWithArg("-C ", intermediateDir)
455 command.FlagWithInput("-f ", f)
456 }
457 files = append(files, fileToZip{dataZip, ""})
458 }
459
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800460 // Find and mark all the transiently-dependent shared libraries for
461 // packaging.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700462 for _, library := range sharedLibraries {
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800463 files = append(files, fileToZip{library, "lib"})
Mitch Phillips13ed3f52019-11-12 11:12:10 -0800464
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700465 // For each architecture-specific shared library dependency, we need to
466 // install it to the output directory. Setup the install destination here,
467 // which will be used by $(copy-many-files) in the Make backend.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700468 installDestination := sharedLibraryInstallLocation(
469 library, ccModule.Host(), archString)
Colin Crossdc809f92019-11-20 15:58:32 -0800470 if sharedLibraryInstalled[installDestination] {
471 continue
472 }
473 sharedLibraryInstalled[installDestination] = true
Mitch Phillips0bf97132020-03-06 09:38:12 -0800474
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700475 // Escape all the variables, as the install destination here will be called
476 // via. $(eval) in Make.
477 installDestination = strings.ReplaceAll(
478 installDestination, "$", "$$")
479 s.sharedLibInstallStrings = append(s.sharedLibInstallStrings,
480 library.String()+":"+installDestination)
Mitch Phillips0bf97132020-03-06 09:38:12 -0800481
482 // Ensure that on device, the library is also reinstalled to the /symbols/
483 // dir. Symbolized DSO's are always installed to the device when fuzzing, but
484 // we want symbolization tools (like `stack`) to be able to find the symbols
485 // in $ANDROID_PRODUCT_OUT/symbols automagically.
486 if !ccModule.Host() {
487 symbolsInstallDestination := sharedLibrarySymbolsInstallLocation(library, archString)
488 symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
489 s.sharedLibInstallStrings = append(s.sharedLibInstallStrings,
490 library.String()+":"+symbolsInstallDestination)
491 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700492 }
493
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700494 // The executable.
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800495 files = append(files, fileToZip{ccModule.UnstrippedOutputFile(), ""})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700496
497 // The dictionary.
498 if fuzzModule.dictionary != nil {
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800499 files = append(files, fileToZip{fuzzModule.dictionary, ""})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700500 }
Kris Alderf979ee32019-10-22 10:52:01 -0700501
502 // Additional fuzz config.
503 if fuzzModule.config != nil {
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800504 files = append(files, fileToZip{fuzzModule.config, ""})
Kris Alderf979ee32019-10-22 10:52:01 -0700505 }
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800506
507 fuzzZip := archDir.Join(ctx, module.Name()+".zip")
Colin Crossf1a035e2020-11-16 17:32:30 -0800508 command := builder.Command().BuiltTool("soong_zip").
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800509 Flag("-j").
510 FlagWithOutput("-o ", fuzzZip)
511 for _, file := range files {
512 if file.DestinationPathPrefix != "" {
513 command.FlagWithArg("-P ", file.DestinationPathPrefix)
514 } else {
515 command.Flag("-P ''")
516 }
517 command.FlagWithInput("-f ", file.SourceFilePath)
518 }
519
Colin Crossf1a035e2020-11-16 17:32:30 -0800520 builder.Build("create-"+fuzzZip.String(),
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800521 "Package "+module.Name()+" for "+archString+"-"+hostOrTargetString)
522
Mitch Phillips18e67192020-02-24 08:26:20 -0800523 // Don't add modules to 'make haiku' that are set to not be exported to the
524 // fuzzing infrastructure.
525 if config := fuzzModule.Properties.Fuzz_config; config != nil {
526 if ccModule.Host() && !BoolDefault(config.Fuzz_on_haiku_host, true) {
527 return
528 } else if !BoolDefault(config.Fuzz_on_haiku_device, true) {
529 return
530 }
531 }
532
533 s.fuzzTargets[module.Name()] = true
Colin Crossdc809f92019-11-20 15:58:32 -0800534 archDirs[archOs] = append(archDirs[archOs], fileToZip{fuzzZip, ""})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700535 })
536
Colin Crossdc809f92019-11-20 15:58:32 -0800537 var archOsList []archOs
538 for archOs := range archDirs {
539 archOsList = append(archOsList, archOs)
540 }
541 sort.Slice(archOsList, func(i, j int) bool { return archOsList[i].dir < archOsList[j].dir })
542
543 for _, archOs := range archOsList {
544 filesToZip := archDirs[archOs]
545 arch := archOs.arch
546 hostOrTarget := archOs.hostOrTarget
Colin Crossf1a035e2020-11-16 17:32:30 -0800547 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700548 outputFile := android.PathForOutput(ctx, "fuzz-"+hostOrTarget+"-"+arch+".zip")
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700549 s.packages = append(s.packages, outputFile)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700550
Colin Crossf1a035e2020-11-16 17:32:30 -0800551 command := builder.Command().BuiltTool("soong_zip").
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700552 Flag("-j").
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800553 FlagWithOutput("-o ", outputFile).
554 Flag("-L 0") // No need to try and re-compress the zipfiles.
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700555
556 for _, fileToZip := range filesToZip {
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800557 if fileToZip.DestinationPathPrefix != "" {
558 command.FlagWithArg("-P ", fileToZip.DestinationPathPrefix)
559 } else {
560 command.Flag("-P ''")
561 }
562 command.FlagWithInput("-f ", fileToZip.SourceFilePath)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700563 }
564
Colin Crossf1a035e2020-11-16 17:32:30 -0800565 builder.Build("create-fuzz-package-"+arch+"-"+hostOrTarget,
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700566 "Create fuzz target packages for "+arch+"-"+hostOrTarget)
567 }
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700568}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700569
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700570func (s *fuzzPackager) MakeVars(ctx android.MakeVarsContext) {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700571 packages := s.packages.Strings()
572 sort.Strings(packages)
573 sort.Strings(s.sharedLibInstallStrings)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700574 // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
575 // ready to handle phony targets created in Soong. In the meantime, this
576 // exports the phony 'fuzz' target and dependencies on packages to
577 // core/main.mk so that we can use dist-for-goals.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700578 ctx.Strict("SOONG_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
579 ctx.Strict("FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
580 strings.Join(s.sharedLibInstallStrings, " "))
581
582 // Preallocate the slice of fuzz targets to minimise memory allocations.
583 fuzzTargets := make([]string, 0, len(s.fuzzTargets))
584 for target, _ := range s.fuzzTargets {
585 fuzzTargets = append(fuzzTargets, target)
586 }
587 sort.Strings(fuzzTargets)
588 ctx.Strict("ALL_FUZZ_TARGETS", strings.Join(fuzzTargets, " "))
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700589}