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 | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 19 | "strings" |
Mitch Phillips | 4de896e | 2019-08-28 16:04:36 -0700 | [diff] [blame] | 20 | |
| 21 | "github.com/google/blueprint/proptools" |
| 22 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 23 | "android/soong/android" |
| 24 | "android/soong/cc/config" |
| 25 | ) |
| 26 | |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 27 | type FuzzProperties struct { |
| 28 | // Optional list of seed files to be installed to the fuzz target's output |
| 29 | // directory. |
| 30 | Corpus []string `android:"path"` |
| 31 | // Optional dictionary to be installed to the fuzz target's output directory. |
| 32 | Dictionary *string `android:"path"` |
| 33 | } |
| 34 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 35 | func init() { |
| 36 | android.RegisterModuleType("cc_fuzz", FuzzFactory) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 37 | android.RegisterSingletonType("cc_fuzz_packaging", fuzzPackagingFactory) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at |
| 41 | // $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on |
| 42 | // your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree. |
| 43 | func FuzzFactory() android.Module { |
| 44 | module := NewFuzz(android.HostAndDeviceSupported) |
| 45 | return module.Init() |
| 46 | } |
| 47 | |
| 48 | func NewFuzzInstaller() *baseInstaller { |
| 49 | return NewBaseInstaller("fuzz", "fuzz", InstallInData) |
| 50 | } |
| 51 | |
| 52 | type fuzzBinary struct { |
| 53 | *binaryDecorator |
| 54 | *baseCompiler |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 55 | |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame^] | 56 | Properties FuzzProperties |
| 57 | dictionary android.Path |
| 58 | corpus android.Paths |
| 59 | corpusIntermediateDir android.Path |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | func (fuzz *fuzzBinary) linkerProps() []interface{} { |
| 63 | props := fuzz.binaryDecorator.linkerProps() |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 64 | props = append(props, &fuzz.Properties) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 65 | return props |
| 66 | } |
| 67 | |
| 68 | func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) { |
| 69 | // Add ../lib[64] to rpath so that out/host/linux-x86/fuzz/<fuzzer> can |
| 70 | // find out/host/linux-x86/lib[64]/library.so |
| 71 | runpaths := []string{"../lib"} |
| 72 | for _, runpath := range runpaths { |
| 73 | if ctx.toolchain().Is64Bit() { |
| 74 | runpath += "64" |
| 75 | } |
| 76 | fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths = append( |
| 77 | fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths, runpath) |
| 78 | } |
| 79 | |
| 80 | // add "" to rpath so that fuzzer binaries can find libraries in their own fuzz directory |
| 81 | fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths = append( |
| 82 | fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths, "") |
| 83 | |
| 84 | fuzz.binaryDecorator.linkerInit(ctx) |
| 85 | } |
| 86 | |
| 87 | func (fuzz *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
| 88 | deps.StaticLibs = append(deps.StaticLibs, |
| 89 | config.LibFuzzerRuntimeLibrary(ctx.toolchain())) |
| 90 | deps = fuzz.binaryDecorator.linkerDeps(ctx, deps) |
| 91 | return deps |
| 92 | } |
| 93 | |
| 94 | func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 95 | flags = fuzz.binaryDecorator.linkerFlags(ctx, flags) |
| 96 | return flags |
| 97 | } |
| 98 | |
| 99 | func (fuzz *fuzzBinary) install(ctx ModuleContext, file android.Path) { |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 100 | fuzz.binaryDecorator.baseInstaller.dir = filepath.Join( |
| 101 | "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
| 102 | fuzz.binaryDecorator.baseInstaller.dir64 = filepath.Join( |
| 103 | "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 104 | fuzz.binaryDecorator.baseInstaller.install(ctx, file) |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 105 | |
| 106 | fuzz.corpus = android.PathsForModuleSrc(ctx, fuzz.Properties.Corpus) |
Mitch Phillips | 8a2bc0b | 2019-10-17 15:04:01 -0700 | [diff] [blame^] | 107 | builder := android.NewRuleBuilder() |
| 108 | intermediateDir := android.PathForModuleOut(ctx, "corpus") |
| 109 | for _, entry := range fuzz.corpus { |
| 110 | builder.Command().Text("cp"). |
| 111 | Input(entry). |
| 112 | Output(intermediateDir.Join(ctx, entry.Base())) |
| 113 | } |
| 114 | builder.Build(pctx, ctx, "copy_corpus", "copy corpus") |
| 115 | fuzz.corpusIntermediateDir = intermediateDir |
| 116 | |
Mitch Phillips | 4e4ab8a | 2019-09-13 17:32:50 -0700 | [diff] [blame] | 117 | if fuzz.Properties.Dictionary != nil { |
| 118 | fuzz.dictionary = android.PathForModuleSrc(ctx, *fuzz.Properties.Dictionary) |
| 119 | if fuzz.dictionary.Ext() != ".dict" { |
| 120 | ctx.PropertyErrorf("dictionary", |
| 121 | "Fuzzer dictionary %q does not have '.dict' extension", |
| 122 | fuzz.dictionary.String()) |
| 123 | } |
| 124 | } |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | func NewFuzz(hod android.HostOrDeviceSupported) *Module { |
| 128 | module, binary := NewBinary(hod) |
| 129 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 130 | binary.baseInstaller = NewFuzzInstaller() |
| 131 | module.sanitize.SetSanitizer(fuzzer, true) |
| 132 | |
| 133 | fuzz := &fuzzBinary{ |
| 134 | binaryDecorator: binary, |
| 135 | baseCompiler: NewBaseCompiler(), |
| 136 | } |
| 137 | module.compiler = fuzz |
| 138 | module.linker = fuzz |
| 139 | module.installer = fuzz |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 140 | |
| 141 | // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin. |
| 142 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 143 | disableDarwinAndLinuxBionic := struct { |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 144 | Target struct { |
| 145 | Darwin struct { |
| 146 | Enabled *bool |
| 147 | } |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 148 | Linux_bionic struct { |
| 149 | Enabled *bool |
| 150 | } |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 151 | } |
| 152 | }{} |
Alex Light | 71123ec | 2019-07-24 13:34:19 -0700 | [diff] [blame] | 153 | disableDarwinAndLinuxBionic.Target.Darwin.Enabled = BoolPtr(false) |
| 154 | disableDarwinAndLinuxBionic.Target.Linux_bionic.Enabled = BoolPtr(false) |
| 155 | ctx.AppendProperties(&disableDarwinAndLinuxBionic) |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame] | 156 | }) |
| 157 | |
Mitch Phillips | d0bd16d | 2019-08-22 15:47:09 -0700 | [diff] [blame] | 158 | // Statically link the STL. This allows fuzz target deployment to not have to |
| 159 | // include the STL. |
| 160 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
| 161 | staticStlLinkage := struct { |
Mitch Phillips | 302f964 | 2019-10-14 16:40:26 -0700 | [diff] [blame] | 162 | Target struct { |
| 163 | Linux_glibc struct { |
| 164 | Stl *string |
| 165 | } |
| 166 | } |
Mitch Phillips | d0bd16d | 2019-08-22 15:47:09 -0700 | [diff] [blame] | 167 | }{} |
| 168 | |
Mitch Phillips | 302f964 | 2019-10-14 16:40:26 -0700 | [diff] [blame] | 169 | staticStlLinkage.Target.Linux_glibc.Stl = proptools.StringPtr("libc++_static") |
Mitch Phillips | d0bd16d | 2019-08-22 15:47:09 -0700 | [diff] [blame] | 170 | ctx.AppendProperties(&staticStlLinkage) |
| 171 | }) |
| 172 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 173 | return module |
| 174 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 175 | |
| 176 | // Responsible for generating GNU Make rules that package fuzz targets into |
| 177 | // their architecture & target/host specific zip file. |
| 178 | type fuzzPackager struct { |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 179 | packages android.Paths |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | func fuzzPackagingFactory() android.Singleton { |
| 183 | return &fuzzPackager{} |
| 184 | } |
| 185 | |
| 186 | type fileToZip struct { |
| 187 | SourceFilePath android.Path |
| 188 | DestinationPathPrefix string |
| 189 | } |
| 190 | |
| 191 | func (s *fuzzPackager) GenerateBuildActions(ctx android.SingletonContext) { |
| 192 | // Map between each architecture + host/device combination, and the files that |
| 193 | // need to be packaged (in the tuple of {source file, destination folder in |
| 194 | // archive}). |
| 195 | archDirs := make(map[android.OutputPath][]fileToZip) |
| 196 | |
| 197 | ctx.VisitAllModules(func(module android.Module) { |
| 198 | // Discard non-fuzz targets. |
| 199 | ccModule, ok := module.(*Module) |
| 200 | if !ok { |
| 201 | return |
| 202 | } |
| 203 | fuzzModule, ok := ccModule.compiler.(*fuzzBinary) |
| 204 | if !ok { |
| 205 | return |
| 206 | } |
| 207 | |
| 208 | // Discard vendor-NDK-linked modules, they're duplicates of fuzz targets |
| 209 | // we're going to package anyway. |
| 210 | if ccModule.useVndk() || !ccModule.Enabled() { |
| 211 | return |
| 212 | } |
| 213 | |
| 214 | hostOrTargetString := "target" |
| 215 | if ccModule.Host() { |
| 216 | hostOrTargetString = "host" |
| 217 | } |
| 218 | |
| 219 | archString := ccModule.Arch().ArchType.String() |
| 220 | archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString) |
| 221 | |
| 222 | // The executable. |
| 223 | archDirs[archDir] = append(archDirs[archDir], |
| 224 | fileToZip{ccModule.outputFile.Path(), ccModule.Name()}) |
| 225 | |
| 226 | // The corpora. |
| 227 | for _, corpusEntry := range fuzzModule.corpus { |
| 228 | archDirs[archDir] = append(archDirs[archDir], |
Mitch Phillips | 641575a | 2019-10-09 17:34:42 -0700 | [diff] [blame] | 229 | fileToZip{corpusEntry, ccModule.Name() + "/corpus"}) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // The dictionary. |
| 233 | if fuzzModule.dictionary != nil { |
| 234 | archDirs[archDir] = append(archDirs[archDir], |
| 235 | fileToZip{fuzzModule.dictionary, ccModule.Name()}) |
| 236 | } |
| 237 | }) |
| 238 | |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 239 | for archDir, filesToZip := range archDirs { |
| 240 | arch := archDir.Base() |
| 241 | hostOrTarget := filepath.Base(filepath.Dir(archDir.String())) |
| 242 | builder := android.NewRuleBuilder() |
| 243 | outputFile := android.PathForOutput(ctx, "fuzz-"+hostOrTarget+"-"+arch+".zip") |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 244 | s.packages = append(s.packages, outputFile) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 245 | |
| 246 | command := builder.Command().BuiltTool(ctx, "soong_zip"). |
| 247 | Flag("-j"). |
| 248 | FlagWithOutput("-o ", outputFile) |
| 249 | |
| 250 | for _, fileToZip := range filesToZip { |
| 251 | command.FlagWithArg("-P ", fileToZip.DestinationPathPrefix). |
| 252 | FlagWithInput("-f ", fileToZip.SourceFilePath) |
| 253 | } |
| 254 | |
| 255 | builder.Build(pctx, ctx, "create-fuzz-package-"+arch+"-"+hostOrTarget, |
| 256 | "Create fuzz target packages for "+arch+"-"+hostOrTarget) |
| 257 | } |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 258 | } |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 259 | |
Mitch Phillips | a0a5e19 | 2019-09-27 14:00:06 -0700 | [diff] [blame] | 260 | func (s *fuzzPackager) MakeVars(ctx android.MakeVarsContext) { |
| 261 | // TODO(mitchp): Migrate this to use MakeVarsContext::DistForGoal() when it's |
| 262 | // ready to handle phony targets created in Soong. In the meantime, this |
| 263 | // exports the phony 'fuzz' target and dependencies on packages to |
| 264 | // core/main.mk so that we can use dist-for-goals. |
| 265 | ctx.Strict("SOONG_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(s.packages.Strings(), " ")) |
Mitch Phillips | d3254b4 | 2019-09-24 13:03:28 -0700 | [diff] [blame] | 266 | } |