Skip to content

Commit 2c50840

Browse files
celinvaltedinski
authored andcommitted
Add rmc-compiler binary to drive rust compilation (rust-lang#722)
This change introduces a new compilation driver (rmc-compiler) that no longer depends on a modified version of the rust compiler, neither depends on the bootstrap script. The rmc-compiler binary can be build using regular cargo build and can be used as a replacement to rustc with additional options that allow us to compile rust into goto-c.
1 parent 9e9619a commit 2c50840

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+574
-217
lines changed

.github/workflows/format-check.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- name: Get paths for files added
1616
id: git-diff
1717
run: |
18-
ignore='(.clang-format|.diff|.md|.props|expected|ignore|gitignore)$'
18+
ignore='(.clang-format|.diff|.md|.props|expected|ignore|gitignore|Cargo.lock)$'
1919
files=$(git diff --ignore-submodules=all --name-only --diff-filter=A ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep -v -E $ignore | xargs)
2020
echo "::set-output name=paths::$files"
2121

.github/workflows/rmc.yml

+3-5
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,11 @@ jobs:
4646
git config submodule."src/llvm-project".update none
4747
git submodule update --init --depth 1
4848
49-
- name: Build RMC
49+
- name: Build RMC and RMC Library
5050
run: |
5151
export RUST_BACKTRACE=1
52-
./x.py build -i --stage 1 library/std
53-
54-
- name: Build RMC Library
55-
run: bash -x ./scripts/setup/build_rmc_lib.sh
52+
cd src/rmc-compiler
53+
cargo build
5654
5755
- name: Execute RMC regression
5856
run: ./scripts/rmc-regression.sh

Cargo.lock

+18-17
Original file line numberDiff line numberDiff line change
@@ -3324,6 +3324,16 @@ dependencies = [
33243324
"rmc_macros",
33253325
]
33263326

3327+
[[package]]
3328+
name = "rmc-compiler"
3329+
version = "0.1.0"
3330+
dependencies = [
3331+
"clap",
3332+
"rmc_queries",
3333+
"rustc_codegen_rmc",
3334+
"tracing",
3335+
]
3336+
33273337
[[package]]
33283338
name = "rmc-link-restrictions"
33293339
version = "0.1.0"
@@ -3338,6 +3348,13 @@ dependencies = [
33383348
name = "rmc_macros"
33393349
version = "0.1.0"
33403350

3351+
[[package]]
3352+
name = "rmc_queries"
3353+
version = "0.1.0"
3354+
dependencies = [
3355+
"tracing",
3356+
]
3357+
33413358
[[package]]
33423359
name = "rmc_restrictions"
33433360
version = "0.1.0"
@@ -3890,24 +3907,9 @@ dependencies = [
38903907
"libc",
38913908
"measureme 9.1.2",
38923909
"num",
3910+
"rmc_queries",
38933911
"rmc_restrictions",
38943912
"rustc-demangle",
3895-
"rustc_arena",
3896-
"rustc_ast",
3897-
"rustc_attr",
3898-
"rustc_codegen_ssa",
3899-
"rustc_data_structures",
3900-
"rustc_errors",
3901-
"rustc_fs_util",
3902-
"rustc_hir",
3903-
"rustc_index",
3904-
"rustc_llvm",
3905-
"rustc_metadata",
3906-
"rustc_middle",
3907-
"rustc_serialize",
3908-
"rustc_session",
3909-
"rustc_span",
3910-
"rustc_target",
39113913
"serde",
39123914
"serde_json",
39133915
"smallvec",
@@ -4186,7 +4188,6 @@ dependencies = [
41864188
"rustc_borrowck",
41874189
"rustc_builtin_macros",
41884190
"rustc_codegen_llvm",
4189-
"rustc_codegen_rmc",
41904191
"rustc_codegen_ssa",
41914192
"rustc_const_eval",
41924193
"rustc_data_structures",

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ members = [
4141
"src/tools/html-checker",
4242
"src/tools/bump-stage0",
4343
"src/tools/lld-wrapper",
44+
"src/rmc-compiler",
4445
]
4546

4647
exclude = [

compiler/rustc_codegen_rmc/Cargo.toml

-42
This file was deleted.

compiler/rustc_interface/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ rustc_data_structures = { path = "../rustc_data_structures" }
3131
rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }
3232
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
3333
rustc_codegen_llvm = { path = "../rustc_codegen_llvm", optional = true }
34-
rustc_codegen_rmc = { path = "../rustc_codegen_rmc" }
3534
rustc_hir = { path = "../rustc_hir" }
3635
rustc_metadata = { path = "../rustc_metadata" }
3736
rustc_const_eval = { path = "../rustc_const_eval" }

compiler/rustc_interface/src/tests.rs

-2
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,6 @@ fn test_debugging_options_tracking_hash() {
764764
tracked!(relax_elf_relocations, Some(true));
765765
tracked!(relro_level, Some(RelroLevel::Full));
766766
tracked!(remap_cwd_prefix, Some(PathBuf::from("abc")));
767-
tracked!(emit_vtable_restrictions, true);
768767
tracked!(report_delayed_bugs, true);
769768
tracked!(sanitizer, SanitizerSet::ADDRESS);
770769
tracked!(sanitizer_memory_track_origins, 2);
@@ -776,7 +775,6 @@ fn test_debugging_options_tracking_hash() {
776775
tracked!(src_hash_algorithm, Some(SourceFileHashAlgorithm::Sha1));
777776
tracked!(stack_protector, StackProtector::All);
778777
tracked!(symbol_mangling_version, Some(SymbolManglingVersion::V0));
779-
tracked!(symbol_table_passes, vec![String::from("identity")]);
780778
tracked!(teach, true);
781779
tracked!(thinlto, Some(true));
782780
tracked!(thir_unsafeck, true);

compiler/rustc_interface/src/util.rs

-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ pub fn get_codegen_backend(
254254
filename if filename.contains('.') => load_backend_from_dylib(filename.as_ref()),
255255
#[cfg(feature = "llvm")]
256256
"llvm" => rustc_codegen_llvm::LlvmCodegenBackend::new,
257-
"gotoc" => rustc_codegen_rmc::GotocCodegenBackend::new,
258257
backend_name => get_codegen_sysroot(maybe_sysroot, backend_name),
259258
}
260259
});

compiler/rustc_session/src/options.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1329,8 +1329,6 @@ options! {
13291329
"choose which RELRO level to use"),
13301330
remap_cwd_prefix: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
13311331
"remap paths under the current working directory to this path prefix"),
1332-
emit_vtable_restrictions: bool = (false, parse_bool, [TRACKED],
1333-
"restrict the targets of virtual table function pointer calls"),
13341332
simulate_remapped_rust_src_base: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
13351333
"simulate the effect of remap-debuginfo = true at bootstrapping by remapping path \
13361334
to rust's source base directory. only meant for testing purposes"),
@@ -1378,9 +1376,6 @@ options! {
13781376
symbol_mangling_version: Option<SymbolManglingVersion> = (None,
13791377
parse_symbol_mangling_version, [TRACKED],
13801378
"which mangling version to use for symbol names ('legacy' (default) or 'v0')"),
1381-
symbol_table_passes: Vec<String> = (Vec::new(), parse_list, [TRACKED],
1382-
"transformations to perform to the symbol table after it has been generated. \
1383-
space separated"),
13841379
teach: bool = (false, parse_bool, [TRACKED],
13851380
"show extended diagnostic help (default: no)"),
13861381
temps_dir: Option<String> = (None, parse_opt_string, [UNTRACKED],

library/rmc_restrictions/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ edition = "2021"
1111
[dependencies]
1212
rustc_data_structures = { path = "../../compiler/rustc_data_structures" }
1313
serde = {version = "1", features = ["derive"]}
14-
cbmc = { path = "../../compiler/cbmc" }
14+
cbmc = { path = "../../src/rmc-compiler/cbmc" }

scripts/cargo-rmc

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ def main():
2626
if args.gen_c_runnable:
2727
rmc.cargo_build(args.crate, args.target_dir, args, ["gen-c"])
2828

29+
if args.only_codegen:
30+
return 0
31+
2932
pattern = os.path.join(args.target_dir, "debug", "deps", "*.symtab.json")
3033
symbol_table_jsons = glob.glob(pattern)
3134
rmc.ensure(len(symbol_table_jsons) > 0, f"Unexpected number of json outputs: {len(symbol_table_jsons)}")
@@ -52,6 +55,9 @@ def main():
5255

5356
rmc.cargo_build(args.crate, args.target_dir, args)
5457

58+
if args.only_codegen:
59+
return 0
60+
5561
if args.build_target:
5662
pattern = os.path.join(args.target_dir, args.build_target, "debug", "deps", "*.symtab.json")
5763
else:
@@ -169,6 +175,7 @@ def parse_args():
169175
config_group.add_argument("--no-config-toml", action="store_true", help="Do not use any configuration toml")
170176
config_group.add_argument("--build-target", help="Build for the target triple.",
171177
default=default_build_target())
178+
config_group.add_argument("--only-codegen", action="store_true", help="RMC will only compile the crate.")
172179

173180
exclude_flags = []
174181
rmc_flags.add_flags(parser, {"default-target": "target"}, exclude_flags=exclude_flags)

scripts/codegen-firecracker.sh

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ cd $RMC_DIR/firecracker/src/devices/src/virtio/
2626
# Disable warnings until https://github.com/model-checking/rmc/issues/573 is fixed
2727
export RUSTC_LOG=error
2828
export RUST_BACKTRACE=1
29-
export RUSTFLAGS=$(${SCRIPT_DIR}/rmc-rustc --rmc-flags)
30-
export RUSTC=$(${SCRIPT_DIR}/rmc-rustc --rmc-path)
31-
cargo build --target x86_64-unknown-linux-gnu
29+
# RMC cannot locate Cargo.toml correctly: https://github.com/model-checking/rmc/issues/717
30+
cargo rmc --only-codegen --target x86_64-unknown-linux-gnu --no-config-toml
3231

3332
echo
3433
echo "Finished Firecracker codegen regression successfully..."

scripts/rmc-regression.sh

+9-5
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ check-cbmc-viewer-version.py --major 2 --minor 5
2020
# Formatting check
2121
./x.py fmt --check
2222

23-
# Build RMC and RMC library
24-
./x.py build -i --stage 1 library/std ${EXTRA_X_PY_BUILD_ARGS}
25-
./scripts/setup/build_rmc_lib.sh
23+
# Build RMC compiler and RMC library
24+
(cd "${RMC_DIR}/src/rmc-compiler"; cargo build)
25+
26+
# Unit tests
27+
(cd src/rmc-compiler/cbmc; cargo test)
28+
(cd src/rmc-compiler; cargo test)
2629

2730
# Standalone rmc tests, expected tests, and cargo tests
28-
./x.py test -i --stage 1 rmc firecracker prusti smack expected cargo-rmc rmc-docs rmc-fixme
29-
./x.py test -i --stage 0 compiler/cbmc
31+
./x.py build -i src/tools/compiletest --stage 0
32+
export COMPILETEST_FORCE_STAGE0=1 # We don't care about the stage anymore. Remove this once we replace ./x.py test
33+
./x.py test -i --stage 0 rmc firecracker prusti smack expected cargo-rmc rmc-docs rmc-fixme
3034

3135
# Check codegen for the standard library
3236
time "$SCRIPT_DIR"/std-lib-regression.sh

scripts/rmc-rustc

+25-57
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,54 @@
22
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
# SPDX-License-Identifier: Apache-2.0 OR MIT
44

5+
56
# Usage:
6-
# rmc-rustc (--rmc-flags | --rmc-path)
7-
# - This will print the configurations used to run rmc version of rustc.
8-
# rmc-rustc RUSTC_OPTIONS
9-
# - This will run RUSTC with RMC flags + the given RUSTC_OPTIONS
7+
# RMCFLAGS="[<RMC_OPTIONS>]*" rmc-rustc --rmc-flags [<RUSTC_OPTIONS>]*
8+
# - This will run rmc-compiler with RMC options provided via RMCFLAGS
9+
# environment variable + the given RUSTC_OPTIONS.
10+
# rmc-rustc [<RMC_OPTIONS>]* [<RUSTC_OPTIONS>]*
11+
# - This will run rmc-compiler with RMC options + the given RUSTC_OPTIONS
1012
set -eu
1113

1214
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
1315
REPO_DIR="$(dirname $SCRIPT_DIR)"
1416

1517
RMC_PATH=${RMC_PATH:-""}
16-
RMC_LIB_PATH=${RMC_LIB_PATH:-""}
1718

1819
shopt -s nullglob
19-
set_rmc_path() {
20-
local RMC_CANDIDATES=("$REPO_DIR"/build/*/stage1/bin/rustc)
20+
set_paths() {
21+
# TODO: We should build into a specific folder.
22+
local RMC_CANDIDATES=("$REPO_DIR"/target/*/rmc-compiler)
2123

2224
if [ -z "${RMC_PATH}" ]
2325
then
2426
if [[ -z ${RMC_CANDIDATES:-""} ]] || [[ ${#RMC_CANDIDATES[@]} -ne 1 ]]
2527
then
2628
echo "ERROR: Could not find RMC binary."
27-
echo "Looked for: $REPO_DIR/build/*/stage1/bin/rustc"
29+
echo "Looked for: '$REPO_DIR/target/*/rmc-compiler'"
2830
echo "Was RMC successfully built first?"
2931
exit 1
3032
fi
3133
RMC_PATH=${RMC_CANDIDATES[0]}
3234
fi
3335
}
3436

35-
set_rmc_lib_path() {
36-
if [ -z "${RMC_LIB_PATH}" ]
37-
then
38-
local RMC_LIB_CANDIDATES=("$REPO_DIR"/target/*/librmc.rlib)
39-
if [[ -z ${RMC_LIB_CANDIDATES:-""} ]] || [[ ${#RMC_LIB_CANDIDATES[@]} -ne 1 ]]
40-
then
41-
echo "ERROR: Could not find RMC library."
42-
echo "Looked for: \"$REPO_DIR/target/*/librmc.rlib\""
43-
echo "Try running <RMC_ROOT>/scripts/setup/build_rmc_lib.sh"
44-
exit 1
45-
fi
46-
RMC_LIB_PATH=$(dirname ${RMC_LIB_CANDIDATES[0]})
47-
fi
48-
# Having set RMC_LIB_PATH, find RMC_MACRO_LIB_PATH
49-
local MACRO_LIB_CANDIDATE=(${RMC_LIB_PATH}/deps/librmc_macros-*)
50-
if [[ ${#MACRO_LIB_CANDIDATE[@]} -ne 1 ]]
51-
then
52-
echo "ERROR: Could not find RMC library."
53-
echo "Looked for: \"${RMC_LIB_PATH}/deps/librmc_macros-*\""
54-
echo "Try running <RMC_ROOT>/scripts/setup/build_rmc_lib.sh"
55-
exit 1
56-
fi
57-
RMC_MACRO_LIB_PATH="${MACRO_LIB_CANDIDATE[0]}"
58-
}
37+
set_paths
5938

60-
set_rmc_path
61-
if [ "${1:-''}" == "--rmc-path" ]
39+
# Hack to enable cargo rmc
40+
#
41+
# The rmc-compiler requires the flags related to the rmc libraries to be
42+
# in front of the ones that control rustc.
43+
#
44+
# For cargo rmc, cargo sometimes adds flags before the custom RUSTFLAGS, hence,
45+
# we use a special environment variable to set RMC specific flags. These flags
46+
# should only be enabled if --rmc-flags is present.
47+
args="${*#--rmc-flags}"
48+
if [ "$args" != "$*" ]
6249
then
63-
echo ${RMC_PATH}
50+
FLAGS=("${RMCFLAGS} $args")
6451
else
65-
set_rmc_lib_path
66-
RMC_FLAGS="-Z codegen-backend=gotoc \
67-
-C overflow-checks=on \
68-
-C panic=abort \
69-
-Z panic_abort_tests=yes \
70-
-Z trim-diagnostic-paths=no \
71-
-Z human_readable_cgu_names \
72-
--cfg=rmc \
73-
-Zcrate-attr=feature(register_tool) \
74-
-Zcrate-attr=register_tool(rmctool) \
75-
-L ${RMC_LIB_PATH} \
76-
--extern rmc \
77-
-L ${RMC_LIB_PATH}/deps \
78-
--extern librmc_macros=${RMC_MACRO_LIB_PATH} \
79-
"
80-
if [ "${1:-''}" == "--rmc-flags" ]
81-
then
82-
echo ${RMC_FLAGS}
83-
else
84-
# Execute rmc
85-
"${RMC_PATH}" ${RMC_FLAGS} "$@"
86-
fi
52+
FLAGS=("$args")
8753
fi
54+
55+
"${RMC_PATH}" ${FLAGS}

0 commit comments

Comments
 (0)