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 | } |
| 37 | `) |
| 38 | |
| 39 | // Check that appropriate dependencies are added and that the rustlib linkage is correct. |
| 40 | 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] | 41 | if !android.InList("liblibfuzzer_sys.rlib-std", fuzz_libtest_mod.Properties.AndroidMkRlibs) { |
| 42 | t.Errorf("liblibfuzzer_sys rlib library dependency missing for rust_fuzz module. %#v", fuzz_libtest_mod.Properties.AndroidMkRlibs) |
| 43 | } |
| 44 | if !android.InList("libtest_fuzzing.rlib-std", fuzz_libtest_mod.Properties.AndroidMkRlibs) { |
| 45 | t.Errorf("rustlibs not linked as rlib for rust_fuzz module.") |
| 46 | } |
| 47 | |
| 48 | // Check that compiler flags are set appropriately . |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 49 | fuzz_libtest := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Rule("rustc") |
Ivan Lozano | 7f9d7cb | 2023-05-24 15:53:43 +0000 | [diff] [blame] | 50 | if !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-C passes='sancov-module'") || |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 51 | !strings.Contains(fuzz_libtest.Args["rustcFlags"], "--cfg fuzzing") { |
Ivan Lozano | 7f9d7cb | 2023-05-24 15:53:43 +0000 | [diff] [blame] | 52 | t.Errorf("rust_fuzz module does not contain the expected flags (sancov-module, cfg fuzzing).") |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 53 | |
| 54 | } |
| 55 | |
| 56 | // Check that dependencies have 'fuzzer' variants produced for them as well. |
| 57 | libtest_fuzzer := ctx.ModuleForTests("libtest_fuzzing", "android_arm64_armv8-a_rlib_rlib-std_fuzzer").Output("libtest_fuzzing.rlib") |
Ivan Lozano | 7f9d7cb | 2023-05-24 15:53:43 +0000 | [diff] [blame] | 58 | if !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "-C passes='sancov-module'") || |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 59 | !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "--cfg fuzzing") { |
Ivan Lozano | 7f9d7cb | 2023-05-24 15:53:43 +0000 | [diff] [blame] | 60 | 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] | 61 | } |
| 62 | } |
Ivan Lozano | 61c02cc | 2023-06-09 14:06:44 -0400 | [diff] [blame^] | 63 | |
| 64 | func TestRustFuzzDepBundling(t *testing.T) { |
| 65 | ctx := testRust(t, ` |
| 66 | cc_library { |
| 67 | name: "libcc_transitive_dep", |
| 68 | } |
| 69 | cc_library { |
| 70 | name: "libcc_direct_dep", |
| 71 | } |
| 72 | rust_library { |
| 73 | name: "libtest_fuzzing", |
| 74 | crate_name: "test_fuzzing", |
| 75 | srcs: ["foo.rs"], |
| 76 | shared_libs: ["libcc_transitive_dep"], |
| 77 | } |
| 78 | rust_fuzz { |
| 79 | name: "fuzz_libtest", |
| 80 | srcs: ["foo.rs"], |
| 81 | rustlibs: ["libtest_fuzzing"], |
| 82 | shared_libs: ["libcc_direct_dep"], |
| 83 | } |
| 84 | `) |
| 85 | |
| 86 | fuzz_libtest := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Module().(*Module) |
| 87 | |
| 88 | if !strings.Contains(fuzz_libtest.FuzzSharedLibraries().String(), ":libcc_direct_dep.so") { |
| 89 | t.Errorf("rust_fuzz does not contain the expected bundled direct shared libs ('libcc_direct_dep'): %#v", fuzz_libtest.FuzzSharedLibraries().String()) |
| 90 | } |
| 91 | if !strings.Contains(fuzz_libtest.FuzzSharedLibraries().String(), ":libcc_transitive_dep.so") { |
| 92 | t.Errorf("rust_fuzz does not contain the expected bundled transitive shared libs ('libcc_transitive_dep'): %#v", fuzz_libtest.FuzzSharedLibraries().String()) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestCCFuzzDepBundling(t *testing.T) { |
| 97 | ctx := testRust(t, ` |
| 98 | cc_library { |
| 99 | name: "libcc_transitive_dep", |
| 100 | } |
| 101 | rust_ffi { |
| 102 | name: "libtest_fuzzing", |
| 103 | crate_name: "test_fuzzing", |
| 104 | srcs: ["foo.rs"], |
| 105 | shared_libs: ["libcc_transitive_dep"], |
| 106 | } |
| 107 | cc_fuzz { |
| 108 | name: "fuzz_shared_libtest", |
| 109 | shared_libs: ["libtest_fuzzing"], |
| 110 | } |
| 111 | cc_fuzz { |
| 112 | name: "fuzz_static_libtest", |
| 113 | static_libs: ["libtest_fuzzing"], |
| 114 | } |
| 115 | |
| 116 | `) |
| 117 | |
| 118 | fuzz_shared_libtest := ctx.ModuleForTests("fuzz_shared_libtest", "android_arm64_armv8-a_fuzzer").Module().(cc.LinkableInterface) |
| 119 | fuzz_static_libtest := ctx.ModuleForTests("fuzz_static_libtest", "android_arm64_armv8-a_fuzzer").Module().(cc.LinkableInterface) |
| 120 | |
| 121 | if !strings.Contains(fuzz_shared_libtest.FuzzSharedLibraries().String(), ":libcc_transitive_dep.so") { |
| 122 | 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()) |
| 123 | } |
| 124 | if !strings.Contains(fuzz_static_libtest.FuzzSharedLibraries().String(), ":libcc_transitive_dep.so") { |
| 125 | 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()) |
| 126 | } |
| 127 | } |