blob: 6faf55cf602d9006008aab7b2d2e9d89183f63b8 [file] [log] [blame]
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001// 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
15package rust
16
17import (
hamzehc651b522021-04-29 12:50:47 -070018 "path/filepath"
19 "sort"
20 "strings"
21
Ivan Lozano6cd99e62020-02-11 08:24:25 -050022 "android/soong/android"
23 "android/soong/cc"
hamzehc0a671f2021-07-22 12:05:08 -070024 "android/soong/fuzz"
Ivan Lozano6cd99e62020-02-11 08:24:25 -050025 "android/soong/rust/config"
26)
27
28func init() {
29 android.RegisterModuleType("rust_fuzz", RustFuzzFactory)
hamzehc651b522021-04-29 12:50:47 -070030 android.RegisterSingletonType("rust_fuzz_packaging", rustFuzzPackagingFactory)
Ivan Lozano6cd99e62020-02-11 08:24:25 -050031}
32
33type fuzzDecorator struct {
34 *binaryDecorator
35
hamzehc0a671f2021-07-22 12:05:08 -070036 fuzzPackagedModule fuzz.FuzzPackagedModule
Colin Cross31d89b42022-10-04 16:35:39 -070037 sharedLibraries android.Paths
Ivan Lozano6cd99e62020-02-11 08:24:25 -050038}
39
Ivan Lozano5482d6a2021-11-01 10:13:25 -040040var _ compiler = (*fuzzDecorator)(nil)
Ivan Lozano6cd99e62020-02-11 08:24:25 -050041
42// rust_binary produces a binary that is runnable on a device.
43func RustFuzzFactory() android.Module {
44 module, _ := NewRustFuzz(android.HostAndDeviceSupported)
45 return module.Init()
46}
47
48func NewRustFuzz(hod android.HostOrDeviceSupported) (*Module, *fuzzDecorator) {
49 module, binary := NewRustBinary(hod)
50 fuzz := &fuzzDecorator{
51 binaryDecorator: binary,
52 }
53
54 // Change the defaults for the binaryDecorator's baseCompiler
55 fuzz.binaryDecorator.baseCompiler.dir = "fuzz"
56 fuzz.binaryDecorator.baseCompiler.dir64 = "fuzz"
57 fuzz.binaryDecorator.baseCompiler.location = InstallInData
58 module.sanitize.SetSanitizer(cc.Fuzzer, true)
59 module.compiler = fuzz
60 return module, fuzz
61}
62
63func (fuzzer *fuzzDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
64 flags = fuzzer.binaryDecorator.compilerFlags(ctx, flags)
65
66 // `../lib` for installed fuzz targets (both host and device), and `./lib` for fuzz target packages.
67 flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
68 flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
69
70 return flags
71}
72
73func (fuzzer *fuzzDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
Liz Kammer9c210862021-04-12 18:52:29 -040074 if libFuzzerRuntimeLibrary := config.LibFuzzerRuntimeLibrary(ctx.toolchain()); libFuzzerRuntimeLibrary != "" {
75 deps.StaticLibs = append(deps.StaticLibs, libFuzzerRuntimeLibrary)
76 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -050077 deps.SharedLibs = append(deps.SharedLibs, "libc++")
78 deps.Rlibs = append(deps.Rlibs, "liblibfuzzer_sys")
79
80 deps = fuzzer.binaryDecorator.compilerDeps(ctx, deps)
81
82 return deps
83}
84
85func (fuzzer *fuzzDecorator) compilerProps() []interface{} {
86 return append(fuzzer.binaryDecorator.compilerProps(),
hamzeh41ad8812021-07-07 14:00:07 -070087 &fuzzer.fuzzPackagedModule.FuzzProperties)
Ivan Lozano6cd99e62020-02-11 08:24:25 -050088}
89
Colin Cross31d89b42022-10-04 16:35:39 -070090func (fuzzer *fuzzDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
91 out := fuzzer.binaryDecorator.compile(ctx, flags, deps)
92
93 // Grab the list of required shared libraries.
Muhammad Haseeb Ahmad431ddf92022-10-20 00:55:58 +000094 fuzzer.sharedLibraries, _ = cc.CollectAllSharedDependencies(ctx)
Colin Cross31d89b42022-10-04 16:35:39 -070095
96 return out
97}
98
Ivan Lozano6cd99e62020-02-11 08:24:25 -050099func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage {
100 return RlibLinkage
101}
102
Liz Kammer356f7d42021-01-26 09:18:53 -0500103func (fuzzer *fuzzDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500104 return rlibAutoDep
105}
hamzehc651b522021-04-29 12:50:47 -0700106
107// Responsible for generating GNU Make rules that package fuzz targets into
108// their architecture & target/host specific zip file.
109type rustFuzzPackager struct {
hamzehc0a671f2021-07-22 12:05:08 -0700110 fuzz.FuzzPackager
hamzehc651b522021-04-29 12:50:47 -0700111}
112
113func rustFuzzPackagingFactory() android.Singleton {
114 return &rustFuzzPackager{}
115}
116
hamzehc651b522021-04-29 12:50:47 -0700117func (s *rustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
hamzehc651b522021-04-29 12:50:47 -0700118 // Map between each architecture + host/device combination.
hamzehc0a671f2021-07-22 12:05:08 -0700119 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
hamzehc651b522021-04-29 12:50:47 -0700120
121 // List of individual fuzz targets.
hamzeh41ad8812021-07-07 14:00:07 -0700122 s.FuzzTargets = make(map[string]bool)
hamzehc651b522021-04-29 12:50:47 -0700123
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400124 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
125 // multiple fuzzers that depend on the same shared library.
126 sharedLibraryInstalled := make(map[string]bool)
127
hamzehc651b522021-04-29 12:50:47 -0700128 ctx.VisitAllModules(func(module android.Module) {
129 // Discard non-fuzz targets.
130 rustModule, ok := module.(*Module)
131 if !ok {
132 return
133 }
134
hamzehc0a671f2021-07-22 12:05:08 -0700135 if ok := fuzz.IsValid(rustModule.FuzzModule); !ok || rustModule.Properties.PreventInstall {
hamzeh41ad8812021-07-07 14:00:07 -0700136 return
137 }
138
hamzehc651b522021-04-29 12:50:47 -0700139 fuzzModule, ok := rustModule.compiler.(*fuzzDecorator)
140 if !ok {
141 return
142 }
143
hamzehc651b522021-04-29 12:50:47 -0700144 hostOrTargetString := "target"
145 if rustModule.Host() {
146 hostOrTargetString = "host"
147 }
148
149 archString := rustModule.Arch().ArchType.String()
150 archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
hamzehc0a671f2021-07-22 12:05:08 -0700151 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
hamzehc651b522021-04-29 12:50:47 -0700152
hamzehc0a671f2021-07-22 12:05:08 -0700153 var files []fuzz.FileToZip
hamzehc651b522021-04-29 12:50:47 -0700154 builder := android.NewRuleBuilder(pctx, ctx)
155
hamzeh41ad8812021-07-07 14:00:07 -0700156 // Package the artifacts (data, corpus, config and dictionary into a zipfile.
157 files = s.PackageArtifacts(ctx, module, fuzzModule.fuzzPackagedModule, archDir, builder)
hamzehc651b522021-04-29 12:50:47 -0700158
159 // The executable.
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400160 files = append(files, fuzz.FileToZip{rustModule.UnstrippedOutputFile(), ""})
hamzehc651b522021-04-29 12:50:47 -0700161
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400162 // Package shared libraries
Colin Cross31d89b42022-10-04 16:35:39 -0700163 files = append(files, cc.GetSharedLibsToZip(fuzzModule.sharedLibraries, rustModule, &s.FuzzPackager, archString, "lib", &sharedLibraryInstalled)...)
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400164
hamzeh41ad8812021-07-07 14:00:07 -0700165 archDirs[archOs], ok = s.BuildZipFile(ctx, module, fuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
166 if !ok {
167 return
hamzehc651b522021-04-29 12:50:47 -0700168 }
169
hamzehc651b522021-04-29 12:50:47 -0700170 })
hamzehc0a671f2021-07-22 12:05:08 -0700171 s.CreateFuzzPackage(ctx, archDirs, fuzz.Rust, pctx)
hamzehc651b522021-04-29 12:50:47 -0700172}
173
174func (s *rustFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
hamzeh41ad8812021-07-07 14:00:07 -0700175 packages := s.Packages.Strings()
hamzehc651b522021-04-29 12:50:47 -0700176 sort.Strings(packages)
177
178 ctx.Strict("SOONG_RUST_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
179
hamzeh41ad8812021-07-07 14:00:07 -0700180 // Preallocate the slice of fuzz targets to minimize memory allocations.
181 s.PreallocateSlice(ctx, "ALL_RUST_FUZZ_TARGETS")
hamzehc651b522021-04-29 12:50:47 -0700182}
183
184func (fuzz *fuzzDecorator) install(ctx ModuleContext) {
185 fuzz.binaryDecorator.baseCompiler.dir = filepath.Join(
186 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
187 fuzz.binaryDecorator.baseCompiler.dir64 = filepath.Join(
188 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
189 fuzz.binaryDecorator.baseCompiler.install(ctx)
190
hamzeh41ad8812021-07-07 14:00:07 -0700191 if fuzz.fuzzPackagedModule.FuzzProperties.Corpus != nil {
192 fuzz.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Corpus)
hamzehc651b522021-04-29 12:50:47 -0700193 }
hamzeh41ad8812021-07-07 14:00:07 -0700194 if fuzz.fuzzPackagedModule.FuzzProperties.Data != nil {
195 fuzz.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Data)
hamzehc651b522021-04-29 12:50:47 -0700196 }
hamzeh41ad8812021-07-07 14:00:07 -0700197 if fuzz.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
198 fuzz.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzz.fuzzPackagedModule.FuzzProperties.Dictionary)
hamzehc651b522021-04-29 12:50:47 -0700199 }
200}