blob: 2b91c57c33ca45853527c6349b616a90ffe920c8 [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
Colin Cross597bad62024-10-08 15:10:55 -070060// fuzzTransitionMutator creates variants to propagate the FuzzFramework value down to dependencies.
61type fuzzTransitionMutator struct{}
62
63func (f *fuzzTransitionMutator) Split(ctx android.BaseModuleContext) []string {
64 return []string{""}
65}
66
67func (f *fuzzTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
68 m, ok := ctx.Module().(*Module)
69 if !ok {
70 return ""
71 }
72
73 if m.fuzzer == nil {
74 return ""
75 }
76
77 if m.sanitize == nil {
78 return ""
79 }
80
81 isFuzzerPointer := m.sanitize.getSanitizerBoolPtr(Fuzzer)
82 if isFuzzerPointer == nil || !*isFuzzerPointer {
83 return ""
84 }
85
86 if m.fuzzer.Properties.FuzzFramework != "" {
87 return m.fuzzer.Properties.FuzzFramework.Variant()
88 }
89
90 return sourceVariation
91}
92
93func (f *fuzzTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
94 m, ok := ctx.Module().(*Module)
95 if !ok {
96 return ""
97 }
98
99 if m.fuzzer == nil {
100 return ""
101 }
102
103 if m.sanitize == nil {
104 return ""
105 }
106
107 isFuzzerPointer := m.sanitize.getSanitizerBoolPtr(Fuzzer)
108 if isFuzzerPointer == nil || !*isFuzzerPointer {
109 return ""
110 }
111
112 return incomingVariation
113}
114
115func (f *fuzzTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
116 m, ok := ctx.Module().(*Module)
Cory Barkera1da26f2022-06-07 20:12:06 +0000117 if !ok {
118 return
119 }
120
Colin Cross597bad62024-10-08 15:10:55 -0700121 if m.fuzzer == nil {
Cory Barkera1da26f2022-06-07 20:12:06 +0000122 return
123 }
124
Colin Cross597bad62024-10-08 15:10:55 -0700125 if variation != "" {
126 m.fuzzer.Properties.FuzzFramework = fuzz.FrameworkFromVariant(variation)
127 m.SetHideFromMake()
128 m.SetPreventInstall()
129 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000130}
131
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700132// cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at
133// $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on
134// your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree.
Cory Barkera1da26f2022-06-07 20:12:06 +0000135func LibFuzzFactory() android.Module {
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000136 module := NewFuzzer(android.HostAndDeviceSupported)
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000137 module.testModule = true
Cory Barkera1da26f2022-06-07 20:12:06 +0000138 return module.Init()
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700139}
140
141type fuzzBinary struct {
142 *binaryDecorator
143 *baseCompiler
Cory Barkera1da26f2022-06-07 20:12:06 +0000144 fuzzPackagedModule fuzz.FuzzPackagedModule
hamzeh41ad8812021-07-07 14:00:07 -0700145 installedSharedDeps []string
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000146 sharedLibraries android.RuleBuilderInstalls
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800147 data []android.DataPath
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700148}
149
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400150func (fuzz *fuzzBinary) fuzzBinary() bool {
151 return true
152}
153
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700154func (fuzz *fuzzBinary) linkerProps() []interface{} {
155 props := fuzz.binaryDecorator.linkerProps()
hamzeh41ad8812021-07-07 14:00:07 -0700156 props = append(props, &fuzz.fuzzPackagedModule.FuzzProperties)
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000157
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700158 return props
159}
160
161func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) {
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700162 fuzz.binaryDecorator.linkerInit(ctx)
163}
164
Cory Barkera1da26f2022-06-07 20:12:06 +0000165func (fuzzBin *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000166 if ctx.Config().Getenv("FUZZ_FRAMEWORK") == "AFL" {
Cory Barkera1da26f2022-06-07 20:12:06 +0000167 deps.HeaderLibs = append(deps.HeaderLibs, "libafl_headers")
Cory Barkera1da26f2022-06-07 20:12:06 +0000168 } else {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +0900169 deps.StaticLibs = append(deps.StaticLibs, config.LibFuzzerRuntimeLibrary())
Kris Alderd406da12022-10-21 09:34:21 -0700170 // Fuzzers built with HWASAN should use the interceptors for better
171 // mutation based on signals in strcmp, memcpy, etc. This is only needed for
172 // fuzz targets, not generic HWASAN-ified binaries or libraries.
173 if module, ok := ctx.Module().(*Module); ok {
174 if module.IsSanitizerEnabled(Hwasan) {
Kiyoung Kim0d8908c2024-05-07 14:47:35 +0900175 deps.StaticLibs = append(deps.StaticLibs, config.LibFuzzerRuntimeInterceptors())
Kris Alderd406da12022-10-21 09:34:21 -0700176 }
177 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000178 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000179
180 deps = fuzzBin.binaryDecorator.linkerDeps(ctx, deps)
181 return deps
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700182}
183
184func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Steven Morelandc3f083e2024-10-19 02:08:15 +0000185 var subdir string
186 if ctx.isForPlatform() {
187 subdir = "lib"
188 } else if ctx.inVendor() {
Steven Morelandd86fec52023-12-28 01:09:40 +0000189 subdir = "lib/vendor"
Steven Morelandc3f083e2024-10-19 02:08:15 +0000190 } else {
191 ctx.ModuleErrorf("Fuzzer must be system or vendor variant")
Steven Morelandd86fec52023-12-28 01:09:40 +0000192 }
193
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700194 flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800195 // RunPaths on devices isn't instantiated by the base linker. `../lib` for
196 // installed fuzz targets (both host and device), and `./lib` for fuzz
197 // target packages.
Steven Morelandd86fec52023-12-28 01:09:40 +0000198 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/`+subdir)
Cory Barkera1da26f2022-06-07 20:12:06 +0000199
Kris Alderc2634812022-10-25 10:58:59 -0700200 // When running on device, fuzz targets with vendor: true set will be in
201 // fuzzer_name/vendor/fuzzer_name (note the extra 'vendor' and thus need to
202 // link with libraries in ../../lib/. Non-vendor binaries only need to look
203 // one level up, in ../lib/.
204 if ctx.inVendor() {
Steven Morelandd86fec52023-12-28 01:09:40 +0000205 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../../`+subdir)
Kris Alderc2634812022-10-25 10:58:59 -0700206 } else {
Steven Morelandd86fec52023-12-28 01:09:40 +0000207 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../`+subdir)
Kris Alderc2634812022-10-25 10:58:59 -0700208 }
209
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700210 return flags
211}
212
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800213func (fuzz *fuzzBinary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
214 fuzz.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
215 moduleInfoJSON.Class = []string{"EXECUTABLES"}
216}
217
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400218// IsValidSharedDependency takes a module and determines if it is a unique shared library
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700219// that should be installed in the fuzz target output directories. This function
220// returns true, unless:
Colin Crossd079e0b2022-08-16 10:27:33 -0700221// - The module is not an installable shared library, or
222// - The module is a header or stub, or
223// - The module is a prebuilt and its source is available, or
224// - The module is a versioned member of an SDK snapshot.
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400225func IsValidSharedDependency(dependency android.Module) bool {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700226 // TODO(b/144090547): We should be parsing these modules using
227 // ModuleDependencyTag instead of the current brute-force checking.
228
Colin Cross31076b32020-10-23 17:22:06 -0700229 linkable, ok := dependency.(LinkableInterface)
230 if !ok || !linkable.CcLibraryInterface() {
231 // Discard non-linkables.
232 return false
233 }
234
235 if !linkable.Shared() {
236 // Discard static libs.
237 return false
238 }
239
Colin Cross31076b32020-10-23 17:22:06 -0700240 if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800241 // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not
242 // be excluded on the basis of they're not CCLibrary()'s.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700243 return false
244 }
245
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800246 // We discarded module stubs libraries above, but the LLNDK prebuilts stubs
247 // libraries must be handled differently - by looking for the stubDecorator.
248 // Discard LLNDK prebuilts stubs as well.
249 if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary {
250 if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary {
251 return false
252 }
Victor Chang00c144f2021-02-09 12:30:33 +0000253 // Discard installable:false libraries because they are expected to be absent
254 // in runtime.
Colin Cross1bc94122021-10-28 13:25:54 -0700255 if !proptools.BoolDefault(ccLibrary.Installable(), true) {
Victor Chang00c144f2021-02-09 12:30:33 +0000256 return false
257 }
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800258 }
259
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100260 // If the same library is present both as source and a prebuilt we must pick
261 // only one to avoid a conflict. Always prefer the source since the prebuilt
262 // probably won't be built with sanitizers enabled.
Paul Duffinf7c99f52021-04-28 10:41:21 +0100263 if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() {
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100264 return false
265 }
266
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700267 return true
268}
269
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500270func SharedLibraryInstallLocation(
Steven Morelandd86fec52023-12-28 01:09:40 +0000271 libraryBase string, isHost bool, isVendor bool, fuzzDir string, archString string) string {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700272 installLocation := "$(PRODUCT_OUT)/data"
273 if isHost {
274 installLocation = "$(HOST_OUT)"
275 }
Steven Morelandd86fec52023-12-28 01:09:40 +0000276 subdir := "lib"
277 if isVendor {
278 subdir = "lib/vendor"
279 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700280 installLocation = filepath.Join(
Steven Morelandd86fec52023-12-28 01:09:40 +0000281 installLocation, fuzzDir, archString, subdir, libraryBase)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700282 return installLocation
283}
284
Mitch Phillips0bf97132020-03-06 09:38:12 -0800285// Get the device-only shared library symbols install directory.
Steven Morelandd86fec52023-12-28 01:09:40 +0000286func SharedLibrarySymbolsInstallLocation(libraryBase string, isVendor bool, fuzzDir string, archString string) string {
287 subdir := "lib"
288 if isVendor {
289 subdir = "lib/vendor"
290 }
291 return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, subdir, libraryBase)
Mitch Phillips0bf97132020-03-06 09:38:12 -0800292}
293
Cory Barkera1da26f2022-06-07 20:12:06 +0000294func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500295 fuzzBin.fuzzPackagedModule = PackageFuzzModule(ctx, fuzzBin.fuzzPackagedModule, pctx)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700296
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800297 installBase := "fuzz"
298
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700299 // Grab the list of required shared libraries.
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000300 fuzzBin.sharedLibraries, _ = CollectAllSharedDependencies(ctx)
Colin Crossdc809f92019-11-20 15:58:32 -0800301
Steven Morelandd86fec52023-12-28 01:09:40 +0000302 // TODO: does not mirror Android linkernamespaces
303 // the logic here has special cases for vendor, but it would need more work to
304 // work in arbitrary partitions, so just surface errors early for a few cases
305 //
306 // Even without these, there are certain situations across linkernamespaces
307 // that this won't support. For instance, you might have:
308 //
309 // my_fuzzer (vendor) -> libbinder_ndk (core) -> libbinder (vendor)
310 //
311 // This dependency chain wouldn't be possible to express in the current
312 // logic because all the deps currently match the variant of the source
313 // module.
314
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000315 for _, ruleBuilderInstall := range fuzzBin.sharedLibraries {
316 install := ruleBuilderInstall.To
Cory Barkera1da26f2022-06-07 20:12:06 +0000317 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500318 SharedLibraryInstallLocation(
Steven Morelandd86fec52023-12-28 01:09:40 +0000319 install, ctx.Host(), ctx.inVendor(), installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800320
321 // Also add the dependency on the shared library symbols dir.
322 if !ctx.Host() {
Cory Barkera1da26f2022-06-07 20:12:06 +0000323 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
Steven Morelandd86fec52023-12-28 01:09:40 +0000324 SharedLibrarySymbolsInstallLocation(install, ctx.inVendor(), installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800325 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700326 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800327
328 for _, d := range fuzzBin.fuzzPackagedModule.Corpus {
329 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, RelativeInstallPath: "corpus", WithoutRel: true})
330 }
331
332 for _, d := range fuzzBin.fuzzPackagedModule.Data {
333 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, RelativeInstallPath: "data"})
334 }
335
336 if d := fuzzBin.fuzzPackagedModule.Dictionary; d != nil {
337 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, WithoutRel: true})
338 }
339
340 if d := fuzzBin.fuzzPackagedModule.Config; d != nil {
341 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, WithoutRel: true})
342 }
343
344 fuzzBin.binaryDecorator.baseInstaller.dir = filepath.Join(
345 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
346 fuzzBin.binaryDecorator.baseInstaller.dir64 = filepath.Join(
347 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
348 fuzzBin.binaryDecorator.baseInstaller.installTestData(ctx, fuzzBin.data)
349 fuzzBin.binaryDecorator.baseInstaller.install(ctx, file)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700350}
351
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500352func PackageFuzzModule(ctx android.ModuleContext, fuzzPackagedModule fuzz.FuzzPackagedModule, pctx android.PackageContext) fuzz.FuzzPackagedModule {
353 fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Corpus)
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500354
355 fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Data)
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500356
357 if fuzzPackagedModule.FuzzProperties.Dictionary != nil {
358 fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzzPackagedModule.FuzzProperties.Dictionary)
359 if fuzzPackagedModule.Dictionary.Ext() != ".dict" {
360 ctx.PropertyErrorf("dictionary",
361 "Fuzzer dictionary %q does not have '.dict' extension",
362 fuzzPackagedModule.Dictionary.String())
363 }
364 }
365
366 if fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
367 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
368 android.WriteFileRule(ctx, configPath, fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
369 fuzzPackagedModule.Config = configPath
370 }
371 return fuzzPackagedModule
372}
373
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000374func NewFuzzer(hod android.HostOrDeviceSupported) *Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800375 module, binary := newBinary(hod)
Cory Barkera1da26f2022-06-07 20:12:06 +0000376 baseInstallerPath := "fuzz"
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700377
Cory Barkera1da26f2022-06-07 20:12:06 +0000378 binary.baseInstaller = NewBaseInstaller(baseInstallerPath, baseInstallerPath, InstallInData)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700379
Cory Barkera1da26f2022-06-07 20:12:06 +0000380 fuzzBin := &fuzzBinary{
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700381 binaryDecorator: binary,
382 baseCompiler: NewBaseCompiler(),
383 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000384 module.compiler = fuzzBin
385 module.linker = fuzzBin
386 module.installer = fuzzBin
Colin Crosseec9b282019-07-18 16:20:52 -0700387
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000388 module.fuzzer.Properties.FuzzFramework = fuzz.LibFuzzer
389
Colin Crosseec9b282019-07-18 16:20:52 -0700390 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
391 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400392
393 extraProps := struct {
394 Sanitize struct {
395 Fuzzer *bool
396 }
Colin Crosseec9b282019-07-18 16:20:52 -0700397 Target struct {
398 Darwin struct {
399 Enabled *bool
400 }
Alex Light71123ec2019-07-24 13:34:19 -0700401 Linux_bionic struct {
402 Enabled *bool
403 }
Colin Crosseec9b282019-07-18 16:20:52 -0700404 }
405 }{}
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400406 extraProps.Sanitize.Fuzzer = BoolPtr(true)
407 extraProps.Target.Darwin.Enabled = BoolPtr(false)
408 extraProps.Target.Linux_bionic.Enabled = BoolPtr(false)
409 ctx.AppendProperties(&extraProps)
Cory Barkera1da26f2022-06-07 20:12:06 +0000410
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000411 targetFramework := fuzz.GetFramework(ctx, fuzz.Cc)
412 if !fuzz.IsValidFrameworkForModule(targetFramework, fuzz.Cc, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzzing_frameworks) {
413 ctx.Module().Disable()
414 return
415 }
416
417 if targetFramework == fuzz.AFL {
Cole Faust96a692b2024-08-08 14:47:51 -0700418 fuzzBin.baseCompiler.Properties.Srcs.AppendSimpleValue([]string{":aflpp_driver", ":afl-compiler-rt"})
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000419 module.fuzzer.Properties.FuzzFramework = fuzz.AFL
420 }
421 })
Cory Barker74aea6c2022-08-08 15:55:12 +0000422
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700423 return module
424}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700425
426// Responsible for generating GNU Make rules that package fuzz targets into
427// their architecture & target/host specific zip file.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500428type ccRustFuzzPackager struct {
hamzehc0a671f2021-07-22 12:05:08 -0700429 fuzz.FuzzPackager
Cole Faust06ea5312023-10-18 17:38:40 -0700430 fuzzPackagingArchModules string
431 fuzzTargetSharedDepsInstallPairs string
432 allFuzzTargetsName string
433 onlyIncludePresubmits bool
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700434}
435
436func fuzzPackagingFactory() android.Singleton {
Cory Barkera1da26f2022-06-07 20:12:06 +0000437
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500438 fuzzPackager := &ccRustFuzzPackager{
Cory Barkera1da26f2022-06-07 20:12:06 +0000439 fuzzPackagingArchModules: "SOONG_FUZZ_PACKAGING_ARCH_MODULES",
440 fuzzTargetSharedDepsInstallPairs: "FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
441 allFuzzTargetsName: "ALL_FUZZ_TARGETS",
Cole Faust06ea5312023-10-18 17:38:40 -0700442 onlyIncludePresubmits: false,
David Fufd121fc2023-07-07 18:11:51 +0000443 }
444 return fuzzPackager
445}
446
447func fuzzPackagingFactoryPresubmit() android.Singleton {
448
449 fuzzPackager := &ccRustFuzzPackager{
450 fuzzPackagingArchModules: "SOONG_PRESUBMIT_FUZZ_PACKAGING_ARCH_MODULES",
451 fuzzTargetSharedDepsInstallPairs: "PRESUBMIT_FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
452 allFuzzTargetsName: "ALL_PRESUBMIT_FUZZ_TARGETS",
Cole Faust06ea5312023-10-18 17:38:40 -0700453 onlyIncludePresubmits: true,
Cory Barkera1da26f2022-06-07 20:12:06 +0000454 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000455 return fuzzPackager
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700456}
457
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500458func (s *ccRustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700459 // Map between each architecture + host/device combination, and the files that
460 // need to be packaged (in the tuple of {source file, destination folder in
461 // archive}).
hamzehc0a671f2021-07-22 12:05:08 -0700462 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700463
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700464 // List of individual fuzz targets, so that 'make fuzz' also installs the targets
465 // to the correct output directories as well.
hamzeh41ad8812021-07-07 14:00:07 -0700466 s.FuzzTargets = make(map[string]bool)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700467
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400468 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
469 // multiple fuzzers that depend on the same shared library.
470 sharedLibraryInstalled := make(map[string]bool)
471
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700472 ctx.VisitAllModules(func(module android.Module) {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500473 ccModule, ok := module.(LinkableInterface)
474 if !ok || ccModule.PreventInstall() {
hamzeh41ad8812021-07-07 14:00:07 -0700475 return
476 }
hamzeh41ad8812021-07-07 14:00:07 -0700477 // Discard non-fuzz targets.
Cole Fausta963b942024-04-11 17:43:00 -0700478 if ok := fuzz.IsValid(ctx, ccModule.FuzzModuleStruct()); !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700479 return
480 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700481
Cory Barkera1da26f2022-06-07 20:12:06 +0000482 sharedLibsInstallDirPrefix := "lib"
Steven Morelandd86fec52023-12-28 01:09:40 +0000483 if ccModule.InVendor() {
484 sharedLibsInstallDirPrefix = "lib/vendor"
485 }
486
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500487 if !ccModule.IsFuzzModule() {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700488 return
489 }
490
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700491 hostOrTargetString := "target"
Colin Cross64a4a5f2023-05-16 17:54:27 -0700492 if ccModule.Target().HostCross {
493 hostOrTargetString = "host_cross"
494 } else if ccModule.Host() {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700495 hostOrTargetString = "host"
496 }
David Fufd121fc2023-07-07 18:11:51 +0000497 if s.onlyIncludePresubmits == true {
498 hostOrTargetString = "presubmit-" + hostOrTargetString
499 }
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700500
Cory Barkera1da26f2022-06-07 20:12:06 +0000501 fpm := fuzz.FuzzPackagedModule{}
502 if ok {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500503 fpm = ccModule.FuzzPackagedModule()
Cory Barkera1da26f2022-06-07 20:12:06 +0000504 }
505
506 intermediatePath := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000507
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500508 archString := ccModule.Target().Arch.ArchType.String()
Cory Barkera1da26f2022-06-07 20:12:06 +0000509 archDir := android.PathForIntermediates(ctx, intermediatePath, hostOrTargetString, archString)
hamzehc0a671f2021-07-22 12:05:08 -0700510 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700511
hamzehc0a671f2021-07-22 12:05:08 -0700512 var files []fuzz.FileToZip
Colin Crossf1a035e2020-11-16 17:32:30 -0800513 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800514
hamzeh41ad8812021-07-07 14:00:07 -0700515 // Package the corpus, data, dict and config into a zipfile.
Cory Barkera1da26f2022-06-07 20:12:06 +0000516 files = s.PackageArtifacts(ctx, module, fpm, archDir, builder)
Tri Voad172d82019-11-27 13:45:45 -0800517
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400518 // Package shared libraries
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500519 files = append(files, GetSharedLibsToZip(ccModule.FuzzSharedLibraries(), ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700520
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700521 // The executable.
Colin Cross80462dc2023-05-08 15:09:31 -0700522 files = append(files, fuzz.FileToZip{SourceFilePath: android.OutputFileForModule(ctx, ccModule, "unstripped")})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700523
David Fufd121fc2023-07-07 18:11:51 +0000524 if s.onlyIncludePresubmits == true {
525 if fpm.FuzzProperties.Fuzz_config == nil {
526 return
527 }
Cole Faust06ea5312023-10-18 17:38:40 -0700528 if !BoolDefault(fpm.FuzzProperties.Fuzz_config.Use_for_presubmit, false) {
David Fufd121fc2023-07-07 18:11:51 +0000529 return
530 }
531 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000532 archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
hamzeh41ad8812021-07-07 14:00:07 -0700533 if !ok {
534 return
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700535 }
536 })
537
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000538 s.CreateFuzzPackage(ctx, archDirs, fuzz.Cc, pctx)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700539}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700540
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500541func (s *ccRustFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
hamzeh41ad8812021-07-07 14:00:07 -0700542 packages := s.Packages.Strings()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700543 sort.Strings(packages)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400544 sort.Strings(s.FuzzPackager.SharedLibInstallStrings)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700545 // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
546 // ready to handle phony targets created in Soong. In the meantime, this
547 // exports the phony 'fuzz' target and dependencies on packages to
548 // core/main.mk so that we can use dist-for-goals.
Cory Barkera1da26f2022-06-07 20:12:06 +0000549
550 ctx.Strict(s.fuzzPackagingArchModules, strings.Join(packages, " "))
551
552 ctx.Strict(s.fuzzTargetSharedDepsInstallPairs,
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400553 strings.Join(s.FuzzPackager.SharedLibInstallStrings, " "))
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700554
555 // Preallocate the slice of fuzz targets to minimise memory allocations.
Cory Barkera1da26f2022-06-07 20:12:06 +0000556 s.PreallocateSlice(ctx, s.allFuzzTargetsName)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700557}
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400558
559// GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for
560// packaging.
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000561func 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 -0400562 var files []fuzz.FileToZip
563
Cory Barkera1da26f2022-06-07 20:12:06 +0000564 fuzzDir := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000565
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000566 for _, ruleBuilderInstall := range sharedLibraries {
567 library := ruleBuilderInstall.From
568 install := ruleBuilderInstall.To
Colin Cross80462dc2023-05-08 15:09:31 -0700569 files = append(files, fuzz.FileToZip{
570 SourceFilePath: library,
571 DestinationPathPrefix: destinationPathPrefix,
572 DestinationPath: install,
573 })
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400574
575 // For each architecture-specific shared library dependency, we need to
576 // install it to the output directory. Setup the install destination here,
577 // which will be used by $(copy-many-files) in the Make backend.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500578 installDestination := SharedLibraryInstallLocation(
Steven Morelandd86fec52023-12-28 01:09:40 +0000579 install, module.Host(), module.InVendor(), fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400580 if (*sharedLibraryInstalled)[installDestination] {
581 continue
582 }
583 (*sharedLibraryInstalled)[installDestination] = true
584
585 // Escape all the variables, as the install destination here will be called
586 // via. $(eval) in Make.
587 installDestination = strings.ReplaceAll(
588 installDestination, "$", "$$")
589 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
590 library.String()+":"+installDestination)
591
592 // Ensure that on device, the library is also reinstalled to the /symbols/
593 // dir. Symbolized DSO's are always installed to the device when fuzzing, but
594 // we want symbolization tools (like `stack`) to be able to find the symbols
595 // in $ANDROID_PRODUCT_OUT/symbols automagically.
596 if !module.Host() {
Steven Morelandd86fec52023-12-28 01:09:40 +0000597 symbolsInstallDestination := SharedLibrarySymbolsInstallLocation(install, module.InVendor(), fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400598 symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
599 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
600 library.String()+":"+symbolsInstallDestination)
601 }
602 }
603 return files
604}
Colin Cross31d89b42022-10-04 16:35:39 -0700605
606// CollectAllSharedDependencies search over the provided module's dependencies using
607// VisitDirectDeps and WalkDeps to enumerate all shared library dependencies.
608// VisitDirectDeps is used first to avoid incorrectly using the core libraries (sanitizer
609// runtimes, libc, libdl, etc.) from a dependency. This may cause issues when dependencies
610// have explicit sanitizer tags, as we may get a dependency on an unsanitized libc, etc.
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000611func CollectAllSharedDependencies(ctx android.ModuleContext) (android.RuleBuilderInstalls, []android.Module) {
Colin Cross31d89b42022-10-04 16:35:39 -0700612 seen := make(map[string]bool)
613 recursed := make(map[string]bool)
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000614 deps := []android.Module{}
Colin Cross31d89b42022-10-04 16:35:39 -0700615
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000616 var sharedLibraries android.RuleBuilderInstalls
Colin Cross31d89b42022-10-04 16:35:39 -0700617
618 // Enumerate the first level of dependencies, as we discard all non-library
619 // modules in the BFS loop below.
620 ctx.VisitDirectDeps(func(dep android.Module) {
621 if !IsValidSharedDependency(dep) {
622 return
623 }
Colin Cross313aa542023-12-13 13:47:44 -0800624 sharedLibraryInfo, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, dep, SharedLibraryInfoProvider)
625 if !hasSharedLibraryInfo {
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000626 return
627 }
Colin Cross31d89b42022-10-04 16:35:39 -0700628 if seen[ctx.OtherModuleName(dep)] {
629 return
630 }
631 seen[ctx.OtherModuleName(dep)] = true
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000632 deps = append(deps, dep)
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000633
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000634 installDestination := sharedLibraryInfo.SharedLibrary.Base()
635 ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, dep, "unstripped"), installDestination}
636 sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
Colin Cross31d89b42022-10-04 16:35:39 -0700637 })
638
639 ctx.WalkDeps(func(child, parent android.Module) bool {
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400640
641 // 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 -0400642 // shared dependencies (even for rust_ffi_rlib or rust_ffi_static)
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400643 if rustmod, ok := child.(LinkableInterface); ok && rustmod.RustLibraryInterface() && !rustmod.Shared() {
644 if recursed[ctx.OtherModuleName(child)] {
645 return false
646 }
647 recursed[ctx.OtherModuleName(child)] = true
648 return true
649 }
650
Colin Cross31d89b42022-10-04 16:35:39 -0700651 if !IsValidSharedDependency(child) {
652 return false
653 }
Colin Cross313aa542023-12-13 13:47:44 -0800654 sharedLibraryInfo, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, child, SharedLibraryInfoProvider)
655 if !hasSharedLibraryInfo {
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000656 return false
657 }
Colin Cross31d89b42022-10-04 16:35:39 -0700658 if !seen[ctx.OtherModuleName(child)] {
659 seen[ctx.OtherModuleName(child)] = true
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000660 deps = append(deps, child)
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000661
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000662 installDestination := sharedLibraryInfo.SharedLibrary.Base()
663 ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, child, "unstripped"), installDestination}
664 sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
Colin Cross31d89b42022-10-04 16:35:39 -0700665 }
666
667 if recursed[ctx.OtherModuleName(child)] {
668 return false
669 }
670 recursed[ctx.OtherModuleName(child)] = true
671 return true
672 })
673
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000674 return sharedLibraries, deps
Colin Cross31d89b42022-10-04 16:35:39 -0700675}