blob: 1770d2e655936a694b361bb479aff33d34c4e393 [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"
Ivan Lozano6cd99e62020-02-11 08:24:25 -050023)
24
25func init() {
26 android.RegisterModuleType("rust_fuzz", RustFuzzFactory)
Ivan Lozano2fcbffa2023-07-27 10:40:52 -040027 android.RegisterModuleType("rust_fuzz_host", RustFuzzHostFactory)
Ivan Lozano6cd99e62020-02-11 08:24:25 -050028}
29
30type fuzzDecorator struct {
31 *binaryDecorator
32
Ivan Lozano0f9963e2023-02-06 13:31:02 -050033 fuzzPackagedModule fuzz.FuzzPackagedModule
Hamzeh Zawawy38917492023-04-05 22:08:46 +000034 sharedLibraries android.RuleBuilderInstalls
Ivan Lozano0f9963e2023-02-06 13:31:02 -050035 installedSharedDeps []string
Ivan Lozano6cd99e62020-02-11 08:24:25 -050036}
37
Ivan Lozano5482d6a2021-11-01 10:13:25 -040038var _ compiler = (*fuzzDecorator)(nil)
Ivan Lozano6cd99e62020-02-11 08:24:25 -050039
40// rust_binary produces a binary that is runnable on a device.
41func RustFuzzFactory() android.Module {
42 module, _ := NewRustFuzz(android.HostAndDeviceSupported)
43 return module.Init()
44}
45
Ivan Lozano2fcbffa2023-07-27 10:40:52 -040046func RustFuzzHostFactory() android.Module {
47 module, _ := NewRustFuzz(android.HostSupported)
48 return module.Init()
49}
50
Ivan Lozano6cd99e62020-02-11 08:24:25 -050051func NewRustFuzz(hod android.HostOrDeviceSupported) (*Module, *fuzzDecorator) {
52 module, binary := NewRustBinary(hod)
53 fuzz := &fuzzDecorator{
54 binaryDecorator: binary,
55 }
56
57 // Change the defaults for the binaryDecorator's baseCompiler
58 fuzz.binaryDecorator.baseCompiler.dir = "fuzz"
59 fuzz.binaryDecorator.baseCompiler.dir64 = "fuzz"
60 fuzz.binaryDecorator.baseCompiler.location = InstallInData
61 module.sanitize.SetSanitizer(cc.Fuzzer, true)
Ivan Lozano5467a392023-08-23 14:20:25 -040062
63 // The fuzzer runtime is not present for darwin or bionic host modules, so disable rust_fuzz modules for these.
64 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
65
66 extraProps := struct {
Priyanka Advanied5276f2024-05-01 20:02:36 +000067 Target struct {
68 Darwin struct {
69 Enabled *bool
70 }
71 Linux_bionic struct {
72 Enabled *bool
73 }
74 }
75 }{}
76 extraProps.Target.Darwin.Enabled = cc.BoolPtr(false)
77 extraProps.Target.Linux_bionic.Enabled = cc.BoolPtr(false)
Ivan Lozano5467a392023-08-23 14:20:25 -040078 ctx.AppendProperties(&extraProps)
79 })
80
Ivan Lozano6cd99e62020-02-11 08:24:25 -050081 module.compiler = fuzz
82 return module, fuzz
83}
84
85func (fuzzer *fuzzDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
86 flags = fuzzer.binaryDecorator.compilerFlags(ctx, flags)
87
88 // `../lib` for installed fuzz targets (both host and device), and `./lib` for fuzz target packages.
Ivan Lozano6cd99e62020-02-11 08:24:25 -050089 flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
90
Ivan Lozano0f9963e2023-02-06 13:31:02 -050091 if ctx.InstallInVendor() {
92 flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../../lib`)
93 } else {
94 flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
95
96 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -050097 return flags
98}
99
100func (fuzzer *fuzzDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
Liz Kammer9c210862021-04-12 18:52:29 -0400101 if libFuzzerRuntimeLibrary := config.LibFuzzerRuntimeLibrary(ctx.toolchain()); libFuzzerRuntimeLibrary != "" {
102 deps.StaticLibs = append(deps.StaticLibs, libFuzzerRuntimeLibrary)
103 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500104 deps.SharedLibs = append(deps.SharedLibs, "libc++")
105 deps.Rlibs = append(deps.Rlibs, "liblibfuzzer_sys")
106
107 deps = fuzzer.binaryDecorator.compilerDeps(ctx, deps)
108
109 return deps
110}
111
112func (fuzzer *fuzzDecorator) compilerProps() []interface{} {
113 return append(fuzzer.binaryDecorator.compilerProps(),
hamzeh41ad8812021-07-07 14:00:07 -0700114 &fuzzer.fuzzPackagedModule.FuzzProperties)
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500115}
116
Colin Cross31d89b42022-10-04 16:35:39 -0700117func (fuzzer *fuzzDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Colin Cross31d89b42022-10-04 16:35:39 -0700118
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500119 out := fuzzer.binaryDecorator.compile(ctx, flags, deps)
Colin Cross31d89b42022-10-04 16:35:39 -0700120
121 return out
122}
123
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500124func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage {
125 return RlibLinkage
126}
127
Liz Kammer356f7d42021-01-26 09:18:53 -0500128func (fuzzer *fuzzDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500129 return rlibAutoDep
130}
hamzehc651b522021-04-29 12:50:47 -0700131
hamzehc651b522021-04-29 12:50:47 -0700132func (fuzz *fuzzDecorator) install(ctx ModuleContext) {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500133 fuzz.fuzzPackagedModule = cc.PackageFuzzModule(ctx, fuzz.fuzzPackagedModule, pctx)
134
135 installBase := "fuzz"
136
137 // Grab the list of required shared libraries.
138 fuzz.sharedLibraries, _ = cc.CollectAllSharedDependencies(ctx)
139
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000140 for _, ruleBuilderInstall := range fuzz.sharedLibraries {
141 install := ruleBuilderInstall.To
142
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500143 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
144 cc.SharedLibraryInstallLocation(
Steven Morelandd86fec52023-12-28 01:09:40 +0000145 install, ctx.Host(), ctx.InstallInVendor(), installBase, ctx.Arch().ArchType.String()))
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500146
147 // Also add the dependency on the shared library symbols dir.
148 if !ctx.Host() {
149 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
Steven Morelandd86fec52023-12-28 01:09:40 +0000150 cc.SharedLibrarySymbolsInstallLocation(install, ctx.InstallInVendor(), installBase, ctx.Arch().ArchType.String()))
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500151 }
hamzehc651b522021-04-29 12:50:47 -0700152 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800153
154 var fuzzData []android.DataPath
155 for _, d := range fuzz.fuzzPackagedModule.Corpus {
156 fuzzData = append(fuzzData, android.DataPath{SrcPath: d, RelativeInstallPath: "corpus", WithoutRel: true})
157 }
158
159 for _, d := range fuzz.fuzzPackagedModule.Data {
160 fuzzData = append(fuzzData, android.DataPath{SrcPath: d, RelativeInstallPath: "data"})
161 }
162
163 if d := fuzz.fuzzPackagedModule.Dictionary; d != nil {
164 fuzzData = append(fuzzData, android.DataPath{SrcPath: d, WithoutRel: true})
165 }
166
167 if d := fuzz.fuzzPackagedModule.Config; d != nil {
168 fuzzData = append(fuzzData, android.DataPath{SrcPath: d, WithoutRel: true})
169 }
170
171 fuzz.binaryDecorator.baseCompiler.dir = filepath.Join(
172 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
173 fuzz.binaryDecorator.baseCompiler.dir64 = filepath.Join(
174 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
175 fuzz.binaryDecorator.baseCompiler.installTestData(ctx, fuzzData)
176
177 fuzz.binaryDecorator.baseCompiler.install(ctx)
178
hamzehc651b522021-04-29 12:50:47 -0700179}