| 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 | "strings" | 
|  | 19 | "testing" | 
|  | 20 |  | 
|  | 21 | "android/soong/android" | 
| Ivan Lozano | 61c02cc | 2023-06-09 14:06:44 -0400 | [diff] [blame] | 22 | "android/soong/cc" | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 23 | ) | 
|  | 24 |  | 
|  | 25 | func TestRustFuzz(t *testing.T) { | 
|  | 26 | ctx := testRust(t, ` | 
|  | 27 | rust_library { | 
|  | 28 | name: "libtest_fuzzing", | 
|  | 29 | crate_name: "test_fuzzing", | 
|  | 30 | srcs: ["foo.rs"], | 
|  | 31 | } | 
|  | 32 | rust_fuzz { | 
|  | 33 | name: "fuzz_libtest", | 
|  | 34 | srcs: ["foo.rs"], | 
|  | 35 | rustlibs: ["libtest_fuzzing"], | 
|  | 36 | } | 
| Ivan Lozano | 2fcbffa | 2023-07-27 10:40:52 -0400 | [diff] [blame] | 37 | rust_fuzz_host { | 
|  | 38 | name: "host_fuzzer", | 
|  | 39 | srcs: ["foo.rs"], | 
|  | 40 | } | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 41 | `) | 
|  | 42 |  | 
|  | 43 | // Check that appropriate dependencies are added and that the rustlib linkage is correct. | 
|  | 44 | fuzz_libtest_mod := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Module().(*Module) | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 45 | if !android.InList("liblibfuzzer_sys.rlib-std", fuzz_libtest_mod.Properties.AndroidMkRlibs) { | 
|  | 46 | t.Errorf("liblibfuzzer_sys rlib library dependency missing for rust_fuzz module. %#v", fuzz_libtest_mod.Properties.AndroidMkRlibs) | 
|  | 47 | } | 
|  | 48 | if !android.InList("libtest_fuzzing.rlib-std", fuzz_libtest_mod.Properties.AndroidMkRlibs) { | 
|  | 49 | t.Errorf("rustlibs not linked as rlib for rust_fuzz module.") | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | // Check that compiler flags are set appropriately . | 
| Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 53 | fuzz_libtest := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Rule("rustc") | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 54 | if !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-C passes='sancov-module'") || | 
|  | 55 | !strings.Contains(fuzz_libtest.Args["rustcFlags"], "--cfg fuzzing") { | 
| Ivan Lozano | 7f9d7cb | 2023-05-24 15:53:43 +0000 | [diff] [blame] | 56 | t.Errorf("rust_fuzz module does not contain the expected flags (sancov-module, cfg fuzzing).") | 
| Ivan Lozano | 2fcbffa | 2023-07-27 10:40:52 -0400 | [diff] [blame] | 57 | } | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 58 |  | 
| Ivan Lozano | 2fcbffa | 2023-07-27 10:40:52 -0400 | [diff] [blame] | 59 | // Check that host modules support fuzzing. | 
|  | 60 | host_fuzzer := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Rule("rustc") | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 61 | if !strings.Contains(host_fuzzer.Args["rustcFlags"], "-C passes='sancov-module'") || | 
|  | 62 | !strings.Contains(host_fuzzer.Args["rustcFlags"], "--cfg fuzzing") { | 
| Ivan Lozano | 2fcbffa | 2023-07-27 10:40:52 -0400 | [diff] [blame] | 63 | t.Errorf("rust_fuzz_host module does not contain the expected flags (sancov-module, cfg fuzzing).") | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
|  | 66 | // Check that dependencies have 'fuzzer' variants produced for them as well. | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 67 | libtest_fuzzer := ctx.ModuleForTests("libtest_fuzzing", "android_arm64_armv8-a_rlib_rlib-std_fuzzer").Output("libtest_fuzzing.rlib") | 
|  | 68 | if !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "-C passes='sancov-module'") || | 
|  | 69 | !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "--cfg fuzzing") { | 
|  | 70 | t.Errorf("rust_fuzz dependent library does not contain the expected flags (sancov-module, cfg fuzzing).") | 
| Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 71 | } | 
|  | 72 | } | 
| Ivan Lozano | 61c02cc | 2023-06-09 14:06:44 -0400 | [diff] [blame] | 73 |  | 
|  | 74 | func TestRustFuzzDepBundling(t *testing.T) { | 
|  | 75 | ctx := testRust(t, ` | 
|  | 76 | cc_library { | 
|  | 77 | name: "libcc_transitive_dep", | 
|  | 78 | } | 
|  | 79 | cc_library { | 
|  | 80 | name: "libcc_direct_dep", | 
|  | 81 | } | 
|  | 82 | rust_library { | 
|  | 83 | name: "libtest_fuzzing", | 
|  | 84 | crate_name: "test_fuzzing", | 
|  | 85 | srcs: ["foo.rs"], | 
|  | 86 | shared_libs: ["libcc_transitive_dep"], | 
|  | 87 | } | 
|  | 88 | rust_fuzz { | 
|  | 89 | name: "fuzz_libtest", | 
|  | 90 | srcs: ["foo.rs"], | 
|  | 91 | rustlibs: ["libtest_fuzzing"], | 
|  | 92 | shared_libs: ["libcc_direct_dep"], | 
|  | 93 | } | 
|  | 94 | `) | 
|  | 95 |  | 
|  | 96 | fuzz_libtest := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Module().(*Module) | 
|  | 97 |  | 
|  | 98 | if !strings.Contains(fuzz_libtest.FuzzSharedLibraries().String(), ":libcc_direct_dep.so") { | 
|  | 99 | t.Errorf("rust_fuzz does not contain the expected bundled direct shared libs ('libcc_direct_dep'): %#v", fuzz_libtest.FuzzSharedLibraries().String()) | 
|  | 100 | } | 
|  | 101 | if !strings.Contains(fuzz_libtest.FuzzSharedLibraries().String(), ":libcc_transitive_dep.so") { | 
|  | 102 | t.Errorf("rust_fuzz does not contain the expected bundled transitive shared libs ('libcc_transitive_dep'): %#v", fuzz_libtest.FuzzSharedLibraries().String()) | 
|  | 103 | } | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | func TestCCFuzzDepBundling(t *testing.T) { | 
|  | 107 | ctx := testRust(t, ` | 
|  | 108 | cc_library { | 
|  | 109 | name: "libcc_transitive_dep", | 
|  | 110 | } | 
|  | 111 | rust_ffi { | 
|  | 112 | name: "libtest_fuzzing", | 
|  | 113 | crate_name: "test_fuzzing", | 
|  | 114 | srcs: ["foo.rs"], | 
|  | 115 | shared_libs: ["libcc_transitive_dep"], | 
|  | 116 | } | 
| Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 117 | rust_ffi_static { | 
|  | 118 | name: "libtest_fuzzing_static", | 
|  | 119 | crate_name: "test_fuzzing", | 
|  | 120 | srcs: ["foo.rs"], | 
|  | 121 | shared_libs: ["libcc_transitive_dep"], | 
|  | 122 | } | 
| Ivan Lozano | 61c02cc | 2023-06-09 14:06:44 -0400 | [diff] [blame] | 123 | cc_fuzz { | 
|  | 124 | name: "fuzz_shared_libtest", | 
|  | 125 | shared_libs: ["libtest_fuzzing"], | 
|  | 126 | } | 
|  | 127 | cc_fuzz { | 
|  | 128 | name: "fuzz_static_libtest", | 
| Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 129 | static_libs: ["libtest_fuzzing"], | 
| Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 130 | } | 
|  | 131 | cc_fuzz { | 
|  | 132 | name: "fuzz_staticffi_libtest", | 
| Ivan Lozano | fd47b1a | 2024-05-17 14:13:41 -0400 | [diff] [blame] | 133 | static_libs: ["libtest_fuzzing_static"], | 
| Ivan Lozano | 61c02cc | 2023-06-09 14:06:44 -0400 | [diff] [blame] | 134 | } | 
| Ivan Lozano | 61c02cc | 2023-06-09 14:06:44 -0400 | [diff] [blame] | 135 | `) | 
|  | 136 |  | 
|  | 137 | fuzz_shared_libtest := ctx.ModuleForTests("fuzz_shared_libtest", "android_arm64_armv8-a_fuzzer").Module().(cc.LinkableInterface) | 
|  | 138 | fuzz_static_libtest := ctx.ModuleForTests("fuzz_static_libtest", "android_arm64_armv8-a_fuzzer").Module().(cc.LinkableInterface) | 
| Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 139 | fuzz_staticffi_libtest := ctx.ModuleForTests("fuzz_staticffi_libtest", "android_arm64_armv8-a_fuzzer").Module().(cc.LinkableInterface) | 
| Ivan Lozano | 61c02cc | 2023-06-09 14:06:44 -0400 | [diff] [blame] | 140 |  | 
|  | 141 | if !strings.Contains(fuzz_shared_libtest.FuzzSharedLibraries().String(), ":libcc_transitive_dep.so") { | 
|  | 142 | t.Errorf("cc_fuzz does not contain the expected bundled transitive shared libs from rust_ffi_shared ('libcc_transitive_dep'): %#v", fuzz_shared_libtest.FuzzSharedLibraries().String()) | 
|  | 143 | } | 
|  | 144 | if !strings.Contains(fuzz_static_libtest.FuzzSharedLibraries().String(), ":libcc_transitive_dep.so") { | 
|  | 145 | t.Errorf("cc_fuzz does not contain the expected bundled transitive shared libs from rust_ffi_static ('libcc_transitive_dep'): %#v", fuzz_static_libtest.FuzzSharedLibraries().String()) | 
|  | 146 | } | 
| Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 147 | if !strings.Contains(fuzz_staticffi_libtest.FuzzSharedLibraries().String(), ":libcc_transitive_dep.so") { | 
|  | 148 | t.Errorf("cc_fuzz does not contain the expected bundled transitive shared libs from rust_ffi_rlib ('libcc_transitive_dep'): %#v", fuzz_staticffi_libtest.FuzzSharedLibraries().String()) | 
|  | 149 | } | 
| Ivan Lozano | 61c02cc | 2023-06-09 14:06:44 -0400 | [diff] [blame] | 150 | } |