Skip to content

Commit

Permalink
build: Rust 1.39 update
Browse files Browse the repository at this point in the history
- cargo: configured the build dir via `.cargo/config`;
- devctr: removed env variable used to override build dir; updated rust
  toolchain to 1.39;
- clippy: added safety doc comments;
- vsock: switched to using zeroed memory for the TX buffer, instead of
  `mem::uninitialized`, since the latter is both unsafe and deprecated
  by Rust 1.39.

Signed-off-by: Dan Horobeanu <[email protected]>
  • Loading branch information
dhrgit authored and acatangiu committed Nov 29, 2019
1 parent b6f4636 commit a8c9dff
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 25 deletions.
1 change: 1 addition & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[build]
target = "x86_64-unknown-linux-musl"
target-dir = "build/cargo_target"

[target.'cfg(any(target_arch="arm", target_arch="aarch64"))']
# On aarch64 musl depends on some libgcc functions (i.e `__addtf3` and other `*tf3` functions) for logic that uses
Expand Down
17 changes: 4 additions & 13 deletions src/devices/src/virtio/vsock/csm/txbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//

use std::io::Write;
use std::mem;
use std::num::Wrapping;

use super::defs;
Expand All @@ -14,7 +13,7 @@ use super::{Error, Result};
/// the host can't read fast enough.
pub struct TxBuf {
/// The actual u8 buffer - only allocated after the first push.
data: Option<Box<[u8; Self::SIZE]>>,
data: Option<Box<[u8]>>,
/// Ring-buffer head offset - where new data is pushed to.
head: Wrapping<u32>,
/// Ring-buffer tail offset - where data is flushed from.
Expand Down Expand Up @@ -50,17 +49,9 @@ impl TxBuf {
return Err(Error::TxBufFull);
}

// We're using a closure here to return the boxed slice, instead of a value (i.e.
// `get_or_insert_with()` instead of `get_or_insert()`), because we only want the box
// created when `self.data` is None. If we were to use `get_or_insert(box)`, the box
// argument would always get evaluated (which implies a heap allocation), even though
// it would later be discarded (when `self.data.is_some()`). Apparently, clippy fails
// to see this, and insists on issuing some warning.
let data = self.data.get_or_insert_with(||
// Using uninitialized memory here is quite safe, since we never read from any
// area of the buffer before writing to it. First we push, then we flush only
// what had been prviously pushed.
Box::new(unsafe {mem::uninitialized::<[u8; Self::SIZE]>()}));
let data = self
.data
.get_or_insert_with(|| vec![0u8; Self::SIZE].into_boxed_slice());

// Buffer head, as an offset into the data slice.
let head_ofs = self.head.0 as usize % Self::SIZE;
Expand Down
16 changes: 10 additions & 6 deletions src/utils/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ impl fmt::Display for Error {
pub type Result<T> = std::result::Result<T, Error>;

/// Reads a struct from an input buffer.
/// This is unsafe because the struct is initialized to unverified data read from the input.
/// `read_struct` should only be called to fill plain old data structs. It is not endian safe.
///
/// # Arguments
///
/// * `f` - The input to read from. Often this is a file.
/// * `out` - The struct to fill with data read from `f`.
///
/// # Safety
///
/// This is unsafe because the struct is initialized to unverified data read from the input.
/// `read_struct` should only be called to fill plain old data structs. It is not endian safe.
pub unsafe fn read_struct<T: Copy, F: Read>(f: &mut F, out: &mut T) -> Result<()> {
let out_slice = std::slice::from_raw_parts_mut(out as *mut T as *mut u8, mem::size_of::<T>());
f.read_exact(out_slice).map_err(Error::ReadStruct)?;
Expand All @@ -41,15 +44,16 @@ pub unsafe fn read_struct<T: Copy, F: Read>(f: &mut F, out: &mut T) -> Result<()

/// Reads an array of structs from an input buffer. Returns a Vec of structs initialized with data
/// from the specified input.
/// This is unsafe because the structs are initialized to unverified data read from the input.
/// `read_struct_slice` should only be called for plain old data structs. It is not endian safe.
///
/// # Arguments
///
/// * `f` - The input to read from. Often this is a file.
/// * `len` - The number of structs to fill with data read from `f`.
// This lint check is now deprecated - https://github.com/rust-lang/rust-clippy/pull/3478/files
// we can safely allow this.
///
/// # Safety
///
/// This is unsafe because the structs are initialized to unverified data read from the input.
/// `read_struct_slice` should only be called for plain old data structs. It is not endian safe.
pub unsafe fn read_struct_slice<T: Copy, F: Read>(f: &mut F, len: usize) -> Result<Vec<T>> {
let mut out: Vec<T> = Vec::with_capacity(len);
out.set_len(len);
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/build/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import host_tools.cargo_build as host # pylint: disable=import-error

COVERAGE_TARGET_PCT = 84.8
COVERAGE_TARGET_PCT = 85.2
COVERAGE_MAX_DELTA = 0.01

CARGO_KCOV_REL_PATH = os.path.join(host.CARGO_BUILD_REL_PATH, 'kcov')
Expand Down
3 changes: 1 addition & 2 deletions tools/devctr/Dockerfile.aarch64
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FROM ubuntu:18.04
# The Rust toolchain layer will get updated most frequently, but we could keep the system
# dependencies layer intact for much longer.

ARG RUST_TOOLCHAIN="1.38.0"
ARG RUST_TOOLCHAIN="1.39.0"
ARG TMP_BUILD_DIR=/tmp/build
ARG FIRECRACKER_SRC_DIR="/firecracker"
ARG FIRECRACKER_BUILD_DIR="$FIRECRACKER_SRC_DIR/build"
Expand All @@ -14,7 +14,6 @@ ARG CARGO_GIT_REGISTRY_DIR="$FIRECRACKER_BUILD_DIR/cargo_git_registry"
ENV CARGO_HOME=/usr/local/rust
ENV RUSTUP_HOME=/usr/local/rust
ENV PATH="$PATH:$CARGO_HOME/bin"
ENV CARGO_TARGET_DIR="$FIRECRACKER_BUILD_DIR/cargo_target"

# Install system dependecies
#
Expand Down
3 changes: 1 addition & 2 deletions tools/devctr/Dockerfile.x86_64
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FROM ubuntu:18.04
# The Rust toolchain layer will get updated most frequently, but we could keep the system
# dependencies layer intact for much longer.

ARG RUST_TOOLCHAIN="1.38.0"
ARG RUST_TOOLCHAIN="1.39.0"
ARG TMP_BUILD_DIR=/tmp/build
ARG FIRECRACKER_SRC_DIR="/firecracker"
ARG FIRECRACKER_BUILD_DIR="$FIRECRACKER_SRC_DIR/build"
Expand All @@ -14,7 +14,6 @@ ARG CARGO_GIT_REGISTRY_DIR="$FIRECRACKER_BUILD_DIR/cargo_git_registry"
ENV CARGO_HOME=/usr/local/rust
ENV RUSTUP_HOME=/usr/local/rust
ENV PATH="$PATH:$CARGO_HOME/bin"
ENV CARGO_TARGET_DIR="$FIRECRACKER_BUILD_DIR/cargo_target"

# Install system dependecies
#
Expand Down
2 changes: 1 addition & 1 deletion tools/devtool
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
# Development container image (name:tag)
# This should be updated whenever we upgrade the development container.
# (Yet another step on our way to reproducible builds.)
DEVCTR_IMAGE="fcuvm/dev:v12"
DEVCTR_IMAGE="fcuvm/dev:v13"

# Naming things is hard
MY_NAME="Firecracker $(basename "$0")"
Expand Down

0 comments on commit a8c9dff

Please sign in to comment.