blob: 865665eef9d1b34b69599d8b25eef937a1a2d0d5 [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"
22)
23
24func TestRustFuzz(t *testing.T) {
25 ctx := testRust(t, `
26 rust_library {
27 name: "libtest_fuzzing",
28 crate_name: "test_fuzzing",
29 srcs: ["foo.rs"],
30 }
31 rust_fuzz {
32 name: "fuzz_libtest",
33 srcs: ["foo.rs"],
34 rustlibs: ["libtest_fuzzing"],
35 }
36 `)
37
38 // Check that appropriate dependencies are added and that the rustlib linkage is correct.
39 fuzz_libtest_mod := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Module().(*Module)
Ivan Lozano6cd99e62020-02-11 08:24:25 -050040 if !android.InList("liblibfuzzer_sys.rlib-std", fuzz_libtest_mod.Properties.AndroidMkRlibs) {
41 t.Errorf("liblibfuzzer_sys rlib library dependency missing for rust_fuzz module. %#v", fuzz_libtest_mod.Properties.AndroidMkRlibs)
42 }
43 if !android.InList("libtest_fuzzing.rlib-std", fuzz_libtest_mod.Properties.AndroidMkRlibs) {
44 t.Errorf("rustlibs not linked as rlib for rust_fuzz module.")
45 }
46
47 // Check that compiler flags are set appropriately .
Ivan Lozano8d10fc32021-11-05 16:36:47 -040048 fuzz_libtest := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Rule("rustc")
Tri Vo505b0e82021-04-08 15:50:48 -070049 if !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-Z sanitizer=hwaddress") ||
Charisee5ddec432022-03-01 03:02:51 +000050 !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-C passes='sancov-module'") ||
Ivan Lozano6cd99e62020-02-11 08:24:25 -050051 !strings.Contains(fuzz_libtest.Args["rustcFlags"], "--cfg fuzzing") {
Charisee5ddec432022-03-01 03:02:51 +000052 t.Errorf("rust_fuzz module does not contain the expected flags (sancov-module, cfg fuzzing, hwaddress sanitizer).")
Ivan Lozano6cd99e62020-02-11 08:24:25 -050053
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")
Tri Vo505b0e82021-04-08 15:50:48 -070058 if !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "-Z sanitizer=hwaddress") ||
Charisee5ddec432022-03-01 03:02:51 +000059 !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "-C passes='sancov-module'") ||
Ivan Lozano6cd99e62020-02-11 08:24:25 -050060 !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "--cfg fuzzing") {
Charisee5ddec432022-03-01 03:02:51 +000061 t.Errorf("rust_fuzz dependent library does not contain the expected flags (sancov-module, cfg fuzzing, hwaddress sanitizer).")
Ivan Lozano6cd99e62020-02-11 08:24:25 -050062 }
63}