Skip to content

Commit d6e46a1

Browse files
Nick Walkerttuegel
authored andcommitted
Implement getExitCode (#499)
* exit{} attribute * execGetExitCode, extracts an exit cell's contents, returns the corresponding exit code * generalize mkSymbol_ over the returned symbol's pattern phantom type * add execGetExitCode tests * Add exit{} attribute tests * rename attribute field Co-Authored-By: njohnwalker <[email protected]> * review * New mkEqualityAxiom * remove unused attribute exit{}
1 parent 47ee38f commit d6e46a1

File tree

4 files changed

+124
-4
lines changed

4 files changed

+124
-4
lines changed

kore/app/exec/Main.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ mainWithOptions
351351
case searchParameters of
352352
Nothing -> do
353353
pat <- exec indexedModule strategy' purePattern
354-
return (ExitSuccess, pat)
354+
exitCode <- execGetExitCode indexedModule strategy' pat
355+
return (exitCode, pat)
355356
Just (searchPattern, searchConfig) -> do
356357
pat <-
357358
search

kore/src/Kore/AST/Valid.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ applySymbol
422422
, valid ~ Valid (variable level) level
423423
, pattern' ~ PurePattern level domain variable valid
424424
)
425-
=> SentenceSymbol level pattern'
425+
=> SentenceSymbol level pattern''
426426
-- ^ 'Symbol' declaration
427427
-> [Sort level]
428428
-- ^ 'Symbol' sort parameters
@@ -477,7 +477,7 @@ applySymbol_
477477
, valid ~ Valid (variable level) level
478478
, pattern' ~ PurePattern level domain variable valid
479479
)
480-
=> SentenceSymbol level pattern'
480+
=> SentenceSymbol level pattern''
481481
-> [pattern']
482482
-> pattern'
483483
applySymbol_ sentence = applySymbol sentence []

kore/src/Kore/Exec.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Expose concrete execution as a library
1010
-}
1111
module Kore.Exec
1212
( exec
13+
, execGetExitCode
1314
, search
1415
, prove
1516
, proveWithRepl
@@ -22,19 +23,25 @@ import Control.Monad.Trans.Except
2223
( runExceptT )
2324
import qualified Data.Bifunctor as Bifunctor
2425
import qualified Data.Map.Strict as Map
26+
import System.Exit
27+
( ExitCode (..) )
2528

2629
import Data.Limit
2730
( Limit (..) )
2831
import Kore.AST.Common
32+
import Kore.AST.Identifier
2933
import Kore.AST.MetaOrObject
3034
( Object (..) )
3135
import Kore.AST.Valid
3236
import qualified Kore.Attribute.Axiom as Attribute
3337
import qualified Kore.Builtin as Builtin
38+
import qualified Kore.Domain.Builtin as Domain
3439
import Kore.IndexedModule.IndexedModule
3540
( VerifiedModule )
3641
import Kore.IndexedModule.MetadataTools
3742
( MetadataTools (..), extractMetadataTools )
43+
import Kore.IndexedModule.Resolvers
44+
( resolveSymbol )
3845
import qualified Kore.Logger as Log
3946
import Kore.OnePath.Verification
4047
( Axiom (Axiom), Claim (Claim), defaultStrategy, verify )
@@ -134,6 +141,29 @@ exec indexedModule strategy purePattern = do
134141
where
135142
Valid { patternSort } = extract purePattern
136143

144+
-- | Project the value of the exit cell, if it is present.
145+
execGetExitCode
146+
:: VerifiedModule StepperAttributes Attribute.Axiom
147+
-- ^ The main module
148+
-> ([Rewrite] -> [Strategy (Prim Rewrite)])
149+
-- ^ The strategy to use for execution; see examples in "Kore.Step.Step"
150+
-> CommonStepPattern Object
151+
-- ^ The final pattern (top cell) to extract the exit code
152+
-> Simplifier ExitCode
153+
execGetExitCode indexedModule strategy' purePattern =
154+
case resolveSymbol indexedModule $ noLocationId "LblgetExitCode" of
155+
Left _ -> return ExitSuccess
156+
Right (_, exitCodeSymbol) -> do
157+
exitCodePattern <- exec indexedModule strategy'
158+
$ applySymbol_ exitCodeSymbol [purePattern]
159+
case exitCodePattern of
160+
DV_ _ (Domain.BuiltinInt (Domain.InternalInt _ 0)) ->
161+
return ExitSuccess
162+
DV_ _ (Domain.BuiltinInt (Domain.InternalInt _ exit)) ->
163+
return $ ExitFailure $ fromInteger exit
164+
_ ->
165+
return $ ExitFailure 111
166+
137167
-- | Symbolic search
138168
search
139169
:: VerifiedModule StepperAttributes Attribute.Axiom

kore/test/Test/Kore/Exec.hs

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
module Test.Kore.Exec
22
( test_exec
33
, test_search
4+
, test_execGetExitCode
45
) where
56

67
import Test.Tasty
78
( TestTree, testGroup )
89
import Test.Tasty.HUnit
9-
( testCase )
10+
( assertEqual, testCase )
1011

1112
import Control.Applicative
1213
( liftA2 )
@@ -19,6 +20,8 @@ import Data.Set
1920
import qualified Data.Set as Set
2021
import Data.Text
2122
( Text )
23+
import System.Exit
24+
( ExitCode (..) )
2225

2326
import Kore.AST.Kore
2427
import Kore.AST.Sentence
@@ -29,7 +32,9 @@ import Kore.ASTVerifier.DefinitionVerifier
2932
import qualified Kore.Attribute.Axiom as Attribute
3033
import Kore.Attribute.Constructor
3134
import Kore.Attribute.Functional
35+
import Kore.Attribute.Hook
3236
import qualified Kore.Builtin as Builtin
37+
import qualified Kore.Builtin.Int as Int
3338
import Kore.Exec
3439
import Kore.IndexedModule.IndexedModule
3540
( VerifiedModule )
@@ -280,3 +285,87 @@ applyToNoArgs sort name =
280285
, symbolOrAliasParams = []
281286
}
282287
[]
288+
289+
test_execGetExitCode :: TestTree
290+
test_execGetExitCode =
291+
testGroup "execGetExitCode"
292+
[ makeTestCase "No getExitCode symbol => ExitSuccess"
293+
testModuleNoSymbol 42 ExitSuccess
294+
, makeTestCase "No getExitCode simplification axiom => ExitFailure 111"
295+
testModuleNoAxiom 42 $ ExitFailure 111
296+
, makeTestCase "Exit cell contains 0 => ExitSuccess"
297+
testModuleSuccessfulSimplification 0 ExitSuccess
298+
, makeTestCase "Exit cell contains 42 => ExitFailure 42"
299+
testModuleSuccessfulSimplification 42 $ ExitFailure 42
300+
]
301+
where
302+
unlimited :: Limit Integer
303+
unlimited = Unlimited
304+
305+
makeTestCase name testModule inputInteger expectedCode =
306+
testCase name
307+
$ actual testModule inputInteger >>= assertEqual "" expectedCode
308+
309+
actual testModule exitCode =
310+
SMT.runSMT SMT.defaultConfig
311+
$ evalSimplifier emptyLogger
312+
$ execGetExitCode
313+
(verifiedMyModule testModule)
314+
(Limit.replicate unlimited . anyRewrite)
315+
$ Int.asInternal myIntSort exitCode
316+
317+
-- Module with no getExitCode symbol
318+
testModuleNoSymbol = Module
319+
{ moduleName = ModuleName "MY-MODULE"
320+
, moduleSentences = []
321+
, moduleAttributes = Attributes []
322+
}
323+
-- simplification of the exit code pattern will not produce an integer
324+
-- (no axiom present for the symbol)
325+
testModuleNoAxiom = Module
326+
{ moduleName = ModuleName "MY-MODULE"
327+
, moduleSentences =
328+
[ asSentence intSortDecl
329+
, asSentence getExitCodeDecl
330+
]
331+
, moduleAttributes = Attributes []
332+
}
333+
-- simplification succeeds
334+
testModuleSuccessfulSimplification = Module
335+
{ moduleName = ModuleName "MY-MODULE"
336+
, moduleSentences =
337+
[ asSentence intSortDecl
338+
, asSentence getExitCodeDecl
339+
, mockGetExitCodeAxiom
340+
]
341+
, moduleAttributes = Attributes []
342+
}
343+
344+
myIntSortId = testId "Int"
345+
346+
myIntSort = SortActualSort $ SortActual myIntSortId []
347+
348+
intSortDecl :: VerifiedKoreSentenceSort Object
349+
intSortDecl = SentenceSort
350+
{ sentenceSortName = myIntSortId
351+
, sentenceSortParameters = []
352+
, sentenceSortAttributes = Attributes [hookAttribute Int.sort]
353+
}
354+
355+
getExitCodeId = testId "LblgetExitCode"
356+
357+
getExitCodeDecl :: VerifiedKoreSentenceSymbol Object
358+
getExitCodeDecl =
359+
( mkSymbol_ getExitCodeId [myIntSort] myIntSort )
360+
{ sentenceSymbolAttributes = Attributes [functionalAttribute] }
361+
362+
mockGetExitCodeAxiom =
363+
mkEqualityAxiom
364+
(mkApp myIntSort getExitCodeSym [mkVar v]) (mkVar v) Nothing
365+
where
366+
v = Variable
367+
{ variableName = testId "V"
368+
, variableCounter = mempty
369+
, variableSort = myIntSort
370+
}
371+
getExitCodeSym = SymbolOrAlias getExitCodeId []

0 commit comments

Comments
 (0)