blob: 0aa9d4ba18c6081689a2f45bb4d036a11bd0fd8a [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 Morelandd86fec52023-12-28 01:09:40 +0000185 subdir := "lib"
186 if ctx.inVendor() {
187 subdir = "lib/vendor"
188 }
189
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700190 flags = fuzz.binaryDecorator.linkerFlags(ctx, flags)
Mitch Phillips1f7f54f2019-11-14 14:50:47 -0800191 // RunPaths on devices isn't instantiated by the base linker. `../lib` for
192 // installed fuzz targets (both host and device), and `./lib` for fuzz
193 // target packages.
Steven Morelandd86fec52023-12-28 01:09:40 +0000194 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/`+subdir)
Cory Barkera1da26f2022-06-07 20:12:06 +0000195
Kris Alderc2634812022-10-25 10:58:59 -0700196 // When running on device, fuzz targets with vendor: true set will be in
197 // fuzzer_name/vendor/fuzzer_name (note the extra 'vendor' and thus need to
198 // link with libraries in ../../lib/. Non-vendor binaries only need to look
199 // one level up, in ../lib/.
200 if ctx.inVendor() {
Steven Morelandd86fec52023-12-28 01:09:40 +0000201 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../../`+subdir)
Kris Alderc2634812022-10-25 10:58:59 -0700202 } else {
Steven Morelandd86fec52023-12-28 01:09:40 +0000203 flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../`+subdir)
Kris Alderc2634812022-10-25 10:58:59 -0700204 }
205
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700206 return flags
207}
208
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800209func (fuzz *fuzzBinary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
210 fuzz.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
211 moduleInfoJSON.Class = []string{"EXECUTABLES"}
212}
213
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400214// IsValidSharedDependency takes a module and determines if it is a unique shared library
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700215// that should be installed in the fuzz target output directories. This function
216// returns true, unless:
Colin Crossd079e0b2022-08-16 10:27:33 -0700217// - The module is not an installable shared library, or
218// - The module is a header or stub, or
219// - The module is a prebuilt and its source is available, or
220// - The module is a versioned member of an SDK snapshot.
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400221func IsValidSharedDependency(dependency android.Module) bool {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700222 // TODO(b/144090547): We should be parsing these modules using
223 // ModuleDependencyTag instead of the current brute-force checking.
224
Colin Cross31076b32020-10-23 17:22:06 -0700225 linkable, ok := dependency.(LinkableInterface)
226 if !ok || !linkable.CcLibraryInterface() {
227 // Discard non-linkables.
228 return false
229 }
230
231 if !linkable.Shared() {
232 // Discard static libs.
233 return false
234 }
235
Colin Cross31076b32020-10-23 17:22:06 -0700236 if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() {
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800237 // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not
238 // be excluded on the basis of they're not CCLibrary()'s.
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700239 return false
240 }
241
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800242 // We discarded module stubs libraries above, but the LLNDK prebuilts stubs
243 // libraries must be handled differently - by looking for the stubDecorator.
244 // Discard LLNDK prebuilts stubs as well.
245 if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary {
246 if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary {
247 return false
248 }
Victor Chang00c144f2021-02-09 12:30:33 +0000249 // Discard installable:false libraries because they are expected to be absent
250 // in runtime.
Colin Cross1bc94122021-10-28 13:25:54 -0700251 if !proptools.BoolDefault(ccLibrary.Installable(), true) {
Victor Chang00c144f2021-02-09 12:30:33 +0000252 return false
253 }
Mitch Phillipsf50bddb2019-11-12 14:03:31 -0800254 }
255
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100256 // If the same library is present both as source and a prebuilt we must pick
257 // only one to avoid a conflict. Always prefer the source since the prebuilt
258 // probably won't be built with sanitizers enabled.
Paul Duffinf7c99f52021-04-28 10:41:21 +0100259 if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() {
Martin Stjernholm02460ab2020-10-06 02:36:43 +0100260 return false
261 }
262
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700263 return true
264}
265
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500266func SharedLibraryInstallLocation(
Steven Morelandd86fec52023-12-28 01:09:40 +0000267 libraryBase string, isHost bool, isVendor bool, fuzzDir string, archString string) string {
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700268 installLocation := "$(PRODUCT_OUT)/data"
269 if isHost {
270 installLocation = "$(HOST_OUT)"
271 }
Steven Morelandd86fec52023-12-28 01:09:40 +0000272 subdir := "lib"
273 if isVendor {
274 subdir = "lib/vendor"
275 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700276 installLocation = filepath.Join(
Steven Morelandd86fec52023-12-28 01:09:40 +0000277 installLocation, fuzzDir, archString, subdir, libraryBase)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700278 return installLocation
279}
280
Mitch Phillips0bf97132020-03-06 09:38:12 -0800281// Get the device-only shared library symbols install directory.
Steven Morelandd86fec52023-12-28 01:09:40 +0000282func SharedLibrarySymbolsInstallLocation(libraryBase string, isVendor bool, fuzzDir string, archString string) string {
283 subdir := "lib"
284 if isVendor {
285 subdir = "lib/vendor"
286 }
287 return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, subdir, libraryBase)
Mitch Phillips0bf97132020-03-06 09:38:12 -0800288}
289
Cory Barkera1da26f2022-06-07 20:12:06 +0000290func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500291 fuzzBin.fuzzPackagedModule = PackageFuzzModule(ctx, fuzzBin.fuzzPackagedModule, pctx)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700292
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800293 installBase := "fuzz"
294
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700295 // Grab the list of required shared libraries.
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000296 fuzzBin.sharedLibraries, _ = CollectAllSharedDependencies(ctx)
Colin Crossdc809f92019-11-20 15:58:32 -0800297
Steven Morelandd86fec52023-12-28 01:09:40 +0000298 // TODO: does not mirror Android linkernamespaces
299 // the logic here has special cases for vendor, but it would need more work to
300 // work in arbitrary partitions, so just surface errors early for a few cases
301 //
302 // Even without these, there are certain situations across linkernamespaces
303 // that this won't support. For instance, you might have:
304 //
305 // my_fuzzer (vendor) -> libbinder_ndk (core) -> libbinder (vendor)
306 //
307 // This dependency chain wouldn't be possible to express in the current
308 // logic because all the deps currently match the variant of the source
309 // module.
310
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000311 for _, ruleBuilderInstall := range fuzzBin.sharedLibraries {
312 install := ruleBuilderInstall.To
Cory Barkera1da26f2022-06-07 20:12:06 +0000313 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500314 SharedLibraryInstallLocation(
Steven Morelandd86fec52023-12-28 01:09:40 +0000315 install, ctx.Host(), ctx.inVendor(), installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800316
317 // Also add the dependency on the shared library symbols dir.
318 if !ctx.Host() {
Cory Barkera1da26f2022-06-07 20:12:06 +0000319 fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
Steven Morelandd86fec52023-12-28 01:09:40 +0000320 SharedLibrarySymbolsInstallLocation(install, ctx.inVendor(), installBase, ctx.Arch().ArchType.String()))
Mitch Phillips0bf97132020-03-06 09:38:12 -0800321 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700322 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800323
324 for _, d := range fuzzBin.fuzzPackagedModule.Corpus {
325 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, RelativeInstallPath: "corpus", WithoutRel: true})
326 }
327
328 for _, d := range fuzzBin.fuzzPackagedModule.Data {
329 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, RelativeInstallPath: "data"})
330 }
331
332 if d := fuzzBin.fuzzPackagedModule.Dictionary; d != nil {
333 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, WithoutRel: true})
334 }
335
336 if d := fuzzBin.fuzzPackagedModule.Config; d != nil {
337 fuzzBin.data = append(fuzzBin.data, android.DataPath{SrcPath: d, WithoutRel: true})
338 }
339
340 fuzzBin.binaryDecorator.baseInstaller.dir = filepath.Join(
341 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
342 fuzzBin.binaryDecorator.baseInstaller.dir64 = filepath.Join(
343 installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
344 fuzzBin.binaryDecorator.baseInstaller.installTestData(ctx, fuzzBin.data)
345 fuzzBin.binaryDecorator.baseInstaller.install(ctx, file)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700346}
347
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500348func PackageFuzzModule(ctx android.ModuleContext, fuzzPackagedModule fuzz.FuzzPackagedModule, pctx android.PackageContext) fuzz.FuzzPackagedModule {
349 fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Corpus)
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500350
351 fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Data)
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500352
353 if fuzzPackagedModule.FuzzProperties.Dictionary != nil {
354 fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzzPackagedModule.FuzzProperties.Dictionary)
355 if fuzzPackagedModule.Dictionary.Ext() != ".dict" {
356 ctx.PropertyErrorf("dictionary",
357 "Fuzzer dictionary %q does not have '.dict' extension",
358 fuzzPackagedModule.Dictionary.String())
359 }
360 }
361
362 if fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
363 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
364 android.WriteFileRule(ctx, configPath, fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
365 fuzzPackagedModule.Config = configPath
366 }
367 return fuzzPackagedModule
368}
369
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000370func NewFuzzer(hod android.HostOrDeviceSupported) *Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800371 module, binary := newBinary(hod)
Cory Barkera1da26f2022-06-07 20:12:06 +0000372 baseInstallerPath := "fuzz"
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700373
Cory Barkera1da26f2022-06-07 20:12:06 +0000374 binary.baseInstaller = NewBaseInstaller(baseInstallerPath, baseInstallerPath, InstallInData)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700375
Cory Barkera1da26f2022-06-07 20:12:06 +0000376 fuzzBin := &fuzzBinary{
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700377 binaryDecorator: binary,
378 baseCompiler: NewBaseCompiler(),
379 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000380 module.compiler = fuzzBin
381 module.linker = fuzzBin
382 module.installer = fuzzBin
Colin Crosseec9b282019-07-18 16:20:52 -0700383
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000384 module.fuzzer.Properties.FuzzFramework = fuzz.LibFuzzer
385
Colin Crosseec9b282019-07-18 16:20:52 -0700386 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
387 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400388
389 extraProps := struct {
390 Sanitize struct {
391 Fuzzer *bool
392 }
Colin Crosseec9b282019-07-18 16:20:52 -0700393 Target struct {
394 Darwin struct {
395 Enabled *bool
396 }
Alex Light71123ec2019-07-24 13:34:19 -0700397 Linux_bionic struct {
398 Enabled *bool
399 }
Colin Crosseec9b282019-07-18 16:20:52 -0700400 }
401 }{}
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400402 extraProps.Sanitize.Fuzzer = BoolPtr(true)
403 extraProps.Target.Darwin.Enabled = BoolPtr(false)
404 extraProps.Target.Linux_bionic.Enabled = BoolPtr(false)
405 ctx.AppendProperties(&extraProps)
Cory Barkera1da26f2022-06-07 20:12:06 +0000406
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000407 targetFramework := fuzz.GetFramework(ctx, fuzz.Cc)
408 if !fuzz.IsValidFrameworkForModule(targetFramework, fuzz.Cc, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzzing_frameworks) {
409 ctx.Module().Disable()
410 return
411 }
412
413 if targetFramework == fuzz.AFL {
Cole Faust96a692b2024-08-08 14:47:51 -0700414 fuzzBin.baseCompiler.Properties.Srcs.AppendSimpleValue([]string{":aflpp_driver", ":afl-compiler-rt"})
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000415 module.fuzzer.Properties.FuzzFramework = fuzz.AFL
416 }
417 })
Cory Barker74aea6c2022-08-08 15:55:12 +0000418
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700419 return module
420}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700421
422// Responsible for generating GNU Make rules that package fuzz targets into
423// their architecture & target/host specific zip file.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500424type ccRustFuzzPackager struct {
hamzehc0a671f2021-07-22 12:05:08 -0700425 fuzz.FuzzPackager
Cole Faust06ea5312023-10-18 17:38:40 -0700426 fuzzPackagingArchModules string
427 fuzzTargetSharedDepsInstallPairs string
428 allFuzzTargetsName string
429 onlyIncludePresubmits bool
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700430}
431
432func fuzzPackagingFactory() android.Singleton {
Cory Barkera1da26f2022-06-07 20:12:06 +0000433
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500434 fuzzPackager := &ccRustFuzzPackager{
Cory Barkera1da26f2022-06-07 20:12:06 +0000435 fuzzPackagingArchModules: "SOONG_FUZZ_PACKAGING_ARCH_MODULES",
436 fuzzTargetSharedDepsInstallPairs: "FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
437 allFuzzTargetsName: "ALL_FUZZ_TARGETS",
Cole Faust06ea5312023-10-18 17:38:40 -0700438 onlyIncludePresubmits: false,
David Fufd121fc2023-07-07 18:11:51 +0000439 }
440 return fuzzPackager
441}
442
443func fuzzPackagingFactoryPresubmit() android.Singleton {
444
445 fuzzPackager := &ccRustFuzzPackager{
446 fuzzPackagingArchModules: "SOONG_PRESUBMIT_FUZZ_PACKAGING_ARCH_MODULES",
447 fuzzTargetSharedDepsInstallPairs: "PRESUBMIT_FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
448 allFuzzTargetsName: "ALL_PRESUBMIT_FUZZ_TARGETS",
Cole Faust06ea5312023-10-18 17:38:40 -0700449 onlyIncludePresubmits: true,
Cory Barkera1da26f2022-06-07 20:12:06 +0000450 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000451 return fuzzPackager
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700452}
453
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500454func (s *ccRustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700455 // Map between each architecture + host/device combination, and the files that
456 // need to be packaged (in the tuple of {source file, destination folder in
457 // archive}).
hamzehc0a671f2021-07-22 12:05:08 -0700458 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700459
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700460 // List of individual fuzz targets, so that 'make fuzz' also installs the targets
461 // to the correct output directories as well.
hamzeh41ad8812021-07-07 14:00:07 -0700462 s.FuzzTargets = make(map[string]bool)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700463
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400464 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
465 // multiple fuzzers that depend on the same shared library.
466 sharedLibraryInstalled := make(map[string]bool)
467
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700468 ctx.VisitAllModules(func(module android.Module) {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500469 ccModule, ok := module.(LinkableInterface)
470 if !ok || ccModule.PreventInstall() {
hamzeh41ad8812021-07-07 14:00:07 -0700471 return
472 }
hamzeh41ad8812021-07-07 14:00:07 -0700473 // Discard non-fuzz targets.
Cole Fausta963b942024-04-11 17:43:00 -0700474 if ok := fuzz.IsValid(ctx, ccModule.FuzzModuleStruct()); !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700475 return
476 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700477
Cory Barkera1da26f2022-06-07 20:12:06 +0000478 sharedLibsInstallDirPrefix := "lib"
Steven Morelandd86fec52023-12-28 01:09:40 +0000479 if ccModule.InVendor() {
480 sharedLibsInstallDirPrefix = "lib/vendor"
481 }
482
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500483 if !ccModule.IsFuzzModule() {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700484 return
485 }
486
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700487 hostOrTargetString := "target"
Colin Cross64a4a5f2023-05-16 17:54:27 -0700488 if ccModule.Target().HostCross {
489 hostOrTargetString = "host_cross"
490 } else if ccModule.Host() {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700491 hostOrTargetString = "host"
492 }
David Fufd121fc2023-07-07 18:11:51 +0000493 if s.onlyIncludePresubmits == true {
494 hostOrTargetString = "presubmit-" + hostOrTargetString
495 }
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700496
Cory Barkera1da26f2022-06-07 20:12:06 +0000497 fpm := fuzz.FuzzPackagedModule{}
498 if ok {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500499 fpm = ccModule.FuzzPackagedModule()
Cory Barkera1da26f2022-06-07 20:12:06 +0000500 }
501
502 intermediatePath := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000503
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500504 archString := ccModule.Target().Arch.ArchType.String()
Cory Barkera1da26f2022-06-07 20:12:06 +0000505 archDir := android.PathForIntermediates(ctx, intermediatePath, hostOrTargetString, archString)
hamzehc0a671f2021-07-22 12:05:08 -0700506 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700507
hamzehc0a671f2021-07-22 12:05:08 -0700508 var files []fuzz.FileToZip
Colin Crossf1a035e2020-11-16 17:32:30 -0800509 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800510
hamzeh41ad8812021-07-07 14:00:07 -0700511 // Package the corpus, data, dict and config into a zipfile.
Cory Barkera1da26f2022-06-07 20:12:06 +0000512 files = s.PackageArtifacts(ctx, module, fpm, archDir, builder)
Tri Voad172d82019-11-27 13:45:45 -0800513
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400514 // Package shared libraries
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500515 files = append(files, GetSharedLibsToZip(ccModule.FuzzSharedLibraries(), ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700516
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700517 // The executable.
Colin Cross80462dc2023-05-08 15:09:31 -0700518 files = append(files, fuzz.FileToZip{SourceFilePath: android.OutputFileForModule(ctx, ccModule, "unstripped")})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700519
David Fufd121fc2023-07-07 18:11:51 +0000520 if s.onlyIncludePresubmits == true {
521 if fpm.FuzzProperties.Fuzz_config == nil {
522 return
523 }
Cole Faust06ea5312023-10-18 17:38:40 -0700524 if !BoolDefault(fpm.FuzzProperties.Fuzz_config.Use_for_presubmit, false) {
David Fufd121fc2023-07-07 18:11:51 +0000525 return
526 }
527 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000528 archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
hamzeh41ad8812021-07-07 14:00:07 -0700529 if !ok {
530 return
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700531 }
532 })
533
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000534 s.CreateFuzzPackage(ctx, archDirs, fuzz.Cc, pctx)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700535}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700536
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500537func (s *ccRustFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
hamzeh41ad8812021-07-07 14:00:07 -0700538 packages := s.Packages.Strings()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700539 sort.Strings(packages)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400540 sort.Strings(s.FuzzPackager.SharedLibInstallStrings)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700541 // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
542 // ready to handle phony targets created in Soong. In the meantime, this
543 // exports the phony 'fuzz' target and dependencies on packages to
544 // core/main.mk so that we can use dist-for-goals.
Cory Barkera1da26f2022-06-07 20:12:06 +0000545
546 ctx.Strict(s.fuzzPackagingArchModules, strings.Join(packages, " "))
547
548 ctx.Strict(s.fuzzTargetSharedDepsInstallPairs,
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400549 strings.Join(s.FuzzPackager.SharedLibInstallStrings, " "))
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700550
551 // Preallocate the slice of fuzz targets to minimise memory allocations.
Cory Barkera1da26f2022-06-07 20:12:06 +0000552 s.PreallocateSlice(ctx, s.allFuzzTargetsName)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700553}
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400554
555// GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for
556// packaging.
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000557func 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 -0400558 var files []fuzz.FileToZip
559
Cory Barkera1da26f2022-06-07 20:12:06 +0000560 fuzzDir := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000561
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000562 for _, ruleBuilderInstall := range sharedLibraries {
563 library := ruleBuilderInstall.From
564 install := ruleBuilderInstall.To
Colin Cross80462dc2023-05-08 15:09:31 -0700565 files = append(files, fuzz.FileToZip{
566 SourceFilePath: library,
567 DestinationPathPrefix: destinationPathPrefix,
568 DestinationPath: install,
569 })
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400570
571 // For each architecture-specific shared library dependency, we need to
572 // install it to the output directory. Setup the install destination here,
573 // which will be used by $(copy-many-files) in the Make backend.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500574 installDestination := SharedLibraryInstallLocation(
Steven Morelandd86fec52023-12-28 01:09:40 +0000575 install, module.Host(), module.InVendor(), fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400576 if (*sharedLibraryInstalled)[installDestination] {
577 continue
578 }
579 (*sharedLibraryInstalled)[installDestination] = true
580
581 // Escape all the variables, as the install destination here will be called
582 // via. $(eval) in Make.
583 installDestination = strings.ReplaceAll(
584 installDestination, "$", "$$")
585 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
586 library.String()+":"+installDestination)
587
588 // Ensure that on device, the library is also reinstalled to the /symbols/
589 // dir. Symbolized DSO's are always installed to the device when fuzzing, but
590 // we want symbolization tools (like `stack`) to be able to find the symbols
591 // in $ANDROID_PRODUCT_OUT/symbols automagically.
592 if !module.Host() {
Steven Morelandd86fec52023-12-28 01:09:40 +0000593 symbolsInstallDestination := SharedLibrarySymbolsInstallLocation(install, module.InVendor(), fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400594 symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
595 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
596 library.String()+":"+symbolsInstallDestination)
597 }
598 }
599 return files
600}
Colin Cross31d89b42022-10-04 16:35:39 -0700601
602// CollectAllSharedDependencies search over the provided module's dependencies using
603// VisitDirectDeps and WalkDeps to enumerate all shared library dependencies.
604// VisitDirectDeps is used first to avoid incorrectly using the core libraries (sanitizer
605// runtimes, libc, libdl, etc.) from a dependency. This may cause issues when dependencies
606// have explicit sanitizer tags, as we may get a dependency on an unsanitized libc, etc.
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000607func CollectAllSharedDependencies(ctx android.ModuleContext) (android.RuleBuilderInstalls, []android.Module) {
Colin Cross31d89b42022-10-04 16:35:39 -0700608 seen := make(map[string]bool)
609 recursed := make(map[string]bool)
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000610 deps := []android.Module{}
Colin Cross31d89b42022-10-04 16:35:39 -0700611
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000612 var sharedLibraries android.RuleBuilderInstalls
Colin Cross31d89b42022-10-04 16:35:39 -0700613
614 // Enumerate the first level of dependencies, as we discard all non-library
615 // modules in the BFS loop below.
616 ctx.VisitDirectDeps(func(dep android.Module) {
617 if !IsValidSharedDependency(dep) {
618 return
619 }
Colin Cross313aa542023-12-13 13:47:44 -0800620 sharedLibraryInfo, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, dep, SharedLibraryInfoProvider)
621 if !hasSharedLibraryInfo {
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000622 return
623 }
Colin Cross31d89b42022-10-04 16:35:39 -0700624 if seen[ctx.OtherModuleName(dep)] {
625 return
626 }
627 seen[ctx.OtherModuleName(dep)] = true
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000628 deps = append(deps, dep)
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000629
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000630 installDestination := sharedLibraryInfo.SharedLibrary.Base()
631 ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, dep, "unstripped"), installDestination}
632 sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
Colin Cross31d89b42022-10-04 16:35:39 -0700633 })
634
635 ctx.WalkDeps(func(child, parent android.Module) bool {
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400636
637 // 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 -0400638 // shared dependencies (even for rust_ffi_rlib or rust_ffi_static)
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400639 if rustmod, ok := child.(LinkableInterface); ok && rustmod.RustLibraryInterface() && !rustmod.Shared() {
640 if recursed[ctx.OtherModuleName(child)] {
641 return false
642 }
643 recursed[ctx.OtherModuleName(child)] = true
644 return true
645 }
646
Colin Cross31d89b42022-10-04 16:35:39 -0700647 if !IsValidSharedDependency(child) {
648 return false
649 }
Colin Cross313aa542023-12-13 13:47:44 -0800650 sharedLibraryInfo, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, child, SharedLibraryInfoProvider)
651 if !hasSharedLibraryInfo {
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000652 return false
653 }
Colin Cross31d89b42022-10-04 16:35:39 -0700654 if !seen[ctx.OtherModuleName(child)] {
655 seen[ctx.OtherModuleName(child)] = true
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000656 deps = append(deps, child)
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000657
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000658 installDestination := sharedLibraryInfo.SharedLibrary.Base()
659 ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, child, "unstripped"), installDestination}
660 sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
Colin Cross31d89b42022-10-04 16:35:39 -0700661 }
662
663 if recursed[ctx.OtherModuleName(child)] {
664 return false
665 }
666 recursed[ctx.OtherModuleName(child)] = true
667 return true
668 })
669
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000670 return sharedLibraries, deps
Colin Cross31d89b42022-10-04 16:35:39 -0700671}