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