/src/arm-ffa/fuzz/fuzz_targets/success_args.rs
Line | Count | Source |
1 | | // SPDX-FileCopyrightText: Copyright The arm-ffa Contributors. |
2 | | // SPDX-License-Identifier: MIT OR Apache-2.0 |
3 | | |
4 | | //! Converts `SuccessArgs` into FF-A call specific success arguments. |
5 | | |
6 | | #![no_main] |
7 | | |
8 | | use arm_ffa::{ |
9 | | interface_args::{SuccessArgs, SuccessArgsFeatures, SuccessArgsIdGet, SuccessArgsSpmIdGet}, |
10 | | memory_management::{SuccessArgsMemOp, SuccessArgsMemPermGet}, |
11 | | notification::{ |
12 | | NotificationGetFlags, SuccessArgsNotificationGet, SuccessArgsNotificationInfoGet, |
13 | | SuccessArgsNotificationInfoGet32, SuccessArgsNotificationInfoGet64, |
14 | | }, |
15 | | partition_info::{ |
16 | | PartitionInfoGetFlags, SuccessArgsPartitionInfoGet, SuccessArgsPartitionInfoGetRegs, |
17 | | }, |
18 | | }; |
19 | | use libfuzzer_sys::fuzz_target; |
20 | | use zerocopy::transmute; |
21 | | |
22 | | /// Checks if `SuccessArgs` was unchanged after converting to specific type then back. |
23 | 905 | fn check_roundtrip(orig: SuccessArgs, res: SuccessArgs) { |
24 | 905 | if let (SuccessArgs::Args32(orig), SuccessArgs::Args32(res)) = (orig, res) { |
25 | 4.50k | for (reg_in, reg_out) in orig.iter().zip(res) { |
26 | 4.50k | assert_eq!(reg_in & reg_out, reg_out, "Mismatching registers"); |
27 | | } |
28 | 155 | } else if let (SuccessArgs::Args64(orig), SuccessArgs::Args64(res)) = (orig, res) { |
29 | 2.48k | for (reg_in, reg_out) in orig.iter().zip(res) { |
30 | 2.48k | assert_eq!(reg_in & reg_out, reg_out, "Mismatching registers"); |
31 | | } |
32 | | } else { |
33 | 0 | panic!("Got back different type of SuccessArgs") |
34 | | } |
35 | 905 | } |
36 | | |
37 | | /// Checks if assembling SuccessArgsNotificationInfoGet by hand gives back the original |
38 | | /// `SuccessArgs`. |
39 | 105 | fn check_notif_info_get<const N: usize>(orig: SuccessArgs, info: SuccessArgsNotificationInfoGet<N>) |
40 | 105 | where |
41 | 105 | SuccessArgsNotificationInfoGet<N>: Into<SuccessArgs>, |
42 | | { |
43 | 105 | check_roundtrip(orig, info.into()); |
44 | 105 | let mut out = <SuccessArgsNotificationInfoGet<N>>::default(); |
45 | 664 | for (endpoint, list) in info.iter() { |
46 | 664 | if out.add_list(endpoint, list).is_err() { |
47 | 0 | panic!("Failed manually cloning SuccessArgsNotificationInfoGet") |
48 | 664 | } |
49 | | } |
50 | 105 | check_roundtrip(orig, out.into()); |
51 | 105 | if out.add_list(42, &[1, 2, 3]).is_ok() { |
52 | 57 | let _: SuccessArgs = out.into(); |
53 | 57 | } else { |
54 | 48 | check_roundtrip(orig, out.into()); |
55 | 48 | } |
56 | 105 | } success_args::check_notif_info_get::<20> Line | Count | Source | 39 | 54 | fn check_notif_info_get<const N: usize>(orig: SuccessArgs, info: SuccessArgsNotificationInfoGet<N>) | 40 | 54 | where | 41 | 54 | SuccessArgsNotificationInfoGet<N>: Into<SuccessArgs>, | 42 | | { | 43 | 54 | check_roundtrip(orig, info.into()); | 44 | 54 | let mut out = <SuccessArgsNotificationInfoGet<N>>::default(); | 45 | 471 | for (endpoint, list) in info.iter() { | 46 | 471 | if out.add_list(endpoint, list).is_err() { | 47 | 0 | panic!("Failed manually cloning SuccessArgsNotificationInfoGet") | 48 | 471 | } | 49 | | } | 50 | 54 | check_roundtrip(orig, out.into()); | 51 | 54 | if out.add_list(42, &[1, 2, 3]).is_ok() { | 52 | 28 | let _: SuccessArgs = out.into(); | 53 | 28 | } else { | 54 | 26 | check_roundtrip(orig, out.into()); | 55 | 26 | } | 56 | 54 | } |
success_args::check_notif_info_get::<10> Line | Count | Source | 39 | 51 | fn check_notif_info_get<const N: usize>(orig: SuccessArgs, info: SuccessArgsNotificationInfoGet<N>) | 40 | 51 | where | 41 | 51 | SuccessArgsNotificationInfoGet<N>: Into<SuccessArgs>, | 42 | | { | 43 | 51 | check_roundtrip(orig, info.into()); | 44 | 51 | let mut out = <SuccessArgsNotificationInfoGet<N>>::default(); | 45 | 193 | for (endpoint, list) in info.iter() { | 46 | 193 | if out.add_list(endpoint, list).is_err() { | 47 | 0 | panic!("Failed manually cloning SuccessArgsNotificationInfoGet") | 48 | 193 | } | 49 | | } | 50 | 51 | check_roundtrip(orig, out.into()); | 51 | 51 | if out.add_list(42, &[1, 2, 3]).is_ok() { | 52 | 29 | let _: SuccessArgs = out.into(); | 53 | 29 | } else { | 54 | 22 | check_roundtrip(orig, out.into()); | 55 | 22 | } | 56 | 51 | } |
|
57 | | |
58 | | fuzz_target!(|data: &[u8]| { |
59 | | let args = if let Ok(data) = <[u8; 6 * 4]>::try_from(data) { |
60 | | SuccessArgs::Args32(transmute!(data)) |
61 | | } else if let Ok(data) = <[u8; 16 * 8]>::try_from(data) { |
62 | | SuccessArgs::Args64(transmute!(data)) |
63 | | } else { |
64 | | return; |
65 | | }; |
66 | | |
67 | | // FFA_FEATURES |
68 | | if let Ok(features) = SuccessArgsFeatures::try_from(args) { |
69 | | check_roundtrip(args, features.into()); |
70 | | } |
71 | | |
72 | | // FFA_ID_GET |
73 | | if let Ok(id) = SuccessArgsIdGet::try_from(args) { |
74 | | check_roundtrip(args, id.into()); |
75 | | } |
76 | | |
77 | | // FFA_MEM_DONATE, FFA_MEM_LEND, FFA_MEM_SHARE |
78 | | if let Ok(mem_op) = SuccessArgsMemOp::try_from(args) { |
79 | | check_roundtrip(args, mem_op.into()); |
80 | | } |
81 | | |
82 | | // FFA_MEM_PERM_GET |
83 | | if let Ok(mem_perm) = SuccessArgsMemPermGet::try_from(args) { |
84 | | check_roundtrip(args, mem_perm.into()); |
85 | | } |
86 | | |
87 | | // FFA_NOTIFICATION_GET |
88 | | let notifications = SuccessArgsNotificationGet::try_from(( |
89 | | NotificationGetFlags { |
90 | | sp_bitmap_id: true, |
91 | | vm_bitmap_id: true, |
92 | | spm_bitmap_id: true, |
93 | | hyp_bitmap_id: true, |
94 | | }, |
95 | | args, |
96 | | )); |
97 | | if let Ok(notifications) = notifications { |
98 | | check_roundtrip(args, notifications.into()); |
99 | | } |
100 | | |
101 | | // FFA_NOTIFICATION_INFO_GET_32 |
102 | | if let Ok(info) = SuccessArgsNotificationInfoGet32::try_from(args) { |
103 | | check_notif_info_get(args, info); |
104 | | } |
105 | | |
106 | | // FFA_NOTIFICATION_INFO_GET_64 |
107 | | if let Ok(info) = SuccessArgsNotificationInfoGet64::try_from(args) { |
108 | | check_notif_info_get(args, info); |
109 | | } |
110 | | |
111 | | // FFA_PARTITION_INFO_GET |
112 | | let part_info = |
113 | | SuccessArgsPartitionInfoGet::try_from((PartitionInfoGetFlags { count_only: false }, args)); |
114 | | if let Ok(part_info) = part_info { |
115 | | check_roundtrip(args, part_info.into()); |
116 | | } |
117 | | |
118 | | // FFA_PARTITION_INFO_GET_REGS |
119 | | if let Ok(regs) = SuccessArgsPartitionInfoGetRegs::try_from(args) { |
120 | | check_roundtrip(args, regs.into()); |
121 | | } |
122 | | |
123 | | // FFA_SPM_ID_GET |
124 | | if let Ok(spm_id) = SuccessArgsSpmIdGet::try_from(args) { |
125 | | check_roundtrip(args, spm_id.into()); |
126 | | } |
127 | | }); |