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 | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 106 | // RunPaths on devices isn't instantiated by the base linker. |
| 107 | flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN/../lib`) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 108 | return flags |
| 109 | } |
| 110 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 111 | // This function performs a breadth-first search over the provided module's |
| 112 | // dependencies using `visitDirectDeps` to enumerate all shared library |
| 113 | // dependencies. We require breadth-first expansion, as otherwise we may |
| 114 | // incorrectly use the core libraries (sanitizer runtimes, libc, libdl, etc.) |
| 115 | // from a dependency. This may cause issues when dependencies have explicit |
| 116 | // sanitizer tags, as we may get a dependency on an unsanitized libc, etc. |
| 117 | func collectAllSharedDependencies( |
| 118 | module android.Module, |
| 119 | sharedDeps map[string]android.Path, |
| 120 | ctx android.SingletonContext) { |
| 121 | var fringe []android.Module |
| 122 | |
| 123 | // Enumerate the first level of dependencies, as we discard all non-library |
| 124 | // modules in the BFS loop below. |
| 125 | ctx.VisitDirectDeps(module, func(dep android.Module) { |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame^] | 126 | if isValidSharedDependency(dep, sharedDeps) { |
| 127 | fringe = append(fringe, dep) |
| 128 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 129 | }) |
| 130 | |
| 131 | for i := 0; i < len(fringe); i++ { |
| 132 | module := fringe[i] |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame^] | 133 | if _, exists := sharedDeps[module.Name()]; exists { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 134 | continue |
| 135 | } |
| 136 | |
| 137 | ccModule := module.(*Module) |
| 138 | sharedDeps[ccModule.Name()] = ccModule.UnstrippedOutputFile() |
| 139 | ctx.VisitDirectDeps(module, func(dep android.Module) { |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame^] | 140 | if isValidSharedDependency(dep, sharedDeps) { |
| 141 | fringe = append(fringe, dep) |
| 142 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 143 | }) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // This function takes a module and determines if it is a unique shared library |
| 148 | // that should be installed in the fuzz target output directories. This function |
| 149 | // returns true, unless: |
| 150 | // - The module already exists in `sharedDeps`, or |
| 151 | // - The module is not a shared library, or |
| 152 | // - The module is a header, stub, or vendor-linked library. |
| 153 | func isValidSharedDependency( |
| 154 | dependency android.Module, |
| 155 | sharedDeps map[string]android.Path) bool { |
| 156 | // TODO(b/144090547): We should be parsing these modules using |
| 157 | // ModuleDependencyTag instead of the current brute-force checking. |
| 158 | |
| 159 | if linkable, ok := dependency.(LinkableInterface); !ok || // Discard non-linkables. |
| 160 | !linkable.CcLibraryInterface() || !linkable.Shared() || // Discard static libs. |
| 161 | linkable.UseVndk() || // Discard vendor linked libraries. |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame^] | 162 | // Discard stubs libs (only CCLibrary variants). Prebuilt libraries should not |
| 163 | // be excluded on the basis of they're not CCLibrary()'s. |
| 164 | (linkable.CcLibrary() && linkable.BuildStubs()) { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 165 | return false |
| 166 | } |
| 167 | |
Mitch Phillips | f50bddb | 2019-11-12 14:03:31 -0800 | [diff] [blame^] | 168 | // We discarded module stubs libraries above, but the LLNDK prebuilts stubs |
| 169 | // libraries must be handled differently - by looking for the stubDecorator. |
| 170 | // Discard LLNDK prebuilts stubs as well. |
| 171 | if ccLibrary, isCcLibrary := dependency.(*Module); isCcLibrary { |
| 172 | if _, isLLndkStubLibrary := ccLibrary.linker.(*stubDecorator); isLLndkStubLibrary { |
| 173 | return false |
| 174 | } |
| 175 | } |
| 176 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 177 | // If this library has already been traversed, we don't need to do any more work. |
| 178 | if _, exists := sharedDeps[dependency.Name()]; exists { |
| 179 | return false |
| 180 | } |
| 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. |
| 236 | sharedLibraries := make(map[string]android.Path) |
| 237 | ctx.WalkDeps(func(child, parent android.Module) bool { |
| 238 | if isValidSharedDependency(child, sharedLibraries) { |
| 239 | sharedLibraries[child.Name()] = child.(*Module).UnstrippedOutputFile() |
| 240 | return true |
| 241 | } |
| 242 | return false |
| 243 | }) |
| 244 | |
| 245 | for _, lib := range sharedLibraries { |
| 246 | fuzz.installedSharedDeps = append(fuzz.installedSharedDeps, |
| 247 | sharedLibraryInstallLocation( |
| 248 | lib, ctx.Host(), ctx.Arch().ArchType.String())) |
| 249 | } |
Mitch Phillips | 0553ba3 | 2019-11-11 07:03:42 -0800 | [diff] [blame] | 250 | |
| 251 | sort.Strings(fuzz.installedSharedDeps) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | func NewFuzz(hod android.HostOrDeviceSupported) *Module { |
| 255 | module, binary := NewBinary(hod) |
| 256 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 257 | binary.baseInstaller = NewFuzzInstaller() |
| 258 | module.sanitize.SetSanitizer(fuzzer, true) |
| 259 | |
| 260 | fuzz := &fuzzBinary{ |
| 261 | binaryDecorator: binary, |
| 262 | baseCompiler: NewBaseCompiler(), |
| 263 | } |
| 264 | module.compiler = fuzz |
| 265 | module.linker = fuzz |
| 266 | module.installer = fuzz |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 267 | |
| 268 | // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin. |
| 269 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 270 | disableDarwinAndLinuxBionic := struct { |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 271 | Target struct { |
| 272 | Darwin struct { |
| 273 | Enabled *bool |
| 274 | } |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 275 | Linux_bionic struct { |
| 276 | Enabled *bool |
| 277 | } |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 278 | } |
| 279 | }{} |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 280 | disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false) |
| 281 | disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false) |
| 282 | ctx.AppendProperties(&disableDarwinAndLinuxBionic) |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 283 | }) |
| 284 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 285 | return module |
| 286 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 287 | |
| 288 | // Responsible for generating GNU Make rules that package fuzz targets into |
| 289 | // their architecture & target/host specific zip file. |
| 290 | type fuzzPackager struct { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 291 | packages android.Paths |
| 292 | sharedLibInstallStrings []string |
| 293 | fuzzTargets map[string]bool |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | func fuzzPackagingFactory() android.Singleton { |
| 297 | return &fuzzPackager{} |
| 298 | } |
| 299 | |
| 300 | type fileToZip struct { |
| 301 | SourceFilePath android.Path |
| 302 | DestinationPathPrefix string |
| 303 | } |
| 304 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 305 | type archAndLibraryKey struct { |
| 306 | ArchDir android.OutputPath |
| 307 | Library android.Path |
| 308 | } |
| 309 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 310 | func (s *fuzzPackager) GenerateBuildActions(ctx android.SingletonContext) { |
| 311 | // Map between each architecture + host/device combination, and the files that |
| 312 | // need to be packaged (in the tuple of {source file, destination folder in |
| 313 | // archive}). |
| 314 | archDirs := make(map[android.OutputPath][]fileToZip) |
| 315 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 316 | // List of shared library dependencies for each architecture + host/device combo. |
| 317 | archSharedLibraryDeps := make(map[archAndLibraryKey]bool) |
| 318 | |
| 319 | // List of individual fuzz targets, so that 'make fuzz' also installs the targets |
| 320 | // to the correct output directories as well. |
| 321 | s.fuzzTargets = make(map[string]bool) |
| 322 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 323 | ctx.VisitAllModules(func(module android.Module) { |
| 324 | // Discard non-fuzz targets. |
| 325 | ccModule, ok := module.(*Module) |
| 326 | if !ok { |
| 327 | return |
| 328 | } |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 329 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 330 | fuzzModule, ok := ccModule.compiler.(*fuzzBinary) |
| 331 | if !ok { |
| 332 | return |
| 333 | } |
| 334 | |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 335 | // Discard vendor-NDK-linked + recovery modules, they're duplicates of |
| 336 | // fuzz targets we're going to package anyway. |
| 337 | if !ccModule.Enabled() || ccModule.Properties.PreventInstall || |
| 338 | ccModule.UseVndk() || ccModule.InRecovery() { |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 339 | return |
| 340 | } |
| 341 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 342 | s.fuzzTargets[module.Name()] = true |
| 343 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 344 | hostOrTargetString := "target" |
| 345 | if ccModule.Host() { |
| 346 | hostOrTargetString = "host" |
| 347 | } |
| 348 | |
| 349 | archString := ccModule.Arch().ArchType.String() |
| 350 | archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString) |
| 351 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 352 | // Grab the list of required shared libraries. |
| 353 | sharedLibraries := make(map[string]android.Path) |
| 354 | collectAllSharedDependencies(module, sharedLibraries, ctx) |
| 355 | |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 356 | var files []fileToZip |
| 357 | builder := android.NewRuleBuilder() |
| 358 | |
| 359 | // Package the corpora into a zipfile. |
| 360 | if fuzzModule.corpus != nil { |
| 361 | corpusZip := archDir.Join(ctx, module.Name()+"_seed_corpus.zip") |
| 362 | command := builder.Command().BuiltTool(ctx, "soong_zip"). |
| 363 | Flag("-j"). |
| 364 | FlagWithOutput("-o ", corpusZip) |
| 365 | command.FlagWithRspFileInputList("-l ", fuzzModule.corpus) |
| 366 | files = append(files, fileToZip{corpusZip, ""}) |
| 367 | } |
| 368 | |
| 369 | // Find and mark all the transiently-dependent shared libraries for |
| 370 | // packaging. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 371 | for _, library := range sharedLibraries { |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 372 | files = append(files, fileToZip{library, "lib"}) |
Mitch Phillips | 13ed3f5 | 2019-11-12 11:12:10 -0800 | [diff] [blame] | 373 | |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 374 | if _, exists := archSharedLibraryDeps[archAndLibraryKey{archDir, library}]; exists { |
| 375 | continue |
| 376 | } |
| 377 | |
| 378 | // For each architecture-specific shared library dependency, we need to |
| 379 | // install it to the output directory. Setup the install destination here, |
| 380 | // which will be used by $(copy-many-files) in the Make backend. |
| 381 | archSharedLibraryDeps[archAndLibraryKey{archDir, library}] = true |
| 382 | installDestination := sharedLibraryInstallLocation( |
| 383 | library, ccModule.Host(), archString) |
| 384 | // Escape all the variables, as the install destination here will be called |
| 385 | // via. $(eval) in Make. |
| 386 | installDestination = strings.ReplaceAll( |
| 387 | installDestination, "$", "$$") |
| 388 | s.sharedLibInstallStrings = append(s.sharedLibInstallStrings, |
| 389 | library.String()+":"+installDestination) |
| 390 | } |
| 391 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 392 | // The executable. |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 393 | files = append(files, fileToZip{ccModule.UnstrippedOutputFile(), ""}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 394 | |
| 395 | // The dictionary. |
| 396 | if fuzzModule.dictionary != nil { |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 397 | files = append(files, fileToZip{fuzzModule.dictionary, ""}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 398 | } |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 399 | |
| 400 | // Additional fuzz config. |
| 401 | if fuzzModule.config != nil { |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 402 | files = append(files, fileToZip{fuzzModule.config, ""}) |
Kris Alder | f979ee3 | 2019-10-22 10:52:01 -0700 | [diff] [blame] | 403 | } |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 404 | |
| 405 | fuzzZip := archDir.Join(ctx, module.Name()+".zip") |
| 406 | command := builder.Command().BuiltTool(ctx, "soong_zip"). |
| 407 | Flag("-j"). |
| 408 | FlagWithOutput("-o ", fuzzZip) |
| 409 | for _, file := range files { |
| 410 | if file.DestinationPathPrefix != "" { |
| 411 | command.FlagWithArg("-P ", file.DestinationPathPrefix) |
| 412 | } else { |
| 413 | command.Flag("-P ''") |
| 414 | } |
| 415 | command.FlagWithInput("-f ", file.SourceFilePath) |
| 416 | } |
| 417 | |
| 418 | builder.Build(pctx, ctx, "create-"+fuzzZip.String(), |
| 419 | "Package "+module.Name()+" for "+archString+"-"+hostOrTargetString) |
| 420 | |
| 421 | archDirs[archDir] = append(archDirs[archDir], fileToZip{fuzzZip, ""}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 422 | }) |
| 423 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 424 | for archDir, filesToZip := range archDirs { |
| 425 | arch := archDir.Base() |
| 426 | hostOrTarget := filepath.Base(filepath.Dir(archDir.String())) |
| 427 | builder := android.NewRuleBuilder() |
| 428 | outputFile := android.PathForOutput(ctx, "fuzz-"+hostOrTarget+"-"+arch+".zip") |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 429 | s.packages = append(s.packages, outputFile) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 430 | |
| 431 | command := builder.Command().BuiltTool(ctx, "soong_zip"). |
| 432 | Flag("-j"). |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 433 | FlagWithOutput("-o ", outputFile). |
| 434 | Flag("-L 0") // No need to try and re-compress the zipfiles. |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 435 | |
| 436 | for _, fileToZip := range filesToZip { |
Mitch Phillips | 2edbe8e | 2019-11-13 08:36:07 -0800 | [diff] [blame] | 437 | if fileToZip.DestinationPathPrefix != "" { |
| 438 | command.FlagWithArg("-P ", fileToZip.DestinationPathPrefix) |
| 439 | } else { |
| 440 | command.Flag("-P ''") |
| 441 | } |
| 442 | command.FlagWithInput("-f ", fileToZip.SourceFilePath) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | builder.Build(pctx, ctx, "create-fuzz-package-"+arch+"-"+hostOrTarget, |
| 446 | "Create fuzz target packages for "+arch+"-"+hostOrTarget) |
| 447 | } |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 448 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 449 | |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 450 | func (s *fuzzPackager) MakeVars(ctx android.MakeVarsContext) { |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 451 | packages := s.packages.Strings() |
| 452 | sort.Strings(packages) |
| 453 | sort.Strings(s.sharedLibInstallStrings) |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 454 | // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's |
| 455 | // ready to handle phony targets created in Soong. In the meantime, this |
| 456 | // exports the phony 'fuzz' target and dependencies on packages to |
| 457 | // core/main.mk so that we can use dist-for-goals. |
Mitch Phillips | e1ee1a1 | 2019-10-17 19:20:41 -0700 | [diff] [blame] | 458 | ctx.Strict("SOONG_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " ")) |
| 459 | ctx.Strict("FUZZ_TARGET_SHARED_DEPS_INSTALL_PAIRS", |
| 460 | strings.Join(s.sharedLibInstallStrings, " ")) |
| 461 | |
| 462 | // Preallocate the slice of fuzz targets to minimise memory allocations. |
| 463 | fuzzTargets := make([]string, 0, len(s.fuzzTargets)) |
| 464 | for target, _ := range s.fuzzTargets { |
| 465 | fuzzTargets = append(fuzzTargets, target) |
| 466 | } |
| 467 | sort.Strings(fuzzTargets) |
| 468 | ctx.Strict("ALL_FUZZ_TARGETS", strings.Join(fuzzTargets, " ")) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 469 | } |