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 ( |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 18 | "encoding/json" |
Mitch Phillips | 4de896e | 2019-08-28 16:04:36 -0700 | [diff] [blame] | 19 | "path/filepath" |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 20 | "sort" |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 21 | "strings" |
Mitch Phillips | 4de896e | 2019-08-28 16:04:36 -0700 | [diff] [blame] | 22 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 23 | "android/soong/android" |
| 24 | "android/soong/cc/config" |
| 25 | ) |
| 26 | |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 27 | type FuzzConfig struct { |
| 28 | // Email address of people to CC on bugs or contact about this fuzz target. |
| 29 | Cc []string `json:"cc,omitempty"` |
| 30 | // Boolean specifying whether to disable the fuzz target from running |
| 31 | // automatically in continuous fuzzing infrastructure. |
| 32 | Disable *bool `json:"disable,omitempty"` |
| 33 | // Component in Google's bug tracking system that bugs should be filed to. |
| 34 | Componentid *int64 `json:"componentid,omitempty"` |
| 35 | // Hotlists in Google's bug tracking system that bugs should be marked with. |
| 36 | Hotlists []string `json:"hotlists,omitempty"` |
| 37 | } |
| 38 | |
| 39 | func (f *FuzzConfig) String() string { |
| 40 | b, err := json.Marshal(f) |
| 41 | if err != nil { |
| 42 | panic(err) |
| 43 | } |
| 44 | |
| 45 | return string(b) |
| 46 | } |
| 47 | |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 48 | type FuzzProperties struct { |
| 49 | // Optional list of seed files to be installed to the fuzz target's output |
| 50 | // directory. |
| 51 | Corpus []string `android:"path"` |
| 52 | // Optional dictionary to be installed to the fuzz target's output directory. |
| 53 | Dictionary *string `android:"path"` |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 54 | // Config for running the target on fuzzing infrastructure. |
| 55 | Fuzz_config *FuzzConfig |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 58 | func init() { |
| 59 | android.RegisterModuleType("cc_fuzz", FuzzFactory) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 60 | android.RegisterSingletonType("cc_fuzz_packaging", fuzzPackagingFactory) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at |
| 64 | // $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on |
| 65 | // your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree. |
| 66 | func FuzzFactory() android.Module { |
| 67 | module := NewFuzz(android.HostAndDeviceSupported) |
| 68 | return module.Init() |
| 69 | } |
| 70 | |
| 71 | func NewFuzzInstaller() *baseInstaller { |
| 72 | return NewBaseInstaller("fuzz", "fuzz", InstallInData) |
| 73 | } |
| 74 | |
| 75 | type fuzzBinary struct { |
| 76 | *binaryDecorator |
| 77 | *baseCompiler |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 78 | |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame] | 79 | Properties FuzzProperties |
| 80 | dictionary android.Path |
| 81 | corpus android.Paths |
| 82 | corpusIntermediateDir android.Path |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 83 | config android.Path |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 84 | installedSharedDeps []string |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | func (fuzz *fuzzBinary) linkerProps() []interface{} { |
| 88 | props := fuzz.binaryDecorator.linkerProps() |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 89 | props = append(props, &fuzz.Properties) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 90 | return props |
| 91 | } |
| 92 | |
| 93 | func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) { |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 94 | fuzz.binaryDecorator.linkerInit(ctx) |
| 95 | } |
| 96 | |
| 97 | func (fuzz *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
| 98 | deps.StaticLibs = append(deps.StaticLibs, |
| 99 | config.LibFuzzerRuntimeLibrary(ctx.toolchain())) |
| 100 | deps = fuzz.binaryDecorator.linkerDeps(ctx, deps) |
| 101 | return deps |
| 102 | } |
| 103 | |
| 104 | func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 105 | flags = fuzz.binaryDecorator.linkerFlags(ctx, flags) |
Mitch Phillips | 1f7f54f | 2019-11-14 14:50:47 -0800 | [diff] [blame] | 106 | // RunPaths on devices isn't instantiated by the base linker. `../lib` for |
| 107 | // installed fuzz targets (both host and device), and `./lib` for fuzz |
| 108 | // target packages. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 109 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`) |
Mitch Phillips | 1f7f54f | 2019-11-14 14:50:47 -0800 | [diff] [blame] | 110 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/lib`) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 111 | return flags |
| 112 | } |
| 113 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 114 | // This function performs a breadth-first search over the provided module's |
| 115 | // dependencies using `visitDirectDeps` to enumerate all shared library |
| 116 | // dependencies. We require breadth-first expansion, as otherwise we may |
| 117 | // incorrectly use the core libraries (sanitizer runtimes, libc, libdl, etc.) |
| 118 | // from a dependency. This may cause issues when dependencies have explicit |
| 119 | // sanitizer tags, as we may get a dependency on an unsanitized libc, etc. |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 120 | func collectAllSharedDependencies(ctx android.SingletonContext, module android.Module) android.Paths { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 121 | var fringe []android.Module |
| 122 | |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 123 | seen := make(map[android.Module]bool) |
| 124 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 125 | // Enumerate the first level of dependencies, as we discard all non-library |
| 126 | // modules in the BFS loop below. |
| 127 | ctx.VisitDirectDeps(module, func(dep android.Module) { |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 128 | if isValidSharedDependency(dep) { |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 129 | fringe = append(fringe, dep) |
| 130 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 131 | }) |
| 132 | |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 133 | var sharedLibraries android.Paths |
| 134 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 135 | for i := 0; i < len(fringe); i++ { |
| 136 | module := fringe[i] |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 137 | if seen[module] { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 138 | continue |
| 139 | } |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 140 | seen[module] = true |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 141 | |
| 142 | ccModule := module.(*Module) |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 143 | sharedLibraries = append(sharedLibraries, ccModule.UnstrippedOutputFile()) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 144 | ctx.VisitDirectDeps(module, func(dep android.Module) { |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 145 | if isValidSharedDependency(dep) && !seen[dep] { |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 146 | fringe = append(fringe, dep) |
| 147 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 148 | }) |
| 149 | } |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 150 | |
| 151 | return sharedLibraries |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // This function takes a module and determines if it is a unique shared library |
| 155 | // that should be installed in the fuzz target output directories. This function |
| 156 | // returns true, unless: |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 157 | // - The module is not a shared library, or |
| 158 | // - The module is a header, stub, or vendor-linked library. |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [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 | |
| 163 | if linkable, ok := dependency.(LinkableInterface); !ok || // Discard non-linkables. |
| 164 | !linkable.CcLibraryInterface() || !linkable.Shared() || // Discard static libs. |
| 165 | linkable.UseVndk() || // Discard vendor linked libraries. |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 166 | // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not |
| 167 | // be excluded on the basis of they're not CCLibrary()'s. |
| 168 | (linkable.CcLibrary() && linkable.BuildStubs()) { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 169 | return false |
| 170 | } |
| 171 | |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame] | 172 | // We discarded module stubs libraries above, but the LLNDK prebuilts stubs |
| 173 | // libraries must be handled differently - by looking for the stubDecorator. |
| 174 | // Discard LLNDK prebuilts stubs as well. |
| 175 | if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary { |
| 176 | if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary { |
| 177 | return false |
| 178 | } |
| 179 | } |
| 180 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 181 | return true |
| 182 | } |
| 183 | |
| 184 | func sharedLibraryInstallLocation( |
| 185 | libraryPath android.Path, isHost bool, archString string) string { |
| 186 | installLocation := "$(PRODUCT_OUT)/data" |
| 187 | if isHost { |
| 188 | installLocation = "$(HOST_OUT)" |
| 189 | } |
| 190 | installLocation = filepath.Join( |
| 191 | installLocation, "fuzz", archString, "lib", libraryPath.Base()) |
| 192 | return installLocation |
| 193 | } |
| 194 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 195 | func (fuzz *fuzzBinary) install(ctx ModuleContext, file android.Path) { |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 196 | fuzz.binaryDecorator.baseInstaller.dir = filepath.Join( |
| 197 | "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
| 198 | fuzz.binaryDecorator.baseInstaller.dir64 = filepath.Join( |
| 199 | "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 200 | fuzz.binaryDecorator.baseInstaller.install(ctx, file) |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 201 | |
| 202 | fuzz.corpus = android.PathsForModuleSrc(ctx, fuzz.Properties.Corpus) |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame] | 203 | builder := android.NewRuleBuilder() |
| 204 | intermediateDir := android.PathForModuleOut(ctx, "corpus") |
| 205 | for _, entry := range fuzz.corpus { |
| 206 | builder.Command().Text("cp"). |
| 207 | Input(entry). |
| 208 | Output(intermediateDir.Join(ctx, entry.Base())) |
| 209 | } |
| 210 | builder.Build(pctx, ctx, "copy_corpus", "copy corpus") |
| 211 | fuzz.corpusIntermediateDir = intermediateDir |
| 212 | |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 213 | if fuzz.Properties.Dictionary != nil { |
| 214 | fuzz.dictionary = android.PathForModuleSrc(ctx, *fuzz.Properties.Dictionary) |
| 215 | if fuzz.dictionary.Ext() != ".dict" { |
| 216 | ctx.PropertyErrorf("dictionary", |
| 217 | "Fuzzer dictionary %q does not have '.dict' extension", |
| 218 | fuzz.dictionary.String()) |
| 219 | } |
| 220 | } |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 221 | |
| 222 | if fuzz.Properties.Fuzz_config != nil { |
Kris Alder | db97af4 | 2019-10-30 10:17:04 -0700 | [diff] [blame] | 223 | configPath := android.PathForModuleOut(ctx, "config").Join(ctx, "config.json") |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 224 | ctx.Build(pctx, android.BuildParams{ |
| 225 | Rule: android.WriteFile, |
| 226 | Description: "fuzzer infrastructure configuration", |
| 227 | Output: configPath, |
| 228 | Args: map[string]string{ |
| 229 | "content": fuzz.Properties.Fuzz_config.String(), |
| 230 | }, |
| 231 | }) |
| 232 | fuzz.config = configPath |
| 233 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 234 | |
| 235 | // Grab the list of required shared libraries. |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 236 | seen := make(map[android.Module]bool) |
| 237 | var sharedLibraries android.Paths |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 238 | ctx.WalkDeps(func(child, parent android.Module) bool { |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 239 | if seen[child] { |
| 240 | return false |
| 241 | } |
| 242 | seen[child] = true |
| 243 | |
| 244 | if isValidSharedDependency(child) { |
| 245 | sharedLibraries = append(sharedLibraries, child.(*Module).UnstrippedOutputFile()) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 246 | return true |
| 247 | } |
| 248 | return false |
| 249 | }) |
| 250 | |
| 251 | for _, lib := range sharedLibraries { |
| 252 | fuzz.installedSharedDeps = append(fuzz.installedSharedDeps, |
| 253 | sharedLibraryInstallLocation( |
| 254 | lib, ctx.Host(), ctx.Arch().ArchType.String())) |
| 255 | } |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | func NewFuzz(hod android.HostOrDeviceSupported) *Module { |
| 259 | module, binary := NewBinary(hod) |
| 260 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 261 | binary.baseInstaller = NewFuzzInstaller() |
| 262 | module.sanitize.SetSanitizer(fuzzer, true) |
| 263 | |
| 264 | fuzz := &fuzzBinary{ |
| 265 | binaryDecorator: binary, |
| 266 | baseCompiler: NewBaseCompiler(), |
| 267 | } |
| 268 | module.compiler = fuzz |
| 269 | module.linker = fuzz |
| 270 | module.installer = fuzz |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 271 | |
| 272 | // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin. |
| 273 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 274 | disableDarwinAndLinuxBionic := struct { |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 275 | Target struct { |
| 276 | Darwin struct { |
| 277 | Enabled *bool |
| 278 | } |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 279 | Linux_bionic struct { |
| 280 | Enabled *bool |
| 281 | } |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 282 | } |
| 283 | }{} |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 284 | disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false) |
| 285 | disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false) |
| 286 | ctx.AppendProperties(&disableDarwinAndLinuxBionic) |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 287 | }) |
| 288 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 289 | return module |
| 290 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 291 | |
| 292 | // Responsible for generating GNU Make rules that package fuzz targets into |
| 293 | // their architecture & target/host specific zip file. |
| 294 | type fuzzPackager struct { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 295 | packages android.Paths |
| 296 | sharedLibInstallStrings []string |
| 297 | fuzzTargets map[string]bool |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | func fuzzPackagingFactory() android.Singleton { |
| 301 | return &fuzzPackager{} |
| 302 | } |
| 303 | |
| 304 | type fileToZip struct { |
| 305 | SourceFilePath android.Path |
| 306 | DestinationPathPrefix string |
| 307 | } |
| 308 | |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 309 | type archOs struct { |
| 310 | hostOrTarget string |
| 311 | arch string |
| 312 | dir string |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 315 | func (s *fuzzPackager) GenerateBuildActions(ctx android.SingletonContext) { |
| 316 | // Map between each architecture + host/device combination, and the files that |
| 317 | // need to be packaged (in the tuple of {source file, destination folder in |
| 318 | // archive}). |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 319 | archDirs := make(map[archOs][]fileToZip) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 320 | |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 321 | // Map tracking whether each shared library has an install rule to avoid duplicate install rules from |
| 322 | // multiple fuzzers that depend on the same shared library. |
| 323 | sharedLibraryInstalled := make(map[string]bool) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 324 | |
| 325 | // List of individual fuzz targets, so that 'make fuzz' also installs the targets |
| 326 | // to the correct output directories as well. |
| 327 | s.fuzzTargets = make(map[string]bool) |
| 328 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 329 | ctx.VisitAllModules(func(module android.Module) { |
| 330 | // Discard non-fuzz targets. |
| 331 | ccModule, ok := module.(*Module) |
| 332 | if !ok { |
| 333 | return |
| 334 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 335 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 336 | fuzzModule, ok := ccModule.compiler.(*fuzzBinary) |
| 337 | if !ok { |
| 338 | return |
| 339 | } |
| 340 | |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 341 | // Discard vendor-NDK-linked + recovery modules, they're duplicates of |
| 342 | // fuzz targets we're going to package anyway. |
| 343 | if !ccModule.Enabled() || ccModule.Properties.PreventInstall || |
| 344 | ccModule.UseVndk() || ccModule.InRecovery() { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 345 | return |
| 346 | } |
| 347 | |
Mitch Phillips | 6a9bf21 | 2019-12-05 07:36:11 -0800 | [diff] [blame^] | 348 | // Discard modules that are in an unavailable namespace. |
| 349 | if !ccModule.ExportedToMake() { |
| 350 | return |
| 351 | } |
| 352 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 353 | s.fuzzTargets[module.Name()] = true |
| 354 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 355 | hostOrTargetString := "target" |
| 356 | if ccModule.Host() { |
| 357 | hostOrTargetString = "host" |
| 358 | } |
| 359 | |
| 360 | archString := ccModule.Arch().ArchType.String() |
| 361 | archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString) |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 362 | archOs := archOs{hostOrTarget: hostOrTargetString, arch: archString, dir: archDir.String()} |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 363 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 364 | // Grab the list of required shared libraries. |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 365 | sharedLibraries := collectAllSharedDependencies(ctx, module) |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 366 | |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 367 | var files []fileToZip |
| 368 | builder := android.NewRuleBuilder() |
| 369 | |
| 370 | // Package the corpora into a zipfile. |
| 371 | if fuzzModule.corpus != nil { |
| 372 | corpusZip := archDir.Join(ctx, module.Name()+"_seed_corpus.zip") |
| 373 | command := builder.Command().BuiltTool(ctx, "soong_zip"). |
| 374 | Flag("-j"). |
| 375 | FlagWithOutput("-o ", corpusZip) |
| 376 | command.FlagWithRspFileInputList("-l ", fuzzModule.corpus) |
| 377 | files = append(files, fileToZip{corpusZip, ""}) |
| 378 | } |
| 379 | |
| 380 | // Find and mark all the transiently-dependent shared libraries for |
| 381 | // packaging. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 382 | for _, library := range sharedLibraries { |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 383 | files = append(files, fileToZip{library, "lib"}) |
Mitch Phillips | 13ed3f5 | 2019-11-12 11:12:10 -0800 | [diff] [blame] | 384 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 385 | // For each architecture-specific shared library dependency, we need to |
| 386 | // install it to the output directory. Setup the install destination here, |
| 387 | // which will be used by $(copy-many-files) in the Make backend. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 388 | installDestination := sharedLibraryInstallLocation( |
| 389 | library, ccModule.Host(), archString) |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 390 | if sharedLibraryInstalled[installDestination] { |
| 391 | continue |
| 392 | } |
| 393 | sharedLibraryInstalled[installDestination] = true |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 394 | // Escape all the variables, as the install destination here will be called |
| 395 | // via. $(eval) in Make. |
| 396 | installDestination = strings.ReplaceAll( |
| 397 | installDestination, "$", "$$") |
| 398 | s.sharedLibInstallStrings = append(s.sharedLibInstallStrings, |
| 399 | library.String()+":"+installDestination) |
| 400 | } |
| 401 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 402 | // The executable. |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 403 | files = append(files, fileToZip{ccModule.UnstrippedOutputFile(), ""}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 404 | |
| 405 | // The dictionary. |
| 406 | if fuzzModule.dictionary != nil { |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 407 | files = append(files, fileToZip{fuzzModule.dictionary, ""}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 408 | } |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 409 | |
| 410 | // Additional fuzz config. |
| 411 | if fuzzModule.config != nil { |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 412 | files = append(files, fileToZip{fuzzModule.config, ""}) |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 413 | } |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 414 | |
| 415 | fuzzZip := archDir.Join(ctx, module.Name()+".zip") |
| 416 | command := builder.Command().BuiltTool(ctx, "soong_zip"). |
| 417 | Flag("-j"). |
| 418 | FlagWithOutput("-o ", fuzzZip) |
| 419 | for _, file := range files { |
| 420 | if file.DestinationPathPrefix != "" { |
| 421 | command.FlagWithArg("-P ", file.DestinationPathPrefix) |
| 422 | } else { |
| 423 | command.Flag("-P ''") |
| 424 | } |
| 425 | command.FlagWithInput("-f ", file.SourceFilePath) |
| 426 | } |
| 427 | |
| 428 | builder.Build(pctx, ctx, "create-"+fuzzZip.String(), |
| 429 | "Package "+module.Name()+" for "+archString+"-"+hostOrTargetString) |
| 430 | |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 431 | archDirs[archOs] = append(archDirs[archOs], fileToZip{fuzzZip, ""}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 432 | }) |
| 433 | |
Colin Cross | dc809f9 | 2019-11-20 15:58:32 -0800 | [diff] [blame] | 434 | var archOsList []archOs |
| 435 | for archOs := range archDirs { |
| 436 | archOsList = append(archOsList, archOs) |
| 437 | } |
| 438 | sort.Slice(archOsList, func(i, j int) bool { return archOsList[i].dir < archOsList[j].dir }) |
| 439 | |
| 440 | for _, archOs := range archOsList { |
| 441 | filesToZip := archDirs[archOs] |
| 442 | arch := archOs.arch |
| 443 | hostOrTarget := archOs.hostOrTarget |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 444 | builder := android.NewRuleBuilder() |
| 445 | outputFile := android.PathForOutput(ctx, "fuzz-"+hostOrTarget+"-"+arch+".zip") |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 446 | s.packages = append(s.packages, outputFile) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 447 | |
| 448 | command := builder.Command().BuiltTool(ctx, "soong_zip"). |
| 449 | Flag("-j"). |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 450 | FlagWithOutput("-o ", outputFile). |
| 451 | Flag("-L 0") // No need to try and re-compress the zipfiles. |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 452 | |
| 453 | for _, fileToZip := range filesToZip { |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 454 | if fileToZip.DestinationPathPrefix != "" { |
| 455 | command.FlagWithArg("-P ", fileToZip.DestinationPathPrefix) |
| 456 | } else { |
| 457 | command.Flag("-P ''") |
| 458 | } |
| 459 | command.FlagWithInput("-f ", fileToZip.SourceFilePath) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | builder.Build(pctx, ctx, "create-fuzz-package-"+arch+"-"+hostOrTarget, |
| 463 | "Create fuzz target packages for "+arch+"-"+hostOrTarget) |
| 464 | } |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 465 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 466 | |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 467 | func (s *fuzzPackager) MakeVars(ctx android.MakeVarsContext) { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 468 | packages := s.packages.Strings() |
| 469 | sort.Strings(packages) |
| 470 | sort.Strings(s.sharedLibInstallStrings) |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 471 | // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's |
| 472 | // ready to handle phony targets created in Soong. In the meantime, this |
| 473 | // exports the phony 'fuzz' target and dependencies on packages to |
| 474 | // core/main.mk so that we can use dist-for-goals. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 475 | ctx.Strict("SOONG_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " ")) |
| 476 | ctx.Strict("FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS", |
| 477 | strings.Join(s.sharedLibInstallStrings, " ")) |
| 478 | |
| 479 | // Preallocate the slice of fuzz targets to minimise memory allocations. |
| 480 | fuzzTargets := make([]string, 0, len(s.fuzzTargets)) |
| 481 | for target, _ := range s.fuzzTargets { |
| 482 | fuzzTargets = append(fuzzTargets, target) |
| 483 | } |
| 484 | sort.Strings(fuzzTargets) |
| 485 | ctx.Strict("ALL_FUZZ_TARGETS", strings.Join(fuzzTargets, " ")) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 486 | } |