Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 1 | // 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/cc" |
| 20 | "android/soong/rust/config" |
| 21 | ) |
| 22 | |
| 23 | func init() { |
| 24 | android.RegisterModuleType("rust_fuzz", RustFuzzFactory) |
| 25 | } |
| 26 | |
| 27 | type fuzzDecorator struct { |
| 28 | *binaryDecorator |
| 29 | |
| 30 | Properties cc.FuzzProperties |
| 31 | dictionary android.Path |
| 32 | corpus android.Paths |
| 33 | corpusIntermediateDir android.Path |
| 34 | config android.Path |
| 35 | data android.Paths |
| 36 | dataIntermediateDir android.Path |
| 37 | } |
| 38 | |
| 39 | var _ compiler = (*binaryDecorator)(nil) |
| 40 | |
| 41 | // rust_binary produces a binary that is runnable on a device. |
| 42 | func RustFuzzFactory() android.Module { |
| 43 | module, _ := NewRustFuzz(android.HostAndDeviceSupported) |
| 44 | return module.Init() |
| 45 | } |
| 46 | |
| 47 | func 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 | |
| 62 | func (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 | |
| 72 | func (fuzzer *fuzzDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
| 73 | deps.StaticLibs = append(deps.StaticLibs, |
| 74 | config.LibFuzzerRuntimeLibrary(ctx.toolchain())) |
| 75 | deps.SharedLibs = append(deps.SharedLibs, |
| 76 | config.LibclangRuntimeLibrary(ctx.toolchain(), "asan")) |
| 77 | 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 | |
| 85 | func (fuzzer *fuzzDecorator) compilerProps() []interface{} { |
| 86 | return append(fuzzer.binaryDecorator.compilerProps(), |
| 87 | &fuzzer.Properties) |
| 88 | } |
| 89 | |
| 90 | func (fuzzer *fuzzDecorator) stdLinkage(ctx *depsContext) RustLinkage { |
| 91 | return RlibLinkage |
| 92 | } |
| 93 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame^] | 94 | func (fuzzer *fuzzDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 95 | return rlibAutoDep |
| 96 | } |