blob: 89b3d5f6b94314d1fe4c23c4f1721b924f0d697f [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 (
Mitch Phillips4de896e2019-08-28 16:04:36 -070018 "path/filepath"
Mitch Phillipse1ee1a12019-10-17 19:20:41 -070019 "sort"
Mitch Phillipsa0a5e192019-09-27 14:00:06 -070020 "strings"
Mitch Phillips4de896e2019-08-28 16:04:36 -070021
Victor Chang00c144f2021-02-09 12:30:33 +000022 "github.com/google/blueprint/proptools"
23
Mitch Phillipsda9a4632019-07-15 09:34:09 -070024 "android/soong/android"
25 "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070026 "android/soong/fuzz"
Mitch Phillipsda9a4632019-07-15 09:34:09 -070027)
28
29func init() {
Cory Barkera1da26f2022-06-07 20:12:06 +000030 android.RegisterModuleType("cc_fuzz", LibFuzzFactory)
Mitch Phillipsd3254b42019-09-24 13:03:28 -070031 android.RegisterSingletonType("cc_fuzz_packaging", fuzzPackagingFactory)
Cory Barkera1da26f2022-06-07 20:12:06 +000032}
33
34type FuzzProperties struct {
Cory Barker9cfcf6d2022-07-22 17:22:02 +000035 FuzzFramework fuzz.Framework `blueprint:"mutated"`
Cory Barkera1da26f2022-06-07 20:12:06 +000036}
37
38type fuzzer struct {
39 Properties FuzzProperties
40}
41
42func (fuzzer *fuzzer) flags(ctx ModuleContext, flags Flags) Flags {
Cory Barker9cfcf6d2022-07-22 17:22:02 +000043 if fuzzer.Properties.FuzzFramework == fuzz.AFL {
44 flags.Local.CFlags = append(flags.Local.CFlags, []string{
45 "-fsanitize-coverage=trace-pc-guard",
46 "-Wno-unused-result",
47 "-Wno-unused-parameter",
48 "-Wno-unused-function",
49 }...)
Cory Barkera1da26f2022-06-07 20:12:06 +000050 }
51
52 return flags
53}
54
55func (fuzzer *fuzzer) props() []interface{} {
56 return []interface{}{&fuzzer.Properties}
57}
58
59func fuzzMutatorDeps(mctx android.TopDownMutatorContext) {
60 currentModule, ok := mctx.Module().(*Module)
61 if !ok {
62 return
63 }
64
Cory Barker9cfcf6d2022-07-22 17:22:02 +000065 if currentModule.fuzzer == nil {
Cory Barkera1da26f2022-06-07 20:12:06 +000066 return
67 }
68
69 mctx.WalkDeps(func(child android.Module, parent android.Module) bool {
70 c, ok := child.(*Module)
71 if !ok {
72 return false
73 }
74
75 if c.sanitize == nil {
76 return false
77 }
78
79 isFuzzerPointer := c.sanitize.getSanitizerBoolPtr(Fuzzer)
80 if isFuzzerPointer == nil || !*isFuzzerPointer {
81 return false
82 }
83
84 if c.fuzzer == nil {
85 return false
86 }
87
Cory Barker9cfcf6d2022-07-22 17:22:02 +000088 c.fuzzer.Properties.FuzzFramework = currentModule.fuzzer.Properties.FuzzFramework
Cory Barkera1da26f2022-06-07 20:12:06 +000089 return true
90 })
91}
92
Mitch Phillipsda9a4632019-07-15 09:34:09 -070093// cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at
94// $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on
95// your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree.
Cory Barkera1da26f2022-06-07 20:12:06 +000096func LibFuzzFactory() android.Module {
Cory Barker9cfcf6d2022-07-22 17:22:02 +000097 module := NewFuzzer(android.HostAndDeviceSupported)
Cory Barkera1da26f2022-06-07 20:12:06 +000098 return module.Init()
Mitch Phillipsda9a4632019-07-15 09:34:09 -070099}
100
101type fuzzBinary struct {
102 *binaryDecorator
103 *baseCompiler
Cory Barkera1da26f2022-06-07 20:12:06 +0000104 fuzzPackagedModule fuzz.FuzzPackagedModule
hamzeh41ad8812021-07-07 14:00:07 -0700105 installedSharedDeps []string
Colin Cross31d89b42022-10-04 16:35:39 -0700106 sharedLibraries android.Paths
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700107}
108
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400109func (fuzz *fuzzBinary) fuzzBinary() bool {
110 return true
111}
112
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700113func (fuzz *fuzzBinary) linkerProps() []interface{} {
114 props := fuzz.binaryDecorator.linkerProps()
hamzeh41ad8812021-07-07 14:00:07 -0700115 props = append(props, &fuzz.fuzzPackagedModule.FuzzProperties)
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000116
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700117 return props
118}
119
120func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) {
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700121 fuzz.binaryDecorator.linkerInit(ctx)
122}
123
Cory Barkera1da26f2022-06-07 20:12:06 +0000124func (fuzzBin *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000125 if ctx.Config().Getenv("FUZZ_FRAMEWORK") == "AFL" {
Cory Barkera1da26f2022-06-07 20:12:06 +0000126 deps.HeaderLibs = append(deps.HeaderLibs, "libafl_headers")
Cory Barkera1da26f2022-06-07 20:12:06 +0000127 } else {
128 deps.StaticLibs = append(deps.StaticLibs, config.LibFuzzerRuntimeLibrary(ctx.toolchain()))
Cory Barkera1da26f2022-06-07 20:12:06 +0000129 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000130
131 deps = fuzzBin.binaryDecorator.linkerDeps(ctx, deps)
132 return deps
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700133}
134
135func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
136 flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800137 // RunPaths on devices isn't instantiated by the base linker. `../lib` for
138 // installed fuzz targets (both host and device), and `./lib` for fuzz
139 // target packages.
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800140 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
Cory Barkera1da26f2022-06-07 20:12:06 +0000141
Kris Alderc2634812022-10-25 10:58:59 -0700142 // When running on device, fuzz targets with vendor: true set will be in
143 // fuzzer_name/vendor/fuzzer_name (note the extra 'vendor' and thus need to
144 // link with libraries in ../../lib/. Non-vendor binaries only need to look
145 // one level up, in ../lib/.
146 if ctx.inVendor() {
147 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../../lib`)
148 } else {
149 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
150 }
151
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700152 return flags
153}
154
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400155// IsValidSharedDependency takes a module and determines if it is a unique shared library
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700156// that should be installed in the fuzz target output directories. This function
157// returns true, unless:
Colin Crossd079e0b2022-08-16 10:27:33 -0700158// - The module is not an installable shared library, or
159// - The module is a header or stub, or
160// - The module is a prebuilt and its source is available, or
161// - The module is a versioned member of an SDK snapshot.
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400162func IsValidSharedDependency(dependency android.Module) bool {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700163 // TODO(b/144090547): We should be parsing these modules using
164 // ModuleDependencyTag instead of the current brute-force checking.
165
Colin Cross31076b32020-10-23 17:22:06 -0700166 linkable, ok := dependency.(LinkableInterface)
167 if !ok || !linkable.CcLibraryInterface() {
168 // Discard non-linkables.
169 return false
170 }
171
172 if !linkable.Shared() {
173 // Discard static libs.
174 return false
175 }
176
Colin Cross31076b32020-10-23 17:22:06 -0700177 if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800178 // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not
179 // be excluded on the basis of they're not CCLibrary()'s.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700180 return false
181 }
182
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800183 // We discarded module stubs libraries above, but the LLNDK prebuilts stubs
184 // libraries must be handled differently - by looking for the stubDecorator.
185 // Discard LLNDK prebuilts stubs as well.
186 if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary {
187 if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary {
188 return false
189 }
Victor Chang00c144f2021-02-09 12:30:33 +0000190 // Discard installable:false libraries because they are expected to be absent
191 // in runtime.
Colin Cross1bc94122021-10-28 13:25:54 -0700192 if !proptools.BoolDefault(ccLibrary.Installable(), true) {
Victor Chang00c144f2021-02-09 12:30:33 +0000193 return false
194 }
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800195 }
196
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100197 // If the same library is present both as source and a prebuilt we must pick
198 // only one to avoid a conflict. Always prefer the source since the prebuilt
199 // probably won't be built with sanitizers enabled.
Paul Duffinf7c99f52021-04-28 10:41:21 +0100200 if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() {
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100201 return false
202 }
203
204 // Discard versioned members of SDK snapshots, because they will conflict with
205 // unversioned ones.
206 if sdkMember, ok := dependency.(android.SdkAware); ok && !sdkMember.ContainingSdk().Unversioned() {
207 return false
208 }
209
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700210 return true
211}
212
213func sharedLibraryInstallLocation(
Cory Barkera1da26f2022-06-07 20:12:06 +0000214 libraryPath android.Path, isHost bool, fuzzDir string, archString string) string {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700215 installLocation := "$(PRODUCT_OUT)/data"
216 if isHost {
217 installLocation = "$(HOST_OUT)"
218 }
219 installLocation = filepath.Join(
Cory Barkera1da26f2022-06-07 20:12:06 +0000220 installLocation, fuzzDir, archString, "lib", libraryPath.Base())
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700221 return installLocation
222}
223
Mitch Phillips0bf97132020-03-06 09:38:12 -0800224// Get the device-only shared library symbols install directory.
Cory Barkera1da26f2022-06-07 20:12:06 +0000225func sharedLibrarySymbolsInstallLocation(libraryPath android.Path, fuzzDir string, archString string) string {
226 return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, "/lib/", libraryPath.Base())
Mitch Phillips0bf97132020-03-06 09:38:12 -0800227}
228
Cory Barkera1da26f2022-06-07 20:12:06 +0000229func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) {
230 installBase := "fuzz"
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700231
Cory Barkera1da26f2022-06-07 20:12:06 +0000232 fuzzBin.binaryDecorator.baseInstaller.dir = filepath.Join(
233 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
234 fuzzBin.binaryDecorator.baseInstaller.dir64 = filepath.Join(
235 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
236 fuzzBin.binaryDecorator.baseInstaller.install(ctx, file)
237
238 fuzzBin.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzzBin.fuzzPackagedModule.FuzzProperties.Corpus)
Colin Crossf1a035e2020-11-16 17:32:30 -0800239 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700240 intermediateDir := android.PathForModuleOut(ctx, "corpus")
Cory Barkera1da26f2022-06-07 20:12:06 +0000241 for _, entry := range fuzzBin.fuzzPackagedModule.Corpus {
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700242 builder.Command().Text("cp").
243 Input(entry).
244 Output(intermediateDir.Join(ctx, entry.Base()))
245 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800246 builder.Build("copy_corpus", "copy corpus")
Cory Barkera1da26f2022-06-07 20:12:06 +0000247 fuzzBin.fuzzPackagedModule.CorpusIntermediateDir = intermediateDir
Mitch Phillips8a2bc0b2019-10-17 15:04:01 -0700248
Cory Barkera1da26f2022-06-07 20:12:06 +0000249 fuzzBin.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzzBin.fuzzPackagedModule.FuzzProperties.Data)
Colin Crossf1a035e2020-11-16 17:32:30 -0800250 builder = android.NewRuleBuilder(pctx, ctx)
Tri Voad172d82019-11-27 13:45:45 -0800251 intermediateDir = android.PathForModuleOut(ctx, "data")
Cory Barkera1da26f2022-06-07 20:12:06 +0000252 for _, entry := range fuzzBin.fuzzPackagedModule.Data {
Tri Voad172d82019-11-27 13:45:45 -0800253 builder.Command().Text("cp").
254 Input(entry).
255 Output(intermediateDir.Join(ctx, entry.Rel()))
256 }
Colin Crossf1a035e2020-11-16 17:32:30 -0800257 builder.Build("copy_data", "copy data")
Cory Barkera1da26f2022-06-07 20:12:06 +0000258 fuzzBin.fuzzPackagedModule.DataIntermediateDir = intermediateDir
Tri Voad172d82019-11-27 13:45:45 -0800259
Cory Barkera1da26f2022-06-07 20:12:06 +0000260 if fuzzBin.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
261 fuzzBin.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzzBin.fuzzPackagedModule.FuzzProperties.Dictionary)
262 if fuzzBin.fuzzPackagedModule.Dictionary.Ext() != ".dict" {
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700263 ctx.PropertyErrorf("dictionary",
264 "Fuzzer dictionary %q does not have '.dict' extension",
Cory Barkera1da26f2022-06-07 20:12:06 +0000265 fuzzBin.fuzzPackagedModule.Dictionary.String())
Mitch Phillips4e4ab8a2019-09-13 17:32:50 -0700266 }
267 }
Kris Alderf979ee32019-10-22 10:52:01 -0700268
Cory Barkera1da26f2022-06-07 20:12:06 +0000269 if fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
Kris Alderdb97af42019-10-30 10:17:04 -0700270 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
Cory Barkera1da26f2022-06-07 20:12:06 +0000271 android.WriteFileRule(ctx, configPath, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
272 fuzzBin.fuzzPackagedModule.Config = configPath
Kris Alderf979ee32019-10-22 10:52:01 -0700273 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700274
275 // Grab the list of required shared libraries.
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000276 fuzzBin.sharedLibraries, _ = CollectAllSharedDependencies(ctx)
Colin Crossdc809f92019-11-20 15:58:32 -0800277
Colin Cross31d89b42022-10-04 16:35:39 -0700278 for _, lib := range fuzzBin.sharedLibraries {
Cory Barkera1da26f2022-06-07 20:12:06 +0000279 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700280 sharedLibraryInstallLocation(
Cory Barkera1da26f2022-06-07 20:12:06 +0000281 lib, ctx.Host(), installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800282
283 // Also add the dependency on the shared library symbols dir.
284 if !ctx.Host() {
Cory Barkera1da26f2022-06-07 20:12:06 +0000285 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
286 sharedLibrarySymbolsInstallLocation(lib, installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800287 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700288 }
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700289}
290
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000291func NewFuzzer(hod android.HostOrDeviceSupported) *Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400292 module, binary := newBinary(hod, false)
Cory Barkera1da26f2022-06-07 20:12:06 +0000293 baseInstallerPath := "fuzz"
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700294
Cory Barkera1da26f2022-06-07 20:12:06 +0000295 binary.baseInstaller = NewBaseInstaller(baseInstallerPath, baseInstallerPath, InstallInData)
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500296 module.sanitize.SetSanitizer(Fuzzer, true)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700297
Cory Barkera1da26f2022-06-07 20:12:06 +0000298 fuzzBin := &fuzzBinary{
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700299 binaryDecorator: binary,
300 baseCompiler: NewBaseCompiler(),
301 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000302 module.compiler = fuzzBin
303 module.linker = fuzzBin
304 module.installer = fuzzBin
Colin Crosseec9b282019-07-18 16:20:52 -0700305
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000306 module.fuzzer.Properties.FuzzFramework = fuzz.LibFuzzer
307
Colin Crosseec9b282019-07-18 16:20:52 -0700308 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
309 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Alex Light71123ec2019-07-24 13:34:19 -0700310 disableDarwinAndLinuxBionic := struct {
Colin Crosseec9b282019-07-18 16:20:52 -0700311 Target struct {
312 Darwin struct {
313 Enabled *bool
314 }
Alex Light71123ec2019-07-24 13:34:19 -0700315 Linux_bionic struct {
316 Enabled *bool
317 }
Colin Crosseec9b282019-07-18 16:20:52 -0700318 }
319 }{}
Alex Light71123ec2019-07-24 13:34:19 -0700320 disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false)
321 disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false)
322 ctx.AppendProperties(&disableDarwinAndLinuxBionic)
Cory Barkera1da26f2022-06-07 20:12:06 +0000323
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000324 targetFramework := fuzz.GetFramework(ctx, fuzz.Cc)
325 if !fuzz.IsValidFrameworkForModule(targetFramework, fuzz.Cc, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzzing_frameworks) {
326 ctx.Module().Disable()
327 return
328 }
329
330 if targetFramework == fuzz.AFL {
331 fuzzBin.baseCompiler.Properties.Srcs = append(fuzzBin.baseCompiler.Properties.Srcs, ":aflpp_driver", ":afl-compiler-rt")
332 module.fuzzer.Properties.FuzzFramework = fuzz.AFL
333 }
334 })
Cory Barker74aea6c2022-08-08 15:55:12 +0000335
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700336 return module
337}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700338
339// Responsible for generating GNU Make rules that package fuzz targets into
340// their architecture & target/host specific zip file.
hamzeh41ad8812021-07-07 14:00:07 -0700341type ccFuzzPackager struct {
hamzehc0a671f2021-07-22 12:05:08 -0700342 fuzz.FuzzPackager
Cory Barkera1da26f2022-06-07 20:12:06 +0000343 fuzzPackagingArchModules string
344 fuzzTargetSharedDepsInstallPairs string
345 allFuzzTargetsName string
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700346}
347
348func fuzzPackagingFactory() android.Singleton {
Cory Barkera1da26f2022-06-07 20:12:06 +0000349
350 fuzzPackager := &ccFuzzPackager{
351 fuzzPackagingArchModules: "SOONG_FUZZ_PACKAGING_ARCH_MODULES",
352 fuzzTargetSharedDepsInstallPairs: "FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
353 allFuzzTargetsName: "ALL_FUZZ_TARGETS",
354 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000355 return fuzzPackager
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700356}
357
hamzeh41ad8812021-07-07 14:00:07 -0700358func (s *ccFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700359 // Map between each architecture + host/device combination, and the files that
360 // need to be packaged (in the tuple of {source file, destination folder in
361 // archive}).
hamzehc0a671f2021-07-22 12:05:08 -0700362 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700363
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700364 // List of individual fuzz targets, so that 'make fuzz' also installs the targets
365 // to the correct output directories as well.
hamzeh41ad8812021-07-07 14:00:07 -0700366 s.FuzzTargets = make(map[string]bool)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700367
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400368 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
369 // multiple fuzzers that depend on the same shared library.
370 sharedLibraryInstalled := make(map[string]bool)
371
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700372 ctx.VisitAllModules(func(module android.Module) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700373 ccModule, ok := module.(*Module)
hamzeh41ad8812021-07-07 14:00:07 -0700374 if !ok || ccModule.Properties.PreventInstall {
375 return
376 }
377
378 // Discard non-fuzz targets.
hamzehc0a671f2021-07-22 12:05:08 -0700379 if ok := fuzz.IsValid(ccModule.FuzzModule); !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700380 return
381 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700382
Cory Barkera1da26f2022-06-07 20:12:06 +0000383 sharedLibsInstallDirPrefix := "lib"
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700384 fuzzModule, ok := ccModule.compiler.(*fuzzBinary)
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000385 if !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700386 return
387 }
388
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700389 hostOrTargetString := "target"
390 if ccModule.Host() {
391 hostOrTargetString = "host"
392 }
393
Cory Barkera1da26f2022-06-07 20:12:06 +0000394 fpm := fuzz.FuzzPackagedModule{}
395 if ok {
396 fpm = fuzzModule.fuzzPackagedModule
397 }
398
399 intermediatePath := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000400
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700401 archString := ccModule.Arch().ArchType.String()
Cory Barkera1da26f2022-06-07 20:12:06 +0000402 archDir := android.PathForIntermediates(ctx, intermediatePath, hostOrTargetString, archString)
hamzehc0a671f2021-07-22 12:05:08 -0700403 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700404
hamzehc0a671f2021-07-22 12:05:08 -0700405 var files []fuzz.FileToZip
Colin Crossf1a035e2020-11-16 17:32:30 -0800406 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800407
hamzeh41ad8812021-07-07 14:00:07 -0700408 // Package the corpus, data, dict and config into a zipfile.
Cory Barkera1da26f2022-06-07 20:12:06 +0000409 files = s.PackageArtifacts(ctx, module, fpm, archDir, builder)
Tri Voad172d82019-11-27 13:45:45 -0800410
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400411 // Package shared libraries
Colin Cross31d89b42022-10-04 16:35:39 -0700412 files = append(files, GetSharedLibsToZip(fuzzModule.sharedLibraries, ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700413
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700414 // The executable.
Colin Cross31d89b42022-10-04 16:35:39 -0700415 files = append(files, fuzz.FileToZip{android.OutputFileForModule(ctx, ccModule, "unstripped"), ""})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700416
Cory Barkera1da26f2022-06-07 20:12:06 +0000417 archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
hamzeh41ad8812021-07-07 14:00:07 -0700418 if !ok {
419 return
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700420 }
421 })
422
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000423 s.CreateFuzzPackage(ctx, archDirs, fuzz.Cc, pctx)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700424}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700425
hamzeh41ad8812021-07-07 14:00:07 -0700426func (s *ccFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
427 packages := s.Packages.Strings()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700428 sort.Strings(packages)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400429 sort.Strings(s.FuzzPackager.SharedLibInstallStrings)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700430 // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
431 // ready to handle phony targets created in Soong. In the meantime, this
432 // exports the phony 'fuzz' target and dependencies on packages to
433 // core/main.mk so that we can use dist-for-goals.
Cory Barkera1da26f2022-06-07 20:12:06 +0000434
435 ctx.Strict(s.fuzzPackagingArchModules, strings.Join(packages, " "))
436
437 ctx.Strict(s.fuzzTargetSharedDepsInstallPairs,
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400438 strings.Join(s.FuzzPackager.SharedLibInstallStrings, " "))
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700439
440 // Preallocate the slice of fuzz targets to minimise memory allocations.
Cory Barkera1da26f2022-06-07 20:12:06 +0000441 s.PreallocateSlice(ctx, s.allFuzzTargetsName)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700442}
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400443
444// GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for
445// packaging.
Cory Barkera1da26f2022-06-07 20:12:06 +0000446func GetSharedLibsToZip(sharedLibraries android.Paths, module LinkableInterface, s *fuzz.FuzzPackager, archString string, destinationPathPrefix string, sharedLibraryInstalled *map[string]bool) []fuzz.FileToZip {
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400447 var files []fuzz.FileToZip
448
Cory Barkera1da26f2022-06-07 20:12:06 +0000449 fuzzDir := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000450
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400451 for _, library := range sharedLibraries {
Cory Barkera1da26f2022-06-07 20:12:06 +0000452 files = append(files, fuzz.FileToZip{library, destinationPathPrefix})
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400453
454 // For each architecture-specific shared library dependency, we need to
455 // install it to the output directory. Setup the install destination here,
456 // which will be used by $(copy-many-files) in the Make backend.
457 installDestination := sharedLibraryInstallLocation(
Cory Barkera1da26f2022-06-07 20:12:06 +0000458 library, module.Host(), fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400459 if (*sharedLibraryInstalled)[installDestination] {
460 continue
461 }
462 (*sharedLibraryInstalled)[installDestination] = true
463
464 // Escape all the variables, as the install destination here will be called
465 // via. $(eval) in Make.
466 installDestination = strings.ReplaceAll(
467 installDestination, "$", "$$")
468 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
469 library.String()+":"+installDestination)
470
471 // Ensure that on device, the library is also reinstalled to the /symbols/
472 // dir. Symbolized DSO's are always installed to the device when fuzzing, but
473 // we want symbolization tools (like `stack`) to be able to find the symbols
474 // in $ANDROID_PRODUCT_OUT/symbols automagically.
475 if !module.Host() {
Cory Barkera1da26f2022-06-07 20:12:06 +0000476 symbolsInstallDestination := sharedLibrarySymbolsInstallLocation(library, fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400477 symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
478 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
479 library.String()+":"+symbolsInstallDestination)
480 }
481 }
482 return files
483}
Colin Cross31d89b42022-10-04 16:35:39 -0700484
485// CollectAllSharedDependencies search over the provided module's dependencies using
486// VisitDirectDeps and WalkDeps to enumerate all shared library dependencies.
487// VisitDirectDeps is used first to avoid incorrectly using the core libraries (sanitizer
488// runtimes, libc, libdl, etc.) from a dependency. This may cause issues when dependencies
489// have explicit sanitizer tags, as we may get a dependency on an unsanitized libc, etc.
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000490func CollectAllSharedDependencies(ctx android.ModuleContext) (android.Paths, []android.Module) {
Colin Cross31d89b42022-10-04 16:35:39 -0700491 seen := make(map[string]bool)
492 recursed := make(map[string]bool)
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000493 deps := []android.Module{}
Colin Cross31d89b42022-10-04 16:35:39 -0700494
495 var sharedLibraries android.Paths
496
497 // Enumerate the first level of dependencies, as we discard all non-library
498 // modules in the BFS loop below.
499 ctx.VisitDirectDeps(func(dep android.Module) {
500 if !IsValidSharedDependency(dep) {
501 return
502 }
503 if seen[ctx.OtherModuleName(dep)] {
504 return
505 }
506 seen[ctx.OtherModuleName(dep)] = true
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000507 deps = append(deps, dep)
Colin Cross31d89b42022-10-04 16:35:39 -0700508 sharedLibraries = append(sharedLibraries, android.OutputFileForModule(ctx, dep, "unstripped"))
509 })
510
511 ctx.WalkDeps(func(child, parent android.Module) bool {
512 if !IsValidSharedDependency(child) {
513 return false
514 }
515 if !seen[ctx.OtherModuleName(child)] {
516 seen[ctx.OtherModuleName(child)] = true
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000517 deps = append(deps, child)
Colin Cross31d89b42022-10-04 16:35:39 -0700518 sharedLibraries = append(sharedLibraries, android.OutputFileForModule(ctx, child, "unstripped"))
519 }
520
521 if recursed[ctx.OtherModuleName(child)] {
522 return false
523 }
524 recursed[ctx.OtherModuleName(child)] = true
525 return true
526 })
527
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000528 return sharedLibraries, deps
Colin Cross31d89b42022-10-04 16:35:39 -0700529}