blob: f5642d67d4fb4f0184ecea536895009cb54c5d28 [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)
LaMont Jones0c10e4d2023-05-16 00:58:37 +000031 android.RegisterParallelSingletonType("cc_fuzz_packaging", fuzzPackagingFactory)
David Fufd121fc2023-07-07 18:11:51 +000032 android.RegisterParallelSingletonType("cc_fuzz_presubmit_packaging", fuzzPackagingFactoryPresubmit)
Cory Barkera1da26f2022-06-07 20:12:06 +000033}
34
35type FuzzProperties struct {
Cory Barker9cfcf6d2022-07-22 17:22:02 +000036 FuzzFramework fuzz.Framework `blueprint:"mutated"`
Cory Barkera1da26f2022-06-07 20:12:06 +000037}
38
39type fuzzer struct {
40 Properties FuzzProperties
41}
42
43func (fuzzer *fuzzer) flags(ctx ModuleContext, flags Flags) Flags {
Cory Barker9cfcf6d2022-07-22 17:22:02 +000044 if fuzzer.Properties.FuzzFramework == fuzz.AFL {
45 flags.Local.CFlags = append(flags.Local.CFlags, []string{
46 "-fsanitize-coverage=trace-pc-guard",
47 "-Wno-unused-result",
48 "-Wno-unused-parameter",
49 "-Wno-unused-function",
50 }...)
Cory Barkera1da26f2022-06-07 20:12:06 +000051 }
52
53 return flags
54}
55
56func (fuzzer *fuzzer) props() []interface{} {
57 return []interface{}{&fuzzer.Properties}
58}
59
60func fuzzMutatorDeps(mctx android.TopDownMutatorContext) {
61 currentModule, ok := mctx.Module().(*Module)
62 if !ok {
63 return
64 }
65
Cory Barker9cfcf6d2022-07-22 17:22:02 +000066 if currentModule.fuzzer == nil {
Cory Barkera1da26f2022-06-07 20:12:06 +000067 return
68 }
69
70 mctx.WalkDeps(func(child android.Module, parent android.Module) bool {
71 c, ok := child.(*Module)
72 if !ok {
73 return false
74 }
75
76 if c.sanitize == nil {
77 return false
78 }
79
80 isFuzzerPointer := c.sanitize.getSanitizerBoolPtr(Fuzzer)
81 if isFuzzerPointer == nil || !*isFuzzerPointer {
82 return false
83 }
84
85 if c.fuzzer == nil {
86 return false
87 }
88
Cory Barker9cfcf6d2022-07-22 17:22:02 +000089 c.fuzzer.Properties.FuzzFramework = currentModule.fuzzer.Properties.FuzzFramework
Cory Barkera1da26f2022-06-07 20:12:06 +000090 return true
91 })
92}
93
Mitch Phillipsda9a4632019-07-15 09:34:09 -070094// cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at
95// $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on
96// your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree.
Cory Barkera1da26f2022-06-07 20:12:06 +000097func LibFuzzFactory() android.Module {
Cory Barker9cfcf6d2022-07-22 17:22:02 +000098 module := NewFuzzer(android.HostAndDeviceSupported)
Aditya Choudhary87b2ab22023-11-17 15:27:06 +000099 module.testModule = true
Cory Barkera1da26f2022-06-07 20:12:06 +0000100 return module.Init()
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700101}
102
103type fuzzBinary struct {
104 *binaryDecorator
105 *baseCompiler
Cory Barkera1da26f2022-06-07 20:12:06 +0000106 fuzzPackagedModule fuzz.FuzzPackagedModule
hamzeh41ad8812021-07-07 14:00:07 -0700107 installedSharedDeps []string
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000108 sharedLibraries android.RuleBuilderInstalls
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800109 data []android.DataPath
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700110}
111
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400112func (fuzz *fuzzBinary) fuzzBinary() bool {
113 return true
114}
115
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700116func (fuzz *fuzzBinary) linkerProps() []interface{} {
117 props := fuzz.binaryDecorator.linkerProps()
hamzeh41ad8812021-07-07 14:00:07 -0700118 props = append(props, &fuzz.fuzzPackagedModule.FuzzProperties)
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000119
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700120 return props
121}
122
123func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) {
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700124 fuzz.binaryDecorator.linkerInit(ctx)
125}
126
Cory Barkera1da26f2022-06-07 20:12:06 +0000127func (fuzzBin *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000128 if ctx.Config().Getenv("FUZZ_FRAMEWORK") == "AFL" {
Cory Barkera1da26f2022-06-07 20:12:06 +0000129 deps.HeaderLibs = append(deps.HeaderLibs, "libafl_headers")
Cory Barkera1da26f2022-06-07 20:12:06 +0000130 } else {
131 deps.StaticLibs = append(deps.StaticLibs, config.LibFuzzerRuntimeLibrary(ctx.toolchain()))
Kris Alderd406da12022-10-21 09:34:21 -0700132 // Fuzzers built with HWASAN should use the interceptors for better
133 // mutation based on signals in strcmp, memcpy, etc. This is only needed for
134 // fuzz targets, not generic HWASAN-ified binaries or libraries.
135 if module, ok := ctx.Module().(*Module); ok {
136 if module.IsSanitizerEnabled(Hwasan) {
137 deps.StaticLibs = append(deps.StaticLibs, config.LibFuzzerRuntimeInterceptors(ctx.toolchain()))
138 }
139 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000140 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000141
142 deps = fuzzBin.binaryDecorator.linkerDeps(ctx, deps)
143 return deps
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700144}
145
146func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
147 flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800148 // RunPaths on devices isn't instantiated by the base linker. `../lib` for
149 // installed fuzz targets (both host and device), and `./lib` for fuzz
150 // target packages.
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800151 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
Cory Barkera1da26f2022-06-07 20:12:06 +0000152
Kris Alderc2634812022-10-25 10:58:59 -0700153 // When running on device, fuzz targets with vendor: true set will be in
154 // fuzzer_name/vendor/fuzzer_name (note the extra 'vendor' and thus need to
155 // link with libraries in ../../lib/. Non-vendor binaries only need to look
156 // one level up, in ../lib/.
157 if ctx.inVendor() {
158 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../../lib`)
159 } else {
160 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
161 }
162
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700163 return flags
164}
165
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400166// IsValidSharedDependency takes a module and determines if it is a unique shared library
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700167// that should be installed in the fuzz target output directories. This function
168// returns true, unless:
Colin Crossd079e0b2022-08-16 10:27:33 -0700169// - The module is not an installable shared library, or
170// - The module is a header or stub, or
171// - The module is a prebuilt and its source is available, or
172// - The module is a versioned member of an SDK snapshot.
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400173func IsValidSharedDependency(dependency android.Module) bool {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700174 // TODO(b/144090547): We should be parsing these modules using
175 // ModuleDependencyTag instead of the current brute-force checking.
176
Colin Cross31076b32020-10-23 17:22:06 -0700177 linkable, ok := dependency.(LinkableInterface)
178 if !ok || !linkable.CcLibraryInterface() {
179 // Discard non-linkables.
180 return false
181 }
182
183 if !linkable.Shared() {
184 // Discard static libs.
185 return false
186 }
187
Colin Cross31076b32020-10-23 17:22:06 -0700188 if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800189 // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not
190 // be excluded on the basis of they're not CCLibrary()'s.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700191 return false
192 }
193
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800194 // We discarded module stubs libraries above, but the LLNDK prebuilts stubs
195 // libraries must be handled differently - by looking for the stubDecorator.
196 // Discard LLNDK prebuilts stubs as well.
197 if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary {
198 if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary {
199 return false
200 }
Victor Chang00c144f2021-02-09 12:30:33 +0000201 // Discard installable:false libraries because they are expected to be absent
202 // in runtime.
Colin Cross1bc94122021-10-28 13:25:54 -0700203 if !proptools.BoolDefault(ccLibrary.Installable(), true) {
Victor Chang00c144f2021-02-09 12:30:33 +0000204 return false
205 }
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800206 }
207
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100208 // If the same library is present both as source and a prebuilt we must pick
209 // only one to avoid a conflict. Always prefer the source since the prebuilt
210 // probably won't be built with sanitizers enabled.
Paul Duffinf7c99f52021-04-28 10:41:21 +0100211 if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() {
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100212 return false
213 }
214
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700215 return true
216}
217
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500218func SharedLibraryInstallLocation(
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000219 libraryBase string, isHost bool, fuzzDir string, archString string) string {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700220 installLocation := "$(PRODUCT_OUT)/data"
221 if isHost {
222 installLocation = "$(HOST_OUT)"
223 }
224 installLocation = filepath.Join(
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000225 installLocation, fuzzDir, archString, "lib", libraryBase)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700226 return installLocation
227}
228
Mitch Phillips0bf97132020-03-06 09:38:12 -0800229// Get the device-only shared library symbols install directory.
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000230func SharedLibrarySymbolsInstallLocation(libraryBase string, fuzzDir string, archString string) string {
231 return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, "/lib/", libraryBase)
Mitch Phillips0bf97132020-03-06 09:38:12 -0800232}
233
Cory Barkera1da26f2022-06-07 20:12:06 +0000234func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500235 fuzzBin.fuzzPackagedModule = PackageFuzzModule(ctx, fuzzBin.fuzzPackagedModule, pctx)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700236
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800237 installBase := "fuzz"
238
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700239 // Grab the list of required shared libraries.
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000240 fuzzBin.sharedLibraries, _ = CollectAllSharedDependencies(ctx)
Colin Crossdc809f92019-11-20 15:58:32 -0800241
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000242 for _, ruleBuilderInstall := range fuzzBin.sharedLibraries {
243 install := ruleBuilderInstall.To
Cory Barkera1da26f2022-06-07 20:12:06 +0000244 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500245 SharedLibraryInstallLocation(
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000246 install, ctx.Host(), installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800247
248 // Also add the dependency on the shared library symbols dir.
249 if !ctx.Host() {
Cory Barkera1da26f2022-06-07 20:12:06 +0000250 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000251 SharedLibrarySymbolsInstallLocation(install, installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800252 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700253 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800254
255 for _, d := range fuzzBin.fuzzPackagedModule.Corpus {
256 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, RelativeInstallPath: "corpus", WithoutRel: true})
257 }
258
259 for _, d := range fuzzBin.fuzzPackagedModule.Data {
260 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, RelativeInstallPath: "data"})
261 }
262
263 if d := fuzzBin.fuzzPackagedModule.Dictionary; d != nil {
264 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, WithoutRel: true})
265 }
266
267 if d := fuzzBin.fuzzPackagedModule.Config; d != nil {
268 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, WithoutRel: true})
269 }
270
271 fuzzBin.binaryDecorator.baseInstaller.dir = filepath.Join(
272 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
273 fuzzBin.binaryDecorator.baseInstaller.dir64 = filepath.Join(
274 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
275 fuzzBin.binaryDecorator.baseInstaller.installTestData(ctx, fuzzBin.data)
276 fuzzBin.binaryDecorator.baseInstaller.install(ctx, file)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700277}
278
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500279func PackageFuzzModule(ctx android.ModuleContext, fuzzPackagedModule fuzz.FuzzPackagedModule, pctx android.PackageContext) fuzz.FuzzPackagedModule {
280 fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Corpus)
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500281
282 fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Data)
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500283
284 if fuzzPackagedModule.FuzzProperties.Dictionary != nil {
285 fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzzPackagedModule.FuzzProperties.Dictionary)
286 if fuzzPackagedModule.Dictionary.Ext() != ".dict" {
287 ctx.PropertyErrorf("dictionary",
288 "Fuzzer dictionary %q does not have '.dict' extension",
289 fuzzPackagedModule.Dictionary.String())
290 }
291 }
292
293 if fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
294 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
295 android.WriteFileRule(ctx, configPath, fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
296 fuzzPackagedModule.Config = configPath
297 }
298 return fuzzPackagedModule
299}
300
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000301func NewFuzzer(hod android.HostOrDeviceSupported) *Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800302 module, binary := newBinary(hod)
Cory Barkera1da26f2022-06-07 20:12:06 +0000303 baseInstallerPath := "fuzz"
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700304
Cory Barkera1da26f2022-06-07 20:12:06 +0000305 binary.baseInstaller = NewBaseInstaller(baseInstallerPath, baseInstallerPath, InstallInData)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700306
Cory Barkera1da26f2022-06-07 20:12:06 +0000307 fuzzBin := &fuzzBinary{
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700308 binaryDecorator: binary,
309 baseCompiler: NewBaseCompiler(),
310 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000311 module.compiler = fuzzBin
312 module.linker = fuzzBin
313 module.installer = fuzzBin
Colin Crosseec9b282019-07-18 16:20:52 -0700314
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000315 module.fuzzer.Properties.FuzzFramework = fuzz.LibFuzzer
316
Colin Crosseec9b282019-07-18 16:20:52 -0700317 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
318 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400319
320 extraProps := struct {
321 Sanitize struct {
322 Fuzzer *bool
323 }
Colin Crosseec9b282019-07-18 16:20:52 -0700324 Target struct {
325 Darwin struct {
326 Enabled *bool
327 }
Alex Light71123ec2019-07-24 13:34:19 -0700328 Linux_bionic struct {
329 Enabled *bool
330 }
Colin Crosseec9b282019-07-18 16:20:52 -0700331 }
332 }{}
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400333 extraProps.Sanitize.Fuzzer = BoolPtr(true)
334 extraProps.Target.Darwin.Enabled = BoolPtr(false)
335 extraProps.Target.Linux_bionic.Enabled = BoolPtr(false)
336 ctx.AppendProperties(&extraProps)
Cory Barkera1da26f2022-06-07 20:12:06 +0000337
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000338 targetFramework := fuzz.GetFramework(ctx, fuzz.Cc)
339 if !fuzz.IsValidFrameworkForModule(targetFramework, fuzz.Cc, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzzing_frameworks) {
340 ctx.Module().Disable()
341 return
342 }
343
344 if targetFramework == fuzz.AFL {
345 fuzzBin.baseCompiler.Properties.Srcs = append(fuzzBin.baseCompiler.Properties.Srcs, ":aflpp_driver", ":afl-compiler-rt")
346 module.fuzzer.Properties.FuzzFramework = fuzz.AFL
347 }
348 })
Cory Barker74aea6c2022-08-08 15:55:12 +0000349
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700350 return module
351}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700352
353// Responsible for generating GNU Make rules that package fuzz targets into
354// their architecture & target/host specific zip file.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500355type ccRustFuzzPackager struct {
hamzehc0a671f2021-07-22 12:05:08 -0700356 fuzz.FuzzPackager
Cole Faust06ea5312023-10-18 17:38:40 -0700357 fuzzPackagingArchModules string
358 fuzzTargetSharedDepsInstallPairs string
359 allFuzzTargetsName string
360 onlyIncludePresubmits bool
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700361}
362
363func fuzzPackagingFactory() android.Singleton {
Cory Barkera1da26f2022-06-07 20:12:06 +0000364
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500365 fuzzPackager := &ccRustFuzzPackager{
Cory Barkera1da26f2022-06-07 20:12:06 +0000366 fuzzPackagingArchModules: "SOONG_FUZZ_PACKAGING_ARCH_MODULES",
367 fuzzTargetSharedDepsInstallPairs: "FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
368 allFuzzTargetsName: "ALL_FUZZ_TARGETS",
Cole Faust06ea5312023-10-18 17:38:40 -0700369 onlyIncludePresubmits: false,
David Fufd121fc2023-07-07 18:11:51 +0000370 }
371 return fuzzPackager
372}
373
374func fuzzPackagingFactoryPresubmit() android.Singleton {
375
376 fuzzPackager := &ccRustFuzzPackager{
377 fuzzPackagingArchModules: "SOONG_PRESUBMIT_FUZZ_PACKAGING_ARCH_MODULES",
378 fuzzTargetSharedDepsInstallPairs: "PRESUBMIT_FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
379 allFuzzTargetsName: "ALL_PRESUBMIT_FUZZ_TARGETS",
Cole Faust06ea5312023-10-18 17:38:40 -0700380 onlyIncludePresubmits: true,
Cory Barkera1da26f2022-06-07 20:12:06 +0000381 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000382 return fuzzPackager
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700383}
384
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500385func (s *ccRustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700386 // Map between each architecture + host/device combination, and the files that
387 // need to be packaged (in the tuple of {source file, destination folder in
388 // archive}).
hamzehc0a671f2021-07-22 12:05:08 -0700389 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700390
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700391 // List of individual fuzz targets, so that 'make fuzz' also installs the targets
392 // to the correct output directories as well.
hamzeh41ad8812021-07-07 14:00:07 -0700393 s.FuzzTargets = make(map[string]bool)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700394
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400395 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
396 // multiple fuzzers that depend on the same shared library.
397 sharedLibraryInstalled := make(map[string]bool)
398
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700399 ctx.VisitAllModules(func(module android.Module) {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500400 ccModule, ok := module.(LinkableInterface)
401 if !ok || ccModule.PreventInstall() {
hamzeh41ad8812021-07-07 14:00:07 -0700402 return
403 }
hamzeh41ad8812021-07-07 14:00:07 -0700404 // Discard non-fuzz targets.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500405 if ok := fuzz.IsValid(ccModule.FuzzModuleStruct()); !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700406 return
407 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700408
Cory Barkera1da26f2022-06-07 20:12:06 +0000409 sharedLibsInstallDirPrefix := "lib"
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500410 if !ccModule.IsFuzzModule() {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700411 return
412 }
413
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700414 hostOrTargetString := "target"
Colin Cross64a4a5f2023-05-16 17:54:27 -0700415 if ccModule.Target().HostCross {
416 hostOrTargetString = "host_cross"
417 } else if ccModule.Host() {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700418 hostOrTargetString = "host"
419 }
David Fufd121fc2023-07-07 18:11:51 +0000420 if s.onlyIncludePresubmits == true {
421 hostOrTargetString = "presubmit-" + hostOrTargetString
422 }
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700423
Cory Barkera1da26f2022-06-07 20:12:06 +0000424 fpm := fuzz.FuzzPackagedModule{}
425 if ok {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500426 fpm = ccModule.FuzzPackagedModule()
Cory Barkera1da26f2022-06-07 20:12:06 +0000427 }
428
429 intermediatePath := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000430
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500431 archString := ccModule.Target().Arch.ArchType.String()
Cory Barkera1da26f2022-06-07 20:12:06 +0000432 archDir := android.PathForIntermediates(ctx, intermediatePath, hostOrTargetString, archString)
hamzehc0a671f2021-07-22 12:05:08 -0700433 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700434
hamzehc0a671f2021-07-22 12:05:08 -0700435 var files []fuzz.FileToZip
Colin Crossf1a035e2020-11-16 17:32:30 -0800436 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800437
hamzeh41ad8812021-07-07 14:00:07 -0700438 // Package the corpus, data, dict and config into a zipfile.
Cory Barkera1da26f2022-06-07 20:12:06 +0000439 files = s.PackageArtifacts(ctx, module, fpm, archDir, builder)
Tri Voad172d82019-11-27 13:45:45 -0800440
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400441 // Package shared libraries
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500442 files = append(files, GetSharedLibsToZip(ccModule.FuzzSharedLibraries(), ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700443
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700444 // The executable.
Colin Cross80462dc2023-05-08 15:09:31 -0700445 files = append(files, fuzz.FileToZip{SourceFilePath: android.OutputFileForModule(ctx, ccModule, "unstripped")})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700446
David Fufd121fc2023-07-07 18:11:51 +0000447 if s.onlyIncludePresubmits == true {
448 if fpm.FuzzProperties.Fuzz_config == nil {
449 return
450 }
Cole Faust06ea5312023-10-18 17:38:40 -0700451 if !BoolDefault(fpm.FuzzProperties.Fuzz_config.Use_for_presubmit, false) {
David Fufd121fc2023-07-07 18:11:51 +0000452 return
453 }
454 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000455 archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
hamzeh41ad8812021-07-07 14:00:07 -0700456 if !ok {
457 return
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700458 }
459 })
460
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000461 s.CreateFuzzPackage(ctx, archDirs, fuzz.Cc, pctx)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700462}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700463
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500464func (s *ccRustFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
hamzeh41ad8812021-07-07 14:00:07 -0700465 packages := s.Packages.Strings()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700466 sort.Strings(packages)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400467 sort.Strings(s.FuzzPackager.SharedLibInstallStrings)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700468 // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
469 // ready to handle phony targets created in Soong. In the meantime, this
470 // exports the phony 'fuzz' target and dependencies on packages to
471 // core/main.mk so that we can use dist-for-goals.
Cory Barkera1da26f2022-06-07 20:12:06 +0000472
473 ctx.Strict(s.fuzzPackagingArchModules, strings.Join(packages, " "))
474
475 ctx.Strict(s.fuzzTargetSharedDepsInstallPairs,
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400476 strings.Join(s.FuzzPackager.SharedLibInstallStrings, " "))
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700477
478 // Preallocate the slice of fuzz targets to minimise memory allocations.
Cory Barkera1da26f2022-06-07 20:12:06 +0000479 s.PreallocateSlice(ctx, s.allFuzzTargetsName)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700480}
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400481
482// GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for
483// packaging.
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000484func GetSharedLibsToZip(sharedLibraries android.RuleBuilderInstalls, module LinkableInterface, s *fuzz.FuzzPackager, archString string, destinationPathPrefix string, sharedLibraryInstalled *map[string]bool) []fuzz.FileToZip {
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400485 var files []fuzz.FileToZip
486
Cory Barkera1da26f2022-06-07 20:12:06 +0000487 fuzzDir := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000488
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000489 for _, ruleBuilderInstall := range sharedLibraries {
490 library := ruleBuilderInstall.From
491 install := ruleBuilderInstall.To
Colin Cross80462dc2023-05-08 15:09:31 -0700492 files = append(files, fuzz.FileToZip{
493 SourceFilePath: library,
494 DestinationPathPrefix: destinationPathPrefix,
495 DestinationPath: install,
496 })
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400497
498 // For each architecture-specific shared library dependency, we need to
499 // install it to the output directory. Setup the install destination here,
500 // which will be used by $(copy-many-files) in the Make backend.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500501 installDestination := SharedLibraryInstallLocation(
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000502 install, module.Host(), fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400503 if (*sharedLibraryInstalled)[installDestination] {
504 continue
505 }
506 (*sharedLibraryInstalled)[installDestination] = true
507
508 // Escape all the variables, as the install destination here will be called
509 // via. $(eval) in Make.
510 installDestination = strings.ReplaceAll(
511 installDestination, "$", "$$")
512 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
513 library.String()+":"+installDestination)
514
515 // Ensure that on device, the library is also reinstalled to the /symbols/
516 // dir. Symbolized DSO's are always installed to the device when fuzzing, but
517 // we want symbolization tools (like `stack`) to be able to find the symbols
518 // in $ANDROID_PRODUCT_OUT/symbols automagically.
519 if !module.Host() {
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000520 symbolsInstallDestination := SharedLibrarySymbolsInstallLocation(install, fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400521 symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
522 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
523 library.String()+":"+symbolsInstallDestination)
524 }
525 }
526 return files
527}
Colin Cross31d89b42022-10-04 16:35:39 -0700528
529// CollectAllSharedDependencies search over the provided module's dependencies using
530// VisitDirectDeps and WalkDeps to enumerate all shared library dependencies.
531// VisitDirectDeps is used first to avoid incorrectly using the core libraries (sanitizer
532// runtimes, libc, libdl, etc.) from a dependency. This may cause issues when dependencies
533// have explicit sanitizer tags, as we may get a dependency on an unsanitized libc, etc.
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000534func CollectAllSharedDependencies(ctx android.ModuleContext) (android.RuleBuilderInstalls, []android.Module) {
Colin Cross31d89b42022-10-04 16:35:39 -0700535 seen := make(map[string]bool)
536 recursed := make(map[string]bool)
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000537 deps := []android.Module{}
Colin Cross31d89b42022-10-04 16:35:39 -0700538
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000539 var sharedLibraries android.RuleBuilderInstalls
Colin Cross31d89b42022-10-04 16:35:39 -0700540
541 // Enumerate the first level of dependencies, as we discard all non-library
542 // modules in the BFS loop below.
543 ctx.VisitDirectDeps(func(dep android.Module) {
544 if !IsValidSharedDependency(dep) {
545 return
546 }
Colin Cross313aa542023-12-13 13:47:44 -0800547 sharedLibraryInfo, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, dep, SharedLibraryInfoProvider)
548 if !hasSharedLibraryInfo {
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000549 return
550 }
Colin Cross31d89b42022-10-04 16:35:39 -0700551 if seen[ctx.OtherModuleName(dep)] {
552 return
553 }
554 seen[ctx.OtherModuleName(dep)] = true
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000555 deps = append(deps, dep)
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000556
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000557 installDestination := sharedLibraryInfo.SharedLibrary.Base()
558 ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, dep, "unstripped"), installDestination}
559 sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
Colin Cross31d89b42022-10-04 16:35:39 -0700560 })
561
562 ctx.WalkDeps(func(child, parent android.Module) bool {
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400563
564 // If this is a Rust module which is not rust_ffi_shared, we still want to bundle any transitive
565 // shared dependencies (even for rust_ffi_static)
566 if rustmod, ok := child.(LinkableInterface); ok && rustmod.RustLibraryInterface() && !rustmod.Shared() {
567 if recursed[ctx.OtherModuleName(child)] {
568 return false
569 }
570 recursed[ctx.OtherModuleName(child)] = true
571 return true
572 }
573
Colin Cross31d89b42022-10-04 16:35:39 -0700574 if !IsValidSharedDependency(child) {
575 return false
576 }
Colin Cross313aa542023-12-13 13:47:44 -0800577 sharedLibraryInfo, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, child, SharedLibraryInfoProvider)
578 if !hasSharedLibraryInfo {
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000579 return false
580 }
Colin Cross31d89b42022-10-04 16:35:39 -0700581 if !seen[ctx.OtherModuleName(child)] {
582 seen[ctx.OtherModuleName(child)] = true
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000583 deps = append(deps, child)
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000584
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000585 installDestination := sharedLibraryInfo.SharedLibrary.Base()
586 ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, child, "unstripped"), installDestination}
587 sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
Colin Cross31d89b42022-10-04 16:35:39 -0700588 }
589
590 if recursed[ctx.OtherModuleName(child)] {
591 return false
592 }
593 recursed[ctx.OtherModuleName(child)] = true
594 return true
595 })
596
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000597 return sharedLibraries, deps
Colin Cross31d89b42022-10-04 16:35:39 -0700598}