xref: /linux/drivers/android/binder/error.rs (revision 7d1f84f1b94e79cb95d974ab3ec54cb3a4d64f5b)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 // Copyright (C) 2025 Google LLC.
4 
5 use core::panic::Location;
6 use kernel::fmt;
7 use kernel::prelude::*;
8 
9 use crate::defs::*;
10 
11 pub(crate) type BinderResult<T = ()> = core::result::Result<T, BinderError>;
12 
13 /// Wraps a `&'static Location` to format it as `filename.rs:line` without the directory prefix.
14 #[derive(Copy, Clone)]
15 #[repr(transparent)]
16 pub(crate) struct ErrorLocation(&'static Location<'static>);
17 
18 // SAFETY: `ErrorLocation` is `repr(transparent)` over a shared reference, which is part of the
19 // option layout optimization guarantee, so all zeroes is a valid representation for `None`.
20 unsafe impl pin_init::ZeroableOption for ErrorLocation {}
21 
22 impl ErrorLocation {
23     #[track_caller]
24     pub(crate) const fn caller() -> Self {
25         Self(Location::caller())
26     }
27 }
28 
29 impl fmt::Display for ErrorLocation {
30     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31         let file = match self.0.file().rsplit_once('/') {
32             Some((_, file)) => file,
33             None => self.0.file(),
34         };
35         write!(f, "{}:{}", file, self.0.line())
36     }
37 }
38 
39 /// An error that will be returned to userspace via the `BINDER_WRITE_READ` ioctl rather than via
40 /// errno.
41 pub(crate) struct BinderError {
42     pub(crate) reply: u32,
43     pub(crate) source: Option<Error>,
44     pub(crate) line: ErrorLocation,
45 }
46 
47 impl BinderError {
48     #[track_caller]
49     pub(crate) fn new_dead() -> Self {
50         Self {
51             reply: BR_DEAD_REPLY,
52             source: None,
53             line: ErrorLocation::caller(),
54         }
55     }
56 
57     #[track_caller]
58     pub(crate) fn new_frozen() -> Self {
59         Self {
60             reply: BR_FROZEN_REPLY,
61             source: None,
62             line: ErrorLocation::caller(),
63         }
64     }
65 
66     #[track_caller]
67     pub(crate) fn new_frozen_oneway() -> Self {
68         Self {
69             reply: BR_TRANSACTION_PENDING_FROZEN,
70             source: None,
71             line: ErrorLocation::caller(),
72         }
73     }
74 }
75 
76 /// Convert an errno into a `BinderError` and store the errno used to construct it. The errno
77 /// should be stored as the thread's extended error when given to userspace.
78 impl From<Error> for BinderError {
79     #[track_caller]
80     fn from(source: Error) -> Self {
81         Self {
82             reply: BR_FAILED_REPLY,
83             source: Some(source),
84             line: ErrorLocation::caller(),
85         }
86     }
87 }
88 
89 impl From<kernel::fs::file::BadFdError> for BinderError {
90     #[track_caller]
91     fn from(source: kernel::fs::file::BadFdError) -> Self {
92         BinderError::from(Error::from(source))
93     }
94 }
95 
96 impl From<kernel::alloc::AllocError> for BinderError {
97     #[track_caller]
98     fn from(_: kernel::alloc::AllocError) -> Self {
99         Self {
100             reply: BR_FAILED_REPLY,
101             source: Some(ENOMEM),
102             line: ErrorLocation::caller(),
103         }
104     }
105 }
106 
107 impl fmt::Debug for BinderError {
108     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109         match self.reply {
110             BR_FAILED_REPLY => match self.source.as_ref() {
111                 Some(source) => source.fmt(f),
112                 None => f.pad("BR_FAILED_REPLY"),
113             },
114             BR_DEAD_REPLY => f.pad("BR_DEAD_REPLY"),
115             BR_FROZEN_REPLY => f.pad("BR_FROZEN_REPLY"),
116             BR_TRANSACTION_PENDING_FROZEN => f.pad("BR_TRANSACTION_PENDING_FROZEN"),
117             BR_TRANSACTION_COMPLETE => f.pad("BR_TRANSACTION_COMPLETE"),
118             _ => match self.source.as_ref() {
119                 Some(source) => source.fmt(f),
120                 None => self.reply.fmt(f),
121             },
122         }
123     }
124 }
125