blob: 0d4622a7e8c403140c92a865e95e47656ea33eb8 [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 (
18 "strings"
19 "testing"
20
21 "android/soong/android"
Ivan Lozano61c02cc2023-06-09 14:06:44 -040022 "android/soong/cc"
Ivan Lozano6cd99e62020-02-11 08:24:25 -050023)
24
25func 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 Lozano2fcbffa2023-07-27 10:40:52 -040037 rust_fuzz_host {
38 name: "host_fuzzer",
39 srcs: ["foo.rs"],
40 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -050041 `)
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 Lozano6cd99e62020-02-11 08:24:25 -050045 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 Lozano8d10fc32021-11-05 16:36:47 -040053 fuzz_libtest := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Rule("rustc")
Wen-yi Chu41326c12023-09-22 03:58:59 +000054 if !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-C passes='sancov-module'") ||
55 !strings.Contains(fuzz_libtest.Args["rustcFlags"], "--cfg fuzzing") {
Ivan Lozano7f9d7cb2023-05-24 15:53:43 +000056 t.Errorf("rust_fuzz module does not contain the expected flags (sancov-module, cfg fuzzing).")
Ivan Lozano2fcbffa2023-07-27 10:40:52 -040057 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -050058
Ivan Lozano2fcbffa2023-07-27 10:40:52 -040059 // Check that host modules support fuzzing.
60 host_fuzzer := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Rule("rustc")
Wen-yi Chu41326c12023-09-22 03:58:59 +000061 if !strings.Contains(host_fuzzer.Args["rustcFlags"], "-C passes='sancov-module'") ||
62 !strings.Contains(host_fuzzer.Args["rustcFlags"], "--cfg fuzzing") {
Ivan Lozano2fcbffa2023-07-27 10:40:52 -040063 t.Errorf("rust_fuzz_host module does not contain the expected flags (sancov-module, cfg fuzzing).")
Ivan Lozano6cd99e62020-02-11 08:24:25 -050064 }
65
66 // Check that dependencies have 'fuzzer' variants produced for them as well.
Wen-yi Chu41326c12023-09-22 03:58:59 +000067 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 Lozano6cd99e62020-02-11 08:24:25 -050071 }
72}
Ivan Lozano61c02cc2023-06-09 14:06:44 -040073
74func 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
106func 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 }
117 cc_fuzz {
118 name: "fuzz_shared_libtest",
119 shared_libs: ["libtest_fuzzing"],
120 }
121 cc_fuzz {
122 name: "fuzz_static_libtest",
Ivan Lozano0a468a42024-05-13 21:03:34 -0400123 static_rlibs: ["libtest_fuzzing"],
124 }
125 cc_fuzz {
126 name: "fuzz_staticffi_libtest",
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400127 static_libs: ["libtest_fuzzing"],
128 }
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400129 `)
130
131 fuzz_shared_libtest := ctx.ModuleForTests("fuzz_shared_libtest", "android_arm64_armv8-a_fuzzer").Module().(cc.LinkableInterface)
132 fuzz_static_libtest := ctx.ModuleForTests("fuzz_static_libtest", "android_arm64_armv8-a_fuzzer").Module().(cc.LinkableInterface)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400133 fuzz_staticffi_libtest := ctx.ModuleForTests("fuzz_staticffi_libtest", "android_arm64_armv8-a_fuzzer").Module().(cc.LinkableInterface)
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400134
135 if !strings.Contains(fuzz_shared_libtest.FuzzSharedLibraries().String(), ":libcc_transitive_dep.so") {
136 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())
137 }
138 if !strings.Contains(fuzz_static_libtest.FuzzSharedLibraries().String(), ":libcc_transitive_dep.so") {
139 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())
140 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400141 if !strings.Contains(fuzz_staticffi_libtest.FuzzSharedLibraries().String(), ":libcc_transitive_dep.so") {
142 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())
143 }
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400144}