1
1
# Coding conventions
2
2
3
- ## Formatting
3
+ One of our major goals with OpenVMM is to provide a high quality coding
4
+ experience for contributors, starting first-and-foremost by having a consistent
5
+ set of coding conventions in the project.
4
6
5
- ** TL;DR:** Run [ ` cargo xtask fmt ` ] ( ./scripts.md#cargo-xtask ) to catch (and fix)
6
- formatting issues.
7
+ [ Do your part] ( https://youtu.be/-_7FaWnlhS4?t=10 ) and keep OpenVMM clean!
7
8
8
- ### ` rustfmt `
9
+ ## ` rustfmt `
9
10
10
- (enforced using [ ` cargo xtask fmt rustfmt ` ] ( ./scripts .md#cargo-xtask ) )
11
+ _ Checked Automatically: _ ** Yes ** (via [ ` cargo xtask fmt rustfmt ` ] ( ../dev_tools/xtask .md ) )
11
12
12
13
OpenVMM source must be formatted using
13
14
[ rustfmt] ( https://github.com/rust-lang/rustfmt ) , which automatically and
@@ -30,28 +31,32 @@ rules that are must be manually enforced:
30
31
> If you're working in a file and notice that it isn't following a certain
31
32
> convention, please take a moment to fix it!
32
33
33
- Assuming you've followed the guide and set up ` rust-analyzer ` to
34
- [ format on save] ( ../ide_setup.md#enabling-format-on-save ) , you should rarely have
35
- to think about formatting in ` .rs ` files.
36
-
37
- ### Project-specific formatting rules
34
+ Assuming you've followed the [ suggested dev env setup] ( ../getting_started/suggested_dev_env.md )
35
+ and set up ` rust-analyzer ` to format-on-save, you should rarely have to think
36
+ about formatting in ` .rs ` files.
38
37
39
- Aside from using ecosystem-standard ` rustfmt ` formatting, OpenVMM also includes a
40
- handful of (automatically enforced) project-specific formatting rules.
38
+ ## House Rules
41
39
42
- #### House Rules
40
+ _ Checked Automatically: _ ** Yes ** (via [ ` cargo xtask fmt house-rules ` ] ( ../dev_tools/xtask.md ) )
43
41
44
- (enforced using [ ` cargo xtask fmt house-rules ` ] ( ./scripts.md#cargo-xtask ) )
42
+ "House-rules" are a set of misc code lints that are specific to OpenVMM, which
43
+ are enforced using a custom in-house tool:
45
44
46
- These are a few minor bits of code-style that are specific to OpenVMM:
45
+ - enforce the presence of the standard Microsoft copyright header
46
+ - enforce in-repo crate names don't use '-' in their name (use '_ ' instead)
47
+ - enforce Cargo.toml files don't include autogenerated "see more keys" comments
48
+ - enforce Cargo.toml files don't contain author or version fields
49
+ - enforce files end with a single trailing newline
50
+ - deny usage of ` #[repr(packed)] ` (you want ` #[repr(C, packed)] ` )
51
+ - justify usage of ` cfg(target_arch = ...) ` (use ` guest_arch ` instead!)
52
+ - justify usage of ` allow(unsafe_code) ` with an UNSAFETY comment
47
53
48
- - Enforcing the standard Microsoft copyright header at the top of each file
49
- - Enforcing single-trailing-newlines in code
50
- - Ensuring crate names do not include ` - ` in their name (use ` _ ` instead!)
54
+ Some of these lints are self explanatory, whereas others are described in more
55
+ detail elsewhere on this page.
51
56
52
- #### Unused ` Cargo.toml ` Dependencies
57
+ ## Unused ` Cargo.toml ` Dependencies
53
58
54
- (enforced using [ ` cargo xtask fmt unused-deps ` ] ( ./scripts .md#cargo-xtask ) )
59
+ _ Checked Automatically: _ ** Yes ** (via [ ` cargo xtask fmt unused-deps ` ] ( ../dev_tools/xtask .md ) )
55
60
56
61
We have an in-repo fork of
57
62
[ ` cargo-machete ` ] ( https://github.com/bnjbvr/cargo-machete ) that ensures
@@ -61,9 +66,9 @@ Avoiding unused dependencies makes it easier to reason about what a crate is
61
66
doing just by looking at its dependencies, and also helps cut-down on
62
67
incremental compile times.
63
68
64
- #### Formatting ` Cargo.toml `
69
+ ## Formatting ( ` Cargo.toml ` )
65
70
66
- ** Not yet enforced by ` cargo xtask fmt ` ! Coming Soon™️ **
71
+ _ Checked Automatically: _ ** No **
67
72
68
73
When defining dependencies in ` Cargo.toml ` files, please organize dependencies
69
74
into the following groups:
@@ -89,11 +94,11 @@ So, for example:
89
94
90
95
``` toml
91
96
[package ]
92
- name = " hvlite "
97
+ name = " openvmm "
93
98
94
99
[dependencies ]
95
100
# crate-specific subcrates
96
- hvlite_core .workspace = true
101
+ openvmm_core .workspace = true
97
102
...
98
103
99
104
# /vmcore
@@ -118,13 +123,15 @@ clap.workspace = true
118
123
...
119
124
```
120
125
121
- ## Linting (` clippy ` )
126
+ ## Linting (via ` clippy ` )
127
+
128
+ _ Checked Automatically:_ ** Yes**
122
129
123
130
OpenVMM uses [ ` cargo clippy ` ] ( https://github.com/rust-lang/rust-clippy ) to
124
131
supplement rustc's built-in lints.
125
132
126
133
Assuming you've followed the guide and set up ` rust-analyzer ` to
127
- [ use clippy] ( ../ide_setup .md#enabling-clippy ) , you should see clippy lints
134
+ [ use clippy] ( ../getting_started/suggested_dev_env .md#enabling-clippy ) , you should see clippy lints
128
135
appear inline when working on Rust code.
129
136
130
137
The CI runs ` cargo clippy ` on every crate in the repo prior to building the
@@ -154,18 +161,18 @@ libc::cmsghdr {
154
161
}
155
162
```
156
163
157
- ### OpenVMM's Project-Specific ` clippy ` Configuration
164
+ ### OpenVMM's ` clippy ` Configuration
158
165
159
166
We stick fairly close to the default set of rustc / clippy lints, though there
160
167
are some default lints that we've decided to disabled project wide, and other
161
168
non-default lints which we've explicitly opted into.
162
169
163
- See
164
- [ ` .cargo/config. toml` ] ( https://github.com/microsoft/openvmm?path=/.cargo/config .toml )
170
+ See the ` [workspace.lints] ` sections of OpenVMM's root
171
+ [ ` Cargo. toml` ] ( https://github.com/microsoft/openvmm?path=/Cargo .toml )
165
172
for a list of globally enabled/disabled lints, along with justification as to
166
173
why certain lints have been enabled/disabled.
167
174
168
- ## Unsafe Code
175
+ ## Unsafe Code Policy
169
176
170
177
> _ Preface:_ When possible, try to avoid introducing new ` unsafe ` code!
171
178
>
@@ -198,29 +205,24 @@ Editing a file containing unsafe code will trigger CI to automatically add the
198
205
OpenVMM Unsafe Approvers group to your PR. This is to ensure that all unsafe code
199
206
is audited for correctness by area experts.
200
207
201
- ## Additional special considerations
202
-
203
- The following rules are unique to the OpenVMM codebase, and may be unfamiliar to
204
- devs coming from other Rust open-source projects.
208
+ ## Uses of ` cfg(target_arch = ...) ` must be justified
205
209
206
- ### Uses of ` cfg(target_arch = ...) ` must be justified
210
+ _ Checked Automatically: _ ** Yes ** (via [ ` cargo xtask fmt house-rules ` ] ( ../dev_tools/xtask.md ) )
207
211
208
- ** TL;DR:** Unless you're working on something that's * genuinely* tied to the
209
- host's CPU architecture, you almost certainly want to use `cfg(guest_arch =
210
- ...)` instead.
212
+ Unless you're working on something that's genuinely tied to the host's CPU
213
+ architecture, you should use ` cfg(guest_arch = ...) ` instead of ` cfg(target_arch = ...) ` .
211
214
212
- OpenVMM is a multi-architecture VMM framework, capable of running both x86 and
213
- Aarch64 guests (and potentially others in the future).
215
+ OpenVMM is a multi-architecture VMM framework, capable of running running x64
216
+ guests on a x64 host, as well as Aarch64 guests on Aarch64 hosts (with the
217
+ potential of adding additional platforms in the future).
214
218
215
- Often times, the host architecture and the guest architecture will match,
216
- thereby enabling the use of hardware-accelerated CPU virtualization backends
217
- (e.g: KVM on Linux, WHP in Windows, etc...). That said, it's probable that at
218
- some point in the future, OpenVMM will also support * mismatched* guest/host
219
- architectures via an emulated CPU virtualization backend (akin to what QEMU
220
- does).
219
+ At the moment, OpenVMM requires that the host architecture and guest
220
+ architecture match. That said, it's possible that at some point in the future,
221
+ OpenVMM may also support _ mismatched_ guest/host architectures, via an emulated
222
+ CPU virtualization backend (akin to QEMU).
221
223
222
- Having an emulated CPU backend would enable
223
- OpenVMM to support such useful scenarios as:
224
+ Having an emulated CPU backend would enable OpenVMM to support such useful
225
+ scenarios as:
224
226
225
227
- running Arch64 guests on x86 machines
226
228
- running x86 guests on ARM
@@ -251,7 +253,9 @@ intrinsic (such as CPUID, or a SIMD instruction), or when implementing a
251
253
252
254
...otherwise, use ` cfg(guest_arch = ...) ` !
253
255
254
- ### Avoid ` Default ` when using ` zerocopy::FromZeroes `
256
+ ## Avoid ` Default ` when using ` zerocopy::FromZeroes `
257
+
258
+ _ Checked Automatically:_ ** No**
255
259
256
260
The rule:
257
261
@@ -266,7 +270,7 @@ The why:
266
270
- There are plenty of types that don't have a ` Default ` value, but _ do_ have a
267
271
valid all-zero repr
268
272
269
- #### Additional context
273
+ ### Additional context
270
274
271
275
As per the Rust docs for [ ` Default::default ` ] ( https://doc.rust-lang.org/std/default/trait.Default.html#tymethod.default ) :
272
276
@@ -288,7 +292,7 @@ they are initially allocated as all-zero, and then get "filled in" by some
288
292
secondary init code. Notably: it's quite rare for that initial "all-zero" struct
289
293
to be a valid instance of the type!
290
294
291
- ##### Example: C FFI
295
+ #### Example: C FFI
292
296
293
297
For example, a common pattern in C libraries might look something like:
294
298
@@ -372,7 +376,9 @@ impl Default for Handle {
372
376
}
373
377
```
374
378
375
- ### Avoid Requiring ` Debug ` on Traits
379
+ ## Avoid Requiring ` Debug ` on Traits
380
+
381
+ _ Checked Automatically:_ ** No**
376
382
377
383
** TL;DR:** Don't do this:
378
384
@@ -396,7 +402,9 @@ Moreover, it's usually not good form to leave tracing statements that log a
396
402
struct's ` Debug ` representation in production. Prefer tracing just the fields
397
403
you're interested in, and/or connecting objects to the ` Inspect ` graph.
398
404
399
- ### Crate Naming
405
+ ## Crate Naming
406
+
407
+ _ Checked Automatically:_ ** Yes** (via [ ` cargo xtask fmt house-rules ` ] ( ../dev_tools/xtask.md ) )
400
408
401
409
** Crates must be named with underscores, not dashes and underscores used in
402
410
folder names.**
0 commit comments