/src/arm-ffa/fuzz/fuzz_targets/interface.rs
Line | Count | Source |
1 | | // SPDX-FileCopyrightText: Copyright The arm-ffa Contributors. |
2 | | // SPDX-License-Identifier: MIT OR Apache-2.0 |
3 | | |
4 | | //! Converting from 18 registers to an `Interface` then back to 18 register as defined in v1.2 spec. |
5 | | //! Check if original and the processed register values match. |
6 | | |
7 | | #![no_main] |
8 | | |
9 | | use arm_ffa::{ |
10 | | Interface, |
11 | | interface_args::{ConsoleLogChars, LogChars}, |
12 | | }; |
13 | | use libfuzzer_sys::fuzz_target; |
14 | | use zerocopy::{FromBytes, Immutable, IntoBytes}; |
15 | | |
16 | | /// Checks `LogChars`' helper functions |
17 | 25 | fn test_logchars_fns<T>(chars: LogChars<T>) |
18 | 25 | where |
19 | 25 | T: Clone + IntoBytes + FromBytes + Immutable, |
20 | | { |
21 | 25 | chars.empty(); |
22 | 25 | chars.full(); |
23 | 25 | chars.bytes(); |
24 | 25 | chars.clone().push("test".as_bytes()); |
25 | 25 | } interface::test_logchars_fns::<[u32; 6]> Line | Count | Source | 17 | 10 | fn test_logchars_fns<T>(chars: LogChars<T>) | 18 | 10 | where | 19 | 10 | T: Clone + IntoBytes + FromBytes + Immutable, | 20 | | { | 21 | 10 | chars.empty(); | 22 | 10 | chars.full(); | 23 | 10 | chars.bytes(); | 24 | 10 | chars.clone().push("test".as_bytes()); | 25 | 10 | } |
interface::test_logchars_fns::<[u64; 16]> Line | Count | Source | 17 | 15 | fn test_logchars_fns<T>(chars: LogChars<T>) | 18 | 15 | where | 19 | 15 | T: Clone + IntoBytes + FromBytes + Immutable, | 20 | | { | 21 | 15 | chars.empty(); | 22 | 15 | chars.full(); | 23 | 15 | chars.bytes(); | 24 | 15 | chars.clone().push("test".as_bytes()); | 25 | 15 | } |
|
26 | | |
27 | | fuzz_target!(|data: &[u8]| { |
28 | | let mut regs_in: Vec<_> = match data.as_chunks() { |
29 | | ([], _) => return, |
30 | 1.06M | (chunks, []) => chunks.iter().map(|it| u64::from_ne_bytes(*it)).collect(), |
31 | | _ => return, |
32 | | }; |
33 | | |
34 | | if regs_in.len() > 18 { |
35 | | return; |
36 | | } |
37 | | |
38 | | // Mask function ID to 32 bits |
39 | | regs_in[0] &= 0x0000_0000_ffff_ffff; |
40 | | |
41 | | // Unpack interface from registers |
42 | | let Ok(interface) = Interface::try_from(®s_in[..]) else { |
43 | | return; |
44 | | }; |
45 | | |
46 | | match interface { |
47 | | Interface::ConsoleLog { chars } => match chars { |
48 | | ConsoleLogChars::Chars32(chars) => test_logchars_fns(chars), |
49 | | ConsoleLogChars::Chars64(chars) => test_logchars_fns(chars), |
50 | | }, |
51 | | Interface::MemPermGet { addr, .. } | Interface::MemPermSet { addr, .. } => { |
52 | | addr.address(); |
53 | | } |
54 | | _ => {} |
55 | | } |
56 | | |
57 | | let _ver = interface.minimum_ffa_version(); |
58 | | |
59 | | // Pack parsed interface back to registers |
60 | | let mut regs_out = [0; 18]; |
61 | | interface.to_regs(&mut regs_out); |
62 | | |
63 | | if interface.is_32bit() { |
64 | | let mut regs_out8 = [0; 8]; |
65 | | interface.to_regs(&mut regs_out8); |
66 | | } |
67 | | |
68 | | // Comparate the original register contents with the ones after a round of |
69 | | // unpacking-packing. Ignore bits that were set in the original call but are cleared in the |
70 | | // new set of registers. This is necessary, because unpack ignores SBZ bits but pack we set |
71 | | // them to zero. |
72 | | for (reg_in, reg_out) in regs_in.iter().zip(regs_out) { |
73 | | assert_eq!( |
74 | | reg_in & reg_out, |
75 | | reg_out, |
76 | | "Mismatching registers: {:?}", |
77 | | interface |
78 | | ); |
79 | | } |
80 | | }); |