blob: 55921ba4bc87e9fb37a305ba3003f06780a38587 [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
Ivan Lozano6cd99e62020-02-11 08:24:25 -050037}
38
Ivan Lozano5482d6a2021-11-01 10:13:25 -040039var _ compiler = (*fuzzDecorator)(nil)
Ivan Lozano6cd99e62020-02-11 08:24:25 -050040
41// rust_binary produces a binary that is runnable on a device.
42func RustFuzzFactory() android.Module {
43 module, _ := NewRustFuzz(android.HostAndDeviceSupported)
44 return module.Init()
45}
46
47func 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
62func (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
72func (fuzzer *fuzzDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
Liz Kammer9c210862021-04-12 18:52:29 -040073 if libFuzzerRuntimeLibrary := config.LibFuzzerRuntimeLibrary(ctx.toolchain()); libFuzzerRuntimeLibrary != "" {
74 deps.StaticLibs = append(deps.StaticLibs, libFuzzerRuntimeLibrary)
75 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -050076 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
84func (fuzzer *fuzzDecorator) compilerProps() []interface{} {
85 return append(fuzzer.binaryDecorator.compilerProps(),
hamzeh41ad8812021-07-07 14:00:07 -070086 &fuzzer.fuzzPackagedModule.FuzzProperties)
Ivan Lozano6cd99e62020-02-11 08:24:25 -050087}
88
89func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage {
90 return RlibLinkage
91}
92
Liz Kammer356f7d42021-01-26 09:18:53 -050093func (fuzzer *fuzzDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano6cd99e62020-02-11 08:24:25 -050094 return rlibAutoDep
95}
hamzehc651b522021-04-29 12:50:47 -070096
97// Responsible for generating GNU Make rules that package fuzz targets into
98// their architecture & target/host specific zip file.
99type rustFuzzPackager struct {
hamzehc0a671f2021-07-22 12:05:08 -0700100 fuzz.FuzzPackager
hamzehc651b522021-04-29 12:50:47 -0700101}
102
103func rustFuzzPackagingFactory() android.Singleton {
104 return &rustFuzzPackager{}
105}
106
hamzehc651b522021-04-29 12:50:47 -0700107func (s *rustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext) {
hamzehc651b522021-04-29 12:50:47 -0700108 // Map between each architecture + host/device combination.
hamzehc0a671f2021-07-22 12:05:08 -0700109 archDirs := make(map[fuzz.ArchOs][]fuzz.FileToZip)
hamzehc651b522021-04-29 12:50:47 -0700110
111 // List of individual fuzz targets.
hamzeh41ad8812021-07-07 14:00:07 -0700112 s.FuzzTargets = make(map[string]bool)
hamzehc651b522021-04-29 12:50:47 -0700113
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400114 // Map tracking whether each shared library has an install rule to avoid duplicate install rules from
115 // multiple fuzzers that depend on the same shared library.
116 sharedLibraryInstalled := make(map[string]bool)
117
hamzehc651b522021-04-29 12:50:47 -0700118 ctx.VisitAllModules(func(module android.Module) {
119 // Discard non-fuzz targets.
120 rustModule, ok := module.(*Module)
121 if !ok {
122 return
123 }
124
hamzehc0a671f2021-07-22 12:05:08 -0700125 if ok := fuzz.IsValid(rustModule.FuzzModule); !ok || rustModule.Properties.PreventInstall {
hamzeh41ad8812021-07-07 14:00:07 -0700126 return
127 }
128
hamzehc651b522021-04-29 12:50:47 -0700129 fuzzModule, ok := rustModule.compiler.(*fuzzDecorator)
130 if !ok {
131 return
132 }
133
hamzehc651b522021-04-29 12:50:47 -0700134 hostOrTargetString := "target"
135 if rustModule.Host() {
136 hostOrTargetString = "host"
137 }
138
139 archString := rustModule.Arch().ArchType.String()
140 archDir := android.PathForIntermediates(ctx, "fuzz", hostOrTargetString, archString)
hamzehc0a671f2021-07-22 12:05:08 -0700141 archOs := fuzz.ArchOs{HostOrTarget: hostOrTargetString, Arch: archString, Dir: archDir.String()}
hamzehc651b522021-04-29 12:50:47 -0700142
hamzehc0a671f2021-07-22 12:05:08 -0700143 var files []fuzz.FileToZip
hamzehc651b522021-04-29 12:50:47 -0700144 builder := android.NewRuleBuilder(pctx, ctx)
145
hamzeh41ad8812021-07-07 14:00:07 -0700146 // Package the artifacts (data, corpus, config and dictionary into a zipfile.
147 files = s.PackageArtifacts(ctx, module, fuzzModule.fuzzPackagedModule, archDir, builder)
hamzehc651b522021-04-29 12:50:47 -0700148
149 // The executable.
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400150 files = append(files, fuzz.FileToZip{rustModule.UnstrippedOutputFile(), ""})
hamzehc651b522021-04-29 12:50:47 -0700151
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400152 // Grab the list of required shared libraries.
153 sharedLibraries := fuzz.CollectAllSharedDependencies(ctx, module, cc.UnstrippedOutputFile, cc.IsValidSharedDependency)
154
155 // Package shared libraries
156 files = append(files, cc.GetSharedLibsToZip(sharedLibraries, rustModule, &s.FuzzPackager, archString, &sharedLibraryInstalled)...)
157
hamzeh41ad8812021-07-07 14:00:07 -0700158 archDirs[archOs], ok = s.BuildZipFile(ctx, module, fuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
159 if !ok {
160 return
hamzehc651b522021-04-29 12:50:47 -0700161 }
162
hamzehc651b522021-04-29 12:50:47 -0700163 })
hamzehc0a671f2021-07-22 12:05:08 -0700164 s.CreateFuzzPackage(ctx, archDirs, fuzz.Rust, pctx)
hamzehc651b522021-04-29 12:50:47 -0700165}
166
167func (s *rustFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
hamzeh41ad8812021-07-07 14:00:07 -0700168 packages := s.Packages.Strings()
hamzehc651b522021-04-29 12:50:47 -0700169 sort.Strings(packages)
170
171 ctx.Strict("SOONG_RUST_FUZZ_PACKAGING_ARCH_MODULES", strings.Join(packages, " "))
172
hamzeh41ad8812021-07-07 14:00:07 -0700173 // Preallocate the slice of fuzz targets to minimize memory allocations.
174 s.PreallocateSlice(ctx, "ALL_RUST_FUZZ_TARGETS")
hamzehc651b522021-04-29 12:50:47 -0700175}
176
177func (fuzz *fuzzDecorator) install(ctx ModuleContext) {
178 fuzz.binaryDecorator.baseCompiler.dir = filepath.Join(
179 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
180 fuzz.binaryDecorator.baseCompiler.dir64 = filepath.Join(
181 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
182 fuzz.binaryDecorator.baseCompiler.install(ctx)
183
hamzeh41ad8812021-07-07 14:00:07 -0700184 if fuzz.fuzzPackagedModule.FuzzProperties.Corpus != nil {
185 fuzz.fuzzPackagedModule.Corpus = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Corpus)
hamzehc651b522021-04-29 12:50:47 -0700186 }
hamzeh41ad8812021-07-07 14:00:07 -0700187 if fuzz.fuzzPackagedModule.FuzzProperties.Data != nil {
188 fuzz.fuzzPackagedModule.Data = android.PathsForModuleSrc(ctx, fuzz.fuzzPackagedModule.FuzzProperties.Data)
hamzehc651b522021-04-29 12:50:47 -0700189 }
hamzeh41ad8812021-07-07 14:00:07 -0700190 if fuzz.fuzzPackagedModule.FuzzProperties.Dictionary != nil {
191 fuzz.fuzzPackagedModule.Dictionary = android.PathForModuleSrc(ctx, *fuzz.fuzzPackagedModule.FuzzProperties.Dictionary)
hamzehc651b522021-04-29 12:50:47 -0700192 }
193}