blob: 183849a55a63eabf592f093f820bd9738561d850 [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
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800166func (fuzz *fuzzBinary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
167 fuzz.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
168 moduleInfoJSON.Class = []string{"EXECUTABLES"}
169}
170
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400171// IsValidSharedDependency takes a module and determines if it is a unique shared library
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700172// that should be installed in the fuzz target output directories. This function
173// returns true, unless:
Colin Crossd079e0b2022-08-16 10:27:33 -0700174// - The module is not an installable shared library, or
175// - The module is a header or stub, or
176// - The module is a prebuilt and its source is available, or
177// - The module is a versioned member of an SDK snapshot.
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400178func IsValidSharedDependency(dependency android.Module) bool {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700179 // TODO(b/144090547): We should be parsing these modules using
180 // ModuleDependencyTag instead of the current brute-force checking.
181
Colin Cross31076b32020-10-23 17:22:06 -0700182 linkable, ok := dependency.(LinkableInterface)
183 if !ok || !linkable.CcLibraryInterface() {
184 // Discard non-linkables.
185 return false
186 }
187
188 if !linkable.Shared() {
189 // Discard static libs.
190 return false
191 }
192
Colin Cross31076b32020-10-23 17:22:06 -0700193 if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800194 // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not
195 // be excluded on the basis of they're not CCLibrary()'s.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700196 return false
197 }
198
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800199 // We discarded module stubs libraries above, but the LLNDK prebuilts stubs
200 // libraries must be handled differently - by looking for the stubDecorator.
201 // Discard LLNDK prebuilts stubs as well.
202 if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary {
203 if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary {
204 return false
205 }
Victor Chang00c144f2021-02-09 12:30:33 +0000206 // Discard installable:false libraries because they are expected to be absent
207 // in runtime.
Colin Cross1bc94122021-10-28 13:25:54 -0700208 if !proptools.BoolDefault(ccLibrary.Installable(), true) {
Victor Chang00c144f2021-02-09 12:30:33 +0000209 return false
210 }
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800211 }
212
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100213 // If the same library is present both as source and a prebuilt we must pick
214 // only one to avoid a conflict. Always prefer the source since the prebuilt
215 // probably won't be built with sanitizers enabled.
Paul Duffinf7c99f52021-04-28 10:41:21 +0100216 if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() {
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100217 return false
218 }
219
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700220 return true
221}
222
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500223func SharedLibraryInstallLocation(
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000224 libraryBase string, isHost bool, fuzzDir string, archString string) string {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700225 installLocation := "$(PRODUCT_OUT)/data"
226 if isHost {
227 installLocation = "$(HOST_OUT)"
228 }
229 installLocation = filepath.Join(
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000230 installLocation, fuzzDir, archString, "lib", libraryBase)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700231 return installLocation
232}
233
Mitch Phillips0bf97132020-03-06 09:38:12 -0800234// Get the device-only shared library symbols install directory.
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000235func SharedLibrarySymbolsInstallLocation(libraryBase string, fuzzDir string, archString string) string {
236 return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, "/lib/", libraryBase)
Mitch Phillips0bf97132020-03-06 09:38:12 -0800237}
238
Cory Barkera1da26f2022-06-07 20:12:06 +0000239func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500240 fuzzBin.fuzzPackagedModule = PackageFuzzModule(ctx, fuzzBin.fuzzPackagedModule, pctx)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700241
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800242 installBase := "fuzz"
243
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700244 // Grab the list of required shared libraries.
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000245 fuzzBin.sharedLibraries, _ = CollectAllSharedDependencies(ctx)
Colin Crossdc809f92019-11-20 15:58:32 -0800246
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000247 for _, ruleBuilderInstall := range fuzzBin.sharedLibraries {
248 install := ruleBuilderInstall.To
Cory Barkera1da26f2022-06-07 20:12:06 +0000249 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500250 SharedLibraryInstallLocation(
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000251 install, ctx.Host(), installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800252
253 // Also add the dependency on the shared library symbols dir.
254 if !ctx.Host() {
Cory Barkera1da26f2022-06-07 20:12:06 +0000255 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000256 SharedLibrarySymbolsInstallLocation(install, installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800257 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700258 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800259
260 for _, d := range fuzzBin.fuzzPackagedModule.Corpus {
261 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, RelativeInstallPath: "corpus", WithoutRel: true})
262 }
263
264 for _, d := range fuzzBin.fuzzPackagedModule.Data {
265 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, RelativeInstallPath: "data"})
266 }
267
268 if d := fuzzBin.fuzzPackagedModule.Dictionary; d != nil {
269 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, WithoutRel: true})
270 }
271
272 if d := fuzzBin.fuzzPackagedModule.Config; d != nil {
273 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, WithoutRel: true})
274 }
275
276 fuzzBin.binaryDecorator.baseInstaller.dir = filepath.Join(
277 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
278 fuzzBin.binaryDecorator.baseInstaller.dir64 = filepath.Join(
279 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
280 fuzzBin.binaryDecorator.baseInstaller.installTestData(ctx, fuzzBin.data)
281 fuzzBin.binaryDecorator.baseInstaller.install(ctx, file)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700282}
283
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500284func PackageFuzzModule(ctx android.ModuleContext, fuzzPackagedModule fuzz.FuzzPackagedModule, pctx android.PackageContext) fuzz.FuzzPackagedModule {
285 fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Corpus)
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500286
287 fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Data)
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500288
289 if fuzzPackagedModule.FuzzProperties.Dictionary != nil {
290 fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzzPackagedModule.FuzzProperties.Dictionary)
291 if fuzzPackagedModule.Dictionary.Ext() != ".dict" {
292 ctx.PropertyErrorf("dictionary",
293 "Fuzzer dictionary %q does not have '.dict' extension",
294 fuzzPackagedModule.Dictionary.String())
295 }
296 }
297
298 if fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
299 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
300 android.WriteFileRule(ctx, configPath, fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
301 fuzzPackagedModule.Config = configPath
302 }
303 return fuzzPackagedModule
304}
305
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000306func NewFuzzer(hod android.HostOrDeviceSupported) *Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800307 module, binary := newBinary(hod)
Cory Barkera1da26f2022-06-07 20:12:06 +0000308 baseInstallerPath := "fuzz"
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700309
Cory Barkera1da26f2022-06-07 20:12:06 +0000310 binary.baseInstaller = NewBaseInstaller(baseInstallerPath, baseInstallerPath, InstallInData)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700311
Cory Barkera1da26f2022-06-07 20:12:06 +0000312 fuzzBin := &fuzzBinary{
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700313 binaryDecorator: binary,
314 baseCompiler: NewBaseCompiler(),
315 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000316 module.compiler = fuzzBin
317 module.linker = fuzzBin
318 module.installer = fuzzBin
Colin Crosseec9b282019-07-18 16:20:52 -0700319
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000320 module.fuzzer.Properties.FuzzFramework = fuzz.LibFuzzer
321
Colin Crosseec9b282019-07-18 16:20:52 -0700322 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
323 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400324
325 extraProps := struct {
326 Sanitize struct {
327 Fuzzer *bool
328 }
Colin Crosseec9b282019-07-18 16:20:52 -0700329 Target struct {
330 Darwin struct {
331 Enabled *bool
332 }
Alex Light71123ec2019-07-24 13:34:19 -0700333 Linux_bionic struct {
334 Enabled *bool
335 }
Colin Crosseec9b282019-07-18 16:20:52 -0700336 }
337 }{}
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400338 extraProps.Sanitize.Fuzzer = BoolPtr(true)
339 extraProps.Target.Darwin.Enabled = BoolPtr(false)
340 extraProps.Target.Linux_bionic.Enabled = BoolPtr(false)
341 ctx.AppendProperties(&extraProps)
Cory Barkera1da26f2022-06-07 20:12:06 +0000342
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000343 targetFramework := fuzz.GetFramework(ctx, fuzz.Cc)
344 if !fuzz.IsValidFrameworkForModule(targetFramework, fuzz.Cc, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzzing_frameworks) {
345 ctx.Module().Disable()
346 return
347 }
348
349 if targetFramework == fuzz.AFL {
350 fuzzBin.baseCompiler.Properties.Srcs = append(fuzzBin.baseCompiler.Properties.Srcs, ":aflpp_driver", ":afl-compiler-rt")
351 module.fuzzer.Properties.FuzzFramework = fuzz.AFL
352 }
353 })
Cory Barker74aea6c2022-08-08 15:55:12 +0000354
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700355 return module
356}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700357
358// Responsible for generating GNU Make rules that package fuzz targets into
359// their architecture & target/host specific zip file.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500360type ccRustFuzzPackager struct {
hamzehc0a671f2021-07-22 12:05:08 -0700361 fuzz.FuzzPackager
Cole Faust06ea5312023-10-18 17:38:40 -0700362 fuzzPackagingArchModules string
363 fuzzTargetSharedDepsInstallPairs string
364 allFuzzTargetsName string
365 onlyIncludePresubmits bool
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700366}
367
368func fuzzPackagingFactory() android.Singleton {
Cory Barkera1da26f2022-06-07 20:12:06 +0000369
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500370 fuzzPackager := &ccRustFuzzPackager{
Cory Barkera1da26f2022-06-07 20:12:06 +0000371 fuzzPackagingArchModules: "SOONG_FUZZ_PACKAGING_ARCH_MODULES",
372 fuzzTargetSharedDepsInstallPairs: "FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
373 allFuzzTargetsName: "ALL_FUZZ_TARGETS",
Cole Faust06ea5312023-10-18 17:38:40 -0700374 onlyIncludePresubmits: false,
David Fufd121fc2023-07-07 18:11:51 +0000375 }
376 return fuzzPackager
377}
378
379func fuzzPackagingFactoryPresubmit() android.Singleton {
380
381 fuzzPackager := &ccRustFuzzPackager{
382 fuzzPackagingArchModules: "SOONG_PRESUBMIT_FUZZ_PACKAGING_ARCH_MODULES",
383 fuzzTargetSharedDepsInstallPairs: "PRESUBMIT_FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
384 allFuzzTargetsName: "ALL_PRESUBMIT_FUZZ_TARGETS",
Cole Faust06ea5312023-10-18 17:38:40 -0700385 onlyIncludePresubmits: true,
Cory Barkera1da26f2022-06-07 20:12:06 +0000386 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000387 return fuzzPackager
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700388}
389
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500390func (s *ccRustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700391 // Map between each architecture + host/device combination, and the files that
392 // need to be packaged (in the tuple of {source file, destination folder in
393 // archive}).
hamzehc0a671f2021-07-22 12:05:08 -0700394 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700395
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700396 // List of individual fuzz targets, so that 'make fuzz' also installs the targets
397 // to the correct output directories as well.
hamzeh41ad8812021-07-07 14:00:07 -0700398 s.FuzzTargets = make(map[string]bool)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700399
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400400 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
401 // multiple fuzzers that depend on the same shared library.
402 sharedLibraryInstalled := make(map[string]bool)
403
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700404 ctx.VisitAllModules(func(module android.Module) {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500405 ccModule, ok := module.(LinkableInterface)
406 if !ok || ccModule.PreventInstall() {
hamzeh41ad8812021-07-07 14:00:07 -0700407 return
408 }
hamzeh41ad8812021-07-07 14:00:07 -0700409 // Discard non-fuzz targets.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500410 if ok := fuzz.IsValid(ccModule.FuzzModuleStruct()); !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700411 return
412 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700413
Cory Barkera1da26f2022-06-07 20:12:06 +0000414 sharedLibsInstallDirPrefix := "lib"
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500415 if !ccModule.IsFuzzModule() {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700416 return
417 }
418
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700419 hostOrTargetString := "target"
Colin Cross64a4a5f2023-05-16 17:54:27 -0700420 if ccModule.Target().HostCross {
421 hostOrTargetString = "host_cross"
422 } else if ccModule.Host() {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700423 hostOrTargetString = "host"
424 }
David Fufd121fc2023-07-07 18:11:51 +0000425 if s.onlyIncludePresubmits == true {
426 hostOrTargetString = "presubmit-" + hostOrTargetString
427 }
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700428
Cory Barkera1da26f2022-06-07 20:12:06 +0000429 fpm := fuzz.FuzzPackagedModule{}
430 if ok {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500431 fpm = ccModule.FuzzPackagedModule()
Cory Barkera1da26f2022-06-07 20:12:06 +0000432 }
433
434 intermediatePath := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000435
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500436 archString := ccModule.Target().Arch.ArchType.String()
Cory Barkera1da26f2022-06-07 20:12:06 +0000437 archDir := android.PathForIntermediates(ctx, intermediatePath, hostOrTargetString, archString)
hamzehc0a671f2021-07-22 12:05:08 -0700438 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700439
hamzehc0a671f2021-07-22 12:05:08 -0700440 var files []fuzz.FileToZip
Colin Crossf1a035e2020-11-16 17:32:30 -0800441 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800442
hamzeh41ad8812021-07-07 14:00:07 -0700443 // Package the corpus, data, dict and config into a zipfile.
Cory Barkera1da26f2022-06-07 20:12:06 +0000444 files = s.PackageArtifacts(ctx, module, fpm, archDir, builder)
Tri Voad172d82019-11-27 13:45:45 -0800445
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400446 // Package shared libraries
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500447 files = append(files, GetSharedLibsToZip(ccModule.FuzzSharedLibraries(), ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700448
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700449 // The executable.
Colin Cross80462dc2023-05-08 15:09:31 -0700450 files = append(files, fuzz.FileToZip{SourceFilePath: android.OutputFileForModule(ctx, ccModule, "unstripped")})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700451
David Fufd121fc2023-07-07 18:11:51 +0000452 if s.onlyIncludePresubmits == true {
453 if fpm.FuzzProperties.Fuzz_config == nil {
454 return
455 }
Cole Faust06ea5312023-10-18 17:38:40 -0700456 if !BoolDefault(fpm.FuzzProperties.Fuzz_config.Use_for_presubmit, false) {
David Fufd121fc2023-07-07 18:11:51 +0000457 return
458 }
459 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000460 archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
hamzeh41ad8812021-07-07 14:00:07 -0700461 if !ok {
462 return
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700463 }
464 })
465
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000466 s.CreateFuzzPackage(ctx, archDirs, fuzz.Cc, pctx)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700467}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700468
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500469func (s *ccRustFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
hamzeh41ad8812021-07-07 14:00:07 -0700470 packages := s.Packages.Strings()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700471 sort.Strings(packages)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400472 sort.Strings(s.FuzzPackager.SharedLibInstallStrings)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700473 // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
474 // ready to handle phony targets created in Soong. In the meantime, this
475 // exports the phony 'fuzz' target and dependencies on packages to
476 // core/main.mk so that we can use dist-for-goals.
Cory Barkera1da26f2022-06-07 20:12:06 +0000477
478 ctx.Strict(s.fuzzPackagingArchModules, strings.Join(packages, " "))
479
480 ctx.Strict(s.fuzzTargetSharedDepsInstallPairs,
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400481 strings.Join(s.FuzzPackager.SharedLibInstallStrings, " "))
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700482
483 // Preallocate the slice of fuzz targets to minimise memory allocations.
Cory Barkera1da26f2022-06-07 20:12:06 +0000484 s.PreallocateSlice(ctx, s.allFuzzTargetsName)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700485}
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400486
487// GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for
488// packaging.
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000489func 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 -0400490 var files []fuzz.FileToZip
491
Cory Barkera1da26f2022-06-07 20:12:06 +0000492 fuzzDir := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000493
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000494 for _, ruleBuilderInstall := range sharedLibraries {
495 library := ruleBuilderInstall.From
496 install := ruleBuilderInstall.To
Colin Cross80462dc2023-05-08 15:09:31 -0700497 files = append(files, fuzz.FileToZip{
498 SourceFilePath: library,
499 DestinationPathPrefix: destinationPathPrefix,
500 DestinationPath: install,
501 })
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400502
503 // For each architecture-specific shared library dependency, we need to
504 // install it to the output directory. Setup the install destination here,
505 // which will be used by $(copy-many-files) in the Make backend.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500506 installDestination := SharedLibraryInstallLocation(
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000507 install, module.Host(), fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400508 if (*sharedLibraryInstalled)[installDestination] {
509 continue
510 }
511 (*sharedLibraryInstalled)[installDestination] = true
512
513 // Escape all the variables, as the install destination here will be called
514 // via. $(eval) in Make.
515 installDestination = strings.ReplaceAll(
516 installDestination, "$", "$$")
517 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
518 library.String()+":"+installDestination)
519
520 // Ensure that on device, the library is also reinstalled to the /symbols/
521 // dir. Symbolized DSO's are always installed to the device when fuzzing, but
522 // we want symbolization tools (like `stack`) to be able to find the symbols
523 // in $ANDROID_PRODUCT_OUT/symbols automagically.
524 if !module.Host() {
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000525 symbolsInstallDestination := SharedLibrarySymbolsInstallLocation(install, fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400526 symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
527 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
528 library.String()+":"+symbolsInstallDestination)
529 }
530 }
531 return files
532}
Colin Cross31d89b42022-10-04 16:35:39 -0700533
534// CollectAllSharedDependencies search over the provided module's dependencies using
535// VisitDirectDeps and WalkDeps to enumerate all shared library dependencies.
536// VisitDirectDeps is used first to avoid incorrectly using the core libraries (sanitizer
537// runtimes, libc, libdl, etc.) from a dependency. This may cause issues when dependencies
538// have explicit sanitizer tags, as we may get a dependency on an unsanitized libc, etc.
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000539func CollectAllSharedDependencies(ctx android.ModuleContext) (android.RuleBuilderInstalls, []android.Module) {
Colin Cross31d89b42022-10-04 16:35:39 -0700540 seen := make(map[string]bool)
541 recursed := make(map[string]bool)
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000542 deps := []android.Module{}
Colin Cross31d89b42022-10-04 16:35:39 -0700543
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000544 var sharedLibraries android.RuleBuilderInstalls
Colin Cross31d89b42022-10-04 16:35:39 -0700545
546 // Enumerate the first level of dependencies, as we discard all non-library
547 // modules in the BFS loop below.
548 ctx.VisitDirectDeps(func(dep android.Module) {
549 if !IsValidSharedDependency(dep) {
550 return
551 }
Colin Cross313aa542023-12-13 13:47:44 -0800552 sharedLibraryInfo, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, dep, SharedLibraryInfoProvider)
553 if !hasSharedLibraryInfo {
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000554 return
555 }
Colin Cross31d89b42022-10-04 16:35:39 -0700556 if seen[ctx.OtherModuleName(dep)] {
557 return
558 }
559 seen[ctx.OtherModuleName(dep)] = true
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000560 deps = append(deps, dep)
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000561
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000562 installDestination := sharedLibraryInfo.SharedLibrary.Base()
563 ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, dep, "unstripped"), installDestination}
564 sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
Colin Cross31d89b42022-10-04 16:35:39 -0700565 })
566
567 ctx.WalkDeps(func(child, parent android.Module) bool {
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400568
569 // If this is a Rust module which is not rust_ffi_shared, we still want to bundle any transitive
570 // shared dependencies (even for rust_ffi_static)
571 if rustmod, ok := child.(LinkableInterface); ok && rustmod.RustLibraryInterface() && !rustmod.Shared() {
572 if recursed[ctx.OtherModuleName(child)] {
573 return false
574 }
575 recursed[ctx.OtherModuleName(child)] = true
576 return true
577 }
578
Colin Cross31d89b42022-10-04 16:35:39 -0700579 if !IsValidSharedDependency(child) {
580 return false
581 }
Colin Cross313aa542023-12-13 13:47:44 -0800582 sharedLibraryInfo, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, child, SharedLibraryInfoProvider)
583 if !hasSharedLibraryInfo {
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000584 return false
585 }
Colin Cross31d89b42022-10-04 16:35:39 -0700586 if !seen[ctx.OtherModuleName(child)] {
587 seen[ctx.OtherModuleName(child)] = true
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000588 deps = append(deps, child)
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000589
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000590 installDestination := sharedLibraryInfo.SharedLibrary.Base()
591 ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, child, "unstripped"), installDestination}
592 sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
Colin Cross31d89b42022-10-04 16:35:39 -0700593 }
594
595 if recursed[ctx.OtherModuleName(child)] {
596 return false
597 }
598 recursed[ctx.OtherModuleName(child)] = true
599 return true
600 })
601
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000602 return sharedLibraries, deps
Colin Cross31d89b42022-10-04 16:35:39 -0700603}