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