blob: 2f00aa7e8070ca5fd07f281feea543dcb3a972f0 [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 {
Steven Morelandd86fec52023-12-28 01:09:40 +0000147 subdir := "lib"
148 if ctx.inVendor() {
149 subdir = "lib/vendor"
150 }
151
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700152 flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800153 // RunPaths on devices isn't instantiated by the base linker. `../lib` for
154 // installed fuzz targets (both host and device), and `./lib` for fuzz
155 // target packages.
Steven Morelandd86fec52023-12-28 01:09:40 +0000156 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/`+subdir)
Cory Barkera1da26f2022-06-07 20:12:06 +0000157
Kris Alderc2634812022-10-25 10:58:59 -0700158 // When running on device, fuzz targets with vendor: true set will be in
159 // fuzzer_name/vendor/fuzzer_name (note the extra 'vendor' and thus need to
160 // link with libraries in ../../lib/. Non-vendor binaries only need to look
161 // one level up, in ../lib/.
162 if ctx.inVendor() {
Steven Morelandd86fec52023-12-28 01:09:40 +0000163 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../../`+subdir)
Kris Alderc2634812022-10-25 10:58:59 -0700164 } else {
Steven Morelandd86fec52023-12-28 01:09:40 +0000165 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../`+subdir)
Kris Alderc2634812022-10-25 10:58:59 -0700166 }
167
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700168 return flags
169}
170
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800171func (fuzz *fuzzBinary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
172 fuzz.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
173 moduleInfoJSON.Class = []string{"EXECUTABLES"}
174}
175
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400176// IsValidSharedDependency takes a module and determines if it is a unique shared library
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700177// that should be installed in the fuzz target output directories. This function
178// returns true, unless:
Colin Crossd079e0b2022-08-16 10:27:33 -0700179// - The module is not an installable shared library, or
180// - The module is a header or stub, or
181// - The module is a prebuilt and its source is available, or
182// - The module is a versioned member of an SDK snapshot.
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400183func IsValidSharedDependency(dependency android.Module) bool {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700184 // TODO(b/144090547): We should be parsing these modules using
185 // ModuleDependencyTag instead of the current brute-force checking.
186
Colin Cross31076b32020-10-23 17:22:06 -0700187 linkable, ok := dependency.(LinkableInterface)
188 if !ok || !linkable.CcLibraryInterface() {
189 // Discard non-linkables.
190 return false
191 }
192
193 if !linkable.Shared() {
194 // Discard static libs.
195 return false
196 }
197
Colin Cross31076b32020-10-23 17:22:06 -0700198 if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800199 // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not
200 // be excluded on the basis of they're not CCLibrary()'s.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700201 return false
202 }
203
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800204 // We discarded module stubs libraries above, but the LLNDK prebuilts stubs
205 // libraries must be handled differently - by looking for the stubDecorator.
206 // Discard LLNDK prebuilts stubs as well.
207 if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary {
208 if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary {
209 return false
210 }
Victor Chang00c144f2021-02-09 12:30:33 +0000211 // Discard installable:false libraries because they are expected to be absent
212 // in runtime.
Colin Cross1bc94122021-10-28 13:25:54 -0700213 if !proptools.BoolDefault(ccLibrary.Installable(), true) {
Victor Chang00c144f2021-02-09 12:30:33 +0000214 return false
215 }
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800216 }
217
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100218 // If the same library is present both as source and a prebuilt we must pick
219 // only one to avoid a conflict. Always prefer the source since the prebuilt
220 // probably won't be built with sanitizers enabled.
Paul Duffinf7c99f52021-04-28 10:41:21 +0100221 if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() {
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100222 return false
223 }
224
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700225 return true
226}
227
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500228func SharedLibraryInstallLocation(
Steven Morelandd86fec52023-12-28 01:09:40 +0000229 libraryBase string, isHost bool, isVendor bool, fuzzDir string, archString string) string {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700230 installLocation := "$(PRODUCT_OUT)/data"
231 if isHost {
232 installLocation = "$(HOST_OUT)"
233 }
Steven Morelandd86fec52023-12-28 01:09:40 +0000234 subdir := "lib"
235 if isVendor {
236 subdir = "lib/vendor"
237 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700238 installLocation = filepath.Join(
Steven Morelandd86fec52023-12-28 01:09:40 +0000239 installLocation, fuzzDir, archString, subdir, libraryBase)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700240 return installLocation
241}
242
Mitch Phillips0bf97132020-03-06 09:38:12 -0800243// Get the device-only shared library symbols install directory.
Steven Morelandd86fec52023-12-28 01:09:40 +0000244func SharedLibrarySymbolsInstallLocation(libraryBase string, isVendor bool, fuzzDir string, archString string) string {
245 subdir := "lib"
246 if isVendor {
247 subdir = "lib/vendor"
248 }
249 return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, subdir, libraryBase)
Mitch Phillips0bf97132020-03-06 09:38:12 -0800250}
251
Cory Barkera1da26f2022-06-07 20:12:06 +0000252func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500253 fuzzBin.fuzzPackagedModule = PackageFuzzModule(ctx, fuzzBin.fuzzPackagedModule, pctx)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700254
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800255 installBase := "fuzz"
256
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700257 // Grab the list of required shared libraries.
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000258 fuzzBin.sharedLibraries, _ = CollectAllSharedDependencies(ctx)
Colin Crossdc809f92019-11-20 15:58:32 -0800259
Steven Morelandd86fec52023-12-28 01:09:40 +0000260 // TODO: does not mirror Android linkernamespaces
261 // the logic here has special cases for vendor, but it would need more work to
262 // work in arbitrary partitions, so just surface errors early for a few cases
263 //
264 // Even without these, there are certain situations across linkernamespaces
265 // that this won't support. For instance, you might have:
266 //
267 // my_fuzzer (vendor) -> libbinder_ndk (core) -> libbinder (vendor)
268 //
269 // This dependency chain wouldn't be possible to express in the current
270 // logic because all the deps currently match the variant of the source
271 // module.
272
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000273 for _, ruleBuilderInstall := range fuzzBin.sharedLibraries {
274 install := ruleBuilderInstall.To
Cory Barkera1da26f2022-06-07 20:12:06 +0000275 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500276 SharedLibraryInstallLocation(
Steven Morelandd86fec52023-12-28 01:09:40 +0000277 install, ctx.Host(), ctx.inVendor(), installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800278
279 // Also add the dependency on the shared library symbols dir.
280 if !ctx.Host() {
Cory Barkera1da26f2022-06-07 20:12:06 +0000281 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
Steven Morelandd86fec52023-12-28 01:09:40 +0000282 SharedLibrarySymbolsInstallLocation(install, ctx.inVendor(), installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800283 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700284 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800285
286 for _, d := range fuzzBin.fuzzPackagedModule.Corpus {
287 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, RelativeInstallPath: "corpus", WithoutRel: true})
288 }
289
290 for _, d := range fuzzBin.fuzzPackagedModule.Data {
291 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, RelativeInstallPath: "data"})
292 }
293
294 if d := fuzzBin.fuzzPackagedModule.Dictionary; d != nil {
295 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, WithoutRel: true})
296 }
297
298 if d := fuzzBin.fuzzPackagedModule.Config; d != nil {
299 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, WithoutRel: true})
300 }
301
302 fuzzBin.binaryDecorator.baseInstaller.dir = filepath.Join(
303 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
304 fuzzBin.binaryDecorator.baseInstaller.dir64 = filepath.Join(
305 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
306 fuzzBin.binaryDecorator.baseInstaller.installTestData(ctx, fuzzBin.data)
307 fuzzBin.binaryDecorator.baseInstaller.install(ctx, file)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700308}
309
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500310func PackageFuzzModule(ctx android.ModuleContext, fuzzPackagedModule fuzz.FuzzPackagedModule, pctx android.PackageContext) fuzz.FuzzPackagedModule {
311 fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Corpus)
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500312
313 fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Data)
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500314
315 if fuzzPackagedModule.FuzzProperties.Dictionary != nil {
316 fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzzPackagedModule.FuzzProperties.Dictionary)
317 if fuzzPackagedModule.Dictionary.Ext() != ".dict" {
318 ctx.PropertyErrorf("dictionary",
319 "Fuzzer dictionary %q does not have '.dict' extension",
320 fuzzPackagedModule.Dictionary.String())
321 }
322 }
323
324 if fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
325 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
326 android.WriteFileRule(ctx, configPath, fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
327 fuzzPackagedModule.Config = configPath
328 }
329 return fuzzPackagedModule
330}
331
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000332func NewFuzzer(hod android.HostOrDeviceSupported) *Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800333 module, binary := newBinary(hod)
Cory Barkera1da26f2022-06-07 20:12:06 +0000334 baseInstallerPath := "fuzz"
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700335
Cory Barkera1da26f2022-06-07 20:12:06 +0000336 binary.baseInstaller = NewBaseInstaller(baseInstallerPath, baseInstallerPath, InstallInData)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700337
Cory Barkera1da26f2022-06-07 20:12:06 +0000338 fuzzBin := &fuzzBinary{
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700339 binaryDecorator: binary,
340 baseCompiler: NewBaseCompiler(),
341 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000342 module.compiler = fuzzBin
343 module.linker = fuzzBin
344 module.installer = fuzzBin
Colin Crosseec9b282019-07-18 16:20:52 -0700345
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000346 module.fuzzer.Properties.FuzzFramework = fuzz.LibFuzzer
347
Colin Crosseec9b282019-07-18 16:20:52 -0700348 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
349 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400350
351 extraProps := struct {
352 Sanitize struct {
353 Fuzzer *bool
354 }
Colin Crosseec9b282019-07-18 16:20:52 -0700355 Target struct {
356 Darwin struct {
357 Enabled *bool
358 }
Alex Light71123ec2019-07-24 13:34:19 -0700359 Linux_bionic struct {
360 Enabled *bool
361 }
Colin Crosseec9b282019-07-18 16:20:52 -0700362 }
363 }{}
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400364 extraProps.Sanitize.Fuzzer = BoolPtr(true)
365 extraProps.Target.Darwin.Enabled = BoolPtr(false)
366 extraProps.Target.Linux_bionic.Enabled = BoolPtr(false)
367 ctx.AppendProperties(&extraProps)
Cory Barkera1da26f2022-06-07 20:12:06 +0000368
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000369 targetFramework := fuzz.GetFramework(ctx, fuzz.Cc)
370 if !fuzz.IsValidFrameworkForModule(targetFramework, fuzz.Cc, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzzing_frameworks) {
371 ctx.Module().Disable()
372 return
373 }
374
375 if targetFramework == fuzz.AFL {
376 fuzzBin.baseCompiler.Properties.Srcs = append(fuzzBin.baseCompiler.Properties.Srcs, ":aflpp_driver", ":afl-compiler-rt")
377 module.fuzzer.Properties.FuzzFramework = fuzz.AFL
378 }
379 })
Cory Barker74aea6c2022-08-08 15:55:12 +0000380
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700381 return module
382}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700383
384// Responsible for generating GNU Make rules that package fuzz targets into
385// their architecture & target/host specific zip file.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500386type ccRustFuzzPackager struct {
hamzehc0a671f2021-07-22 12:05:08 -0700387 fuzz.FuzzPackager
Cole Faust06ea5312023-10-18 17:38:40 -0700388 fuzzPackagingArchModules string
389 fuzzTargetSharedDepsInstallPairs string
390 allFuzzTargetsName string
391 onlyIncludePresubmits bool
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700392}
393
394func fuzzPackagingFactory() android.Singleton {
Cory Barkera1da26f2022-06-07 20:12:06 +0000395
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500396 fuzzPackager := &ccRustFuzzPackager{
Cory Barkera1da26f2022-06-07 20:12:06 +0000397 fuzzPackagingArchModules: "SOONG_FUZZ_PACKAGING_ARCH_MODULES",
398 fuzzTargetSharedDepsInstallPairs: "FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
399 allFuzzTargetsName: "ALL_FUZZ_TARGETS",
Cole Faust06ea5312023-10-18 17:38:40 -0700400 onlyIncludePresubmits: false,
David Fufd121fc2023-07-07 18:11:51 +0000401 }
402 return fuzzPackager
403}
404
405func fuzzPackagingFactoryPresubmit() android.Singleton {
406
407 fuzzPackager := &ccRustFuzzPackager{
408 fuzzPackagingArchModules: "SOONG_PRESUBMIT_FUZZ_PACKAGING_ARCH_MODULES",
409 fuzzTargetSharedDepsInstallPairs: "PRESUBMIT_FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
410 allFuzzTargetsName: "ALL_PRESUBMIT_FUZZ_TARGETS",
Cole Faust06ea5312023-10-18 17:38:40 -0700411 onlyIncludePresubmits: true,
Cory Barkera1da26f2022-06-07 20:12:06 +0000412 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000413 return fuzzPackager
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700414}
415
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500416func (s *ccRustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700417 // Map between each architecture + host/device combination, and the files that
418 // need to be packaged (in the tuple of {source file, destination folder in
419 // archive}).
hamzehc0a671f2021-07-22 12:05:08 -0700420 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700421
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700422 // List of individual fuzz targets, so that 'make fuzz' also installs the targets
423 // to the correct output directories as well.
hamzeh41ad8812021-07-07 14:00:07 -0700424 s.FuzzTargets = make(map[string]bool)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700425
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400426 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
427 // multiple fuzzers that depend on the same shared library.
428 sharedLibraryInstalled := make(map[string]bool)
429
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700430 ctx.VisitAllModules(func(module android.Module) {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500431 ccModule, ok := module.(LinkableInterface)
432 if !ok || ccModule.PreventInstall() {
hamzeh41ad8812021-07-07 14:00:07 -0700433 return
434 }
hamzeh41ad8812021-07-07 14:00:07 -0700435 // Discard non-fuzz targets.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500436 if ok := fuzz.IsValid(ccModule.FuzzModuleStruct()); !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700437 return
438 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700439
Cory Barkera1da26f2022-06-07 20:12:06 +0000440 sharedLibsInstallDirPrefix := "lib"
Steven Morelandd86fec52023-12-28 01:09:40 +0000441 if ccModule.InVendor() {
442 sharedLibsInstallDirPrefix = "lib/vendor"
443 }
444
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500445 if !ccModule.IsFuzzModule() {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700446 return
447 }
448
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700449 hostOrTargetString := "target"
Colin Cross64a4a5f2023-05-16 17:54:27 -0700450 if ccModule.Target().HostCross {
451 hostOrTargetString = "host_cross"
452 } else if ccModule.Host() {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700453 hostOrTargetString = "host"
454 }
David Fufd121fc2023-07-07 18:11:51 +0000455 if s.onlyIncludePresubmits == true {
456 hostOrTargetString = "presubmit-" + hostOrTargetString
457 }
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700458
Cory Barkera1da26f2022-06-07 20:12:06 +0000459 fpm := fuzz.FuzzPackagedModule{}
460 if ok {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500461 fpm = ccModule.FuzzPackagedModule()
Cory Barkera1da26f2022-06-07 20:12:06 +0000462 }
463
464 intermediatePath := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000465
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500466 archString := ccModule.Target().Arch.ArchType.String()
Cory Barkera1da26f2022-06-07 20:12:06 +0000467 archDir := android.PathForIntermediates(ctx, intermediatePath, hostOrTargetString, archString)
hamzehc0a671f2021-07-22 12:05:08 -0700468 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700469
hamzehc0a671f2021-07-22 12:05:08 -0700470 var files []fuzz.FileToZip
Colin Crossf1a035e2020-11-16 17:32:30 -0800471 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800472
hamzeh41ad8812021-07-07 14:00:07 -0700473 // Package the corpus, data, dict and config into a zipfile.
Cory Barkera1da26f2022-06-07 20:12:06 +0000474 files = s.PackageArtifacts(ctx, module, fpm, archDir, builder)
Tri Voad172d82019-11-27 13:45:45 -0800475
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400476 // Package shared libraries
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500477 files = append(files, GetSharedLibsToZip(ccModule.FuzzSharedLibraries(), ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700478
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700479 // The executable.
Colin Cross80462dc2023-05-08 15:09:31 -0700480 files = append(files, fuzz.FileToZip{SourceFilePath: android.OutputFileForModule(ctx, ccModule, "unstripped")})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700481
David Fufd121fc2023-07-07 18:11:51 +0000482 if s.onlyIncludePresubmits == true {
483 if fpm.FuzzProperties.Fuzz_config == nil {
484 return
485 }
Cole Faust06ea5312023-10-18 17:38:40 -0700486 if !BoolDefault(fpm.FuzzProperties.Fuzz_config.Use_for_presubmit, false) {
David Fufd121fc2023-07-07 18:11:51 +0000487 return
488 }
489 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000490 archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
hamzeh41ad8812021-07-07 14:00:07 -0700491 if !ok {
492 return
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700493 }
494 })
495
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000496 s.CreateFuzzPackage(ctx, archDirs, fuzz.Cc, pctx)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700497}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700498
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500499func (s *ccRustFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
hamzeh41ad8812021-07-07 14:00:07 -0700500 packages := s.Packages.Strings()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700501 sort.Strings(packages)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400502 sort.Strings(s.FuzzPackager.SharedLibInstallStrings)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700503 // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
504 // ready to handle phony targets created in Soong. In the meantime, this
505 // exports the phony 'fuzz' target and dependencies on packages to
506 // core/main.mk so that we can use dist-for-goals.
Cory Barkera1da26f2022-06-07 20:12:06 +0000507
508 ctx.Strict(s.fuzzPackagingArchModules, strings.Join(packages, " "))
509
510 ctx.Strict(s.fuzzTargetSharedDepsInstallPairs,
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400511 strings.Join(s.FuzzPackager.SharedLibInstallStrings, " "))
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700512
513 // Preallocate the slice of fuzz targets to minimise memory allocations.
Cory Barkera1da26f2022-06-07 20:12:06 +0000514 s.PreallocateSlice(ctx, s.allFuzzTargetsName)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700515}
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400516
517// GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for
518// packaging.
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000519func 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 -0400520 var files []fuzz.FileToZip
521
Cory Barkera1da26f2022-06-07 20:12:06 +0000522 fuzzDir := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000523
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000524 for _, ruleBuilderInstall := range sharedLibraries {
525 library := ruleBuilderInstall.From
526 install := ruleBuilderInstall.To
Colin Cross80462dc2023-05-08 15:09:31 -0700527 files = append(files, fuzz.FileToZip{
528 SourceFilePath: library,
529 DestinationPathPrefix: destinationPathPrefix,
530 DestinationPath: install,
531 })
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400532
533 // For each architecture-specific shared library dependency, we need to
534 // install it to the output directory. Setup the install destination here,
535 // which will be used by $(copy-many-files) in the Make backend.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500536 installDestination := SharedLibraryInstallLocation(
Steven Morelandd86fec52023-12-28 01:09:40 +0000537 install, module.Host(), module.InVendor(), fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400538 if (*sharedLibraryInstalled)[installDestination] {
539 continue
540 }
541 (*sharedLibraryInstalled)[installDestination] = true
542
543 // Escape all the variables, as the install destination here will be called
544 // via. $(eval) in Make.
545 installDestination = strings.ReplaceAll(
546 installDestination, "$", "$$")
547 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
548 library.String()+":"+installDestination)
549
550 // Ensure that on device, the library is also reinstalled to the /symbols/
551 // dir. Symbolized DSO's are always installed to the device when fuzzing, but
552 // we want symbolization tools (like `stack`) to be able to find the symbols
553 // in $ANDROID_PRODUCT_OUT/symbols automagically.
554 if !module.Host() {
Steven Morelandd86fec52023-12-28 01:09:40 +0000555 symbolsInstallDestination := SharedLibrarySymbolsInstallLocation(install, module.InVendor(), fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400556 symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
557 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
558 library.String()+":"+symbolsInstallDestination)
559 }
560 }
561 return files
562}
Colin Cross31d89b42022-10-04 16:35:39 -0700563
564// CollectAllSharedDependencies search over the provided module's dependencies using
565// VisitDirectDeps and WalkDeps to enumerate all shared library dependencies.
566// VisitDirectDeps is used first to avoid incorrectly using the core libraries (sanitizer
567// runtimes, libc, libdl, etc.) from a dependency. This may cause issues when dependencies
568// have explicit sanitizer tags, as we may get a dependency on an unsanitized libc, etc.
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000569func CollectAllSharedDependencies(ctx android.ModuleContext) (android.RuleBuilderInstalls, []android.Module) {
Colin Cross31d89b42022-10-04 16:35:39 -0700570 seen := make(map[string]bool)
571 recursed := make(map[string]bool)
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000572 deps := []android.Module{}
Colin Cross31d89b42022-10-04 16:35:39 -0700573
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000574 var sharedLibraries android.RuleBuilderInstalls
Colin Cross31d89b42022-10-04 16:35:39 -0700575
576 // Enumerate the first level of dependencies, as we discard all non-library
577 // modules in the BFS loop below.
578 ctx.VisitDirectDeps(func(dep android.Module) {
579 if !IsValidSharedDependency(dep) {
580 return
581 }
Colin Cross313aa542023-12-13 13:47:44 -0800582 sharedLibraryInfo, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, dep, SharedLibraryInfoProvider)
583 if !hasSharedLibraryInfo {
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000584 return
585 }
Colin Cross31d89b42022-10-04 16:35:39 -0700586 if seen[ctx.OtherModuleName(dep)] {
587 return
588 }
589 seen[ctx.OtherModuleName(dep)] = true
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000590 deps = append(deps, dep)
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000591
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000592 installDestination := sharedLibraryInfo.SharedLibrary.Base()
593 ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, dep, "unstripped"), installDestination}
594 sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
Colin Cross31d89b42022-10-04 16:35:39 -0700595 })
596
597 ctx.WalkDeps(func(child, parent android.Module) bool {
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400598
599 // If this is a Rust module which is not rust_ffi_shared, we still want to bundle any transitive
Ivan Lozano0a468a42024-05-13 21:03:34 -0400600 // shared dependencies (even for rust_ffi_rlib or rust_ffi_static)
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400601 if rustmod, ok := child.(LinkableInterface); ok && rustmod.RustLibraryInterface() && !rustmod.Shared() {
602 if recursed[ctx.OtherModuleName(child)] {
603 return false
604 }
605 recursed[ctx.OtherModuleName(child)] = true
606 return true
607 }
608
Colin Cross31d89b42022-10-04 16:35:39 -0700609 if !IsValidSharedDependency(child) {
610 return false
611 }
Colin Cross313aa542023-12-13 13:47:44 -0800612 sharedLibraryInfo, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, child, SharedLibraryInfoProvider)
613 if !hasSharedLibraryInfo {
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000614 return false
615 }
Colin Cross31d89b42022-10-04 16:35:39 -0700616 if !seen[ctx.OtherModuleName(child)] {
617 seen[ctx.OtherModuleName(child)] = true
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000618 deps = append(deps, child)
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000619
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000620 installDestination := sharedLibraryInfo.SharedLibrary.Base()
621 ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, child, "unstripped"), installDestination}
622 sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
Colin Cross31d89b42022-10-04 16:35:39 -0700623 }
624
625 if recursed[ctx.OtherModuleName(child)] {
626 return false
627 }
628 recursed[ctx.OtherModuleName(child)] = true
629 return true
630 })
631
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000632 return sharedLibraries, deps
Colin Cross31d89b42022-10-04 16:35:39 -0700633}