crosvm/bin/clippy
Daniel Verkamp ca0aed3daa hypervisor: kvm: replace mem::transmute with safe loops
The conversion code for KVM <-> hypervisor representation of the Local
APIC state used the unsafe mem::transmute() function to view an array of
i8 as u8 instead for use with the Rust endian conversion functions.
Casting between integer types of the same size with `as` is defined in
Rust as a "no-op" (the bitwise representation is preserved), just like
in C, so transmuting at the slice level is not needed. These can instead
be written as simple loops to avoid the unsafe code.

To ensure this does not regress code quality, I have compared the code
generated for the x86-64 release build.  The kvm_lapic_state to
LapicState conversion compiles to identical code, and the reverse
compiles to slightly different code (the compiler decides to emit a loop
instead of unrolling the 64-element copy), but the conversion of each
element still compiles down to a pair of MOV instructions.

The corresponding unit test has also been updated to avoid transmute, as
it was unnecessary there - the individual array element can be cast with
the `as` operator rather than transmuting the whole array.

BUG=None
TEST=cargo test -p hypervisor

Change-Id: I7e792b5507235e5234afe114a1ca744931e047d5
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2947934
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
2021-08-12 19:27:06 +00:00

99 lines
2.4 KiB
Bash
Executable file

#!/bin/bash
# Copyright 2019 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Run `cargo clippy` on all Rust code in crosvm with a mindful set of lints
# suppressed.
set -eo pipefail
USE_CACHE=false
CLIPPY_ARGS=("$@")
# TODO: When we add more options, use a fancier parsing mechanism such as
# getopts. Also use the Rust convention of -- separating the arguments for this
# script itself from the ones for clippy.
if (("$#" > 0)) && [[ "$1" == "--use-cache" ]]; then
USE_CACHE=true
CLIPPY_ARGS=("${CLIPPY_ARGS[@]:1}")
fi
# Change into directory of script, which is crosvm/bin.
cd "$(dirname "${BASH_SOURCE[0]}")"
# Jump up to root directory of crosvm repo.
cd ..
SUPPRESS=(
# TODO(crbug/908640): To be resolved.
collapsible_if
comparison_chain
missing_safety_doc
wrong_self_convention
# To be fixed in external libraries
upper_case_acronyms
from_over_into
# False positives affecting WlVfd @ `devices/src/virtio/wl.rs`.
# Bug: https://github.com/rust-lang/rust-clippy/issues/6312
field_reassign_with_default
# We don't care about these lints. Okay to remain suppressed globally.
cast_lossless
cognitive_complexity
enum_variant_names
identity_op
len_without_is_empty
len_zero
match_bool
match_wild_err_arm
module_inception
needless_bool
new_without_default
or_fun_call
should_implement_trait
single_char_pattern
too_many_arguments
trivially_copy_pass_by_ref
type_complexity
unreadable_literal
useless_let_if_seq
useless_transmute
new-ret-no-self
)
FEATURES=(
default
direct
audio
gpu
plugin
tpm
usb
video-decoder
video-encoder
wl-dmabuf
x
virgl_renderer_next
composite-disk
virgl_renderer
gfxstream
gdb
)
printf -v FEATURES_LIST '%s,' "${FEATURES[@]}"
# Needed or else clippy won't re-run on code that has already compiled.
if [[ "${USE_CACHE}" == false ]]; then
cargo clean
fi
# Need to set pass --sysroot for cargo-clippy manually.
# cf. https://github.com/rust-lang/rust-clippy/issues/3523
RUST_SYSROOT=$(rustc --print sysroot)
RUSTFLAGS="${RUSTFLAGS:-}"
export RUSTFLAGS="$RUSTFLAGS --sysroot=$RUST_SYSROOT"
cargo clippy --features ${FEATURES_LIST} --all-targets -- \
${SUPPRESS[@]/#/-Aclippy::} "${CLIPPY_ARGS[@]}" -D warnings