hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 1 | // Copyright 2021 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 | |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 15 | package fuzz |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 16 | |
| 17 | // This file contains the common code for compiling C/C++ and Rust fuzzers for Android. |
| 18 | |
| 19 | import ( |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 20 | "encoding/json" |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 21 | "sort" |
| 22 | "strings" |
| 23 | |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
| 25 | |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 26 | "android/soong/android" |
| 27 | ) |
| 28 | |
| 29 | type Lang string |
| 30 | |
| 31 | const ( |
| 32 | Cc Lang = "" |
| 33 | Rust Lang = "rust" |
| 34 | ) |
| 35 | |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 36 | var BoolDefault = proptools.BoolDefault |
| 37 | |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 38 | type FuzzModule struct { |
| 39 | android.ModuleBase |
| 40 | android.DefaultableModuleBase |
| 41 | android.ApexModuleBase |
| 42 | } |
| 43 | |
| 44 | type FuzzPackager struct { |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame^] | 45 | Packages android.Paths |
| 46 | FuzzTargets map[string]bool |
| 47 | SharedLibInstallStrings []string |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | type FileToZip struct { |
| 51 | SourceFilePath android.Path |
| 52 | DestinationPathPrefix string |
| 53 | } |
| 54 | |
| 55 | type ArchOs struct { |
| 56 | HostOrTarget string |
| 57 | Arch string |
| 58 | Dir string |
| 59 | } |
| 60 | |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 61 | type FuzzConfig struct { |
| 62 | // Email address of people to CC on bugs or contact about this fuzz target. |
| 63 | Cc []string `json:"cc,omitempty"` |
| 64 | // Specify whether to enable continuous fuzzing on devices. Defaults to true. |
| 65 | Fuzz_on_haiku_device *bool `json:"fuzz_on_haiku_device,omitempty"` |
| 66 | // Specify whether to enable continuous fuzzing on host. Defaults to true. |
| 67 | Fuzz_on_haiku_host *bool `json:"fuzz_on_haiku_host,omitempty"` |
| 68 | // Component in Google's bug tracking system that bugs should be filed to. |
| 69 | Componentid *int64 `json:"componentid,omitempty"` |
| 70 | // Hotlists in Google's bug tracking system that bugs should be marked with. |
| 71 | Hotlists []string `json:"hotlists,omitempty"` |
| 72 | // Specify whether this fuzz target was submitted by a researcher. Defaults |
| 73 | // to false. |
| 74 | Researcher_submitted *bool `json:"researcher_submitted,omitempty"` |
| 75 | // Specify who should be acknowledged for CVEs in the Android Security |
| 76 | // Bulletin. |
| 77 | Acknowledgement []string `json:"acknowledgement,omitempty"` |
| 78 | // Additional options to be passed to libfuzzer when run in Haiku. |
| 79 | Libfuzzer_options []string `json:"libfuzzer_options,omitempty"` |
| 80 | // Additional options to be passed to HWASAN when running on-device in Haiku. |
| 81 | Hwasan_options []string `json:"hwasan_options,omitempty"` |
| 82 | // Additional options to be passed to HWASAN when running on host in Haiku. |
| 83 | Asan_options []string `json:"asan_options,omitempty"` |
| 84 | } |
| 85 | |
| 86 | type FuzzProperties struct { |
| 87 | // Optional list of seed files to be installed to the fuzz target's output |
| 88 | // directory. |
| 89 | Corpus []string `android:"path"` |
| 90 | // Optional list of data files to be installed to the fuzz target's output |
| 91 | // directory. Directory structure relative to the module is preserved. |
| 92 | Data []string `android:"path"` |
| 93 | // Optional dictionary to be installed to the fuzz target's output directory. |
| 94 | Dictionary *string `android:"path"` |
| 95 | // Config for running the target on fuzzing infrastructure. |
| 96 | Fuzz_config *FuzzConfig |
| 97 | } |
| 98 | |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 99 | type FuzzPackagedModule struct { |
| 100 | FuzzProperties FuzzProperties |
| 101 | Dictionary android.Path |
| 102 | Corpus android.Paths |
| 103 | CorpusIntermediateDir android.Path |
| 104 | Config android.Path |
| 105 | Data android.Paths |
| 106 | DataIntermediateDir android.Path |
| 107 | } |
| 108 | |
| 109 | func IsValid(fuzzModule FuzzModule) bool { |
| 110 | // Discard ramdisk + vendor_ramdisk + recovery modules, they're duplicates of |
| 111 | // fuzz targets we're going to package anyway. |
| 112 | if !fuzzModule.Enabled() || fuzzModule.InRamdisk() || fuzzModule.InVendorRamdisk() || fuzzModule.InRecovery() { |
| 113 | return false |
| 114 | } |
| 115 | |
| 116 | // Discard modules that are in an unavailable namespace. |
| 117 | if !fuzzModule.ExportedToMake() { |
| 118 | return false |
| 119 | } |
| 120 | |
| 121 | return true |
| 122 | } |
| 123 | |
| 124 | func (s *FuzzPackager) PackageArtifacts(ctx android.SingletonContext, module android.Module, fuzzModule FuzzPackagedModule, archDir android.OutputPath, builder *android.RuleBuilder) []FileToZip { |
| 125 | // Package the corpora into a zipfile. |
| 126 | var files []FileToZip |
| 127 | if fuzzModule.Corpus != nil { |
| 128 | corpusZip := archDir.Join(ctx, module.Name()+"_seed_corpus.zip") |
| 129 | command := builder.Command().BuiltTool("soong_zip"). |
| 130 | Flag("-j"). |
| 131 | FlagWithOutput("-o ", corpusZip) |
| 132 | rspFile := corpusZip.ReplaceExtension(ctx, "rsp") |
| 133 | command.FlagWithRspFileInputList("-r ", rspFile, fuzzModule.Corpus) |
| 134 | files = append(files, FileToZip{corpusZip, ""}) |
| 135 | } |
| 136 | |
| 137 | // Package the data into a zipfile. |
| 138 | if fuzzModule.Data != nil { |
| 139 | dataZip := archDir.Join(ctx, module.Name()+"_data.zip") |
| 140 | command := builder.Command().BuiltTool("soong_zip"). |
| 141 | FlagWithOutput("-o ", dataZip) |
| 142 | for _, f := range fuzzModule.Data { |
| 143 | intermediateDir := strings.TrimSuffix(f.String(), f.Rel()) |
| 144 | command.FlagWithArg("-C ", intermediateDir) |
| 145 | command.FlagWithInput("-f ", f) |
| 146 | } |
| 147 | files = append(files, FileToZip{dataZip, ""}) |
| 148 | } |
| 149 | |
| 150 | // The dictionary. |
| 151 | if fuzzModule.Dictionary != nil { |
| 152 | files = append(files, FileToZip{fuzzModule.Dictionary, ""}) |
| 153 | } |
| 154 | |
| 155 | // Additional fuzz config. |
| 156 | if fuzzModule.Config != nil { |
| 157 | files = append(files, FileToZip{fuzzModule.Config, ""}) |
| 158 | } |
| 159 | |
| 160 | return files |
| 161 | } |
| 162 | |
| 163 | func (s *FuzzPackager) BuildZipFile(ctx android.SingletonContext, module android.Module, fuzzModule FuzzPackagedModule, files []FileToZip, builder *android.RuleBuilder, archDir android.OutputPath, archString string, hostOrTargetString string, archOs ArchOs, archDirs map[ArchOs][]FileToZip) ([]FileToZip, bool) { |
| 164 | fuzzZip := archDir.Join(ctx, module.Name()+".zip") |
| 165 | |
| 166 | command := builder.Command().BuiltTool("soong_zip"). |
| 167 | Flag("-j"). |
| 168 | FlagWithOutput("-o ", fuzzZip) |
| 169 | |
| 170 | for _, file := range files { |
| 171 | if file.DestinationPathPrefix != "" { |
| 172 | command.FlagWithArg("-P ", file.DestinationPathPrefix) |
| 173 | } else { |
| 174 | command.Flag("-P ''") |
| 175 | } |
| 176 | command.FlagWithInput("-f ", file.SourceFilePath) |
| 177 | } |
| 178 | |
| 179 | builder.Build("create-"+fuzzZip.String(), |
| 180 | "Package "+module.Name()+" for "+archString+"-"+hostOrTargetString) |
| 181 | |
| 182 | // Don't add modules to 'make haiku-rust' that are set to not be |
| 183 | // exported to the fuzzing infrastructure. |
| 184 | if config := fuzzModule.FuzzProperties.Fuzz_config; config != nil { |
| 185 | if strings.Contains(hostOrTargetString, "host") && !BoolDefault(config.Fuzz_on_haiku_host, true) { |
| 186 | return archDirs[archOs], false |
| 187 | } else if !BoolDefault(config.Fuzz_on_haiku_device, true) { |
| 188 | return archDirs[archOs], false |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | s.FuzzTargets[module.Name()] = true |
| 193 | archDirs[archOs] = append(archDirs[archOs], FileToZip{fuzzZip, ""}) |
| 194 | |
| 195 | return archDirs[archOs], true |
| 196 | } |
| 197 | |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame] | 198 | func (f *FuzzConfig) String() string { |
| 199 | b, err := json.Marshal(f) |
| 200 | if err != nil { |
| 201 | panic(err) |
| 202 | } |
| 203 | |
| 204 | return string(b) |
| 205 | } |
| 206 | |
| 207 | func (s *FuzzPackager) CreateFuzzPackage(ctx android.SingletonContext, archDirs map[ArchOs][]FileToZip, lang Lang, pctx android.PackageContext) { |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 208 | var archOsList []ArchOs |
| 209 | for archOs := range archDirs { |
| 210 | archOsList = append(archOsList, archOs) |
| 211 | } |
| 212 | sort.Slice(archOsList, func(i, j int) bool { return archOsList[i].Dir < archOsList[j].Dir }) |
| 213 | |
| 214 | for _, archOs := range archOsList { |
| 215 | filesToZip := archDirs[archOs] |
| 216 | arch := archOs.Arch |
| 217 | hostOrTarget := archOs.HostOrTarget |
| 218 | builder := android.NewRuleBuilder(pctx, ctx) |
| 219 | zipFileName := "fuzz-" + hostOrTarget + "-" + arch + ".zip" |
| 220 | if lang == Rust { |
| 221 | zipFileName = "fuzz-rust-" + hostOrTarget + "-" + arch + ".zip" |
| 222 | } |
| 223 | outputFile := android.PathForOutput(ctx, zipFileName) |
| 224 | |
| 225 | s.Packages = append(s.Packages, outputFile) |
| 226 | |
| 227 | command := builder.Command().BuiltTool("soong_zip"). |
| 228 | Flag("-j"). |
| 229 | FlagWithOutput("-o ", outputFile). |
| 230 | Flag("-L 0") // No need to try and re-compress the zipfiles. |
| 231 | |
| 232 | for _, fileToZip := range filesToZip { |
| 233 | |
| 234 | if fileToZip.DestinationPathPrefix != "" { |
| 235 | command.FlagWithArg("-P ", fileToZip.DestinationPathPrefix) |
| 236 | } else { |
| 237 | command.Flag("-P ''") |
| 238 | } |
| 239 | command.FlagWithInput("-f ", fileToZip.SourceFilePath) |
| 240 | |
| 241 | } |
| 242 | builder.Build("create-fuzz-package-"+arch+"-"+hostOrTarget, |
| 243 | "Create fuzz target packages for "+arch+"-"+hostOrTarget) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | func (s *FuzzPackager) PreallocateSlice(ctx android.MakeVarsContext, targets string) { |
| 248 | fuzzTargets := make([]string, 0, len(s.FuzzTargets)) |
| 249 | for target, _ := range s.FuzzTargets { |
| 250 | fuzzTargets = append(fuzzTargets, target) |
| 251 | } |
| 252 | sort.Strings(fuzzTargets) |
| 253 | ctx.Strict(targets, strings.Join(fuzzTargets, " ")) |
| 254 | } |
Ivan Lozano | 39b0bf0 | 2021-10-14 12:22:09 -0400 | [diff] [blame^] | 255 | |
| 256 | // CollectAllSharedDependencies performs a breadth-first search over the provided module's |
| 257 | // dependencies using `visitDirectDeps` to enumerate all shared library |
| 258 | // dependencies. We require breadth-first expansion, as otherwise we may |
| 259 | // incorrectly use the core libraries (sanitizer runtimes, libc, libdl, etc.) |
| 260 | // from a dependency. This may cause issues when dependencies have explicit |
| 261 | // sanitizer tags, as we may get a dependency on an unsanitized libc, etc. |
| 262 | func CollectAllSharedDependencies(ctx android.SingletonContext, module android.Module, unstrippedOutputFile func(module android.Module) android.Path, isValidSharedDependency func(dependency android.Module) bool) android.Paths { |
| 263 | var fringe []android.Module |
| 264 | |
| 265 | seen := make(map[string]bool) |
| 266 | |
| 267 | // Enumerate the first level of dependencies, as we discard all non-library |
| 268 | // modules in the BFS loop below. |
| 269 | ctx.VisitDirectDeps(module, func(dep android.Module) { |
| 270 | if isValidSharedDependency(dep) { |
| 271 | fringe = append(fringe, dep) |
| 272 | } |
| 273 | }) |
| 274 | |
| 275 | var sharedLibraries android.Paths |
| 276 | |
| 277 | for i := 0; i < len(fringe); i++ { |
| 278 | module := fringe[i] |
| 279 | if seen[module.Name()] { |
| 280 | continue |
| 281 | } |
| 282 | seen[module.Name()] = true |
| 283 | |
| 284 | sharedLibraries = append(sharedLibraries, unstrippedOutputFile(module)) |
| 285 | ctx.VisitDirectDeps(module, func(dep android.Module) { |
| 286 | if isValidSharedDependency(dep) && !seen[dep.Name()] { |
| 287 | fringe = append(fringe, dep) |
| 288 | } |
| 289 | }) |
| 290 | } |
| 291 | |
| 292 | return sharedLibraries |
| 293 | } |