1 // Copyright (C) 2024 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 //! IPC process
15 
16 use std::ffi::c_void;
17 use std::mem::MaybeUninit;
18 use std::ptr;
19 
20 use crate::skeleton::ffi::IsHandlingTransaction;
21 
22 /// Determine whether the current thread is currently executing an incoming
23 /// transaction.
24 #[inline]
is_handling_transaction() -> bool25 pub fn is_handling_transaction() -> bool {
26     // SAFETY:
27     // Ensure proper usage within the context of the IPC binding system and its
28     // intended behavior.
29     IsHandlingTransaction()
30 }
31 
32 /// Callback to allocate a vector for parcel array read functions.
33 ///
34 /// # Safety
35 ///
36 /// The opaque data pointer passed to the array read function must be a mutable
37 /// pointer to an `Option<Vec<MaybeUninit<T>>>`.
allocate_vec_with_buffer<T>( value: *mut c_void, buffer: *mut *mut T, len: i32, ) -> bool38 pub unsafe extern "C" fn allocate_vec_with_buffer<T>(
39     value: *mut c_void,
40     buffer: *mut *mut T,
41     len: i32,
42 ) -> bool {
43     let res = allocate_vec::<T>(value, len);
44     // `buffer` will be assigned a mutable pointer to the allocated vector data
45     // if this function returns true.
46     let vec = &mut *(value as *mut Option<Vec<MaybeUninit<T>>>);
47     if let Some(new_vec) = vec {
48         *buffer = new_vec.as_mut_ptr() as *mut T;
49     }
50     res
51 }
52 
53 /// Callback to allocate a vector for parcel array read functions.
54 ///
55 /// # Safety
56 ///
57 /// The opaque data pointer passed to the array read function must be a mutable
58 /// pointer to an `Option<Vec<MaybeUninit<T>>>`.
allocate_vec<T>(value: *mut c_void, len: i32) -> bool59 unsafe extern "C" fn allocate_vec<T>(value: *mut c_void, len: i32) -> bool {
60     if len < 0 {
61         return false;
62     }
63     allocate_vec_maybeuninit::<T>(value, len as u32);
64     true
65 }
66 
67 /// # Safety
68 ///
69 /// Ensure that the value pointer is not null
allocate_vec_maybeuninit<T>(value: *mut c_void, len: u32)70 pub(crate) unsafe fn allocate_vec_maybeuninit<T>(value: *mut c_void, len: u32) {
71     let vec = &mut *(value as *mut Option<Vec<MaybeUninit<T>>>);
72     let mut new_vec: Vec<MaybeUninit<T>> = Vec::with_capacity(len as usize);
73 
74     // SAFETY: this is safe because the vector contains MaybeUninit elements which
75     // can be uninitialized
76     new_vec.set_len(len as usize);
77     ptr::write(vec, Some(new_vec));
78 }
79