1 // Copyright 2024, Linaro Limited 2 // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org> 3 // SPDX-License-Identifier: GPL-2.0-or-later 4 5 use std::{ 6 ffi::{c_void, CStr}, 7 ptr::{addr_of, addr_of_mut}, 8 }; 9 10 use qemu_api::{ 11 bindings::{module_call_init, module_init_type, object_new, object_unref, qdev_prop_bool}, 12 c_str, 13 cell::{self, BqlCell}, 14 declare_properties, define_property, 15 prelude::*, 16 qdev::{DeviceClass, DeviceImpl, DeviceState, Property, ResettablePhasesImpl}, 17 qom::{ClassInitImpl, ObjectImpl, ParentField}, 18 sysbus::SysBusDevice, 19 vmstate::VMStateDescription, 20 zeroable::Zeroable, 21 }; 22 23 // Test that macros can compile. 24 pub static VMSTATE: VMStateDescription = VMStateDescription { 25 name: c_str!("name").as_ptr(), 26 unmigratable: true, 27 ..Zeroable::ZERO 28 }; 29 30 #[derive(qemu_api_macros::offsets)] 31 #[repr(C)] 32 #[derive(qemu_api_macros::Object)] 33 pub struct DummyState { 34 parent: ParentField<DeviceState>, 35 migrate_clock: bool, 36 } 37 38 qom_isa!(DummyState: Object, DeviceState); 39 40 pub struct DummyClass { 41 parent_class: <DeviceState as ObjectType>::Class, 42 } 43 44 declare_properties! { 45 DUMMY_PROPERTIES, 46 define_property!( 47 c_str!("migrate-clk"), 48 DummyState, 49 migrate_clock, 50 unsafe { &qdev_prop_bool }, 51 bool 52 ), 53 } 54 55 unsafe impl ObjectType for DummyState { 56 type Class = DummyClass; 57 const TYPE_NAME: &'static CStr = c_str!("dummy"); 58 } 59 60 impl ObjectImpl for DummyState { 61 type ParentType = DeviceState; 62 const ABSTRACT: bool = false; 63 } 64 65 impl ResettablePhasesImpl for DummyState {} 66 67 impl DeviceImpl for DummyState { 68 fn properties() -> &'static [Property] { 69 &DUMMY_PROPERTIES 70 } 71 fn vmsd() -> Option<&'static VMStateDescription> { 72 Some(&VMSTATE) 73 } 74 } 75 76 // `impl<T> ClassInitImpl<DummyClass> for T` doesn't work since it violates 77 // orphan rule. 78 impl ClassInitImpl<DummyClass> for DummyState { 79 fn class_init(klass: &mut DummyClass) { 80 <Self as ClassInitImpl<DeviceClass>>::class_init(&mut klass.parent_class); 81 } 82 } 83 84 #[derive(qemu_api_macros::offsets)] 85 #[repr(C)] 86 #[derive(qemu_api_macros::Object)] 87 pub struct DummyChildState { 88 parent: ParentField<DummyState>, 89 } 90 91 qom_isa!(DummyChildState: Object, DeviceState, DummyState); 92 93 pub struct DummyChildClass { 94 parent_class: <DummyState as ObjectType>::Class, 95 } 96 97 unsafe impl ObjectType for DummyChildState { 98 type Class = DummyChildClass; 99 const TYPE_NAME: &'static CStr = c_str!("dummy_child"); 100 } 101 102 impl ObjectImpl for DummyChildState { 103 type ParentType = DummyState; 104 const ABSTRACT: bool = false; 105 } 106 107 impl ResettablePhasesImpl for DummyChildState {} 108 impl DeviceImpl for DummyChildState {} 109 110 impl ClassInitImpl<DummyClass> for DummyChildState { 111 fn class_init(klass: &mut DummyClass) { 112 <Self as ClassInitImpl<DeviceClass>>::class_init(&mut klass.parent_class); 113 } 114 } 115 116 impl ClassInitImpl<DummyChildClass> for DummyChildState { 117 fn class_init(klass: &mut DummyChildClass) { 118 <Self as ClassInitImpl<DummyClass>>::class_init(&mut klass.parent_class); 119 } 120 } 121 122 fn init_qom() { 123 static ONCE: BqlCell<bool> = BqlCell::new(false); 124 125 cell::bql_start_test(); 126 if !ONCE.get() { 127 unsafe { 128 module_call_init(module_init_type::MODULE_INIT_QOM); 129 } 130 ONCE.set(true); 131 } 132 } 133 134 #[test] 135 /// Create and immediately drop an instance. 136 fn test_object_new() { 137 init_qom(); 138 drop(DummyState::new()); 139 drop(DummyChildState::new()); 140 } 141 142 #[test] 143 #[allow(clippy::redundant_clone)] 144 /// Create, clone and then drop an instance. 145 fn test_clone() { 146 init_qom(); 147 let p = DummyState::new(); 148 assert_eq!(p.clone().typename(), "dummy"); 149 drop(p); 150 } 151 152 #[test] 153 /// Try invoking a method on an object. 154 fn test_typename() { 155 init_qom(); 156 let p = DummyState::new(); 157 assert_eq!(p.typename(), "dummy"); 158 } 159 160 // a note on all "cast" tests: usually, especially for downcasts the desired 161 // class would be placed on the right, for example: 162 // 163 // let sbd_ref = p.dynamic_cast::<SysBusDevice>(); 164 // 165 // Here I am doing the opposite to check that the resulting type is correct. 166 167 #[test] 168 #[allow(clippy::shadow_unrelated)] 169 /// Test casts on shared references. 170 fn test_cast() { 171 init_qom(); 172 let p = DummyState::new(); 173 let p_ptr: *mut DummyState = p.as_mut_ptr(); 174 let p_ref: &mut DummyState = unsafe { &mut *p_ptr }; 175 176 let obj_ref: &Object = p_ref.upcast(); 177 assert_eq!(addr_of!(*obj_ref), p_ptr.cast()); 178 179 let sbd_ref: Option<&SysBusDevice> = obj_ref.dynamic_cast(); 180 assert!(sbd_ref.is_none()); 181 182 let dev_ref: Option<&DeviceState> = obj_ref.downcast(); 183 assert_eq!(addr_of!(*dev_ref.unwrap()), p_ptr.cast()); 184 185 // SAFETY: the cast is wrong, but the value is only used for comparison 186 unsafe { 187 let sbd_ref: &SysBusDevice = obj_ref.unsafe_cast(); 188 assert_eq!(addr_of!(*sbd_ref), p_ptr.cast()); 189 } 190 } 191 192 #[test] 193 #[allow(clippy::shadow_unrelated)] 194 /// Test casts on mutable references. 195 fn test_cast_mut() { 196 init_qom(); 197 let p: *mut DummyState = unsafe { object_new(DummyState::TYPE_NAME.as_ptr()).cast() }; 198 199 let p_ref: &mut DummyState = unsafe { &mut *p }; 200 let obj_ref: &mut Object = p_ref.upcast_mut(); 201 assert_eq!(addr_of_mut!(*obj_ref), p.cast()); 202 203 let sbd_ref: Result<&mut SysBusDevice, &mut Object> = obj_ref.dynamic_cast_mut(); 204 let obj_ref = sbd_ref.unwrap_err(); 205 206 let dev_ref: Result<&mut DeviceState, &mut Object> = obj_ref.downcast_mut(); 207 let dev_ref = dev_ref.unwrap(); 208 assert_eq!(addr_of_mut!(*dev_ref), p.cast()); 209 210 // SAFETY: the cast is wrong, but the value is only used for comparison 211 unsafe { 212 let sbd_ref: &mut SysBusDevice = obj_ref.unsafe_cast_mut(); 213 assert_eq!(addr_of_mut!(*sbd_ref), p.cast()); 214 215 object_unref(p_ref.as_object_mut_ptr().cast::<c_void>()); 216 } 217 } 218