Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 1 | // Copyright 2020 The Android Open Source Project |
| 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 rust |
| 16 | |
| 17 | import ( |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 18 | "path/filepath" |
| 19 | "sort" |
| 20 | "strings" |
| 21 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 22 | "android/soong/android" |
| 23 | "android/soong/cc" |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame^] | 24 | "android/soong/fuzz" |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 25 | "android/soong/rust/config" |
| 26 | ) |
| 27 | |
| 28 | func init() { |
| 29 | android.RegisterModuleType("rust_fuzz", RustFuzzFactory) |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 30 | android.RegisterSingletonType("rust_fuzz_packaging", rustFuzzPackagingFactory) |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | type fuzzDecorator struct { |
| 34 | *binaryDecorator |
| 35 | |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame^] | 36 | fuzzPackagedModule fuzz.FuzzPackagedModule |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | var _ compiler = (*binaryDecorator)(nil) |
| 40 | |
| 41 | // rust_binary produces a binary that is runnable on a device. |
| 42 | func RustFuzzFactory() android.Module { |
| 43 | module, _ := NewRustFuzz(android.HostAndDeviceSupported) |
| 44 | return module.Init() |
| 45 | } |
| 46 | |
| 47 | func NewRustFuzz(hod android.HostOrDeviceSupported) (*Module, *fuzzDecorator) { |
| 48 | module, binary := NewRustBinary(hod) |
| 49 | fuzz := &fuzzDecorator{ |
| 50 | binaryDecorator: binary, |
| 51 | } |
| 52 | |
| 53 | // Change the defaults for the binaryDecorator's baseCompiler |
| 54 | fuzz.binaryDecorator.baseCompiler.dir = "fuzz" |
| 55 | fuzz.binaryDecorator.baseCompiler.dir64 = "fuzz" |
| 56 | fuzz.binaryDecorator.baseCompiler.location = InstallInData |
| 57 | module.sanitize.SetSanitizer(cc.Fuzzer, true) |
| 58 | module.compiler = fuzz |
| 59 | return module, fuzz |
| 60 | } |
| 61 | |
| 62 | func (fuzzer *fuzzDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
| 63 | flags = fuzzer.binaryDecorator.compilerFlags(ctx, flags) |
| 64 | |
| 65 | // `../lib` for installed fuzz targets (both host and device), and `./lib` for fuzz target packages. |
| 66 | flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../lib`) |
| 67 | flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/lib`) |
| 68 | |
| 69 | return flags |
| 70 | } |
| 71 | |
| 72 | func (fuzzer *fuzzDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
Liz Kammer | 9c21086 | 2021-04-12 18:52:29 -0400 | [diff] [blame] | 73 | if libFuzzerRuntimeLibrary := config.LibFuzzerRuntimeLibrary(ctx.toolchain()); libFuzzerRuntimeLibrary != "" { |
| 74 | deps.StaticLibs = append(deps.StaticLibs, libFuzzerRuntimeLibrary) |
| 75 | } |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 76 | deps.SharedLibs = append(deps.SharedLibs, "libc++") |
| 77 | deps.Rlibs = append(deps.Rlibs, "liblibfuzzer_sys") |
| 78 | |
| 79 | deps = fuzzer.binaryDecorator.compilerDeps(ctx, deps) |
| 80 | |
| 81 | return deps |
| 82 | } |
| 83 | |
| 84 | func (fuzzer *fuzzDecorator) compilerProps() []interface{} { |
| 85 | return append(fuzzer.binaryDecorator.compilerProps(), |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 86 | &fuzzer.fuzzPackagedModule.FuzzProperties) |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage { |
| 90 | return RlibLinkage |
| 91 | } |
| 92 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 93 | func (fuzzer *fuzzDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 94 | return rlibAutoDep |
| 95 | } |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 96 | |
| 97 | // Responsible for generating GNU Make rules that package fuzz targets into |
| 98 | // their architecture & target/host specific zip file. |
| 99 | type rustFuzzPackager struct { |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame^] | 100 | fuzz.FuzzPackager |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | func rustFuzzPackagingFactory() android.Singleton { |
| 104 | return &rustFuzzPackager{} |
| 105 | } |
| 106 | |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 107 | func (s *rustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) { |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 108 | // Map between each architecture + host/device combination. |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame^] | 109 | archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip) |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 110 | |
| 111 | // List of individual fuzz targets. |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 112 | s.FuzzTargets = make(map[string]bool) |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 113 | |
| 114 | ctx.VisitAllModules(func(module android.Module) { |
| 115 | // Discard non-fuzz targets. |
| 116 | rustModule, ok := module.(*Module) |
| 117 | if !ok { |
| 118 | return |
| 119 | } |
| 120 | |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame^] | 121 | if ok := fuzz.IsValid(rustModule.FuzzModule); !ok || rustModule.Properties.PreventInstall { |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 122 | return |
| 123 | } |
| 124 | |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 125 | fuzzModule, ok := rustModule.compiler.(*fuzzDecorator) |
| 126 | if !ok { |
| 127 | return |
| 128 | } |
| 129 | |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 130 | hostOrTargetString := "target" |
| 131 | if rustModule.Host() { |
| 132 | hostOrTargetString = "host" |
| 133 | } |
| 134 | |
| 135 | archString := rustModule.Arch().ArchType.String() |
| 136 | archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString) |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame^] | 137 | archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()} |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 138 | |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame^] | 139 | var files []fuzz.FileToZip |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 140 | builder := android.NewRuleBuilder(pctx, ctx) |
| 141 | |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 142 | // Package the artifacts (data, corpus, config and dictionary into a zipfile. |
| 143 | files = s.PackageArtifacts(ctx, module, fuzzModule.fuzzPackagedModule, archDir, builder) |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 144 | |
| 145 | // The executable. |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame^] | 146 | files = append(files, fuzz.FileToZip{rustModule.unstrippedOutputFile.Path(), ""}) |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 147 | |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 148 | archDirs[archOs], ok = s.BuildZipFile(ctx, module, fuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs) |
| 149 | if !ok { |
| 150 | return |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 151 | } |
| 152 | |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 153 | }) |
hamzeh | c0a671f | 2021-07-22 12:05:08 -0700 | [diff] [blame^] | 154 | s.CreateFuzzPackage(ctx, archDirs, fuzz.Rust, pctx) |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | func (s *rustFuzzPackager) MakeVars(ctx android.MakeVarsContext) { |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 158 | packages := s.Packages.Strings() |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 159 | sort.Strings(packages) |
| 160 | |
| 161 | ctx.Strict("SOONG_RUST_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " ")) |
| 162 | |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 163 | // Preallocate the slice of fuzz targets to minimize memory allocations. |
| 164 | s.PreallocateSlice(ctx, "ALL_RUST_FUZZ_TARGETS") |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | func (fuzz *fuzzDecorator) install(ctx ModuleContext) { |
| 168 | fuzz.binaryDecorator.baseCompiler.dir = filepath.Join( |
| 169 | "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
| 170 | fuzz.binaryDecorator.baseCompiler.dir64 = filepath.Join( |
| 171 | "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName()) |
| 172 | fuzz.binaryDecorator.baseCompiler.install(ctx) |
| 173 | |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 174 | if fuzz.fuzzPackagedModule.FuzzProperties.Corpus != nil { |
| 175 | fuzz.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Corpus) |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 176 | } |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 177 | if fuzz.fuzzPackagedModule.FuzzProperties.Data != nil { |
| 178 | fuzz.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Data) |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 179 | } |
hamzeh | 41ad881 | 2021-07-07 14:00:07 -0700 | [diff] [blame] | 180 | if fuzz.fuzzPackagedModule.FuzzProperties.Dictionary != nil { |
| 181 | fuzz.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzz.fuzzPackagedModule.FuzzProperties.Dictionary) |
hamzeh | c651b52 | 2021-04-29 12:50:47 -0700 | [diff] [blame] | 182 | } |
| 183 | } |