diff --git a/.github/workflows/develop.yml b/.github/workflows/develop.yml new file mode 100644 index 00000000..cf404dca --- /dev/null +++ b/.github/workflows/develop.yml @@ -0,0 +1,174 @@ +name: develop + +on: + pull_request: + push: + +jobs: + + linux-x86-deny: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + # https://github.com/EmbarkStudios/cargo-deny-action + - uses: EmbarkStudios/cargo-deny-action@v1 + with: + arguments: --all-features + command: check advisories licenses sources bans + + linux-x86-ci-generated: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Run ci-generated + run: make ci-generated + + linux-x86-ci: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Run ci + run: rustup component add clippy rustfmt && make ci + + linux-x86-ci-asm: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Run ci-asm + run: make ci-asm + + linux-x86-ci-asm-chaos: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Run ci-asm-chaos + run: make ci-asm-chaos + + linux-x86-test-suite: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install dependencies + run: | + sudo apt install -y git \ + build-essential \ + autoconf \ + automake \ + autotools-dev \ + libmpc-dev \ + libmpfr-dev \ + libgmp-dev \ + gawk \ + libtool \ + patchutils \ + libexpat-dev \ + zlib1g-dev + - name: Run test suite + run: | + git clone https://github.com/nervosnetwork/ckb-vm-test-suite + ln -snf .. ckb-vm-test-suite/ckb-vm + docker run --rm -v `pwd`:/code nervos/ckb-riscv-gnu-toolchain:bionic-20210804 cp -r /riscv /code/riscv + cd ckb-vm-test-suite + git checkout 441e0f2149c097ccad133b040544dca13caeb01e + git submodule update --init --recursive + RISCV=`pwd`/../riscv ./test.sh + + linux-x86-test-spawn: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Run spawn tests in release + run: cargo test test_spawn --release --features=asm -- --nocapture + + linux-arm-ci-asm: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install dependencies + run: | + sudo apt-get update -y + sudo apt install -y build-essential \ + autoconf \ + automake \ + autotools-dev \ + libmpc-dev \ + libmpfr-dev \ + libgmp-dev \ + gawk \ + libtool \ + patchutils \ + libexpat-dev \ + zlib1g-dev \ + gcc-aarch64-linux-gnu \ + g++-aarch64-linux-gnu \ + qemu-user-static + rustup target add aarch64-unknown-linux-gnu + - name: Run ci-asm + run: | + export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc && + export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-C link-args=-L -C link-args=/usr/lib/gcc-cross/aarch64-linux-gnu/11" && + export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUNNER="qemu-aarch64-static -L /usr/aarch64-linux-gnu" && + cargo test --features=asm --target aarch64-unknown-linux-gnu + + linux-arm-test-suite: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install dependencies + run: | + sudo apt update -y + sudo apt install -y build-essential \ + autoconf \ + automake \ + autotools-dev \ + libmpc-dev \ + libmpfr-dev \ + libgmp-dev \ + gawk \ + libtool \ + patchutils \ + libexpat-dev \ + zlib1g-dev + rustup target add aarch64-unknown-linux-gnu + - name: Build test suite + run: | + git clone https://github.com/nervosnetwork/ckb-vm-test-suite + ln -snf .. ckb-vm-test-suite/ckb-vm + docker run --rm -v `pwd`:/code nervos/ckb-riscv-gnu-toolchain:bionic-20210804 cp -r /riscv /code/riscv + cd ckb-vm-test-suite + git checkout 441e0f2149c097ccad133b040544dca13caeb01e + git submodule update --init --recursive + RISCV=`pwd`/../riscv ./test.sh --build-only + cd .. + - name: Run test suite + run: | + sudo apt install -y qemu binfmt-support qemu-user-static + sudo apt install -y gcc-multilib + sudo apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu clang + docker run --rm --privileged multiarch/qemu-user-static --reset -p yes + cd ckb-vm-test-suite + cd binary && cargo build --release --target=aarch64-unknown-linux-gnu && cd .. + cd .. + docker run --rm -v `pwd`:/code -t --platform linux/arm64 arm64v8/rust bash -c "RISCV=/dummy /code/ckb-vm-test-suite/test.sh --prebuilt-prefix aarch64-unknown-linux-gnu" + + macos-x86-ci-asm: + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + - name: Run ci-asm + run: make ci-asm + + windows-x86-ci-asm: + runs-on: windows-latest + steps: + - uses: actions/checkout@v3 + - name: Install dependencies + shell: pwsh + # https://github.com/ScoopInstaller/Install#for-admin + run: | + iex "& {$(irm get.scoop.sh)} -RunAsAdmin" + scoop install mingw + - name: Run ci-asm + shell: pwsh + run: | + make ci-asm diff --git a/azure-pipelines.yml b/azure-pipelines.yml deleted file mode 100644 index 9935276c..00000000 --- a/azure-pipelines.yml +++ /dev/null @@ -1,199 +0,0 @@ -trigger: - branches: - include: - - '*' - tags: - include: - - '*' - -variables: - TEST_SUITE_COMMIT: 441e0f2149c097ccad133b040544dca13caeb01e - -jobs: - - job: LinuxCIDeps - pool: - vmImage: 'ubuntu-20.04' - steps: - - template: devtools/azure/linux-dependencies.yml - - script: cargo install cargo-deny --locked --version 0.12.1 - displayName: Install cargo deny - - script: make ci-deps - displayName: Run ci-deps - - - job: LinuxCIGenerated - pool: - vmImage: 'ubuntu-20.04' - steps: - - template: devtools/azure/linux-dependencies.yml - - script: make ci-generated - displayName: Run ci-generated - - - job: LinuxCIASM - pool: - vmImage: 'ubuntu-20.04' - steps: - - template: devtools/azure/linux-dependencies.yml - - script: make ci-asm - displayName: Run ci-asm - - - job: LinuxCI - pool: - vmImage: 'ubuntu-20.04' - steps: - - template: devtools/azure/linux-dependencies.yml - - script: make ci - displayName: Run ci - - - job: LinuxCIASMChaos - pool: - vmImage: 'ubuntu-20.04' - steps: - - template: devtools/azure/linux-dependencies.yml - - script: make ci-asm-chaos - displayName: Run ci-asm-chaos - - - job: LinuxTestSuite - pool: - vmImage: 'ubuntu-20.04' - steps: - - template: devtools/azure/linux-dependencies.yml - - script: | - sudo apt install -y git \ - build-essential \ - autoconf \ - automake \ - autotools-dev \ - libmpc-dev \ - libmpfr-dev \ - libgmp-dev \ - gawk \ - libtool \ - patchutils \ - libexpat-dev \ - zlib1g-dev - - script: | - git clone https://github.com/nervosnetwork/ckb-vm-test-suite && - ln -snf .. ckb-vm-test-suite/ckb-vm && - docker run --rm -v `pwd`:/code nervos/ckb-riscv-gnu-toolchain:bionic-20210804 cp -r /riscv /code/riscv && - cd ckb-vm-test-suite && - git checkout $TEST_SUITE_COMMIT && - git submodule update --init --recursive && - RISCV=`pwd`/../riscv ./test.sh - displayName: Run test suite - - - job: LinuxArm64AsmCI - pool: - vmImage: 'ubuntu-20.04' - steps: - - template: devtools/azure/linux-dependencies.yml - - script: | - sudo apt-get install -y qemu binfmt-support qemu-user-static - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes - echo "Comment: updating crates.io cause oom in arm docker, use -v /usr/local/cargo/registry/:/usr/local/cargo/registry/ to skip this problem" - cargo update - docker run --rm -v `pwd`:/code -v /home/vsts/.cargo/registry/:/usr/local/cargo/registry/ -t arm64v8/rust bash -c "cd /code && cargo test --features=asm" - displayName: Run ci-asm on arm64 machines - - - job: LinuxArm64TestSuite - pool: - vmImage: 'ubuntu-20.04' - steps: - - template: devtools/azure/linux-dependencies.yml - - script: | - sudo apt update -y && - sudo apt install -y git \ - build-essential \ - autoconf \ - automake \ - autotools-dev \ - libmpc-dev \ - libmpfr-dev \ - libgmp-dev \ - gawk \ - libtool \ - patchutils \ - libexpat-dev \ - zlib1g-dev - - script: | - git clone https://github.com/nervosnetwork/ckb-vm-test-suite && - ln -snf .. ckb-vm-test-suite/ckb-vm && - docker run --rm -v `pwd`:/code nervos/ckb-riscv-gnu-toolchain:bionic-20210804 cp -r /riscv /code/riscv && - cd ckb-vm-test-suite && - git checkout $TEST_SUITE_COMMIT && - git submodule update --init --recursive && - RISCV=`pwd`/../riscv ./test.sh --build-only && - cd .. - displayName: Build test suite - - script: | - sudo apt-get install -y qemu binfmt-support qemu-user-static - sudo apt-get install -y gcc-multilib - sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu clang - rustup target add aarch64-unknown-linux-gnu - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes - cd ckb-vm-test-suite && - cd binary && cargo build --release --target=aarch64-unknown-linux-gnu && cd .. && - cd .. && - docker run --rm -v `pwd`:/code -t arm64v8/rust bash -c "RISCV=/dummy /code/ckb-vm-test-suite/test.sh --prebuilt-prefix aarch64-unknown-linux-gnu" - displayName: Run test suite on arm64 machines - - - job: LinuxCodeCoverage - condition: eq(variables['Build.SourceBranch'], 'refs/heads/release') - pool: - vmImage: 'ubuntu-20.04' - steps: - - template: devtools/azure/linux-dependencies.yml - - script: | - sudo apt install -y git \ - build-essential \ - autoconf \ - automake \ - autotools-dev \ - libmpc-dev \ - libmpfr-dev \ - libgmp-dev \ - gawk \ - libtool \ - patchutils \ - libexpat-dev \ - binutils-dev \ - libcurl4-openssl-dev \ - zlib1g-dev \ - libdw-dev \ - libiberty-dev - - script: | - wget https://github.com/SimonKagstrom/kcov/archive/v36.tar.gz && - tar xzf v36.tar.gz && - cd kcov-36 && - mkdir build && - cd build && - cmake .. && - make && - sudo make install && - cd ../.. && - rm -rf kcov-36 v36.tar.gz && - git clone https://github.com/nervosnetwork/ckb-vm-test-suite && - ln -snf .. ckb-vm-test-suite/ckb-vm && - docker run --rm -v `pwd`:/code nervos/ckb-riscv-gnu-toolchain:bionic-20210804 cp -r /riscv /code/riscv && - cd ckb-vm-test-suite && git checkout $TEST_SUITE_COMMIT && git submodule update --init --recursive && - RISCV=`pwd`/../riscv ./test.sh --coverage && - cd .. && - make cov && - bash <(curl -s https://codecov.io/bash) && - echo "Uploaded code coverage" - displayName: Run code coverage - - - job: OSXCI - pool: - vmImage: 'macOS-11' - steps: - - template: devtools/azure/osx-dependencies.yml - - script: make ci-asm - displayName: Run unit tests - - - job: WinCI - pool: - vmImage: 'windows-2019' - steps: - - template: devtools/azure/windows-dependencies.yml - - script: make ci-asm - displayName: Run unit tests diff --git a/build.rs b/build.rs index 381068b4..0334ffd1 100644 --- a/build.rs +++ b/build.rs @@ -13,7 +13,9 @@ fn main() { let target_family = env::var("CARGO_CFG_TARGET_FAMILY").unwrap_or_default(); let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default(); + let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default(); let is_windows = target_family == "windows"; + let is_msvc = is_windows && (target_env == "msvc"); let is_unix = target_family == "unix"; let is_x86_64 = target_arch == "x86_64"; let is_aarch64 = target_arch == "aarch64"; @@ -33,66 +35,20 @@ fn main() { println!("cargo:rerun-if-changed=src/machine/asm/execute_aarch64.S"); println!("cargo:rerun-if-changed=src/machine/asm/cdefinitions_generated.h"); - use cc::Build; - use std::path::Path; - use std::process::Command; + let mut build = cc::Build::new(); - fn run_command(mut c: Command) { - println!("Running Command[{:?}]", c); - - let output = c.output().unwrap_or_else(|e| { - panic!("Error running Command[{:?}], error: {:?}", c, e); - }); - - if !output.status.success() { - use std::io::{self, Write}; - io::stdout() - .write_all(&output.stdout) - .expect("stdout write"); - io::stderr() - .write_all(&output.stderr) - .expect("stderr write"); - - panic!( - "Command[{:?}] exits with non-success status: {:?}", - c, output.status - ); - } - } - - let mut build = Build::new(); - - if is_windows && x64_asm { - let out_dir = env::var("OUT_DIR").unwrap(); - let expand_path = Path::new(&out_dir).join("execute_x64-expanded.S"); - let mut expand_command = Command::new("clang"); - expand_command - .arg("-E") - .arg(format!("-DCKB_VM_VERSION={version}")) - .arg("src/machine/asm/execute_x64.S") - .arg("-o") - .arg(&expand_path); - run_command(expand_command); - - let compile_path = Path::new(&out_dir).join("execute_x64.o"); - let mut compile_command = Command::new("yasm"); - compile_command - .arg("-p") - .arg("gas") - .arg("-f") - .arg("x64") - .arg("-m") - .arg("amd64") - .arg(&expand_path) - .arg("-o") - .arg(&compile_path); - run_command(compile_command); - - build.object(&compile_path); - } else if x64_asm { + if x64_asm { build .define("CKB_VM_VERSION", &*version) .file("src/machine/asm/execute_x64.S"); + if is_msvc { + // For now, only an assembly source code is required for CKB-VM, we won't + // need to build any C source file here. Hence we can use this simpler solution + // to set the default compiler to GCC. We will need to manually trigger the + // command to assemble the assembly code file, should any C source file is also + // required here. + build.compiler("gcc"); + } } else if aarch64_asm { build .define("CKB_VM_VERSION", &*version) diff --git a/definitions/src/asm.rs b/definitions/src/asm.rs index 402505f8..768e46fa 100644 --- a/definitions/src/asm.rs +++ b/definitions/src/asm.rs @@ -2,7 +2,7 @@ use crate::{ instructions::Instruction, MEMORY_FRAMES, MEMORY_FRAMESIZE, MEMORY_FRAME_SHIFTS, RISCV_GENERAL_REGISTER_NUMBER, RISCV_MAX_MEMORY, RISCV_PAGES, RISCV_PAGESIZE, }; -use std::alloc::{alloc, Layout}; +use std::alloc::{alloc_zeroed, Layout}; // The number of trace items to keep pub const TRACE_SIZE: usize = 8192; @@ -88,29 +88,19 @@ impl AsmCoreMachine { std::mem::size_of::() - RISCV_MAX_MEMORY + memory_size; let layout = Layout::array::(machine_size).unwrap(); - let raw_allocation = alloc(layout) as *mut AsmCoreMachine; + let raw_allocation = alloc_zeroed(layout) as *mut AsmCoreMachine; Box::from_raw(raw_allocation) }; - machine.registers = [0; RISCV_GENERAL_REGISTER_NUMBER]; - machine.pc = 0; - machine.next_pc = 0; - machine.running = 0; - machine.cycles = 0; machine.max_cycles = max_cycles; if cfg!(feature = "enable-chaos-mode-by-default") { machine.chaos_mode = 1; - } else { - machine.chaos_mode = 0; } - machine.chaos_seed = 0; - machine.reset_signal = 0; + machine.load_reservation_address = u64::MAX; machine.version = version; machine.isa = isa; - machine.flags = [0; RISCV_PAGES]; for i in 0..TRACE_SIZE { machine.traces[i] = Trace::default(); } - machine.frames = [0; MEMORY_FRAMES]; machine.memory_size = memory_size as u64; machine.frames_size = (memory_size / MEMORY_FRAMESIZE) as u64; diff --git a/devtools/azure/linux-dependencies.yml b/devtools/azure/linux-dependencies.yml deleted file mode 100644 index 05ec5f9c..00000000 --- a/devtools/azure/linux-dependencies.yml +++ /dev/null @@ -1,12 +0,0 @@ -steps: -- script: | - sudo apt install -y build-essential -- script: | - curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.71.1-x86_64-unknown-linux-gnu - set PATH=$PATH:~/.cargo/bin - rustup install nightly - displayName: Install rust -- script: | - rustc --version - cargo --version - displayName: Test/query binaries diff --git a/devtools/azure/osx-dependencies.yml b/devtools/azure/osx-dependencies.yml deleted file mode 100644 index 6fa7d0e8..00000000 --- a/devtools/azure/osx-dependencies.yml +++ /dev/null @@ -1,14 +0,0 @@ -steps: -- script: | - curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.71.1-x86_64-apple-darwin - set PATH=$PATH:~/.cargo/bin - rustup install nightly - displayName: Install rust -- script: rustup component add rustfmt - displayName: Install rustfmt -- script: rustup component add clippy - displayName: Install clippy -- script: | - rustc --version - cargo --version - displayName: Test/query binaries diff --git a/devtools/azure/windows-dependencies.yml b/devtools/azure/windows-dependencies.yml deleted file mode 100644 index 486d41a3..00000000 --- a/devtools/azure/windows-dependencies.yml +++ /dev/null @@ -1,28 +0,0 @@ -steps: -- powershell: | - iwr -useb get.scoop.sh -outfile 'install.ps1' - .\install.ps1 -RunAsAdmin - rm install.ps1 - displayName: Install scoop -- script: | - set PATH=%PATH%;%USERPROFILE%\scoop\shims - echo "##vso[task.setvariable variable=PATH;]%PATH%;%USERPROFILE%\scoop\shims" - scoop help - displayName: Add scoop to path -- script: scoop install llvm - displayName: Install LLVM -- script: scoop install yasm - displayName: Install yasm -- script: | - curl -sSf -o rustup-init.exe https://win.rustup.rs - rustup-init.exe -y --default-toolchain 1.71.1-x86_64-pc-windows-msvc - set PATH=%PATH%;%USERPROFILE%\.cargo\bin - echo "##vso[task.setvariable variable=PATH;]%PATH%;%USERPROFILE%\.cargo\bin" - rustup install nightly - displayName: Install rust -- script: | - rustc --version - cargo --version - clang --version - yasm --version - displayName: Test/query binaries diff --git a/tests/programs/_build_all_native.sh b/tests/programs/_build_all_native.sh index d507c829..d1f2a627 100755 --- a/tests/programs/_build_all_native.sh +++ b/tests/programs/_build_all_native.sh @@ -52,6 +52,7 @@ riscv64-unknown-elf-as -o rorw_in_end_of_aot_block.o rorw_in_end_of_aot_block.S sh rvc_pageend.sh # TODO: sbinvi_aot_load_imm_bug riscv64-unknown-elf-as -o sc_after_sc.o sc_after_sc.S && riscv64-unknown-elf-ld -T sc_after_sc.lds -o sc_after_sc sc_after_sc.o && rm sc_after_sc.o +riscv64-unknown-elf-as -o sc_only.o sc_only.S && riscv64-unknown-elf-ld -T sc_only.lds -o sc_only sc_only.o && rm sc_only.o # SKIP: simple riscv64-unknown-elf-gcc -o simple64 simple.c riscv64-unknown-elf-as -o sp_alignment_test.o sp_alignment_test.S && riscv64-unknown-elf-ld -o sp_alignment_test sp_alignment_test.o && rm sp_alignment_test.o diff --git a/tests/programs/sc_only b/tests/programs/sc_only new file mode 100755 index 00000000..96e107c7 Binary files /dev/null and b/tests/programs/sc_only differ diff --git a/tests/programs/sc_only.S b/tests/programs/sc_only.S new file mode 100644 index 00000000..3a638bfe --- /dev/null +++ b/tests/programs/sc_only.S @@ -0,0 +1,16 @@ +.global _start +_start: + la a0, n0 # a0 holds address of memory location n0 + sc.d a3, a2, (a0) + beqz a3, fail # sc.d must fail +done: + li a0, 0 + li a7, 93 + ecall +fail: + li a0, 1 + li a7, 93 + ecall +.section .data +n0: + .dword 4 # Initialize to 4 diff --git a/tests/programs/sc_only.lds b/tests/programs/sc_only.lds new file mode 100644 index 00000000..fdfa58fb --- /dev/null +++ b/tests/programs/sc_only.lds @@ -0,0 +1,7 @@ +SECTIONS +{ +. = 0x100b0; +.text : { *(.text) } +. = 0x11000; +.data : { *(.data) } +} diff --git a/tests/test_a_extension.rs b/tests/test_a_extension.rs index d6fa9a42..99073492 100644 --- a/tests/test_a_extension.rs +++ b/tests/test_a_extension.rs @@ -33,6 +33,22 @@ pub fn test_sc_after_sc() { } } +#[test] +pub fn test_sc_only() { + let mut machine = machine_build::int_v2_imacb("tests/programs/sc_only"); + let ret = machine.run(); + assert!(ret.is_ok()); + assert_eq!(ret.unwrap(), 0); + + #[cfg(has_asm)] + { + let mut machine_asm = machine_build::asm_v2_imacb("tests/programs/sc_only"); + let ret_asm = machine_asm.run(); + assert!(ret_asm.is_ok()); + assert_eq!(ret_asm.unwrap(), 0); + } +} + #[test] pub fn test_amo_compare() { let mut machine = machine_build::int_v2_imacb("tests/programs/amo_compare");