blob: da111b59e538d20b2c5059c9cddf01aa7d864d7d [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 (
18 "android/soong/android"
19 "android/soong/cc"
hamzehc0a671f2021-07-22 12:05:08 -070020 "android/soong/fuzz"
Ivan Lozano6cd99e62020-02-11 08:24:25 -050021 "android/soong/rust/config"
Colin Cross5c1d5fb2023-11-15 12:39:40 -080022 "path/filepath"
Cole Faust0e0d7492024-04-11 17:43:00 -070023
24 "github.com/google/blueprint/proptools"
Ivan Lozano6cd99e62020-02-11 08:24:25 -050025)
26
27func init() {
28 android.RegisterModuleType("rust_fuzz", RustFuzzFactory)
Ivan Lozano2fcbffa2023-07-27 10:40:52 -040029 android.RegisterModuleType("rust_fuzz_host", RustFuzzHostFactory)
Ivan Lozano6cd99e62020-02-11 08:24:25 -050030}
31
32type fuzzDecorator struct {
33 *binaryDecorator
34
Ivan Lozano0f9963e2023-02-06 13:31:02 -050035 fuzzPackagedModule fuzz.FuzzPackagedModule
Hamzeh Zawawy38917492023-04-05 22:08:46 +000036 sharedLibraries android.RuleBuilderInstalls
Ivan Lozano0f9963e2023-02-06 13:31:02 -050037 installedSharedDeps []string
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
Ivan Lozano2fcbffa2023-07-27 10:40:52 -040048func RustFuzzHostFactory() android.Module {
49 module, _ := NewRustFuzz(android.HostSupported)
50 return module.Init()
51}
52
Ivan Lozano6cd99e62020-02-11 08:24:25 -050053func NewRustFuzz(hod android.HostOrDeviceSupported) (*Module, *fuzzDecorator) {
54 module, binary := NewRustBinary(hod)
55 fuzz := &fuzzDecorator{
56 binaryDecorator: binary,
57 }
58
59 // Change the defaults for the binaryDecorator's baseCompiler
60 fuzz.binaryDecorator.baseCompiler.dir = "fuzz"
61 fuzz.binaryDecorator.baseCompiler.dir64 = "fuzz"
62 fuzz.binaryDecorator.baseCompiler.location = InstallInData
63 module.sanitize.SetSanitizer(cc.Fuzzer, true)
Ivan Lozano5467a392023-08-23 14:20:25 -040064
65 // The fuzzer runtime is not present for darwin or bionic host modules, so disable rust_fuzz modules for these.
66 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
67
68 extraProps := struct {
Cole Faust0e0d7492024-04-11 17:43:00 -070069 Enabled proptools.Configurable[bool]
70 }{
71 Enabled: android.CreateSelectOsToBool(map[string]*bool{
72 "": nil,
73 "darwin": proptools.BoolPtr(false),
74 "linux_bionic": proptools.BoolPtr(false),
75 }),
76 }
Ivan Lozano5467a392023-08-23 14:20:25 -040077 ctx.AppendProperties(&extraProps)
78 })
79
Ivan Lozano6cd99e62020-02-11 08:24:25 -050080 module.compiler = fuzz
81 return module, fuzz
82}
83
84func (fuzzer *fuzzDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
85 flags = fuzzer.binaryDecorator.compilerFlags(ctx, flags)
86
87 // `../lib` for installed fuzz targets (both host and device), and `./lib` for fuzz target packages.
Ivan Lozano6cd99e62020-02-11 08:24:25 -050088 flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
89
Ivan Lozano0f9963e2023-02-06 13:31:02 -050090 if ctx.InstallInVendor() {
91 flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../../lib`)
92 } else {
93 flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
94
95 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -050096 return flags
97}
98
99func (fuzzer *fuzzDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
Liz Kammer9c210862021-04-12 18:52:29 -0400100 if libFuzzerRuntimeLibrary := config.LibFuzzerRuntimeLibrary(ctx.toolchain()); libFuzzerRuntimeLibrary != "" {
101 deps.StaticLibs = append(deps.StaticLibs, libFuzzerRuntimeLibrary)
102 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500103 deps.SharedLibs = append(deps.SharedLibs, "libc++")
104 deps.Rlibs = append(deps.Rlibs, "liblibfuzzer_sys")
105
106 deps = fuzzer.binaryDecorator.compilerDeps(ctx, deps)
107
108 return deps
109}
110
111func (fuzzer *fuzzDecorator) compilerProps() []interface{} {
112 return append(fuzzer.binaryDecorator.compilerProps(),
hamzeh41ad8812021-07-07 14:00:07 -0700113 &fuzzer.fuzzPackagedModule.FuzzProperties)
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500114}
115
Colin Cross31d89b42022-10-04 16:35:39 -0700116func (fuzzer *fuzzDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Colin Cross31d89b42022-10-04 16:35:39 -0700117
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500118 out := fuzzer.binaryDecorator.compile(ctx, flags, deps)
Colin Cross31d89b42022-10-04 16:35:39 -0700119
120 return out
121}
122
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500123func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage {
124 return RlibLinkage
125}
126
Liz Kammer356f7d42021-01-26 09:18:53 -0500127func (fuzzer *fuzzDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500128 return rlibAutoDep
129}
hamzehc651b522021-04-29 12:50:47 -0700130
hamzehc651b522021-04-29 12:50:47 -0700131func (fuzz *fuzzDecorator) install(ctx ModuleContext) {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500132 fuzz.fuzzPackagedModule = cc.PackageFuzzModule(ctx, fuzz.fuzzPackagedModule, pctx)
133
134 installBase := "fuzz"
135
136 // Grab the list of required shared libraries.
137 fuzz.sharedLibraries, _ = cc.CollectAllSharedDependencies(ctx)
138
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000139 for _, ruleBuilderInstall := range fuzz.sharedLibraries {
140 install := ruleBuilderInstall.To
141
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500142 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
143 cc.SharedLibraryInstallLocation(
Steven Morelandd86fec52023-12-28 01:09:40 +0000144 install, ctx.Host(), ctx.InstallInVendor(), installBase, ctx.Arch().ArchType.String()))
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500145
146 // Also add the dependency on the shared library symbols dir.
147 if !ctx.Host() {
148 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
Steven Morelandd86fec52023-12-28 01:09:40 +0000149 cc.SharedLibrarySymbolsInstallLocation(install, ctx.InstallInVendor(), installBase, ctx.Arch().ArchType.String()))
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500150 }
hamzehc651b522021-04-29 12:50:47 -0700151 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800152
153 var fuzzData []android.DataPath
154 for _, d := range fuzz.fuzzPackagedModule.Corpus {
155 fuzzData = append(fuzzData, android.DataPath{SrcPath: d, RelativeInstallPath: "corpus", WithoutRel: true})
156 }
157
158 for _, d := range fuzz.fuzzPackagedModule.Data {
159 fuzzData = append(fuzzData, android.DataPath{SrcPath: d, RelativeInstallPath: "data"})
160 }
161
162 if d := fuzz.fuzzPackagedModule.Dictionary; d != nil {
163 fuzzData = append(fuzzData, android.DataPath{SrcPath: d, WithoutRel: true})
164 }
165
166 if d := fuzz.fuzzPackagedModule.Config; d != nil {
167 fuzzData = append(fuzzData, android.DataPath{SrcPath: d, WithoutRel: true})
168 }
169
170 fuzz.binaryDecorator.baseCompiler.dir = filepath.Join(
171 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
172 fuzz.binaryDecorator.baseCompiler.dir64 = filepath.Join(
173 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
174 fuzz.binaryDecorator.baseCompiler.installTestData(ctx, fuzzData)
175
176 fuzz.binaryDecorator.baseCompiler.install(ctx)
177
hamzehc651b522021-04-29 12:50:47 -0700178}