1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// MIT/Apache2 License
use super::{Connection, PendingRequestFlags, RequestCookie, RequestWorkaround, EXT_KEY_SIZE};
use crate::{util::cycled_zeroes, Fd, Request};
use alloc::{string::ToString, vec, vec::Vec};
use core::{iter, mem};
use tinyvec::TinyVec;
#[cfg(feature = "async")]
use super::AsyncConnection;
#[inline]
fn string_as_array_bytes(s: &str) -> [u8; EXT_KEY_SIZE] {
let mut bytes: [u8; EXT_KEY_SIZE] = [0; EXT_KEY_SIZE];
if s.len() > EXT_KEY_SIZE {
bytes.copy_from_slice(&s.as_bytes()[..24]);
} else {
(&mut bytes[..s.len()]).copy_from_slice(s.as_bytes());
}
bytes
}
impl<Conn> super::Display<Conn> {
#[inline]
fn encode_request<R: Request>(
&mut self,
req: &R,
ext_opcode: Option<u8>,
) -> (u64, TinyVec<[u8; 32]>) {
let sequence = self.request_number;
self.request_number += 1;
// write to bytes
let mut bytes: TinyVec<[u8; 32]> = cycled_zeroes(req.size());
let mut len = req.as_bytes(&mut bytes);
log::trace!("len is {} bytes long", len);
// pad to a multiple of four bytes if we can
let remainder = len % 4;
if remainder != 0 {
let extend_by = 4 - remainder;
bytes.extend(iter::once(0).cycle().take(extend_by));
len += extend_by;
debug_assert_eq!(len % 4, 0);
log::trace!("Extended length is now {}", len);
}
match ext_opcode {
None => {
// First byte is opcode
// Second byte is minor opcode (ignored for now)
log::debug!("Request has opcode {}", R::OPCODE);
bytes[0] = R::OPCODE;
}
Some(extension) => {
// First byte is extension opcode
// Second byte is regular opcode
bytes[0] = extension;
bytes[1] = R::OPCODE;
}
}
// Third and fourth are length
let x_len = len / 4;
log::trace!("xlen is {}", x_len);
let len_bytes = x_len.to_ne_bytes();
bytes[2] = len_bytes[0];
bytes[3] = len_bytes[1];
bytes.truncate(len);
log::trace!("Request has bytes {:?}", &bytes);
let mut flags = PendingRequestFlags {
expects_fds: R::REPLY_EXPECTS_FDS,
..Default::default()
};
// there exists a very enraging bug in the X server, where certain GLX requests have the wrong size
// attached to them. this bug has become so widespread that we have to assume that it exists in all
// versions of the X server.
//
// to summarize, the X server makes an arithmatic error when calculating the length of the reply of
// requests GetFBConfigs and VendorPrivate. in these replies, they forget to multiply the length value
// by two. therefore, on the input end, we have to multiply it by two ourselves.
//
// the reason why this is enraging is because i just came out of combing through the codebase of both
// breadglx and breadx for why this would happen, when it turns out the answer is just "X server broke,
// multiply value by two, lol". the rage i feel that this bug is now baked into the X protocol is
// immeasurable, but not immeasurable enough for me to switch to Wayland
match (
R::EXTENSION,
R::OPCODE,
bytes.get(32..36).map(|a| {
let mut arr: [u8; 4] = [0; 4];
arr.copy_from_slice(a);
u32::from_ne_bytes(arr)
}),
) {
(Some("GLX"), 17, Some(0x10004)) | (Some("GLX"), 21, _) => {
log::debug!("Applying GLX FbConfig workaround to request");
flags.workaround = RequestWorkaround::GlxFbconfigBug;
}
_ => (),
}
if mem::size_of::<R::Reply>() != 0 {
self.expect_reply(sequence, flags);
}
(sequence, bytes)
}
}
impl<Conn: Connection> super::Display<Conn> {
#[inline]
pub fn send_request_internal<R: Request>(
&mut self,
mut req: R,
) -> crate::Result<RequestCookie<R>> {
let ext_opcode = match R::EXTENSION {
None => None,
Some(ext) => Some(self.get_ext_opcode(ext)?),
};
let (sequence, bytes): (u64, TinyVec<[u8; 32]>) = self.encode_request(&req, ext_opcode);
let mut _dummy: Vec<Fd> = vec![];
let fds = match req.file_descriptors() {
Some(fds) => fds,
None => &mut _dummy,
};
self.connection()?.send_packet(&bytes, fds)?;
Ok(RequestCookie::from_sequence(sequence))
}
#[allow(clippy::single_match_else)]
#[inline]
fn get_ext_opcode(&mut self, extname: &'static str) -> crate::Result<u8> {
let sarr = string_as_array_bytes(extname);
match self.extensions.get(&sarr) {
Some(code) => Ok(*code),
None => {
let code = self
.query_extension_immediate(extname.to_string())
.map_err(|_| crate::BreadError::ExtensionNotPresent(extname.into()))?
.major_opcode;
self.extensions.insert(sarr, code);
Ok(code)
}
}
}
}
#[cfg(feature = "async")]
impl<Conn: AsyncConnection + Send> super::Display<Conn> {
#[inline]
pub async fn send_request_internal_async<R: Request>(
&mut self,
mut req: R,
) -> crate::Result<RequestCookie<R>> {
let ext_opcode = match R::EXTENSION {
None => None,
Some(ext) => Some(self.get_ext_opcode_async(ext).await?),
};
let (sequence, bytes) = self.encode_request(&req, ext_opcode);
let mut _dummy: Vec<Fd> = vec![];
let fds = match req.file_descriptors() {
Some(fds) => fds,
None => &mut _dummy,
};
/*
This is a very ugly solution to issue #20
Consider, for a moment, if the future responsible for sending a large amount of memory to the
X server is dropped before it completes. This means the X server now has a portion of the bytes
it needs but expects more. This means that any future requests sent to the X server are now
"tainted" the fact that it's expecting an entirely different set of data from what our client
thinks it's expecting.
We'll take a page out of XCB's book here, which is "if the connection goes kaput, set an error
state in the connection that prevents bytes from being read or written across it." In Rust,
we accomplish this by setting up our connection in the Display as an Option<Connection> which,
in an untainted Display, should always be Some(..). In this function (and in the equivalent
wait_async function that handles reading), we run take() on this Option to get the connection.
Once send_packet_async is done, even if it finished with an error, we return the Connection
to its place in the display. If it isn't there, we can assume it's tainted and we can throw
an error.
*/
let mut connection = self.connection.take().ok_or(crate::BreadError::Tainted)?;
let res = connection.send_packet(&bytes, fds).await;
self.connection = Some(connection);
res?;
Ok(RequestCookie::from_sequence(sequence))
}
#[inline]
async fn get_ext_opcode_async(&mut self, extname: &'static str) -> crate::Result<u8> {
let sarr = string_as_array_bytes(extname);
match self.extensions.get(&sarr) {
Some(code) => Ok(*code),
None => {
let code = self
.query_extension_immediate_async(extname.to_string())
.await
.map_err(|_| crate::BreadError::ExtensionNotPresent(extname.into()))?
.major_opcode;
self.extensions.insert(sarr, code);
Ok(code)
}
}
}
}