Browse Source

Add cargo miri test to CI (#178)

* Add miri step

* Fix miri issues

* Install miri component
Fernando Otero 5 months ago
parent
commit
84ff314bab
5 changed files with 37 additions and 13 deletions
  1. 7 0
      .github/actions/setup/action.yml
  2. 5 1
      .github/workflows/main.yml
  3. 1 0
      package.json
  4. 8 0
      scripts/miri.mts
  5. 16 12
      sdk/pinocchio/src/account_info.rs

+ 7 - 0
.github/actions/setup/action.yml

@@ -91,6 +91,13 @@ runs:
       with:
         tool: cargo-semver-checks@0.39.0
 
+    - name: Install 'cargo-miri'
+      if: ${{ contains(inputs.toolchain, 'lint') }}
+      uses: dtolnay/rust-toolchain@master
+      with:
+        toolchain: ${{ env.TOOLCHAIN_LINT }}
+        components: miri
+
     - name: Cache Cargo Dependencies
       if: ${{ inputs.cargo-cache-key }}
       uses: actions/cache@v4

+ 5 - 1
.github/workflows/main.yml

@@ -22,11 +22,15 @@ jobs:
         uses: ./.github/actions/setup
         with:
           cargo-cache-key: cargo-audit
-          components: audit
+          toolchain: lint
+          components: audit, miri
 
       - name: cargo-audit
         run: pnpm cargo-audit
 
+      - name: cargo-miri
+        run: pnpm miri
+
       - name: Filter members
         id: filter
         run: pnpm tsx ./scripts/setup/members.mts

+ 1 - 0
package.json

@@ -8,6 +8,7 @@
     "format": "tsx ./scripts/format.mts",
     "hack": "tsx ./scripts/hack.mts",
     "lint": "tsx ./scripts/lint.mts",
+    "miri": "tsx ./scripts/miri.mts",
     "semver": "tsx ./scripts/semver.mts",
     "test": "tsx ./scripts/test.mts"
   },

+ 8 - 0
scripts/miri.mts

@@ -0,0 +1,8 @@
+#!/usr/bin/env zx
+import 'zx/globals';
+import { cliArguments, getToolchainArgument } from './setup/shared.mts';
+
+const args = cliArguments();
+const toolchain = getToolchainArgument('lint');
+
+await $`cargo ${toolchain} miri test ${args}`;

+ 16 - 12
sdk/pinocchio/src/account_info.rs

@@ -715,12 +715,13 @@ mod tests {
     #[test]
     fn test_data_ref() {
         let data: [u8; 4] = [0, 1, 2, 3];
-        let state = 1 << DATA_SHIFT;
+        let mut state = 1 << DATA_SHIFT;
 
         let ref_data = Ref {
             value: NonNull::from(&data),
             borrow_shift: DATA_SHIFT,
-            state: NonNull::from(&state),
+            // borrow state must be a mutable reference
+            state: NonNull::from(&mut state),
             marker: PhantomData,
         };
 
@@ -749,12 +750,13 @@ mod tests {
     #[test]
     fn test_lamports_ref() {
         let lamports: u64 = 10000;
-        let state = 1 << LAMPORTS_SHIFT;
+        let mut state = 1 << LAMPORTS_SHIFT;
 
         let ref_lamports = Ref {
             value: NonNull::from(&lamports),
             borrow_shift: LAMPORTS_SHIFT,
-            state: NonNull::from(&state),
+            // borrow state must be a mutable reference
+            state: NonNull::from(&mut state),
             marker: PhantomData,
         };
 
@@ -782,13 +784,14 @@ mod tests {
 
     #[test]
     fn test_data_ref_mut() {
-        let data: [u8; 4] = [0, 1, 2, 3];
-        let state = 0b_0000_1000;
+        let mut data: [u8; 4] = [0, 1, 2, 3];
+        let mut state = 0b_0000_1000;
 
         let ref_data = RefMut {
-            value: NonNull::from(&data),
+            value: NonNull::from(&mut data),
             borrow_mask: DATA_MASK,
-            state: NonNull::from(&state),
+            // borrow state must be a mutable reference
+            state: NonNull::from(&mut state),
             marker: PhantomData,
         };
 
@@ -809,13 +812,14 @@ mod tests {
 
     #[test]
     fn test_lamports_ref_mut() {
-        let lamports: u64 = 10000;
-        let state = 0b_1000_0000;
+        let mut lamports: u64 = 10000;
+        let mut state = 0b_1000_0000;
 
         let ref_lamports = RefMut {
-            value: NonNull::from(&lamports),
+            value: NonNull::from(&mut lamports),
             borrow_mask: LAMPORTS_MASK,
-            state: NonNull::from(&state),
+            // borrow state must be a mutable reference
+            state: NonNull::from(&mut state),
             marker: PhantomData,
         };