Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAW panic when using constraint guards with types dependent on module parameters #1923

Closed
bboston7 opened this issue Aug 29, 2023 · 7 comments · Fixed by #1930
Closed

SAW panic when using constraint guards with types dependent on module parameters #1923

bboston7 opened this issue Aug 29, 2023 · 7 comments · Fixed by #1930
Assignees
Labels
subsystem: cryptol-saw-core Issues related to Cryptol -> saw-core translation with cryptol-saw-core type: bug Issues reporting bugs or unexpected/unwanted behavior

Comments

@bboston7
Copy link
Contributor

Given the parameterized module:

module Parameterized where

parameter
    type gamma1 : #
    type constraint (fin gamma1, gamma1 >= 2, 32 >= width gamma1)

x : [gamma1]
x | () => 0

and instantiation:

module Instantiated = Parameterized where

type gamma1 =  3

and SAW file importing the instantiated module:

import "Instantiated.cry";

SAW panics with the error:

[20:44:58.951] You have encountered a bug in cryptol-saw-core's implementation.
*** Please create an issue at https://github.com/GaloisInc/saw-script/issues

%< --------------------------------------------------- 
  Revision:  0aa2492e78f09b16e8d40e0ac3f71c5b40978731
  Branch:    master (uncommited files present)
  Location:  importType TVBound
  Message:   
CallStack (from HasCallStack):
  panic, called at src/Verifier/SAW/Cryptol/Panic.hs:13:9 in cryptol-saw-core-0.1-inplace:Verifier.SAW.Cryptol.Panic
  panic, called at src/Verifier/SAW/Cryptol.hs:264:37 in cryptol-saw-core-0.1-inplace:Verifier.SAW.Cryptol
%< --------------------------------------------------- 

I'm still debugging exactly what's going on, but here are some things I learned in minimizing the example:

  • When built with profiling to get a stack trace, the relevant stack trace for the error appears to be:
*** Exception (reporting due to +RTS -xc): (THUNK), stack trace:                                                                                                                    [112/15100]
  Panic.panic,                                                                                 
  called from Verifier.SAW.Cryptol.Panic.panic,                                                
  called from Verifier.SAW.Cryptol.importType,                                                 
  called from Verifier.SAW.Cryptol.CAF                                                         
  --> evaluated by: Verifier.SAW.Cryptol.importType,                                           
  called from Verifier.SAW.Cryptol.importExpr,                                                 
  called from Verifier.SAW.Cryptol.importTopLevelDeclGroups,                                   
  called from Verifier.SAW.CryptolEnv.importModule,                                                                                                                                            
  called from SAWScript.Value.getSharedContext,                                                                                                                                                
  called from Lang.Crucible.Utils.StateContT.get,                                              
  called from SAWScript.Value.getTopLevelRW,                                                   
  called from SAWScript.Interpreter.interpretStmt,                                             
  called from Lang.Crucible.Utils.StateContT.<$,                                               
  called from Lang.Crucible.Utils.StateContT.*>,                                               
  called from SAWScript.Value.getOptions,                                                      
  called from Lang.Crucible.Utils.StateContT.lift,                                             
  called from Lang.Crucible.Utils.StateContT.liftIO,                                           
  called from Lang.Crucible.Utils.StateContT.catch,                                            
  called from SAWScript.Value.io,                                                              
  called from SAWScript.Value.bracketTopLevel,                                                 
  called from SAWScript.Interpreter.interpretFile,                                                                                                                                             
  called from SAWScript.Value.runTopLevel,                                                                                                                                                     
  called from SAWScript.Interpreter.processFile,                                                                                                                                               
  called from Main.main 
  • The panic itself occurs because looking up gamma1 in the environment fails (see this line of the translator), so I imagine some other part of the translator isn't adding gamma1 to the environment like it should.
  • I'm not sure whether the constraint guard itself is relevant. The stack trace doesn't include the constraint guard handling part of the cryptol translator, and adding debug tracing shows that this panic occurs before any constraint guard translation. However, I couldn't reproduce this bug without using a constraint guard. Maybe constraint guards change something else earlier in the translation that I didn't properly handle in Add support for translating Cryptol constraint guards #1911.

@qsctr I know you've been looking at the Cryptol translator lately and you have experience with the Cryptol AST. Do you have any ideas about what might be going wrong here?

I'm going to keep digging and I'll add comments for anything else I learn.

@bboston7 bboston7 added type: bug Issues reporting bugs or unexpected/unwanted behavior subsystem: cryptol-saw-core Issues related to Cryptol -> saw-core translation with cryptol-saw-core labels Aug 29, 2023
@qsctr
Copy link
Contributor

qsctr commented Aug 29, 2023

In my testing it does seem to be happening in the constraint guard handling -- specifically in this call to importType.

The declaration that is causing the panic is this one, which has a TVBound referencing gamma1 inside it.

        , NonRecursive
            ( Decl
                { dName = Name
                    { nUnique = 4780
                    , nInfo = GlobalName UserName
                        ( OrigName
                            { ogNamespace = NSValue
                            , ogModule = TopModule
                                ( ModName "Instantiated" NormalName )
                            , ogSource = FromFunctorInst
                            , ogName = Ident False NormalName "x"
                            , ogFromParam = Nothing
                            }
                        )
                    , nFixity = Nothing
                    , nLoc = Range
                        { from = Position
                            { line = 8
                            , col = 1
                            }
                        , to = Position
                            { line = 8
                            , col = 2
                            }
                        , source = "./Parameterized.cry"
                        }
                    }
                , dSignature = Forall
                    { sVars = []
                    , sProps = []
                    , sType = TCon ( TC TCSeq )
                        [ TCon
                            ( TC
                                ( TCNum 3 )
                            ) []
                        , TCon ( TC TCBit ) []
                        ]
                    }
                , dDefinition = DExpr
                    ( EPropGuards
                        [
                            ( []
                            , EVar
                                ( Name
                                    { nUnique = 4781
                                    , nInfo = GlobalName UserName
                                        ( OrigName
                                            { ogNamespace = NSValue
                                            , ogModule = TopModule
                                                ( ModName "Instantiated" NormalName )
                                            , ogSource = FromFunctorInst
                                            , ogName = Ident False NormalName "x()"
                                            , ogFromParam = Nothing
                                            }
                                        )
                                    , nFixity = Nothing
                                    , nLoc = Range
                                        { from = Position
                                            { line = 8
                                            , col = 1
                                            }
                                        , to = Position
                                            { line = 8
                                            , col = 2
                                            }
                                        , source = "./Parameterized.cry"
                                        }
                                    }
                                )
                            )
                        ]
                        ( TCon ( TC TCSeq )
                            [ TVar
                                ( TVBound
                                    ( TParam
                                        { tpUnique = 4782
                                        , tpKind = KNum
                                        , tpFlav = TPModParam
                                            ( Name
                                                { nUnique = 4782
                                                , nInfo = GlobalName UserName
                                                    ( OrigName
                                                        { ogNamespace = NSType
                                                        , ogModule = TopModule
                                                            ( ModName "Instantiated" NormalName )
                                                        , ogSource = FromFunctorInst
                                                        , ogName = Ident False NormalName "gamma1"
                                                        , ogFromParam = Just
                                                            ( Ident False AnonIfaceModName "Parameterized" )
                                                        }
                                                    )
                                                , nFixity = Nothing
                                                , nLoc = Range
                                                    { from = Position
                                                        { line = 1
                                                        , col = 8
                                                        }
                                                    , to = Position
                                                        { line = 1
                                                        , col = 21
                                                        }
                                                    , source = "./Parameterized.cry"
                                                    }
                                                }
                                            )
                                        , tpInfo = TVarInfo
                                            { tvarSource = Range
                                                { from = Position
                                                    { line = 1
                                                    , col = 8
                                                    }
                                                , to = Position
                                                    { line = 1
                                                    , col = 21
                                                    }
                                                , source = "./Parameterized.cry"
                                                }
                                            , tvarDesc = TVFromModParam
                                                ( Name
                                                    { nUnique = 4782
                                                    , nInfo = GlobalName UserName
                                                        ( OrigName
                                                            { ogNamespace = NSType
                                                            , ogModule = TopModule
                                                                ( ModName "Instantiated" NormalName )
                                                            , ogSource = FromFunctorInst
                                                            , ogName = Ident False NormalName "gamma1"
                                                            , ogFromParam = Just
                                                                ( Ident False AnonIfaceModName "Parameterized" )
                                                            }
                                                        )
                                                    , nFixity = Nothing
                                                    , nLoc = Range
                                                        { from = Position
                                                            { line = 1
                                                            , col = 8
                                                            }
                                                        , to = Position
                                                            { line = 1
                                                            , col = 21
                                                            }
                                                        , source = "./Parameterized.cry"
                                                        }
                                                    }
                                                )
                                            }
                                        }
                                    )
                                )
                            , TCon ( TC TCBit ) []
                            ]
                        )
                    )
                , dPragmas = []
                , dInfix = False
                , dFixity = Nothing
                , dDoc = Nothing
                }
            )

@bboston7
Copy link
Contributor Author

Thanks, I was looking for importNumericConstraintAsBool calls and completely missed that importType call. Now to figure out why gamma1 isn't in the environment.

@qsctr
Copy link
Contributor

qsctr commented Aug 29, 2023

Actually, I think this might be a Cryptol bug, because the gamma1 is supposed to be substituted with 3 everywhere in the module after instantiation.

@qsctr
Copy link
Contributor

qsctr commented Aug 29, 2023

I created GaloisInc/cryptol#1569 to track the Cryptol bug. The SAW import seems to work with the Cryptol fix applied.

@bboston7
Copy link
Contributor Author

Great debugging! Thank you for looking into this!

@qsctr
Copy link
Contributor

qsctr commented Sep 1, 2023

The Cryptol fix is merged now

@bboston7 bboston7 self-assigned this Sep 1, 2023
@bboston7
Copy link
Contributor Author

bboston7 commented Sep 1, 2023

Thank you! I'll update the Cryptol submodule in SAW

bboston7 pushed a commit that referenced this issue Sep 1, 2023
Closes #1923

This change updates the cryptol submodule to pull in the fix for
constraint guards with types dependent on module parameters.

See GaloisInc/cryptol#1569 for the relevant
cryptol issue and GaloisInc/cryptol#1570 for the
fix.
bboston7 pushed a commit that referenced this issue Sep 1, 2023
Closes #1923

This change updates the cryptol submodule to pull in the fix for
constraint guards with types dependent on module parameters.

See GaloisInc/cryptol#1569 for the relevant
cryptol issue and GaloisInc/cryptol#1570 for the
fix.
@mergify mergify bot closed this as completed in #1930 Sep 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
subsystem: cryptol-saw-core Issues related to Cryptol -> saw-core translation with cryptol-saw-core type: bug Issues reporting bugs or unexpected/unwanted behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants