Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Mitch Phillips | 4de896e | 2019-08-28 16:04:36 -0700 | [diff] [blame] | 18 | "path/filepath" |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 19 | "sort" |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 20 | "strings" |
Mitch Phillips | 4de896e | 2019-08-28 16:04:36 -0700 | [diff] [blame] | 21 | |
Victor Chang | 00c144f | 2021-02-09 12:30:33 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
| 23 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 24 | "android/soong/android" |
| 25 | "android/soong/cc/config" |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 26 | "android/soong/fuzz" |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | func init() { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 30 | android.RegisterModuleType("cc_fuzz", LibFuzzFactory) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 31 | android.RegisterSingletonType("cc_fuzz_packaging", fuzzPackagingFactory) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type FuzzProperties struct { |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame^] | 35 | FuzzFramework fuzz.Framework `blueprint:"mutated"` |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | type fuzzer struct { |
| 39 | Properties FuzzProperties |
| 40 | } |
| 41 | |
| 42 | func (fuzzer *fuzzer) flags(ctx ModuleContext, flags Flags) Flags { |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame^] | 43 | if fuzzer.Properties.FuzzFramework == fuzz.AFL { |
| 44 | flags.Local.CFlags = append(flags.Local.CFlags, []string{ |
| 45 | "-fsanitize-coverage=trace-pc-guard", |
| 46 | "-Wno-unused-result", |
| 47 | "-Wno-unused-parameter", |
| 48 | "-Wno-unused-function", |
| 49 | }...) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return flags |
| 53 | } |
| 54 | |
| 55 | func (fuzzer *fuzzer) props() []interface{} { |
| 56 | return []interface{}{&fuzzer.Properties} |
| 57 | } |
| 58 | |
| 59 | func fuzzMutatorDeps(mctx android.TopDownMutatorContext) { |
| 60 | currentModule, ok := mctx.Module().(*Module) |
| 61 | if !ok { |
| 62 | return |
| 63 | } |
| 64 | |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame^] | 65 | if currentModule.fuzzer == nil { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 66 | return |
| 67 | } |
| 68 | |
| 69 | mctx.WalkDeps(func(child android.Module, parent android.Module) bool { |
| 70 | c, ok := child.(*Module) |
| 71 | if !ok { |
| 72 | return false |
| 73 | } |
| 74 | |
| 75 | if c.sanitize == nil { |
| 76 | return false |
| 77 | } |
| 78 | |
| 79 | isFuzzerPointer := c.sanitize.getSanitizerBoolPtr(Fuzzer) |
| 80 | if isFuzzerPointer == nil || !*isFuzzerPointer { |
| 81 | return false |
| 82 | } |
| 83 | |
| 84 | if c.fuzzer == nil { |
| 85 | return false |
| 86 | } |
| 87 | |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame^] | 88 | c.fuzzer.Properties.FuzzFramework = currentModule.fuzzer.Properties.FuzzFramework |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 89 | return true |
| 90 | }) |
| 91 | } |
| 92 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 93 | // cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at |
| 94 | // $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on |
| 95 | // your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 96 | func LibFuzzFactory() android.Module { |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame^] | 97 | module := NewFuzzer(android.HostAndDeviceSupported) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 98 | return module.Init() |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | type fuzzBinary struct { |
| 102 | *binaryDecorator |
| 103 | *baseCompiler |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 104 | fuzzPackagedModule fuzz.FuzzPackagedModule |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 105 | installedSharedDeps []string |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 108 | func (fuzz *fuzzBinary) fuzzBinary() bool { |
| 109 | return true |
| 110 | } |
| 111 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 112 | func (fuzz *fuzzBinary) linkerProps() []interface{} { |
| 113 | props := fuzz.binaryDecorator.linkerProps() |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 114 | props = append(props, &fuzz.fuzzPackagedModule.FuzzProperties) |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame^] | 115 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 116 | return props |
| 117 | } |
| 118 | |
| 119 | func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) { |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 120 | fuzz.binaryDecorator.linkerInit(ctx) |
| 121 | } |
| 122 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 123 | func (fuzzBin *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame^] | 124 | if ctx.Config().Getenv("FUZZ_FRAMEWORK") == "AFL" { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 125 | deps.HeaderLibs = append(deps.HeaderLibs, "libafl_headers") |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 126 | } else { |
| 127 | deps.StaticLibs = append(deps.StaticLibs, config.LibFuzzerRuntimeLibrary(ctx.toolchain())) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 128 | } |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame^] | 129 | |
| 130 | deps = fuzzBin.binaryDecorator.linkerDeps(ctx, deps) |
| 131 | return deps |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 135 | flags = fuzz.binaryDecorator.linkerFlags(ctx, flags) |
Mitch Phillips | 1f7f54f | 2019-11-14 14:50:47 -0800 | [diff] [blame] | 136 | // RunPaths on devices isn't instantiated by the base linker. `../lib` for |
| 137 | // installed fuzz targets (both host and device), and `./lib` for fuzz |
| 138 | // target packages. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 139 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`) |
Mitch Phillips | 1f7f54f | 2019-11-14 14:50:47 -0800 | [diff] [blame] | 140 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/lib`) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 141 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 142 | return flags |
| 143 | } |
| 144 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 145 | func UnstrippedOutputFile(module android.Module) android.Path { |
| 146 | if mod, ok := module.(LinkableInterface); ok { |
| 147 | return mod.UnstrippedOutputFile() |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 148 | } |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 149 | panic("UnstrippedOutputFile called on non-LinkableInterface module: " + module.Name()) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 152 | // IsValidSharedDependency takes a module and determines if it is a unique shared library |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 153 | // that should be installed in the fuzz target output directories. This function |
| 154 | // returns true, unless: |
Victor Chang | 00c144f | 2021-02-09 12:30:33 +0000 | [diff] [blame] | 155 | // - The module is not an installable shared library, or |
Kris Alder | 756ec8d | 2021-08-27 22:08:29 +0000 | [diff] [blame] | 156 | // - The module is a header or stub, or |
Martin Stjernholm | 02460ab | 2020-10-06 02:36:43 +0100 | [diff] [blame] | 157 | // - The module is a prebuilt and its source is available, or |
| 158 | // - The module is a versioned member of an SDK snapshot. |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 159 | func IsValidSharedDependency(dependency android.Module) bool { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 160 | // TODO(b/144090547): We should be parsing these modules using |
| 161 | // ModuleDependencyTag instead of the current brute-force checking. |
| 162 | |
Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 163 | linkable, ok := dependency.(LinkableInterface) |
| 164 | if !ok || !linkable.CcLibraryInterface() { |
| 165 | // Discard non-linkables. |
| 166 | return false |
| 167 | } |
| 168 | |
| 169 | if !linkable.Shared() { |
| 170 | // Discard static libs. |
| 171 | return false |
| 172 | } |
| 173 | |
Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 174 | if lib := moduleLibraryInterface(dependency); lib != nil && lib.buildStubs() && linkable.CcLibrary() { |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 175 | // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not |
| 176 | // be excluded on the basis of they're not CCLibrary()'s. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 177 | return false |
| 178 | } |
| 179 | |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 180 | // We discarded module stubs libraries above, but the LLNDK prebuilts stubs |
| 181 | // libraries must be handled differently - by looking for the stubDecorator. |
| 182 | // Discard LLNDK prebuilts stubs as well. |
| 183 | if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary { |
| 184 | if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary { |
| 185 | return false |
| 186 | } |
Victor Chang | 00c144f | 2021-02-09 12:30:33 +0000 | [diff] [blame] | 187 | // Discard installable:false libraries because they are expected to be absent |
| 188 | // in runtime. |
Colin Cross | 1bc9412 | 2021-10-28 13:25:54 -0700 | [diff] [blame] | 189 | if !proptools.BoolDefault(ccLibrary.Installable(), true) { |
Victor Chang | 00c144f | 2021-02-09 12:30:33 +0000 | [diff] [blame] | 190 | return false |
| 191 | } |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Martin Stjernholm | 02460ab | 2020-10-06 02:36:43 +0100 | [diff] [blame] | 194 | // If the same library is present both as source and a prebuilt we must pick |
| 195 | // only one to avoid a conflict. Always prefer the source since the prebuilt |
| 196 | // probably won't be built with sanitizers enabled. |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame] | 197 | if prebuilt := android.GetEmbeddedPrebuilt(dependency); prebuilt != nil && prebuilt.SourceExists() { |
Martin Stjernholm | 02460ab | 2020-10-06 02:36:43 +0100 | [diff] [blame] | 198 | return false |
| 199 | } |
| 200 | |
| 201 | // Discard versioned members of SDK snapshots, because they will conflict with |
| 202 | // unversioned ones. |
| 203 | if sdkMember, ok := dependency.(android.SdkAware); ok && !sdkMember.ContainingSdk().Unversioned() { |
| 204 | return false |
| 205 | } |
| 206 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 207 | return true |
| 208 | } |
| 209 | |
| 210 | func sharedLibraryInstallLocation( |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 211 | libraryPath android.Path, isHost bool, fuzzDir string, archString string) string { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 212 | installLocation := "$(PRODUCT_OUT)/data" |
| 213 | if isHost { |
| 214 | installLocation = "$(HOST_OUT)" |
| 215 | } |
| 216 | installLocation = filepath.Join( |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 217 | installLocation, fuzzDir, archString, "lib", libraryPath.Base()) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 218 | return installLocation |
| 219 | } |
| 220 | |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 221 | // Get the device-only shared library symbols install directory. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 222 | func sharedLibrarySymbolsInstallLocation(libraryPath android.Path, fuzzDir string, archString string) string { |
| 223 | return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, "/lib/", libraryPath.Base()) |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 226 | func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) { |
| 227 | installBase := "fuzz" |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 228 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 229 | fuzzBin.binaryDecorator.baseInstaller.dir = filepath.Join( |
| 230 | installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
| 231 | fuzzBin.binaryDecorator.baseInstaller.dir64 = filepath.Join( |
| 232 | installBase, ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
| 233 | fuzzBin.binaryDecorator.baseInstaller.install(ctx, file) |
| 234 | |
| 235 | fuzzBin.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzzBin.fuzzPackagedModule.FuzzProperties.Corpus) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 236 | builder := android.NewRuleBuilder(pctx, ctx) |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame] | 237 | intermediateDir := android.PathForModuleOut(ctx, "corpus") |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 238 | for _, entry := range fuzzBin.fuzzPackagedModule.Corpus { |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame] | 239 | builder.Command().Text("cp"). |
| 240 | Input(entry). |
| 241 | Output(intermediateDir.Join(ctx, entry.Base())) |
| 242 | } |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 243 | builder.Build("copy_corpus", "copy corpus") |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 244 | fuzzBin.fuzzPackagedModule.CorpusIntermediateDir = intermediateDir |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame] | 245 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 246 | fuzzBin.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzzBin.fuzzPackagedModule.FuzzProperties.Data) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 247 | builder = android.NewRuleBuilder(pctx, ctx) |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 248 | intermediateDir = android.PathForModuleOut(ctx, "data") |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 249 | for _, entry := range fuzzBin.fuzzPackagedModule.Data { |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 250 | builder.Command().Text("cp"). |
| 251 | Input(entry). |
| 252 | Output(intermediateDir.Join(ctx, entry.Rel())) |
| 253 | } |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 254 | builder.Build("copy_data", "copy data") |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 255 | fuzzBin.fuzzPackagedModule.DataIntermediateDir = intermediateDir |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 256 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 257 | if fuzzBin.fuzzPackagedModule.FuzzProperties.Dictionary != nil { |
| 258 | fuzzBin.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzzBin.fuzzPackagedModule.FuzzProperties.Dictionary) |
| 259 | if fuzzBin.fuzzPackagedModule.Dictionary.Ext() != ".dict" { |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 260 | ctx.PropertyErrorf("dictionary", |
| 261 | "Fuzzer dictionary %q does not have '.dict' extension", |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 262 | fuzzBin.fuzzPackagedModule.Dictionary.String()) |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 265 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 266 | if fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzz_config != nil { |
Kris Alder | db97af4 | 2019-10-30 10:17:04 -0700 | [diff] [blame] | 267 | configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json") |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 268 | android.WriteFileRule(ctx, configPath, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzz_config.String()) |
| 269 | fuzzBin.fuzzPackagedModule.Config = configPath |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 270 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 271 | |
| 272 | // Grab the list of required shared libraries. |
Mitch Phillips | c0b442f | 2020-04-27 16:44:58 -0700 | [diff] [blame] | 273 | seen := make(map[string]bool) |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 274 | var sharedLibraries android.Paths |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 275 | ctx.WalkDeps(func(child, parent android.Module) bool { |
Mitch Phillips | c0b442f | 2020-04-27 16:44:58 -0700 | [diff] [blame] | 276 | if seen[child.Name()] { |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 277 | return false |
| 278 | } |
Mitch Phillips | c0b442f | 2020-04-27 16:44:58 -0700 | [diff] [blame] | 279 | seen[child.Name()] = true |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 280 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 281 | if IsValidSharedDependency(child) { |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 282 | sharedLibraries = append(sharedLibraries, child.(*Module).UnstrippedOutputFile()) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 283 | return true |
| 284 | } |
| 285 | return false |
| 286 | }) |
| 287 | |
| 288 | for _, lib := range sharedLibraries { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 289 | fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps, |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 290 | sharedLibraryInstallLocation( |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 291 | lib, ctx.Host(), installBase, ctx.Arch().ArchType.String())) |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 292 | |
| 293 | // Also add the dependency on the shared library symbols dir. |
| 294 | if !ctx.Host() { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 295 | fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps, |
| 296 | sharedLibrarySymbolsInstallLocation(lib, installBase, ctx.Arch().ArchType.String())) |
Mitch Phillips | 0bf9713 | 2020-03-06 09:38:12 -0800 | [diff] [blame] | 297 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 298 | } |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame^] | 301 | func NewFuzzer(hod android.HostOrDeviceSupported) *Module { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 302 | module, binary := newBinary(hod, false) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 303 | baseInstallerPath := "fuzz" |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 304 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 305 | binary.baseInstaller = NewBaseInstaller(baseInstallerPath, baseInstallerPath, InstallInData) |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 306 | module.sanitize.SetSanitizer(Fuzzer, true) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 307 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 308 | fuzzBin := &fuzzBinary{ |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 309 | binaryDecorator: binary, |
| 310 | baseCompiler: NewBaseCompiler(), |
| 311 | } |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 312 | module.compiler = fuzzBin |
| 313 | module.linker = fuzzBin |
| 314 | module.installer = fuzzBin |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 315 | |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame^] | 316 | module.fuzzer.Properties.FuzzFramework = fuzz.LibFuzzer |
| 317 | |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 318 | // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin. |
| 319 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 320 | disableDarwinAndLinuxBionic := struct { |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 321 | Target struct { |
| 322 | Darwin struct { |
| 323 | Enabled *bool |
| 324 | } |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 325 | Linux_bionic struct { |
| 326 | Enabled *bool |
| 327 | } |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 328 | } |
| 329 | }{} |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 330 | disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false) |
| 331 | disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false) |
| 332 | ctx.AppendProperties(&disableDarwinAndLinuxBionic) |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 333 | |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame^] | 334 | targetFramework := fuzz.GetFramework(ctx, fuzz.Cc) |
| 335 | if !fuzz.IsValidFrameworkForModule(targetFramework, fuzz.Cc, fuzzBin.fuzzPackagedModule.FuzzProperties.Fuzzing_frameworks) { |
| 336 | ctx.Module().Disable() |
| 337 | return |
| 338 | } |
| 339 | |
| 340 | if targetFramework == fuzz.AFL { |
| 341 | fuzzBin.baseCompiler.Properties.Srcs = append(fuzzBin.baseCompiler.Properties.Srcs, ":aflpp_driver", ":afl-compiler-rt") |
| 342 | module.fuzzer.Properties.FuzzFramework = fuzz.AFL |
| 343 | } |
| 344 | }) |
Cory Barker | 74aea6c | 2022-08-08 15:55:12 +0000 | [diff] [blame] | 345 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 346 | return module |
| 347 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 348 | |
| 349 | // Responsible for generating GNU Make rules that package fuzz targets into |
| 350 | // their architecture & target/host specific zip file. |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 351 | type ccFuzzPackager struct { |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 352 | fuzz.FuzzPackager |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 353 | fuzzPackagingArchModules string |
| 354 | fuzzTargetSharedDepsInstallPairs string |
| 355 | allFuzzTargetsName string |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | func fuzzPackagingFactory() android.Singleton { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 359 | |
| 360 | fuzzPackager := &ccFuzzPackager{ |
| 361 | fuzzPackagingArchModules: "SOONG_FUZZ_PACKAGING_ARCH_MODULES", |
| 362 | fuzzTargetSharedDepsInstallPairs: "FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS", |
| 363 | allFuzzTargetsName: "ALL_FUZZ_TARGETS", |
| 364 | } |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 365 | return fuzzPackager |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 366 | } |
| 367 | |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 368 | func (s *ccFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 369 | // Map between each architecture + host/device combination, and the files that |
| 370 | // need to be packaged (in the tuple of {source file, destination folder in |
| 371 | // archive}). |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 372 | archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 373 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 374 | // List of individual fuzz targets, so that 'make fuzz' also installs the targets |
| 375 | // to the correct output directories as well. |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 376 | s.FuzzTargets = make(map[string]bool) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 377 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 378 | // Map tracking whether each shared library has an install rule to avoid duplicate install rules from |
| 379 | // multiple fuzzers that depend on the same shared library. |
| 380 | sharedLibraryInstalled := make(map[string]bool) |
| 381 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 382 | ctx.VisitAllModules(func(module android.Module) { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 383 | ccModule, ok := module.(*Module) |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 384 | if !ok || ccModule.Properties.PreventInstall { |
| 385 | return |
| 386 | } |
| 387 | |
| 388 | // Discard non-fuzz targets. |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 389 | if ok := fuzz.IsValid(ccModule.FuzzModule); !ok { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 390 | return |
| 391 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 392 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 393 | sharedLibsInstallDirPrefix := "lib" |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 394 | fuzzModule, ok := ccModule.compiler.(*fuzzBinary) |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame^] | 395 | if !ok { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 396 | return |
| 397 | } |
| 398 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 399 | hostOrTargetString := "target" |
| 400 | if ccModule.Host() { |
| 401 | hostOrTargetString = "host" |
| 402 | } |
| 403 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 404 | fpm := fuzz.FuzzPackagedModule{} |
| 405 | if ok { |
| 406 | fpm = fuzzModule.fuzzPackagedModule |
| 407 | } |
| 408 | |
| 409 | intermediatePath := "fuzz" |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 410 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 411 | archString := ccModule.Arch().ArchType.String() |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 412 | archDir := android.PathForIntermediates(ctx, intermediatePath, hostOrTargetString, archString) |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 413 | archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()} |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 414 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 415 | // Grab the list of required shared libraries. |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 416 | sharedLibraries := fuzz.CollectAllSharedDependencies(ctx, module, UnstrippedOutputFile, IsValidSharedDependency) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 417 | |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 418 | var files []fuzz.FileToZip |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 419 | builder := android.NewRuleBuilder(pctx, ctx) |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 420 | |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 421 | // Package the corpus, data, dict and config into a zipfile. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 422 | files = s.PackageArtifacts(ctx, module, fpm, archDir, builder) |
Tri Vo | ad172d8 | 2019-11-27 13:45:45 -0800 | [diff] [blame] | 423 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 424 | // Package shared libraries |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 425 | files = append(files, GetSharedLibsToZip(sharedLibraries, ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 426 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 427 | // The executable. |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 428 | files = append(files, fuzz.FileToZip{ccModule.UnstrippedOutputFile(), ""}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 429 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 430 | archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs) |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 431 | if !ok { |
| 432 | return |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 433 | } |
| 434 | }) |
| 435 | |
Cory Barker | 9cfcf6d | 2022-07-22 17:22:02 +0000 | [diff] [blame^] | 436 | s.CreateFuzzPackage(ctx, archDirs, fuzz.Cc, pctx) |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 437 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 438 | |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 439 | func (s *ccFuzzPackager) MakeVars(ctx android.MakeVarsContext) { |
| 440 | packages := s.Packages.Strings() |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 441 | sort.Strings(packages) |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 442 | sort.Strings(s.FuzzPackager.SharedLibInstallStrings) |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 443 | // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's |
| 444 | // ready to handle phony targets created in Soong. In the meantime, this |
| 445 | // exports the phony 'fuzz' target and dependencies on packages to |
| 446 | // core/main.mk so that we can use dist-for-goals. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 447 | |
| 448 | ctx.Strict(s.fuzzPackagingArchModules, strings.Join(packages, " ")) |
| 449 | |
| 450 | ctx.Strict(s.fuzzTargetSharedDepsInstallPairs, |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 451 | strings.Join(s.FuzzPackager.SharedLibInstallStrings, " ")) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 452 | |
| 453 | // Preallocate the slice of fuzz targets to minimise memory allocations. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 454 | s.PreallocateSlice(ctx, s.allFuzzTargetsName) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 455 | } |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 456 | |
| 457 | // GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for |
| 458 | // packaging. |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 459 | func GetSharedLibsToZip(sharedLibraries android.Paths, module LinkableInterface, s *fuzz.FuzzPackager, archString string, destinationPathPrefix string, sharedLibraryInstalled *map[string]bool) []fuzz.FileToZip { |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 460 | var files []fuzz.FileToZip |
| 461 | |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 462 | fuzzDir := "fuzz" |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 463 | |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 464 | for _, library := range sharedLibraries { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 465 | files = append(files, fuzz.FileToZip{library, destinationPathPrefix}) |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 466 | |
| 467 | // For each architecture-specific shared library dependency, we need to |
| 468 | // install it to the output directory. Setup the install destination here, |
| 469 | // which will be used by $(copy-many-files) in the Make backend. |
| 470 | installDestination := sharedLibraryInstallLocation( |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 471 | library, module.Host(), fuzzDir, archString) |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 472 | if (*sharedLibraryInstalled)[installDestination] { |
| 473 | continue |
| 474 | } |
| 475 | (*sharedLibraryInstalled)[installDestination] = true |
| 476 | |
| 477 | // Escape all the variables, as the install destination here will be called |
| 478 | // via. $(eval) in Make. |
| 479 | installDestination = strings.ReplaceAll( |
| 480 | installDestination, "$", "$$") |
| 481 | s.SharedLibInstallStrings = append(s.SharedLibInstallStrings, |
| 482 | library.String()+":"+installDestination) |
| 483 | |
| 484 | // Ensure that on device, the library is also reinstalled to the /symbols/ |
| 485 | // dir. Symbolized DSO's are always installed to the device when fuzzing, but |
| 486 | // we want symbolization tools (like `stack`) to be able to find the symbols |
| 487 | // in $ANDROID_PRODUCT_OUT/symbols automagically. |
| 488 | if !module.Host() { |
Cory Barker | a1da26f | 2022-06-07 20:12:06 +0000 | [diff] [blame] | 489 | symbolsInstallDestination := sharedLibrarySymbolsInstallLocation(library, fuzzDir, archString) |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame] | 490 | symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$") |
| 491 | s.SharedLibInstallStrings = append(s.SharedLibInstallStrings, |
| 492 | library.String()+":"+symbolsInstallDestination) |
| 493 | } |
| 494 | } |
| 495 | return files |
| 496 | } |