Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved. |
| 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 cc |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/cc/config" |
| 20 | ) |
| 21 | |
| 22 | func init() { |
| 23 | android.RegisterModuleType("cc_fuzz", FuzzFactory) |
| 24 | } |
| 25 | |
| 26 | // cc_fuzz creates a host/device fuzzer binary. Host binaries can be found at |
| 27 | // $ANDROID_HOST_OUT/fuzz/, and device binaries can be found at /data/fuzz on |
| 28 | // your device, or $ANDROID_PRODUCT_OUT/data/fuzz in your build tree. |
| 29 | func FuzzFactory() android.Module { |
| 30 | module := NewFuzz(android.HostAndDeviceSupported) |
| 31 | return module.Init() |
| 32 | } |
| 33 | |
| 34 | func NewFuzzInstaller() *baseInstaller { |
| 35 | return NewBaseInstaller("fuzz", "fuzz", InstallInData) |
| 36 | } |
| 37 | |
| 38 | type fuzzBinary struct { |
| 39 | *binaryDecorator |
| 40 | *baseCompiler |
| 41 | } |
| 42 | |
| 43 | func (fuzz *fuzzBinary) linkerProps() []interface{} { |
| 44 | props := fuzz.binaryDecorator.linkerProps() |
| 45 | return props |
| 46 | } |
| 47 | |
| 48 | func (fuzz *fuzzBinary) linkerInit(ctx BaseModuleContext) { |
| 49 | // Add ../lib[64] to rpath so that out/host/linux-x86/fuzz/<fuzzer> can |
| 50 | // find out/host/linux-x86/lib[64]/library.so |
| 51 | runpaths := []string{"../lib"} |
| 52 | for _, runpath := range runpaths { |
| 53 | if ctx.toolchain().Is64Bit() { |
| 54 | runpath += "64" |
| 55 | } |
| 56 | fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths = append( |
| 57 | fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths, runpath) |
| 58 | } |
| 59 | |
| 60 | // add "" to rpath so that fuzzer binaries can find libraries in their own fuzz directory |
| 61 | fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths = append( |
| 62 | fuzz.binaryDecorator.baseLinker.dynamicProperties.RunPaths, "") |
| 63 | |
| 64 | fuzz.binaryDecorator.linkerInit(ctx) |
| 65 | } |
| 66 | |
| 67 | func (fuzz *fuzzBinary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
| 68 | deps.StaticLibs = append(deps.StaticLibs, |
| 69 | config.LibFuzzerRuntimeLibrary(ctx.toolchain())) |
| 70 | deps = fuzz.binaryDecorator.linkerDeps(ctx, deps) |
| 71 | return deps |
| 72 | } |
| 73 | |
| 74 | func (fuzz *fuzzBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 75 | flags = fuzz.binaryDecorator.linkerFlags(ctx, flags) |
| 76 | return flags |
| 77 | } |
| 78 | |
| 79 | func (fuzz *fuzzBinary) install(ctx ModuleContext, file android.Path) { |
| 80 | fuzz.binaryDecorator.baseInstaller.dir = "fuzz" |
| 81 | fuzz.binaryDecorator.baseInstaller.dir64 = "fuzz" |
| 82 | fuzz.binaryDecorator.baseInstaller.install(ctx, file) |
| 83 | } |
| 84 | |
| 85 | func NewFuzz(hod android.HostOrDeviceSupported) *Module { |
| 86 | module, binary := NewBinary(hod) |
| 87 | |
| 88 | // TODO(mitchp): The toolchain does not currently export the x86 (32-bit) |
| 89 | // variant of libFuzzer for host. There is no way to only disable the host |
| 90 | // 32-bit variant, so we specify cc_fuzz targets as 64-bit only. This doesn't |
| 91 | // hurt anyone, as cc_fuzz is mostly for experimental targets as of this |
| 92 | // moment. |
| 93 | module.multilib = "64" |
| 94 | |
| 95 | binary.baseInstaller = NewFuzzInstaller() |
| 96 | module.sanitize.SetSanitizer(fuzzer, true) |
| 97 | |
| 98 | fuzz := &fuzzBinary{ |
| 99 | binaryDecorator: binary, |
| 100 | baseCompiler: NewBaseCompiler(), |
| 101 | } |
| 102 | module.compiler = fuzz |
| 103 | module.linker = fuzz |
| 104 | module.installer = fuzz |
Colin Cross | eec9b28 | 2019-07-18 16:20:52 -0700 | [diff] [blame^] | 105 | |
| 106 | // The fuzzer runtime is not present for darwin host modules, disable cc_fuzz modules when targeting darwin. |
| 107 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
| 108 | disableDarwin := struct { |
| 109 | Target struct { |
| 110 | Darwin struct { |
| 111 | Enabled *bool |
| 112 | } |
| 113 | } |
| 114 | }{} |
| 115 | disableDarwin.Target.Darwin.Enabled = BoolPtr(false) |
| 116 | ctx.AppendProperties(&disableDarwin) |
| 117 | }) |
| 118 | |
Mitch Phillips | da9a463 | 2019-07-15 09:34:09 -0700 | [diff] [blame] | 119 | return module |
| 120 | } |