microsoft/openvmm

Public

mirrored from https://github.com/microsoft/openvmmAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
bf06c4c91e2907d0f898c19c69e54cbac1e23ae4

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Guide/src/dev_guide/contrib/code.md

443lines · modecode

1# Coding conventions
2
3One of our major goals with OpenVMM is to provide a high quality coding
4experience for contributors, starting first-and-foremost by having a consistent
5set of coding conventions in the project.
6
7[Do your part](https://youtu.be/-_7FaWnlhS4?t=10) and keep OpenVMM clean!
8
9## `rustfmt`
10
11_Checked Automatically:_ **Yes** (via [`cargo xtask fmt rustfmt`](../dev_tools/xtask.md))
12
13OpenVMM source must be formatted using
14[rustfmt](https://github.com/rust-lang/rustfmt), which automatically and
15mechanically applies standard formatting to all the code. This eliminates time
16spent discussing or reviewing stylistic issues in pull requests.
17
18The CI will run `rustfmt --check` to enforce consistent formatting, and will
19fail if it notices any discrepancies.
20
21Unfortunately, `rustfmt` isn't infinitely customizable, and there are several
22rules that are must be manually enforced:
23
24- All lines must end with LF, not CRLF.
25- Top-level `use` imports should be non-nested.
26
27```admonish note
28Some of these manually-enforced conventions were introduced late into
29OpenVMM's development, and there may be chunks of the codebase that do not
30adhere to these conventions.
31
32If you're working in a file and notice that it isn't following a certain
33convention, please take a moment to fix it!
34```
35
36Assuming you've followed the [suggested dev env setup](../getting_started/suggested_dev_env.md)
37and set up `rust-analyzer` to format-on-save, you should rarely have to think
38about formatting in `.rs` files.
39
40## House Rules
41
42_Checked Automatically:_ **Yes** (via [`cargo xtask fmt house-rules`](../dev_tools/xtask.md))
43
44"House-rules" are a set of misc code lints that are specific to OpenVMM, which
45are enforced using a custom in-house tool:
46
47- enforce the presence of the standard Microsoft copyright header
48- enforce in-repo crate names don't use '-' in their name (use '_' instead)
49- enforce Cargo.toml files don't include autogenerated "see more keys" comments
50- enforce Cargo.toml files don't contain author or version fields
51- enforce files end with a single trailing newline
52- deny usage of `#[repr(packed)]` (you want `#[repr(C, packed)]`)
53- justify usage of `cfg(target_arch = ...)` (use `guest_arch` instead!)
54- justify usage of `allow(unsafe_code)` with an UNSAFETY comment
55
56Some of these lints are self explanatory, whereas others are described in more
57detail elsewhere on this page.
58
59## Unused `Cargo.toml` Dependencies
60
61_Checked Automatically:_ **Yes** (via [`cargo xtask fmt unused-deps`](../dev_tools/xtask.md))
62
63We have an in-repo fork of
64[`cargo-machete`](https://github.com/bnjbvr/cargo-machete) that ensures
65`Cargo.toml` files only include dependencies that are actually being used.
66
67Avoiding unused dependencies makes it easier to reason about what a crate is
68doing just by looking at its dependencies, and also helps cut-down on
69incremental compile times.
70
71## Formatting (`Cargo.toml`)
72
73_Checked Automatically:_ **No**
74
75When defining dependencies in `Cargo.toml` files, please organize dependencies
76into the following groups:
77
78- crate-specific "subcrates"
79- crates under vm/
80- crates under vm/vmcore/
81- crates under support/
82- external dependencies
83
84The rationale here is that crates should be grouped according to how related to
85how _widely applicable_ they are. i.e: crates from crates.io and support are
86widely applicable outside of OpenVMM, whereas crates under vmcore/ only make
87sense within the context of OpenVMM, and crate-specific subcrates are - by
88definition - only applicable to the crate they are being imported from.
89
90Additionally, we make use of the
91[workspace dependencies](https://doc.rust-lang.org/nightly/cargo/reference/workspaces.html#the-dependencies-table)
92feature to ensure that all our dependencies stay in sync. This requires defining
93dependencies in your crate's `Cargo.toml` file and in the project's root `Cargo.toml`.
94
95So, for example:
96
97```toml
98[package]
99name = "openvmm"
100
101[dependencies]
102# crate-specific subcrates
103openvmm_core.workspace = true
104...
105
106# /vmcore
107vmcore.workspace = true
108...
109
110# /vm/devices
111firmware_uefi_custom_vars.workspace = true
112storvsp.workspace = true
113...
114
115# /support
116guid.workspace = true
117inspect.workspace = true
118inspect_proto.workspace = true
119...
120
121# external dependencies
122anyhow.workspace = true
123cfg-if.workspace = true
124clap.workspace = true
125...
126```
127
128## Linting (via `clippy`)
129
130_Checked Automatically:_ **Yes**
131
132OpenVMM uses [`cargo clippy`](https://github.com/rust-lang/rust-clippy) to
133supplement rustc's built-in lints.
134
135Assuming you've followed the guide and set up `rust-analyzer` to
136[use clippy](../getting_started/suggested_dev_env.md#enabling-clippy), you should see clippy lints
137appear inline when working on Rust code.
138
139The CI runs `cargo clippy` on every crate in the repo prior to building the
140project, and will fast-fail if it catches any warnings / errors.
141
142### Suppressing Lints
143
144In general, lints should be fixed by modifying the code to satisfy the lint.
145However, there are cases where a lint may need to be `allow`'d inline.
146
147In these cases, you _must_ provide a inline comment providing reasonable
148justification for the suppressed lint.
149
150e.g:
151
152```rust
153// x86_64-unknown-linux-musl targets have a different type defn for
154// `libc::cmsghdr`, hence why these lints are being suppressed.
155#[allow(clippy::needless_update, clippy::useless_conversion)]
156libc::cmsghdr {
157 cmsg_level: libc::SOL_SOCKET,
158 cmsg_type: libc::SCM_RIGHTS,
159 cmsg_len: (size_of::<libc::cmsghdr>() + size_of_val(fds))
160 .try_into()
161 .unwrap(),
162 ..std::mem::zeroed()
163}
164```
165
166### OpenVMM's `clippy` Configuration
167
168We stick fairly close to the default set of rustc / clippy lints, though there
169are some default lints that we've decided to disabled project wide, and other
170non-default lints which we've explicitly opted into.
171
172See the `[workspace.lints]` sections of OpenVMM's root
173[`Cargo.toml`](https://github.com/microsoft/openvmm?path=/Cargo.toml)
174for a list of globally enabled/disabled lints, along with justification as to
175why certain lints have been enabled/disabled.
176
177## Unsafe Code Policy
178
179```admonish danger
180When possible, try to avoid introducing new `unsafe` code!
181
182Before rolling your own `unsafe` code, check to see if a safe abstraction
183already exists, either in-tree, on crates.io\*, or in the standard library.
184
185\*subject to an unsafe-code audit
186```
187
188Rather than synthesizing our own unsafe code conventions, we follow the
189guidelines outlined in the following two resources:
190
191- [The Rust `unsafe` docs](https://doc.rust-lang.org/std/keyword.unsafe.html)
192- [The Fuchsia "Unsafe code in Rust" Guidelines](https://fuchsia.googlesource.com/fuchsia/+/refs/heads/main/docs/development/languages/rust/unsafe.md)
193
194In a nutshell:
195
196- `unsafe` functions are required to include `/// # Safety` documentation
197 describing any preconditions the caller must uphold when calling the function.
198- `unsafe {}` blocks are required to include a `// SAFETY:` comment describing
199 how the preconditions for calling the `unsafe` function(s) within the block
200 are being satisfied.
201- `allow(unsafe_code)` annotations are required to include an `// UNSAFETY:`
202 comment justifying why the code in question needs to use `unsafe`. This
203 annotation must be placed at the module or crate level.
204
205These requirements are enforced by CI, and will cause the build to fail if
206required documentation is missing.
207
208Editing a file containing unsafe code will trigger CI to automatically add the
209OpenVMM Unsafe Approvers group to your PR. This is to ensure that all unsafe code
210is audited for correctness by area experts.
211
212## Uses of `cfg(target_arch = ...)` must be justified
213
214_Checked Automatically:_ **Yes** (via [`cargo xtask fmt house-rules`](../dev_tools/xtask.md))
215
216Unless you're working on something that's genuinely tied to the host's CPU
217architecture, you should use `cfg(guest_arch = ...)` instead of `cfg(target_arch = ...)`.
218
219OpenVMM is a multi-architecture VMM framework, capable of running running x64
220guests on a x64 host, as well as Aarch64 guests on Aarch64 hosts (with the
221potential of adding additional platforms in the future).
222
223At the moment, OpenVMM requires that the host architecture and guest
224architecture match. That said, it's possible that at some point in the future,
225OpenVMM may also support _mismatched_ guest/host architectures, via an emulated
226CPU virtualization backend (akin to QEMU).
227
228Having an emulated CPU backend would enable OpenVMM to support such useful
229scenarios as:
230
231- running Arch64 guests on x86 machines
232- running x86 guests on ARM
233- running something exotic (e.g: RISC-V) on x86/ARM machines
234 - ...assuming we had the bandwidth to implement + maintain something like that
235- running OpenVMM on systems without hardware-accelerated CPU virtualization enabled
236
237With these scenarios in mind, it would be short-sighted to rely entirely on
238`cfg(target_arch = ...)` to gate guest-facing arch-specific functionality, as it
239would inexorably tie the guest's arch to the host's arch, making any future
240initiatives to pry the two apart significantly more difficult!
241
242As such, the `OpenVMM` repo includes infrastructure to specify a custom,
243OpenVMM-specific `cfg(guest_arch = ...)` cfg parameter.
244
245By default, `cfg(guest_arch = ...)` will act the same way as `cfg(target_arch =
246...)`, but it can be swapped to a different architecture by setting the
247`OPENVMM_GUEST_TARGET` env var at compile-time.
248
249There are very few reasons to use `cfg(target_arch = ...)` within the OpenVMM
250repo, and to enforce this rule, we have an in-house `xtask fmt house-rules`
251check that lints each use of `cfg(target_arch = ...)` to include a
252"justification" for why it's being used.
253
254e.g: `cfg(target_arch = ...)` would be applicable when feature-gating a CPU
255intrinsic (such as CPUID, or a SIMD instruction), or when implementing a
256`*-sys` crate where the underlying C API/ABI varies between architectures.
257
258...otherwise, use `cfg(guest_arch = ...)`!
259
260## Avoid `Default` when using `zerocopy::FromZeroes`
261
262_Checked Automatically:_ **No**
263
264The rule:
265
266- A type can `derive(Default)` **XOR** `derive(FromZeroes)`.
267- A type that is `FromZeroes` can also `impl Default`, but it must be a
268 conscious, explicit choice, with justification (read: inline comment) as to
269 why that particular default value was chosen.
270
271The why:
272
273- The all-zero type is often not a semantically valid `Default` value for a type
274- There are plenty of types that don't have a `Default` value, but _do_ have a
275 valid all-zero repr
276
277### Additional context
278
279As per the Rust docs for [`Default::default`](https://doc.rust-lang.org/std/default/trait.Default.html#tymethod.default):
280
281```admonish quote
282`fn default() -> Self`
283
284Returns the “default value” for a type.
285
286Default values are often some kind of initial value, identity value, or anything
287else that may make sense as a default.
288```
289
290Notably, **default should _not_ be used as some shorthand to "zero initialize"
291values!** For most non-trivial structs, the all-zero representation is _not_ a
292semantically valid `Default`!
293
294This is true in many contexts... but one that's particularly relevant in OpenVMM
295is that of FFI via C-style APIs and ABIs.
296
297In C, it's very common for types to undergo multi-stage instantiation, where
298they are initially allocated as all-zero, and then get "filled in" by some
299secondary init code. Notably: it's quite rare for that initial "all-zero" struct
300to be a valid instance of the type!
301
302#### Example: C FFI
303
304For example, a common pattern in C libraries might look something like:
305
306```c
307struct Handle {
308 uint16_t opaque_handle;
309}
310
311struct Handle handle = {0};
312init_handle(&handle);
313
314update_handle(handle, options);
315do_thing(handle);
316```
317
318In this example: it would be an error to invoke `update_handle` or `do_thing`
319with an all-zero handle, as the type hadn't finished being fully initialized.
320
321If we wanted to use this library from Rust, a "naive" approach would be to do
322something like:
323
324```rust
325#[repr(C)]
326#[derive(Default)]
327struct Handle {
328 opaque_handle: u16,
329}
330
331let mut handle = Handle::default(); // BAD!
332unsafe { init_handle(&handle) };
333
334unsafe { update_handle(handle, options) };
335unsafe { do_thing(handle) };
336```
337
338While this _technically_ works... using `Default` here is kinda bogus!
339
340After all - `Handle::default()` doesn't actually call `init_handle`, which means
341the value returned by `default()` doesn't match the "promise" of the trait!
342Namely: the returned value is not semantically valid yet!
343
344In many other Rust codebase, this "overloading" of `Default` to represent both
345semantically valid value _and_ all-zero values (in FFI) is par for the course,
346as once structs get more complicated, having a `derive` that is able to fully
347init a "uninitialized" struct in-memory is quite handy...
348
349In OpenVMM, we don't do this. Instead, we use a separate trait to init all-zero
350structs.
351
352**In OpenVMM, we use `FromZeroes` and `FromZeroes::new_zeroed()` to work with types
353that have valid all-zero representations, _without_ implying that those types
354also have valid all-zero _default_ values!**
355
356So, for the example above:
357
358```rust
359#[repr(C)]
360#[derive(zerocopy::FromZeroes)]
361struct Handle {
362 opaque_handle: u16
363}
364
365let mut handle = Handle::new_zeroed(); // GOOD!
366unsafe { init_handle(&handle) };
367```
368
369Now, it's impossible for code elsewhere to obtain a `Handle` via
370`Handle::default`, and mistakenly forget to invoke `init_handle` on it.
371
372...but if it so happens that we _do_ want a `Default` impl for `Handle`, we can
373do so by _manually_ implementing `derive(Default)` ourselves:
374
375```rust
376// Default + FromZeroes: `default` returns fully initialized handle
377impl Default for Handle {
378 fn default() -> Handle {
379 let mut handle = Handle::new_zeroed();
380 unsafe { init_handle(&handle) };
381 handle
382 }
383}
384```
385
386## Avoid Requiring `Debug` on Traits
387
388_Checked Automatically:_ **No**
389
390**TL;DR:** Don't do this:
391
392```rust
393trait MyTrait: std::fmt::Debug
394```
395
396Implementations of the standard library's `Debug` trait can be surprisingly large,
397and the final binary size of OpenHCL and related binaries is a major concern
398for us. Unused implementations of this trait are usually removed during the
399optimization process (like all dead code), making this a non-issue. However when
400traits, and more specifically trait objects, are involved, the compiler has a
401much more difficult time proving that implementations are unused. This can
402result in large amounts of functionally dead code ending up in the final
403binaries.
404
405If you need to implement `Debug` for a struct containing such a trait object, you
406will need to do so manually, so that you can skip over that field.
407
408Moreover, it's usually not good form to leave tracing statements that log a
409struct's `Debug` representation in production. Prefer tracing just the fields
410you're interested in, and/or connecting objects to the `Inspect` graph.
411
412## Crate Naming
413
414_Checked Automatically:_ **Yes** (via [`cargo xtask fmt house-rules`](../dev_tools/xtask.md))
415
416**Crates must be named with underscores, not dashes and underscores used in
417folder names.**
418
419- Bad: `my-cool-crate`
420- Good: `my_cool_crate`
421
422Rust does not allow dashes in imports, with any dashes getting replaced with
423underscores when used in the code. Avoiding dashes altogether makes it easier to
424grep for crate names, and makes things more consistent across the repo.
425
426This convention is enforced by CI
427
428**Do not name crates with the words "base, util, common" or other terms that are
429overly general.**
430
431For example, consider a crate that provides a common data structure used by
432multiple devices:
433
434- Bad: `devices_common`
435- Good: `range_map`
436
437Libraries that contain the following eventually become a mishmash of unrelated
438functionality that is located there for convenience. This
439[blog post](https://dave.cheney.net/2019/01/08/avoid-package-names-like-base-util-or-common)
440goes more in-depth as to why.
441
442Instead, name things based on what they logically provide, like functionality or
443data types.
444