zx/
property.rs

1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Type-safe bindings for Zircon object properties.
6
7use crate::{sys, HandleRef, Status};
8use std::ops::Deref;
9use zerocopy::{FromBytes, Immutable, IntoBytes};
10
11/// Object property types for use with [object_get_property()] and [object_set_property].
12#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
13#[repr(transparent)]
14pub struct Property(u32);
15
16impl Deref for Property {
17    type Target = u32;
18
19    fn deref(&self) -> &Self::Target {
20        &self.0
21    }
22}
23
24/// A definition for properties about a zircon object.
25///
26/// # Safety
27///
28/// `PROPERTY` must correspond to a valid property type and `PropTy` must be the
29/// corresponding value type for that property. See
30/// https://fuchsia.dev/fuchsia-src/reference/syscalls/object_get_property for a
31/// list of available properties and their corresponding value types.
32///
33/// It must be valid to treat `PropTy` as its corresponding property type and
34/// writing the property type to a `PropTy` must completely initialize it. That
35/// is, `PropTy` must be "transparent" over the property type.
36pub(crate) unsafe trait PropertyQuery {
37    /// The raw `Property` value
38    const PROPERTY: Property;
39    /// The data type of this property
40    type PropTy: IntoBytes + FromBytes + Immutable;
41}
42
43assoc_values!(Property, [
44    NAME = sys::ZX_PROP_NAME;
45
46    #[cfg(target_arch = "x86_64")]
47    REGISTER_GS = sys::ZX_PROP_REGISTER_GS;
48    #[cfg(target_arch = "x86_64")]
49    REGISTER_FS = sys::ZX_PROP_REGISTER_FS;
50
51    PROCESS_BREAK_ON_LOAD = sys::ZX_PROP_PROCESS_BREAK_ON_LOAD;
52    PROCESS_DEBUG_ADDR = sys::ZX_PROP_PROCESS_DEBUG_ADDR;
53    PROCESS_VDSO_BASE_ADDRESS = sys::ZX_PROP_PROCESS_VDSO_BASE_ADDRESS;
54    SOCKET_RX_THRESHOLD = sys::ZX_PROP_SOCKET_RX_THRESHOLD;
55    SOCKET_TX_THRESHOLD = sys::ZX_PROP_SOCKET_TX_THRESHOLD;
56    CHANNEL_TX_MSG_MAX = sys::ZX_PROP_CHANNEL_TX_MSG_MAX;
57    JOB_KILL_ON_OOM = sys::ZX_PROP_JOB_KILL_ON_OOM;
58    EXCEPTION_STATE = sys::ZX_PROP_EXCEPTION_STATE;
59    VMO_CONTENT_SIZE = sys::ZX_PROP_VMO_CONTENT_SIZE;
60    STREAM_MODE_APPEND = sys::ZX_PROP_STREAM_MODE_APPEND;
61]);
62
63/// Get a property on a zircon object
64pub(crate) fn object_get_property<P: PropertyQuery>(
65    handle: HandleRef<'_>,
66) -> Result<P::PropTy, Status>
67where
68    P::PropTy: FromBytes + Immutable,
69{
70    // this is safe due to the contract on the P::PropTy type in the ObjectProperty trait.
71    let mut out = ::std::mem::MaybeUninit::<P::PropTy>::uninit();
72    let status = unsafe {
73        sys::zx_object_get_property(
74            handle.raw_handle(),
75            *P::PROPERTY,
76            out.as_mut_ptr().cast::<u8>(),
77            std::mem::size_of::<P::PropTy>(),
78        )
79    };
80    Status::ok(status).map(|_| unsafe { out.assume_init() })
81}
82
83/// Set a property on a zircon object
84pub(crate) fn object_set_property<P: PropertyQuery>(
85    handle: HandleRef<'_>,
86    val: &P::PropTy,
87) -> Result<(), Status>
88where
89    P::PropTy: IntoBytes + Immutable,
90{
91    let status = unsafe {
92        sys::zx_object_set_property(
93            handle.raw_handle(),
94            *P::PROPERTY,
95            std::ptr::from_ref(val).cast::<u8>(),
96            std::mem::size_of::<P::PropTy>(),
97        )
98    };
99    Status::ok(status)
100}