1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 //! Pack UniFFI interface metadata into byte arrays
6 //!
7 //! In order to generate foreign bindings, we store interface metadata inside the library file
8 //! using exported static byte arrays. The foreign bindings code reads that metadata from the
9 //! library files and generates bindings based on that.
10 //!
11 //! The metadata static variables are generated by the proc-macros, which is an issue because the
12 //! proc-macros don't have knowledge of the entire interface -- they can only see the item they're
13 //! wrapping. For example, when a proc-macro sees a type name, it doesn't know anything about the
14 //! actual type: it could be a Record, an Enum, or even a type alias for a `Vec<>`/`Result<>`.
15 //!
16 //! This module helps bridge the gap by providing tools that allow the proc-macros to generate code
17 //! to encode the interface metadata:
18 //! - A set of const functions to build up metadata buffers with const expressions
19 //! - The `export_static_metadata_var!` macro, which creates the static variable from a const metadata
20 //! buffer.
21 //! - The `FfiConverter::TYPE_ID_META` const which encodes an identifier for that type in a
22 //! metadata buffer.
23 //!
24 //! `uniffi_bindgen::macro_metadata` contains the code to read the metadata from a library file.
25 //! `fixtures/metadata` has the tests.
26
27 /// Metadata constants, make sure to keep this in sync with copy in `uniffi_meta::reader`
28 pub mod codes {
29 // Top-level metadata item codes
30 pub const FUNC: u8 = 0;
31 pub const METHOD: u8 = 1;
32 pub const RECORD: u8 = 2;
33 pub const ENUM: u8 = 3;
34 pub const INTERFACE: u8 = 4;
35 pub const NAMESPACE: u8 = 6;
36 pub const CONSTRUCTOR: u8 = 7;
37 pub const UDL_FILE: u8 = 8;
38 pub const CALLBACK_INTERFACE: u8 = 9;
39 pub const TRAIT_METHOD: u8 = 10;
40 pub const UNIFFI_TRAIT: u8 = 11;
41 pub const TRAIT_INTERFACE: u8 = 12;
42 pub const CALLBACK_TRAIT_INTERFACE: u8 = 13;
43 pub const UNKNOWN: u8 = 255;
44
45 // Type codes
46 pub const TYPE_U8: u8 = 0;
47 pub const TYPE_U16: u8 = 1;
48 pub const TYPE_U32: u8 = 2;
49 pub const TYPE_U64: u8 = 3;
50 pub const TYPE_I8: u8 = 4;
51 pub const TYPE_I16: u8 = 5;
52 pub const TYPE_I32: u8 = 6;
53 pub const TYPE_I64: u8 = 7;
54 pub const TYPE_F32: u8 = 8;
55 pub const TYPE_F64: u8 = 9;
56 pub const TYPE_BOOL: u8 = 10;
57 pub const TYPE_STRING: u8 = 11;
58 pub const TYPE_OPTION: u8 = 12;
59 pub const TYPE_RECORD: u8 = 13;
60 pub const TYPE_ENUM: u8 = 14;
61 // 15 no longer used.
62 pub const TYPE_INTERFACE: u8 = 16;
63 pub const TYPE_VEC: u8 = 17;
64 pub const TYPE_HASH_MAP: u8 = 18;
65 pub const TYPE_SYSTEM_TIME: u8 = 19;
66 pub const TYPE_DURATION: u8 = 20;
67 pub const TYPE_CALLBACK_INTERFACE: u8 = 21;
68 pub const TYPE_CUSTOM: u8 = 22;
69 pub const TYPE_RESULT: u8 = 23;
70 pub const TYPE_TRAIT_INTERFACE: u8 = 24;
71 pub const TYPE_CALLBACK_TRAIT_INTERFACE: u8 = 25;
72 pub const TYPE_UNIT: u8 = 255;
73
74 // Literal codes for LiteralMetadata
75 pub const LIT_STR: u8 = 0;
76 pub const LIT_INT: u8 = 1;
77 pub const LIT_FLOAT: u8 = 2;
78 pub const LIT_BOOL: u8 = 3;
79 pub const LIT_NONE: u8 = 4;
80 pub const LIT_SOME: u8 = 5;
81 pub const LIT_EMPTY_SEQ: u8 = 6;
82 }
83
84 // For large errors (e.g. enums) a buffer size of ~4k - ~8k
85 // is not enough. See issues on Github: #1968 and #2041 and
86 // for an example see fixture/large-error
87 const BUF_SIZE: usize = 16384;
88
89 // This struct is a kludge around the fact that Rust const generic support doesn't quite handle our
90 // needs.
91 //
92 // We'd like to have code like this in `FfiConverter`:
93 //
94 // ```
95 // const TYPE_ID_META_SIZE: usize;
96 // const TYPE_ID_META: [u8, Self::TYPE_ID_META_SIZE];
97 // ```
98 //
99 // This would define a metadata buffer, correctly size for the data needed. However, associated
100 // consts as generic params aren't supported yet.
101 //
102 // To work around this, we use `const MetadataBuffer` values, which contain fixed-sized buffers
103 // with enough capacity to store our largest metadata arrays. Since the `MetadataBuffer` values
104 // are const, they're only stored at compile time and the extra bytes don't end up contributing to
105 // the final binary size. This was tested on Rust `1.66.0` with `--release` by increasing
106 // `BUF_SIZE` and checking the compiled library sizes.
107 #[derive(Debug)]
108 pub struct MetadataBuffer {
109 pub bytes: [u8; BUF_SIZE],
110 pub size: usize,
111 }
112
113 impl MetadataBuffer {
new() -> Self114 pub const fn new() -> Self {
115 Self {
116 bytes: [0; BUF_SIZE],
117 size: 0,
118 }
119 }
120
from_code(value: u8) -> Self121 pub const fn from_code(value: u8) -> Self {
122 Self::new().concat_value(value)
123 }
124
125 // Concatenate another buffer to this one.
126 //
127 // This consumes self, which is convenient for the proc-macro code and also allows us to avoid
128 // allocated an extra buffer.
concat(mut self, other: MetadataBuffer) -> MetadataBuffer129 pub const fn concat(mut self, other: MetadataBuffer) -> MetadataBuffer {
130 assert!(self.size + other.size <= BUF_SIZE);
131 // It would be nice to use `copy_from_slice()`, but that's not allowed in const functions
132 // as of Rust 1.66.
133 let mut i = 0;
134 while i < other.size {
135 self.bytes[self.size] = other.bytes[i];
136 self.size += 1;
137 i += 1;
138 }
139 self
140 }
141
142 // Concatenate a `u8` value to this buffer
143 //
144 // This consumes self, which is convenient for the proc-macro code and also allows us to avoid
145 // allocated an extra buffer.
concat_value(mut self, value: u8) -> Self146 pub const fn concat_value(mut self, value: u8) -> Self {
147 assert!(self.size < BUF_SIZE);
148 self.bytes[self.size] = value;
149 self.size += 1;
150 self
151 }
152
153 // Concatenate a `u32` value to this buffer
154 //
155 // This consumes self, which is convenient for the proc-macro code and also allows us to avoid
156 // allocated an extra buffer.
concat_u32(mut self, value: u32) -> Self157 pub const fn concat_u32(mut self, value: u32) -> Self {
158 assert!(self.size + 4 <= BUF_SIZE);
159 // store the value as little-endian
160 self.bytes[self.size] = value as u8;
161 self.bytes[self.size + 1] = (value >> 8) as u8;
162 self.bytes[self.size + 2] = (value >> 16) as u8;
163 self.bytes[self.size + 3] = (value >> 24) as u8;
164 self.size += 4;
165 self
166 }
167
168 // Concatenate a `bool` value to this buffer
169 //
170 // This consumes self, which is convenient for the proc-macro code and also allows us to avoid
171 // allocated an extra buffer.
concat_bool(self, value: bool) -> Self172 pub const fn concat_bool(self, value: bool) -> Self {
173 self.concat_value(value as u8)
174 }
175
176 // Option<bool>
concat_option_bool(self, value: Option<bool>) -> Self177 pub const fn concat_option_bool(self, value: Option<bool>) -> Self {
178 self.concat_value(match value {
179 None => 0,
180 Some(false) => 1,
181 Some(true) => 2,
182 })
183 }
184
185 // Concatenate a string to this buffer. The maximum string length is 255 bytes. For longer strings,
186 // use `concat_long_str()`.
187 //
188 // Strings are encoded as a `u8` length, followed by the utf8 data.
189 //
190 // This consumes self, which is convenient for the proc-macro code and also allows us to avoid
191 // allocated an extra buffer.
concat_str(mut self, string: &str) -> Self192 pub const fn concat_str(mut self, string: &str) -> Self {
193 assert!(string.len() < 256);
194 assert!(self.size + string.len() < BUF_SIZE);
195 self.bytes[self.size] = string.len() as u8;
196 self.size += 1;
197 let bytes = string.as_bytes();
198 let mut i = 0;
199 while i < bytes.len() {
200 self.bytes[self.size] = bytes[i];
201 self.size += 1;
202 i += 1;
203 }
204 self
205 }
206
207 // Concatenate a longer string to this buffer.
208 //
209 // Strings are encoded as a `u16` length, followed by the utf8 data.
210 //
211 // This consumes self, which is convenient for the proc-macro code and also allows us to avoid
212 // allocated an extra buffer.
concat_long_str(mut self, string: &str) -> Self213 pub const fn concat_long_str(mut self, string: &str) -> Self {
214 assert!(self.size + string.len() + 1 < BUF_SIZE);
215 let [lo, hi] = (string.len() as u16).to_le_bytes();
216 self.bytes[self.size] = lo;
217 self.bytes[self.size + 1] = hi;
218 self.size += 2;
219 let bytes = string.as_bytes();
220 let mut i = 0;
221 while i < bytes.len() {
222 self.bytes[self.size] = bytes[i];
223 self.size += 1;
224 i += 1;
225 }
226 self
227 }
228
229 // Create an array from this MetadataBuffer
230 //
231 // SIZE should always be `self.size`. This is part of the kludge to hold us over until Rust
232 // gets better const generic support.
into_array<const SIZE: usize>(self) -> [u8; SIZE]233 pub const fn into_array<const SIZE: usize>(self) -> [u8; SIZE] {
234 let mut result: [u8; SIZE] = [0; SIZE];
235 let mut i = 0;
236 while i < SIZE {
237 result[i] = self.bytes[i];
238 i += 1;
239 }
240 result
241 }
242
243 // Create a checksum from this MetadataBuffer
244 //
245 // This is used by the bindings code to verify that the library they link to is the same one
246 // that the bindings were generated from.
checksum(&self) -> u16247 pub const fn checksum(&self) -> u16 {
248 calc_checksum(&self.bytes, self.size)
249 }
250 }
251
252 impl AsRef<[u8]> for MetadataBuffer {
as_ref(&self) -> &[u8]253 fn as_ref(&self) -> &[u8] {
254 &self.bytes[..self.size]
255 }
256 }
257
258 // Create a checksum for a MetadataBuffer
259 //
260 // This is used by the bindings code to verify that the library they link to is the same one
261 // that the bindings were generated from.
checksum_metadata(buf: &[u8]) -> u16262 pub const fn checksum_metadata(buf: &[u8]) -> u16 {
263 calc_checksum(buf, buf.len())
264 }
265
calc_checksum(bytes: &[u8], size: usize) -> u16266 const fn calc_checksum(bytes: &[u8], size: usize) -> u16 {
267 // Taken from the fnv_hash() function from the FNV crate (https://github.com/servo/rust-fnv/blob/master/lib.rs).
268 // fnv_hash() hasn't been released in a version yet.
269 const INITIAL_STATE: u64 = 0xcbf29ce484222325;
270 const PRIME: u64 = 0x100000001b3;
271
272 let mut hash = INITIAL_STATE;
273 let mut i = 0;
274 while i < size {
275 hash ^= bytes[i] as u64;
276 hash = hash.wrapping_mul(PRIME);
277 i += 1;
278 }
279 // Convert the 64-bit hash to a 16-bit hash by XORing everything together
280 (hash ^ (hash >> 16) ^ (hash >> 32) ^ (hash >> 48)) as u16
281 }
282