• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15package rust
16
17import (
18	"strings"
19	"testing"
20
21	"android/soong/android"
22	"android/soong/cc"
23)
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			}
37			rust_fuzz_host {
38				name: "host_fuzzer",
39				srcs: ["foo.rs"],
40			}
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)
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 .
53	fuzz_libtest := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Rule("rustc")
54	if !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-C passes='sancov-module'") ||
55		!strings.Contains(fuzz_libtest.Args["rustcFlags"], "--cfg fuzzing") {
56		t.Errorf("rust_fuzz module does not contain the expected flags (sancov-module, cfg fuzzing).")
57	}
58
59	// Check that host modules support fuzzing.
60	host_fuzzer := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Rule("rustc")
61	if !strings.Contains(host_fuzzer.Args["rustcFlags"], "-C passes='sancov-module'") ||
62		!strings.Contains(host_fuzzer.Args["rustcFlags"], "--cfg fuzzing") {
63		t.Errorf("rust_fuzz_host module does not contain the expected flags (sancov-module, cfg fuzzing).")
64	}
65
66	// Check that dependencies have 'fuzzer' variants produced for them as well.
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).")
71	}
72}
73
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",
123				static_rlibs: ["libtest_fuzzing"],
124			}
125			cc_fuzz {
126				name: "fuzz_staticffi_libtest",
127				static_libs: ["libtest_fuzzing"],
128			}
129	`)
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)
133	fuzz_staticffi_libtest := ctx.ModuleForTests("fuzz_staticffi_libtest", "android_arm64_armv8-a_fuzzer").Module().(cc.LinkableInterface)
134
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	}
141	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	}
144}
145