Skip to content

Commit 1eab705

Browse files
committed
crux-mir-comp: Support nightly-2023-01-23
This patch bumps the `crucible` submodule to bring in the changes from GaloisInc/crucible#1096 and updates the code in `crucible-mir-comp` and `crux-mir-comp` accordingly. Some highlights: * All of the `crux-mir-comp` test cases now use `crux::test` instead of `crux_test`. * All of the `crux-mir-comp` test cases now scrub out the values of crate hash disambiguators to make their output more stable. * The overrides in `crux-mir-comp` no longer depend on the specific disambiguator values being used, which makes them work with the sometimes unpredictable hash values used for crate disambiguators. * I have added a `tyToShape` case for `TyStr` to handle new kinds of static values that arise in the new Rust nightly (mostly coming from constant values in the `fmt` crate). The code for this case is nearly identical to that in the `TySlice` case. * There are now static values of type `TyFnPtr` in the new Rust nightly (mostly coming from constant values in the `fmt` crate), so in order to handle them in `clobberGlobals`, I needed to add a `FnPtrShape` data constructor to `TypeShape`. This is mostly a quick hack, since I implemented all other functionality for `FnPtrShape` with calls to `error`. Nevertheless, that is enough to make all of the tests pass. We can always fill out the calls to `error` later if need be.
1 parent 4a695fb commit 1eab705

Some content is hidden

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

77 files changed

+284
-181
lines changed

crucible-mir-comp/src/Mir/Compositional/Builder.hs

+2
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,8 @@ regToSetup bak p eval shp rv = go shp rv
714714
alloc <- refToAlloc bak p mutbl ty' tpr startRef len
715715
let offsetSv idx sv = if idx == 0 then sv else MS.SetupElem () sv idx
716716
return $ offsetSv idx $ MS.SetupVar alloc
717+
go (FnPtrShape _ _ _) _ =
718+
error "Function pointers not currently supported in overrides"
717719

718720
goFields :: forall ctx. Assignment FieldShape ctx -> Assignment (RegValue' sym) ctx ->
719721
BuilderT sym t (OverrideSim p sym MIR rtp args ret) [MS.SetupValue MIR]

crucible-mir-comp/src/Mir/Compositional/Clobber.hs

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ clobberSymbolic sym loc nameStr shp rv = go shp rv
5959
", but got Any wrapping " ++ show tpr
6060
where shpTpr = StructRepr $ fmapFC fieldShapeType flds
6161
go (TransparentShape _ shp) rv = go shp rv
62+
go (FnPtrShape _ _ _) _rv =
63+
error "Function pointers not currently supported in overrides"
6264
go shp _rv = error $ "clobberSymbolic: " ++ show (shapeType shp) ++ " NYI"
6365

6466
goField :: forall tp. FieldShape tp -> RegValue' sym tp ->
@@ -105,6 +107,8 @@ clobberImmutSymbolic sym loc nameStr shp rv = go shp rv
105107
-- Since this ref is in immutable memory, whatever behavior we're
106108
-- approximating with this clobber can't possibly modify it.
107109
go (RefShape _ _ _tpr) rv = return rv
110+
go (FnPtrShape _ _ _) _rv =
111+
error "Function pointers not currently supported in overrides"
108112

109113
goField :: forall tp. FieldShape tp -> RegValue' sym tp ->
110114
OverrideSim (p sym) sym MIR rtp args ret (RegValue' sym tp)
@@ -133,6 +137,8 @@ freshSymbolic sym loc nameStr shp = go shp
133137
return expr
134138
go (ArrayShape (M.TyArray _ len) _ shp) =
135139
MirVector_Vector <$> V.replicateM len (go shp)
140+
go (FnPtrShape _ _ _) =
141+
error "Function pointers not currently supported in overrides"
136142
go shp = error $ "freshSymbolic: " ++ show (shapeType shp) ++ " NYI"
137143

138144

crucible-mir-comp/src/Mir/Compositional/Convert.hs

+28-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ import qualified Verifier.SAW.Simulator.What4.ReturnTrip as SAW (baseSCType)
4949

5050
import Mir.Intrinsics
5151
import qualified Mir.Mir as M
52-
import Mir.TransTy (tyToRepr, canInitialize, isUnsized, reprTransparentFieldTy)
52+
import Mir.TransTy ( tyListToCtx, tyToRepr, tyToReprCont, canInitialize
53+
, isUnsized, reprTransparentFieldTy )
5354

5455

5556
-- | TypeShape is used to classify MIR `Ty`s and their corresponding
@@ -75,6 +76,11 @@ data TypeShape (tp :: CrucibleType) where
7576
-- a TypeShape. None of our operations need to recurse inside pointers,
7677
-- and also this saves us from some infinite recursion.
7778
RefShape :: M.Ty -> M.Ty -> TypeRepr tp -> TypeShape (MirReferenceType tp)
79+
-- | Note that 'FnPtrShape' contains only 'TypeRepr's for the argument and
80+
-- result types, not 'TypeShape's, as none of our operations need to recurse
81+
-- inside them.
82+
FnPtrShape :: M.Ty -> CtxRepr args -> TypeRepr ret
83+
-> TypeShape (FunctionHandleType args ret)
7884

7985
-- | The TypeShape of a struct field, which might have a MaybeType wrapper to
8086
-- allow for partial initialization.
@@ -108,6 +114,7 @@ tyToShape col ty = go ty
108114
Nothing -> error $ "tyToShape: bad adt: " ++ show ty
109115
M.TyRef ty' mutbl -> goRef ty ty' mutbl
110116
M.TyRawPtr ty' mutbl -> goRef ty ty' mutbl
117+
M.TyFnPtr sig -> goFnPtr ty sig
111118
_ -> error $ "tyToShape: " ++ show ty ++ " NYI"
112119

113120
goPrim :: M.Ty -> Some TypeShape
@@ -139,10 +146,19 @@ tyToShape col ty = go ty
139146
False -> Some $ OptField shp
140147

141148
goRef :: M.Ty -> M.Ty -> M.Mutability -> Some TypeShape
142-
goRef ty (M.TySlice ty') mutbl | Some tpr <- tyToRepr col ty' = Some $
149+
goRef ty ty' mutbl
150+
| M.TySlice slicedTy <- ty'
151+
, Some tpr <- tyToRepr col slicedTy
152+
= Some $
153+
TupleShape ty [refTy, usizeTy]
154+
(Empty
155+
:> ReqField (RefShape refTy slicedTy tpr)
156+
:> ReqField (PrimShape usizeTy BaseUsizeRepr))
157+
| M.TyStr <- ty'
158+
= Some $
143159
TupleShape ty [refTy, usizeTy]
144160
(Empty
145-
:> ReqField (RefShape refTy ty' tpr)
161+
:> ReqField (RefShape refTy (M.TyUint M.B8) (BVRepr (knownNat @8)))
146162
:> ReqField (PrimShape usizeTy BaseUsizeRepr))
147163
where
148164
-- We use a ref (of the same mutability as `ty`) when possible, to
@@ -155,6 +171,12 @@ tyToShape col ty = go ty
155171
"tyToShape: fat pointer " ++ show ty ++ " NYI"
156172
goRef ty ty' _ | Some tpr <- tyToRepr col ty' = Some $ RefShape ty ty' tpr
157173

174+
goFnPtr :: M.Ty -> M.FnSig -> Some TypeShape
175+
goFnPtr ty (M.FnSig args ret _abi _spread) =
176+
tyListToCtx col args $ \argsr ->
177+
tyToReprCont col ret $ \retr ->
178+
Some (FnPtrShape ty argsr retr)
179+
158180
-- | Given a `Ty` and the result of `tyToRepr ty`, produce a `TypeShape` with
159181
-- the same index `tp`. Raises an `error` if the `TypeRepr` doesn't match
160182
-- `tyToRepr ty`.
@@ -177,6 +199,7 @@ shapeType shp = go shp
177199
go (StructShape _ _ _flds) = AnyRepr
178200
go (TransparentShape _ shp) = go shp
179201
go (RefShape _ _ tpr) = MirReferenceRepr tpr
202+
go (FnPtrShape _ args ret) = FunctionHandleRepr args ret
180203

181204
fieldShapeType :: FieldShape tp -> TypeRepr tp
182205
fieldShapeType (ReqField shp) = shapeType shp
@@ -190,6 +213,7 @@ shapeMirTy (ArrayShape ty _ _) = ty
190213
shapeMirTy (StructShape ty _ _) = ty
191214
shapeMirTy (TransparentShape ty _) = ty
192215
shapeMirTy (RefShape ty _ _) = ty
216+
shapeMirTy (FnPtrShape ty _ _) = ty
193217

194218
fieldShapeMirTy :: FieldShape tp -> M.Ty
195219
fieldShapeMirTy (ReqField shp) = shapeMirTy shp
@@ -536,7 +560,7 @@ shapeToTerm sc shp = go shp
536560
go :: forall tp. TypeShape tp -> m SAW.Term
537561
go (UnitShape _) = liftIO $ SAW.scUnitType sc
538562
go (PrimShape _ BaseBoolRepr) = liftIO $ SAW.scBoolType sc
539-
go (PrimShape _ (BaseBVRepr w)) = liftIO $ SAW.scBitvector sc (natValue w)
563+
go (PrimShape _ (BaseBVRepr w)) = liftIO $ SAW.scBitvector sc (natValue w)
540564
go (TupleShape _ _ flds) = do
541565
tys <- toListFC getConst <$> traverseFC (\x -> Const <$> goField x) flds
542566
liftIO $ SAW.scTupleType sc tys

crucible-mir-comp/src/Mir/Compositional/Override.hs

+4
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ matchArg sym sc eval allocSpecs md shp rv sv = go shp rv sv
407407
goRef refTy tpr ref alloc 0
408408
go (RefShape refTy _ tpr) ref (MS.SetupElem () (MS.SetupVar alloc) idx) =
409409
goRef refTy tpr ref alloc idx
410+
go (FnPtrShape _ _ _) _ _ =
411+
error "Function pointers not currently supported in overrides"
410412
go shp _ sv = error $ "matchArg: type error: bad SetupValue " ++
411413
show (MS.ppSetupValue sv) ++ " for " ++ show (shapeType shp)
412414

@@ -528,6 +530,8 @@ setupToReg sym sc termSub regMap allocMap shp sv = go shp sv
528530
Nothing -> error $ "setupToReg: type error: bad reference type for " ++ show alloc ++
529531
": got " ++ show (ptr ^. mpType) ++ " but expected " ++ show tpr
530532
Nothing -> error $ "setupToReg: no definition for " ++ show alloc
533+
go (FnPtrShape _ _ _) _ =
534+
error "Function pointers not currently supported in overrides"
531535
go shp sv = error $ "setupToReg: type error: bad SetupValue for " ++ show (shapeType shp) ++
532536
": " ++ show (MS.ppSetupValue sv)
533537

crux-mir-comp/DESIGN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn f(x: (u8, u8)) -> (u8, u8) {
1919
(x.1, x.0)
2020
}
2121

22-
#[crux_test]
22+
#[crux::test]
2323
fn f_test() {
2424
let x = <(u8, u8)>::symbolic("x");
2525
crucible_assume!(x.0 > 0);
@@ -45,7 +45,7 @@ fn f_spec() -> MethodSpec {
4545
msb.finish()
4646
}
4747

48-
#[crux_test]
48+
#[crux::test]
4949
fn use_f() {
5050
f_spec().enable();
5151

crux-mir-comp/crux-mir-comp.cabal

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ library
2121
default-language: Haskell2010
2222
build-depends: base >= 4.9 && < 5,
2323
text,
24+
extra,
2425
crucible,
2526
parameterized-utils >= 1.0.8,
2627
containers,
@@ -39,6 +40,7 @@ library
3940
hs-source-dirs: src
4041
exposed-modules: Mir.Compositional
4142
Mir.Cryptol
43+
other-modules: Mir.Compositional.DefId
4244

4345
ghc-options: -Wall -Wno-name-shadowing
4446

@@ -104,6 +106,8 @@ test-suite test
104106
config-schema,
105107
config-value,
106108
bytestring,
109+
regex-base,
110+
regex-posix,
107111
utf8-string
108112

109113
default-language: Haskell2010

crux-mir-comp/src/Mir/Compositional.hs

+12-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ where
1212
import Data.Parameterized.Context (pattern Empty, pattern (:>))
1313
import Data.Parameterized.NatRepr
1414
import Data.Text (Text)
15-
import qualified Data.Text as Text
1615

1716
import Lang.Crucible.Backend
1817
import Lang.Crucible.CFG.Core
@@ -28,6 +27,7 @@ import Mir.Intrinsics
2827

2928
import Mir.Compositional.Builder (builderNew)
3029
import Mir.Compositional.Clobber (clobberGlobalsOverride)
30+
import Mir.Compositional.DefId (hasInstPrefix)
3131

3232

3333
compositionalOverrides ::
@@ -40,15 +40,15 @@ compositionalOverrides ::
4040
Maybe (OverrideSim (p sym) sym MIR rtp a r ())
4141
compositionalOverrides _symOnline cs name cfg
4242

43-
| (normDefId "crucible::method_spec::raw::builder_new" <> "::_inst") `Text.isPrefixOf` name
43+
| hasInstPrefix ["crucible", "method_spec", "raw", "builder_new"] explodedName
4444
, Empty <- cfgArgTypes cfg
4545
, MethodSpecBuilderRepr <- cfgReturnType cfg
4646
= Just $ bindFnHandle (cfgHandle cfg) $ UseOverride $
4747
mkOverride' "method_spec_builder_new" MethodSpecBuilderRepr $ do
4848
msb <- builderNew cs (textId name)
4949
return $ MethodSpecBuilder msb
5050

51-
| (normDefId "crucible::method_spec::raw::builder_add_arg" <> "::_inst") `Text.isPrefixOf` name
51+
| hasInstPrefix ["crucible", "method_spec", "raw", "builder_add_arg"] explodedName
5252
, Empty :> MethodSpecBuilderRepr :> MirReferenceRepr _tpr <- cfgArgTypes cfg
5353
, MethodSpecBuilderRepr <- cfgReturnType cfg
5454
= Just $ bindFnHandle (cfgHandle cfg) $ UseOverride $
@@ -58,7 +58,7 @@ compositionalOverrides _symOnline cs name cfg
5858
msb' <- msbAddArg tpr argRef msb
5959
return $ MethodSpecBuilder msb'
6060

61-
| (normDefId "crucible::method_spec::raw::builder_set_return" <> "::_inst") `Text.isPrefixOf` name
61+
| hasInstPrefix ["crucible", "method_spec", "raw", "builder_set_return"] explodedName
6262
, Empty :> MethodSpecBuilderRepr :> MirReferenceRepr _tpr <- cfgArgTypes cfg
6363
, MethodSpecBuilderRepr <- cfgReturnType cfg
6464
= Just $ bindFnHandle (cfgHandle cfg) $ UseOverride $
@@ -68,7 +68,7 @@ compositionalOverrides _symOnline cs name cfg
6868
msb' <- msbSetReturn tpr argRef msb
6969
return $ MethodSpecBuilder msb'
7070

71-
| normDefId "crucible::method_spec::raw::builder_gather_assumes" == name
71+
| ["crucible", "method_spec", "raw", "builder_gather_assumes"] == explodedName
7272
, Empty :> MethodSpecBuilderRepr <- cfgArgTypes cfg
7373
, MethodSpecBuilderRepr <- cfgReturnType cfg
7474
= Just $ bindFnHandle (cfgHandle cfg) $ UseOverride $
@@ -77,7 +77,7 @@ compositionalOverrides _symOnline cs name cfg
7777
msb' <- msbGatherAssumes msb
7878
return $ MethodSpecBuilder msb'
7979

80-
| normDefId "crucible::method_spec::raw::builder_gather_asserts" == name
80+
| ["crucible", "method_spec", "raw", "builder_gather_asserts"] == explodedName
8181
, Empty :> MethodSpecBuilderRepr <- cfgArgTypes cfg
8282
, MethodSpecBuilderRepr <- cfgReturnType cfg
8383
= Just $ bindFnHandle (cfgHandle cfg) $ UseOverride $
@@ -86,7 +86,7 @@ compositionalOverrides _symOnline cs name cfg
8686
msb' <- msbGatherAsserts msb
8787
return $ MethodSpecBuilder msb'
8888

89-
| normDefId "crucible::method_spec::raw::builder_finish" == name
89+
| ["crucible", "method_spec", "raw", "builder_finish"] == explodedName
9090
, Empty :> MethodSpecBuilderRepr <- cfgArgTypes cfg
9191
, MethodSpecRepr <- cfgReturnType cfg
9292
= Just $ bindFnHandle (cfgHandle cfg) $ UseOverride $
@@ -95,15 +95,15 @@ compositionalOverrides _symOnline cs name cfg
9595
msbFinish msb
9696

9797

98-
| normDefId "crucible::method_spec::raw::clobber_globals" == name
98+
| ["crucible", "method_spec", "raw", "clobber_globals"] == explodedName
9999
, Empty <- cfgArgTypes cfg
100100
, UnitRepr <- cfgReturnType cfg
101101
= Just $ bindFnHandle (cfgHandle cfg) $ UseOverride $
102102
mkOverride' "method_spec_clobber_globals" UnitRepr $ do
103103
clobberGlobalsOverride cs
104104

105105

106-
| normDefId "crucible::method_spec::raw::spec_pretty_print" == name
106+
| ["crucible", "method_spec", "raw", "spec_pretty_print"] == explodedName
107107
, Empty :> MethodSpecRepr <- cfgArgTypes cfg
108108
, MirSliceRepr (BVRepr w) <- cfgReturnType cfg
109109
, Just Refl <- testEquality w (knownNat @8)
@@ -112,7 +112,7 @@ compositionalOverrides _symOnline cs name cfg
112112
RegMap (Empty :> RegEntry _tpr (MethodSpec ms _)) <- getOverrideArgs
113113
msPrettyPrint ms
114114

115-
| normDefId "crucible::method_spec::raw::spec_enable" == name
115+
| ["crucible", "method_spec", "raw", "spec_enable"] == explodedName
116116
, Empty :> MethodSpecRepr <- cfgArgTypes cfg
117117
, UnitRepr <- cfgReturnType cfg
118118
= Just $ bindFnHandle (cfgHandle cfg) $ UseOverride $
@@ -122,4 +122,5 @@ compositionalOverrides _symOnline cs name cfg
122122

123123

124124
| otherwise = Nothing
125-
125+
where
126+
explodedName = textIdKey name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
module Mir.Compositional.DefId
3+
( hasInstPrefix
4+
) where
5+
6+
import Data.List.Extra (unsnoc)
7+
import qualified Data.Text as Text
8+
import Data.Text (Text)
9+
10+
import Mir.DefId
11+
12+
-- | Check if @edid@ has the same path as @pfxInit@, plus a final path
13+
-- component that begins with the name @_inst@.
14+
hasInstPrefix :: [Text] -> ExplodedDefId -> Bool
15+
hasInstPrefix pfxInit edid =
16+
case unsnoc edid of
17+
Nothing -> False
18+
Just (edidInit, edidLast) ->
19+
pfxInit == edidInit &&
20+
"_inst" `Text.isPrefixOf` edidLast

crux-mir-comp/src/Mir/Cryptol.hs

+8-4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import qualified Verifier.SAW.Recognizer as SAW (asExtCns)
6262
import qualified Verifier.SAW.TypedTerm as SAW
6363

6464
import Mir.Compositional.Convert
65+
import Mir.Compositional.DefId (hasInstPrefix)
6566

6667

6768
cryptolOverrides ::
@@ -74,7 +75,7 @@ cryptolOverrides ::
7475
Maybe (OverrideSim (p sym) sym MIR rtp a r ())
7576
cryptolOverrides _symOnline cs name cfg
7677

77-
| (normDefId "crucible::cryptol::load" <> "::_inst") `Text.isPrefixOf` name
78+
| hasInstPrefix ["crucible", "cryptol", "load"] explodedName
7879
, Empty
7980
:> MirSliceRepr (BVRepr (testEquality (knownNat @8) -> Just Refl))
8081
:> MirSliceRepr (BVRepr (testEquality (knownNat @8) -> Just Refl))
@@ -90,7 +91,7 @@ cryptolOverrides _symOnline cs name cfg
9091
RegMap (Empty :> RegEntry _tpr modulePathStr :> RegEntry _tpr' nameStr) <- getOverrideArgs
9192
cryptolLoad (cs ^. collection) sig (cfgReturnType cfg) modulePathStr nameStr
9293

93-
| (normDefId "crucible::cryptol::override_" <> "::_inst") `Text.isPrefixOf` name
94+
| hasInstPrefix ["crucible", "cryptol", "override_"] explodedName
9495
, Empty
9596
:> UnitRepr
9697
:> MirSliceRepr (BVRepr (testEquality (knownNat @8) -> Just Refl))
@@ -114,7 +115,7 @@ cryptolOverrides _symOnline cs name cfg
114115
:> RegEntry _tpr' nameStr) <- getOverrideArgs
115116
cryptolOverride (cs ^. collection) mh modulePathStr nameStr
116117

117-
| (normDefId "crucible::cryptol::munge" <> "::_inst") `Text.isPrefixOf` name
118+
| hasInstPrefix ["crucible", "cryptol", "munge"] explodedName
118119
, Empty :> tpr <- cfgArgTypes cfg
119120
, tpr' <- cfgReturnType cfg
120121
, Just Refl <- testEquality tpr tpr'
@@ -131,7 +132,8 @@ cryptolOverrides _symOnline cs name cfg
131132
liftIO $ munge sym shp rv
132133

133134
| otherwise = Nothing
134-
135+
where
136+
explodedName = textIdKey name
135137

136138
cryptolLoad ::
137139
forall sym p t st fs rtp a r tp .
@@ -333,6 +335,8 @@ munge sym shp rv = do
333335
AnyValue tpr <$> goFields flds rvs
334336
| otherwise = error $ "munge: StructShape AnyValue with NYI TypeRepr " ++ show tpr
335337
go (TransparentShape _ shp) rv = go shp rv
338+
go (FnPtrShape _ _ _) _ =
339+
error "Function pointers not currently supported in overrides"
336340
-- TODO: RefShape
337341
go shp _ = error $ "munge: " ++ show (shapeType shp) ++ " NYI"
338342

0 commit comments

Comments
 (0)