blob: d7e7ddfff63a855da76dec3ab3b13072fdce26ef [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"
hamzehc651b522021-04-29 12:50:47 -070019
Ivan Lozano6cd99e62020-02-11 08:24:25 -050020 "android/soong/android"
21 "android/soong/cc"
hamzehc0a671f2021-07-22 12:05:08 -070022 "android/soong/fuzz"
Ivan Lozano6cd99e62020-02-11 08:24:25 -050023 "android/soong/rust/config"
24)
25
26func init() {
27 android.RegisterModuleType("rust_fuzz", RustFuzzFactory)
28}
29
30type fuzzDecorator struct {
31 *binaryDecorator
32
Ivan Lozano0f9963e2023-02-06 13:31:02 -050033 fuzzPackagedModule fuzz.FuzzPackagedModule
34 sharedLibraries android.Paths
35 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
46func 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
61func (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.
Ivan Lozano6cd99e62020-02-11 08:24:25 -050065 flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/lib`)
66
Ivan Lozano0f9963e2023-02-06 13:31:02 -050067 if ctx.InstallInVendor() {
68 flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../../lib`)
69 } else {
70 flags.LinkFlags = append(flags.LinkFlags, `-Wl,-rpath,\$$ORIGIN/../lib`)
71
72 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -050073 return flags
74}
75
76func (fuzzer *fuzzDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
Liz Kammer9c210862021-04-12 18:52:29 -040077 if libFuzzerRuntimeLibrary := config.LibFuzzerRuntimeLibrary(ctx.toolchain()); libFuzzerRuntimeLibrary != "" {
78 deps.StaticLibs = append(deps.StaticLibs, libFuzzerRuntimeLibrary)
79 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -050080 deps.SharedLibs = append(deps.SharedLibs, "libc++")
81 deps.Rlibs = append(deps.Rlibs, "liblibfuzzer_sys")
82
83 deps = fuzzer.binaryDecorator.compilerDeps(ctx, deps)
84
85 return deps
86}
87
88func (fuzzer *fuzzDecorator) compilerProps() []interface{} {
89 return append(fuzzer.binaryDecorator.compilerProps(),
hamzeh41ad8812021-07-07 14:00:07 -070090 &fuzzer.fuzzPackagedModule.FuzzProperties)
Ivan Lozano6cd99e62020-02-11 08:24:25 -050091}
92
Colin Cross31d89b42022-10-04 16:35:39 -070093func (fuzzer *fuzzDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Colin Cross31d89b42022-10-04 16:35:39 -070094
Ivan Lozano0f9963e2023-02-06 13:31:02 -050095 out := fuzzer.binaryDecorator.compile(ctx, flags, deps)
Colin Cross31d89b42022-10-04 16:35:39 -070096
97 return out
98}
99
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500100func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage {
101 return RlibLinkage
102}
103
Liz Kammer356f7d42021-01-26 09:18:53 -0500104func (fuzzer *fuzzDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500105 return rlibAutoDep
106}
hamzehc651b522021-04-29 12:50:47 -0700107
hamzehc651b522021-04-29 12:50:47 -0700108func (fuzz *fuzzDecorator) install(ctx ModuleContext) {
109 fuzz.binaryDecorator.baseCompiler.dir = filepath.Join(
110 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
111 fuzz.binaryDecorator.baseCompiler.dir64 = filepath.Join(
112 "fuzz", ctx.Target().Arch.ArchType.String(), ctx.ModuleName())
113 fuzz.binaryDecorator.baseCompiler.install(ctx)
114
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500115 fuzz.fuzzPackagedModule = cc.PackageFuzzModule(ctx, fuzz.fuzzPackagedModule, pctx)
116
117 installBase := "fuzz"
118
119 // Grab the list of required shared libraries.
120 fuzz.sharedLibraries, _ = cc.CollectAllSharedDependencies(ctx)
121
122 for _, lib := range fuzz.sharedLibraries {
123 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
124 cc.SharedLibraryInstallLocation(
125 lib, ctx.Host(), installBase, ctx.Arch().ArchType.String()))
126
127 // Also add the dependency on the shared library symbols dir.
128 if !ctx.Host() {
129 fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
130 cc.SharedLibrarySymbolsInstallLocation(lib, installBase, ctx.Arch().ArchType.String()))
131 }
hamzehc651b522021-04-29 12:50:47 -0700132 }
133}