blob: 8a974c0f0d0a838bdcef167b472ab31e3571e756 [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 Moreland4fc1fc92024-10-23 21:07:24 +0000185 subdir := "lib"
186 if ctx.inVendor() {
Steven Morelandd86fec52023-12-28 01:09:40 +0000187 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)
Cole Faust65cb40a2024-10-21 15:41:42 -0700350 fuzzPackagedModule.Corpus = append(fuzzPackagedModule.Corpus, android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Device_common_corpus)...)
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500351
352 fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzzPackagedModule.FuzzProperties.Data)
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500353
354 if fuzzPackagedModule.FuzzProperties.Dictionary != nil {
355 fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzzPackagedModule.FuzzProperties.Dictionary)
356 if fuzzPackagedModule.Dictionary.Ext() != ".dict" {
357 ctx.PropertyErrorf("dictionary",
358 "Fuzzer dictionary %q does not have '.dict' extension",
359 fuzzPackagedModule.Dictionary.String())
360 }
361 }
362
363 if fuzzPackagedModule.FuzzProperties.Fuzz_config != nil {
364 configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json")
365 android.WriteFileRule(ctx, configPath, fuzzPackagedModule.FuzzProperties.Fuzz_config.String())
366 fuzzPackagedModule.Config = configPath
367 }
368 return fuzzPackagedModule
369}
370
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000371func NewFuzzer(hod android.HostOrDeviceSupported) *Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800372 module, binary := newBinary(hod)
Cory Barkera1da26f2022-06-07 20:12:06 +0000373 baseInstallerPath := "fuzz"
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700374
Cory Barkera1da26f2022-06-07 20:12:06 +0000375 binary.baseInstaller = NewBaseInstaller(baseInstallerPath, baseInstallerPath, InstallInData)
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700376
Cory Barkera1da26f2022-06-07 20:12:06 +0000377 fuzzBin := &fuzzBinary{
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700378 binaryDecorator: binary,
379 baseCompiler: NewBaseCompiler(),
380 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000381 module.compiler = fuzzBin
382 module.linker = fuzzBin
383 module.installer = fuzzBin
Colin Crosseec9b282019-07-18 16:20:52 -0700384
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000385 module.fuzzer.Properties.FuzzFramework = fuzz.LibFuzzer
386
Colin Crosseec9b282019-07-18 16:20:52 -0700387 // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin.
388 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400389
390 extraProps := struct {
391 Sanitize struct {
392 Fuzzer *bool
393 }
Colin Crosseec9b282019-07-18 16:20:52 -0700394 Target struct {
395 Darwin struct {
396 Enabled *bool
397 }
Alex Light71123ec2019-07-24 13:34:19 -0700398 Linux_bionic struct {
399 Enabled *bool
400 }
Colin Crosseec9b282019-07-18 16:20:52 -0700401 }
402 }{}
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400403 extraProps.Sanitize.Fuzzer = BoolPtr(true)
404 extraProps.Target.Darwin.Enabled = BoolPtr(false)
405 extraProps.Target.Linux_bionic.Enabled = BoolPtr(false)
406 ctx.AppendProperties(&extraProps)
Cory Barkera1da26f2022-06-07 20:12:06 +0000407
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000408 targetFramework := fuzz.GetFramework(ctx, fuzz.Cc)
409 if !fuzz.IsValidFrameworkForModule(targetFramework, fuzz.Cc, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzzing_frameworks) {
410 ctx.Module().Disable()
411 return
412 }
413
414 if targetFramework == fuzz.AFL {
Cole Faust96a692b2024-08-08 14:47:51 -0700415 fuzzBin.baseCompiler.Properties.Srcs.AppendSimpleValue([]string{":aflpp_driver", ":afl-compiler-rt"})
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000416 module.fuzzer.Properties.FuzzFramework = fuzz.AFL
417 }
418 })
Cory Barker74aea6c2022-08-08 15:55:12 +0000419
Mitch Phillipsda9a4632019-07-15 09:34:09 -0700420 return module
421}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700422
423// Responsible for generating GNU Make rules that package fuzz targets into
424// their architecture & target/host specific zip file.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500425type ccRustFuzzPackager struct {
hamzehc0a671f2021-07-22 12:05:08 -0700426 fuzz.FuzzPackager
Cole Faust06ea5312023-10-18 17:38:40 -0700427 fuzzPackagingArchModules string
428 fuzzTargetSharedDepsInstallPairs string
429 allFuzzTargetsName string
430 onlyIncludePresubmits bool
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700431}
432
433func fuzzPackagingFactory() android.Singleton {
Cory Barkera1da26f2022-06-07 20:12:06 +0000434
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500435 fuzzPackager := &ccRustFuzzPackager{
Cory Barkera1da26f2022-06-07 20:12:06 +0000436 fuzzPackagingArchModules: "SOONG_FUZZ_PACKAGING_ARCH_MODULES",
437 fuzzTargetSharedDepsInstallPairs: "FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
438 allFuzzTargetsName: "ALL_FUZZ_TARGETS",
Cole Faust06ea5312023-10-18 17:38:40 -0700439 onlyIncludePresubmits: false,
David Fufd121fc2023-07-07 18:11:51 +0000440 }
441 return fuzzPackager
442}
443
444func fuzzPackagingFactoryPresubmit() android.Singleton {
445
446 fuzzPackager := &ccRustFuzzPackager{
447 fuzzPackagingArchModules: "SOONG_PRESUBMIT_FUZZ_PACKAGING_ARCH_MODULES",
448 fuzzTargetSharedDepsInstallPairs: "PRESUBMIT_FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS",
449 allFuzzTargetsName: "ALL_PRESUBMIT_FUZZ_TARGETS",
Cole Faust06ea5312023-10-18 17:38:40 -0700450 onlyIncludePresubmits: true,
Cory Barkera1da26f2022-06-07 20:12:06 +0000451 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000452 return fuzzPackager
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700453}
454
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500455func (s *ccRustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700456 // Map between each architecture + host/device combination, and the files that
457 // need to be packaged (in the tuple of {source file, destination folder in
458 // archive}).
hamzehc0a671f2021-07-22 12:05:08 -0700459 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700460
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700461 // List of individual fuzz targets, so that 'make fuzz' also installs the targets
462 // to the correct output directories as well.
hamzeh41ad8812021-07-07 14:00:07 -0700463 s.FuzzTargets = make(map[string]bool)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700464
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400465 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
466 // multiple fuzzers that depend on the same shared library.
467 sharedLibraryInstalled := make(map[string]bool)
468
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700469 ctx.VisitAllModules(func(module android.Module) {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500470 ccModule, ok := module.(LinkableInterface)
471 if !ok || ccModule.PreventInstall() {
hamzeh41ad8812021-07-07 14:00:07 -0700472 return
473 }
hamzeh41ad8812021-07-07 14:00:07 -0700474 // Discard non-fuzz targets.
Cole Fausta963b942024-04-11 17:43:00 -0700475 if ok := fuzz.IsValid(ctx, ccModule.FuzzModuleStruct()); !ok {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700476 return
477 }
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700478
Cory Barkera1da26f2022-06-07 20:12:06 +0000479 sharedLibsInstallDirPrefix := "lib"
Steven Morelandd86fec52023-12-28 01:09:40 +0000480 if ccModule.InVendor() {
481 sharedLibsInstallDirPrefix = "lib/vendor"
482 }
483
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500484 if !ccModule.IsFuzzModule() {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700485 return
486 }
487
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700488 hostOrTargetString := "target"
Colin Cross64a4a5f2023-05-16 17:54:27 -0700489 if ccModule.Target().HostCross {
490 hostOrTargetString = "host_cross"
491 } else if ccModule.Host() {
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700492 hostOrTargetString = "host"
493 }
David Fufd121fc2023-07-07 18:11:51 +0000494 if s.onlyIncludePresubmits == true {
495 hostOrTargetString = "presubmit-" + hostOrTargetString
496 }
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700497
Cory Barkera1da26f2022-06-07 20:12:06 +0000498 fpm := fuzz.FuzzPackagedModule{}
499 if ok {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500500 fpm = ccModule.FuzzPackagedModule()
Cory Barkera1da26f2022-06-07 20:12:06 +0000501 }
502
503 intermediatePath := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000504
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500505 archString := ccModule.Target().Arch.ArchType.String()
Cory Barkera1da26f2022-06-07 20:12:06 +0000506 archDir := android.PathForIntermediates(ctx, intermediatePath, hostOrTargetString, archString)
hamzehc0a671f2021-07-22 12:05:08 -0700507 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700508
hamzehc0a671f2021-07-22 12:05:08 -0700509 var files []fuzz.FileToZip
Colin Crossf1a035e2020-11-16 17:32:30 -0800510 builder := android.NewRuleBuilder(pctx, ctx)
Mitch Phillips2edbe8e2019-11-13 08:36:07 -0800511
hamzeh41ad8812021-07-07 14:00:07 -0700512 // Package the corpus, data, dict and config into a zipfile.
Cory Barkera1da26f2022-06-07 20:12:06 +0000513 files = s.PackageArtifacts(ctx, module, fpm, archDir, builder)
Tri Voad172d82019-11-27 13:45:45 -0800514
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400515 // Package shared libraries
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500516 files = append(files, GetSharedLibsToZip(ccModule.FuzzSharedLibraries(), ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...)
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700517
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700518 // The executable.
Colin Cross80462dc2023-05-08 15:09:31 -0700519 files = append(files, fuzz.FileToZip{SourceFilePath: android.OutputFileForModule(ctx, ccModule, "unstripped")})
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700520
David Fufd121fc2023-07-07 18:11:51 +0000521 if s.onlyIncludePresubmits == true {
522 if fpm.FuzzProperties.Fuzz_config == nil {
523 return
524 }
Cole Faust06ea5312023-10-18 17:38:40 -0700525 if !BoolDefault(fpm.FuzzProperties.Fuzz_config.Use_for_presubmit, false) {
David Fufd121fc2023-07-07 18:11:51 +0000526 return
527 }
528 }
Cory Barkera1da26f2022-06-07 20:12:06 +0000529 archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
hamzeh41ad8812021-07-07 14:00:07 -0700530 if !ok {
531 return
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700532 }
533 })
534
Cory Barker9cfcf6d2022-07-22 17:22:02 +0000535 s.CreateFuzzPackage(ctx, archDirs, fuzz.Cc, pctx)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700536}
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700537
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500538func (s *ccRustFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
hamzeh41ad8812021-07-07 14:00:07 -0700539 packages := s.Packages.Strings()
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700540 sort.Strings(packages)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400541 sort.Strings(s.FuzzPackager.SharedLibInstallStrings)
Mitch Phillipsa0a5e192019-09-27 14:00:06 -0700542 // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's
543 // ready to handle phony targets created in Soong. In the meantime, this
544 // exports the phony 'fuzz' target and dependencies on packages to
545 // core/main.mk so that we can use dist-for-goals.
Cory Barkera1da26f2022-06-07 20:12:06 +0000546
547 ctx.Strict(s.fuzzPackagingArchModules, strings.Join(packages, " "))
548
549 ctx.Strict(s.fuzzTargetSharedDepsInstallPairs,
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400550 strings.Join(s.FuzzPackager.SharedLibInstallStrings, " "))
Mitch Phillipse1ee1a12019-10-17 19:20:41 -0700551
552 // Preallocate the slice of fuzz targets to minimise memory allocations.
Cory Barkera1da26f2022-06-07 20:12:06 +0000553 s.PreallocateSlice(ctx, s.allFuzzTargetsName)
Mitch Phillipsd3254b42019-09-24 13:03:28 -0700554}
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400555
556// GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for
557// packaging.
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000558func 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 -0400559 var files []fuzz.FileToZip
560
Cory Barkera1da26f2022-06-07 20:12:06 +0000561 fuzzDir := "fuzz"
Cory Barkera1da26f2022-06-07 20:12:06 +0000562
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000563 for _, ruleBuilderInstall := range sharedLibraries {
564 library := ruleBuilderInstall.From
565 install := ruleBuilderInstall.To
Colin Cross80462dc2023-05-08 15:09:31 -0700566 files = append(files, fuzz.FileToZip{
567 SourceFilePath: library,
568 DestinationPathPrefix: destinationPathPrefix,
569 DestinationPath: install,
570 })
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400571
572 // For each architecture-specific shared library dependency, we need to
573 // install it to the output directory. Setup the install destination here,
574 // which will be used by $(copy-many-files) in the Make backend.
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500575 installDestination := SharedLibraryInstallLocation(
Steven Morelandd86fec52023-12-28 01:09:40 +0000576 install, module.Host(), module.InVendor(), fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400577 if (*sharedLibraryInstalled)[installDestination] {
578 continue
579 }
580 (*sharedLibraryInstalled)[installDestination] = true
581
582 // Escape all the variables, as the install destination here will be called
583 // via. $(eval) in Make.
584 installDestination = strings.ReplaceAll(
585 installDestination, "$", "$$")
586 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
587 library.String()+":"+installDestination)
588
589 // Ensure that on device, the library is also reinstalled to the /symbols/
590 // dir. Symbolized DSO's are always installed to the device when fuzzing, but
591 // we want symbolization tools (like `stack`) to be able to find the symbols
592 // in $ANDROID_PRODUCT_OUT/symbols automagically.
593 if !module.Host() {
Steven Morelandd86fec52023-12-28 01:09:40 +0000594 symbolsInstallDestination := SharedLibrarySymbolsInstallLocation(install, module.InVendor(), fuzzDir, archString)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400595 symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
596 s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
597 library.String()+":"+symbolsInstallDestination)
598 }
599 }
600 return files
601}
Colin Cross31d89b42022-10-04 16:35:39 -0700602
603// CollectAllSharedDependencies search over the provided module's dependencies using
604// VisitDirectDeps and WalkDeps to enumerate all shared library dependencies.
605// VisitDirectDeps is used first to avoid incorrectly using the core libraries (sanitizer
606// runtimes, libc, libdl, etc.) from a dependency. This may cause issues when dependencies
607// have explicit sanitizer tags, as we may get a dependency on an unsanitized libc, etc.
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000608func CollectAllSharedDependencies(ctx android.ModuleContext) (android.RuleBuilderInstalls, []android.Module) {
Colin Cross31d89b42022-10-04 16:35:39 -0700609 seen := make(map[string]bool)
610 recursed := make(map[string]bool)
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000611 deps := []android.Module{}
Colin Cross31d89b42022-10-04 16:35:39 -0700612
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000613 var sharedLibraries android.RuleBuilderInstalls
Colin Cross31d89b42022-10-04 16:35:39 -0700614
615 // Enumerate the first level of dependencies, as we discard all non-library
616 // modules in the BFS loop below.
617 ctx.VisitDirectDeps(func(dep android.Module) {
618 if !IsValidSharedDependency(dep) {
619 return
620 }
Colin Cross313aa542023-12-13 13:47:44 -0800621 sharedLibraryInfo, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, dep, SharedLibraryInfoProvider)
622 if !hasSharedLibraryInfo {
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000623 return
624 }
Colin Cross31d89b42022-10-04 16:35:39 -0700625 if seen[ctx.OtherModuleName(dep)] {
626 return
627 }
628 seen[ctx.OtherModuleName(dep)] = true
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000629 deps = append(deps, dep)
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000630
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000631 installDestination := sharedLibraryInfo.SharedLibrary.Base()
632 ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, dep, "unstripped"), installDestination}
633 sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
Colin Cross31d89b42022-10-04 16:35:39 -0700634 })
635
636 ctx.WalkDeps(func(child, parent android.Module) bool {
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400637
638 // 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 -0400639 // shared dependencies (even for rust_ffi_rlib or rust_ffi_static)
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400640 if rustmod, ok := child.(LinkableInterface); ok && rustmod.RustLibraryInterface() && !rustmod.Shared() {
641 if recursed[ctx.OtherModuleName(child)] {
642 return false
643 }
644 recursed[ctx.OtherModuleName(child)] = true
645 return true
646 }
647
Colin Cross31d89b42022-10-04 16:35:39 -0700648 if !IsValidSharedDependency(child) {
649 return false
650 }
Colin Cross313aa542023-12-13 13:47:44 -0800651 sharedLibraryInfo, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, child, SharedLibraryInfoProvider)
652 if !hasSharedLibraryInfo {
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000653 return false
654 }
Colin Cross31d89b42022-10-04 16:35:39 -0700655 if !seen[ctx.OtherModuleName(child)] {
656 seen[ctx.OtherModuleName(child)] = true
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000657 deps = append(deps, child)
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000658
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000659 installDestination := sharedLibraryInfo.SharedLibrary.Base()
660 ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, child, "unstripped"), installDestination}
661 sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
Colin Cross31d89b42022-10-04 16:35:39 -0700662 }
663
664 if recursed[ctx.OtherModuleName(child)] {
665 return false
666 }
667 recursed[ctx.OtherModuleName(child)] = true
668 return true
669 })
670
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +0000671 return sharedLibraries, deps
Colin Cross31d89b42022-10-04 16:35:39 -0700672}