11{-# LANGUAGE DataKinds #-}
2+ {-# LANGUAGE DeriveAnyClass #-}
3+ {-# LANGUAGE DeriveGeneric #-}
4+ {-# LANGUAGE DerivingStrategies #-}
25{-# LANGUAGE ExplicitNamespaces #-}
36{-# LANGUAGE GADTs #-}
47{-# LANGUAGE OverloadedStrings #-}
@@ -16,6 +19,7 @@ import Control.Lens (view, (^.), to)
1619import Control.Monad (foldM , forM_ )
1720import Control.Monad.IO.Class (liftIO )
1821import Control.Monad.State (execStateT , lift , StateT (.. ))
22+ import Data.Aeson (ToJSON )
1923import qualified Data.Text as Text
2024import qualified Data.Map.Strict as Map
2125import Data.IORef (newIORef , modifyIORef , IORef )
@@ -25,6 +29,7 @@ import Data.List.NonEmpty (NonEmpty(..))
2529import qualified Data.List.NonEmpty as NE
2630import qualified Data.Vector as V
2731import qualified Data.BitVector.Sized as BV
32+ import GHC.Generics (Generic )
2833import qualified Prettyprinter as PP
2934import System.Exit (exitFailure )
3035import System.FilePath ((</>) )
@@ -35,6 +40,8 @@ import qualified Copilot.Core.Type as CT
3540
3641import qualified Copilot.Theorem.What4 as CW4
3742
43+ import qualified Copilot.Verifier.Log as Log
44+
3845import Data.Parameterized.Ctx (EmptyCtx )
3946import Data.Parameterized.Context (pattern Empty )
4047import qualified Data.Parameterized.Context as Ctx
@@ -103,19 +110,14 @@ import Crux.Config (cfgJoin, Config(..))
103110import Crux.Config.Load (fromFile , fromEnv )
104111import Crux.Config.Common (cruxOptions , CruxOptions (.. ), postprocessOptions , outputOptions )
105112import Crux.Goal (proveGoalsOffline , provedGoalsTree )
106- import Crux.Log
107- ( cruxLogMessageToSayWhat , withCruxLogMessage , outputHandle
108- , Logs , SupportsCruxLogMessage , logGoal
109- )
113+ import qualified Crux.Log as Log
110114import Crux.Types (SimCtxt , Crux , ProcessedGoals (.. ), ProofResult (.. ))
111115
112116import Crux.LLVM.Config (llvmCruxConfig , LLVMOptions (.. ))
113117import Crux.LLVM.Compile (genBitCode )
118+ import qualified Crux.LLVM.Log as Log
114119import Crux.LLVM.Simulate (setupSimCtxt , parseLLVM , explainFailure )
115- import CruxLLVMMain
116- ( CruxLLVMLogging , withCruxLLVMLogging
117- , cruxLLVMLoggingToSayWhat , processLLVMOptions
118- )
120+ import CruxLLVMMain (processLLVMOptions )
119121
120122import What4.Config
121123 (extendConfig )
@@ -137,6 +139,7 @@ import What4.Symbol (safeSymbol)
137139
138140verify :: CSettings -> [String ] -> String -> Spec -> IO ()
139141verify csettings0 properties prefix spec =
142+ withCopilotLogging $
140143 do (cruxOpts, llvmOpts, csettings, csrc) <-
141144 do llvmcfg <- llvmCruxConfig
142145 let cfg = cfgJoin cruxOptions llvmcfg
@@ -152,24 +155,26 @@ verify csettings0 properties prefix spec =
152155 let csettings = csettings0{ cSettingsOutputDirectory = odir }
153156 let csrc = odir </> prefix ++ " .c"
154157 let cruxOpts1 = cruxOpts0{ outDir = odir, bldDir = odir, inputFiles = [csrc] }
155- ocfg <- defaultOutputConfig cruxLogMessageToSayWhat
158+ ocfg <- defaultOutputConfig copilotLoggingToSayWhat
156159 let ? outputConfig = ocfg (Just (outputOptions cruxOpts1))
157- cruxOpts2 <- withCruxLogMessage ( postprocessOptions cruxOpts1)
160+ cruxOpts2 <- postprocessOptions cruxOpts1
158161 (cruxOpts3, llvmOpts2) <- processLLVMOptions (cruxOpts2, llvmOpts0{ optLevel = 0 })
159162 return (cruxOpts3, llvmOpts2, csettings, csrc)
160163
161164 compileWith csettings prefix spec
162- putStrLn (" Generated " ++ show csrc)
163-
164- ocfg <- defaultOutputConfig cruxLLVMLoggingToSayWhat
165+ ocfg <- defaultOutputConfig copilotLoggingToSayWhat
165166 let ? outputConfig = ocfg (Just (outputOptions cruxOpts))
166- bcFile <- withCruxLLVMLogging (genBitCode cruxOpts llvmOpts)
167- putStrLn (" Compiled " ++ prefix ++ " into " ++ bcFile)
167+ Log. sayCopilot $ Log. GeneratedCFile csrc
168+
169+ bcFile <- genBitCode cruxOpts llvmOpts
170+ Log. sayCopilot $ Log. CompiledBitcodeFile prefix bcFile
168171
169172 verifyBitcode csettings properties spec cruxOpts llvmOpts bcFile
170173
171174verifyBitcode ::
172- Logs CruxLLVMLogging =>
175+ Log. Logs msgs =>
176+ Log. SupportsCruxLogMessage msgs =>
177+ Log. SupportsCopilotLogMessage msgs =>
173178 CSettings ->
174179 [String ] ->
175180 Spec ->
@@ -184,23 +189,21 @@ verifyBitcode csettings properties spec cruxOpts llvmOpts bcFile =
184189 startCaching sym
185190 bbMapRef <- newIORef mempty
186191 let ? recordLLVMAnnotation = \ an bb -> modifyIORef bbMapRef (Map. insert an bb)
187- ocfg <- defaultOutputConfig cruxLLVMLoggingToSayWhat
188- let ? outputConfig = ocfg (Just (outputOptions cruxOpts))
189192
190193 let adapters = [z3Adapter] -- TODO? configurable
191194 extendConfig (solver_adapter_config_options z3Adapter) (getConfiguration sym)
192195
193196 memVar <- mkMemVar " llvm_memory" halloc
194197
195198 let simctx = (setupSimCtxt halloc sym (memOpts llvmOpts) memVar)
196- { printHandle = view outputHandle ? outputConfig }
199+ { printHandle = view Log. outputHandle ? outputConfig }
197200
198201 llvmMod <- parseLLVM bcFile
199202 (Some trans, _warns) <-
200203 let ? transOpts = transOpts llvmOpts
201204 in translateModule halloc memVar llvmMod
202205
203- putStrLn ( " Translated bitcode into Crucible " )
206+ Log. sayCopilot Log. TranslatedToCrucible
204207
205208 let llvmCtxt = trans ^. transContext
206209 let ? lc = llvmCtxt ^. llvmTypeCtx
@@ -209,11 +212,10 @@ verifyBitcode csettings properties spec cruxOpts llvmOpts bcFile =
209212
210213 llvmPtrWidth llvmCtxt $ \ ptrW ->
211214 withPtrWidth ptrW $
212- withCruxLLVMLogging $
213215 do emptyMem <- initializeAllMemory sym llvmCtxt llvmMod
214216 initialMem <- populateAllGlobals sym (globalInitMap trans) emptyMem
215217
216- putStrLn " Generating proof state data "
218+ Log. sayCopilot Log. GeneratingProofState
217219 proofStateBundle <- CW4. computeBisimulationProofBundle sym properties spec
218220
219221 verifyInitialState cruxOpts adapters bbMapRef simctx initialMem
@@ -224,8 +226,9 @@ verifyBitcode csettings properties spec cruxOpts llvmOpts bcFile =
224226
225227verifyInitialState ::
226228 IsSymInterface sym =>
227- Logs msgs =>
228- SupportsCruxLogMessage msgs =>
229+ Log. Logs msgs =>
230+ Log. SupportsCruxLogMessage msgs =>
231+ Log. SupportsCopilotLogMessage msgs =>
229232 sym ~ ExprBuilder t st fs =>
230233 HasPtrWidth wptr =>
231234 HasLLVMAnn sym =>
@@ -241,20 +244,21 @@ verifyInitialState ::
241244 IO ()
242245verifyInitialState cruxOpts adapters bbMapRef simctx mem initialState =
243246 do let sym = simctx^. ctxSymInterface
244- putStrLn " Computing initial state verification conditions "
247+ Log. sayCopilot $ Log. ComputingConditions Log. InitialState
245248 frm <- pushAssumptionFrame sym
246249
247250 assertStateRelation sym mem initialState
248251
249252 popUntilAssumptionFrame sym frm
250253
251- putStrLn " Proving initial state verification conditions "
254+ Log. sayCopilot $ Log. ProvingConditions Log. InitialState
252255 proveObls cruxOpts adapters bbMapRef simctx
253256
254257verifyStepBisimulation ::
255258 IsSymInterface sym =>
256- Logs msgs =>
257- SupportsCruxLogMessage msgs =>
259+ Log. Logs msgs =>
260+ Log. SupportsCruxLogMessage msgs =>
261+ Log. SupportsCopilotLogMessage msgs =>
258262 sym ~ ExprBuilder t st fs =>
259263 HasPtrWidth wptr =>
260264 HasLLVMAnn sym =>
@@ -277,7 +281,7 @@ verifyStepBisimulation ::
277281 IO ()
278282verifyStepBisimulation cruxOpts adapters csettings bbMapRef simctx llvmMod modTrans memVar mem prfbundle =
279283 do let sym = simctx^. ctxSymInterface
280- putStrLn " Computing step bisimulation verification conditions "
284+ Log. sayCopilot $ Log. ComputingConditions Log. StepBisimulation
281285
282286 frm <- pushAssumptionFrame sym
283287
@@ -303,7 +307,7 @@ verifyStepBisimulation cruxOpts adapters csettings bbMapRef simctx llvmMod modTr
303307
304308 popUntilAssumptionFrame sym frm
305309
306- putStrLn " Proving step bisimulation verification conditions "
310+ Log. sayCopilot $ Log. ProvingConditions Log. StepBisimulation
307311 proveObls cruxOpts adapters bbMapRef simctx
308312
309313
@@ -875,8 +879,9 @@ from a `PtrRepr` in `computeEqualVals` to handle structs.
875879proveObls ::
876880 IsSymInterface sym =>
877881 sym ~ ExprBuilder t st fs =>
878- Logs msgs =>
879- SupportsCruxLogMessage msgs =>
882+ Log. Logs msgs =>
883+ Log. SupportsCruxLogMessage msgs =>
884+ Log. SupportsCopilotLogMessage msgs =>
880885 CruxOptions ->
881886 [SolverAdapter st ] ->
882887 IORef (LLVMAnnMap sym ) ->
@@ -899,14 +904,15 @@ summarizeObls _ (Just obls) = map (view labeledPredMsg . proofGoal) (goalsToList
899904-}
900905
901906presentResults ::
902- Logs msgs =>
907+ Log. Logs msgs =>
908+ Log. SupportsCopilotLogMessage msgs =>
903909 IsSymInterface sym =>
904910 sym ->
905911 (ProcessedGoals , Maybe (Goals (Assumptions sym ) (Assertion sym , [ProgramLoc ], ProofResult sym ))) ->
906912 IO ()
907913presentResults sym (num, goals)
908914 | numTotalGoals == 0
909- = putStrLn $ " All obligations proved by concrete simplification "
915+ = Log. sayCopilot Log. AllGoalsProved
910916
911917 -- All goals were proven
912918 | numProvedGoals == numTotalGoals
@@ -921,8 +927,32 @@ presentResults sym (num, goals)
921927 numProvedGoals = provedGoals num
922928
923929 printGoals =
924- do putStrLn $ unwords [ " Proved " , show numProvedGoals, " of " , show numTotalGoals, " total goals " ]
930+ do Log. sayCopilot $ Log. OnlySomeGoalsProved numProvedGoals numTotalGoals
925931 goals' <- provedGoalsTree sym goals
926932 case goals' of
927- Just g -> logGoal g
933+ Just g -> Log. logGoal g
928934 Nothing -> return ()
935+
936+ data CopilotLogging
937+ = LoggingCrux Log. CruxLogMessage
938+ | LoggingCruxLLVM Log. CruxLLVMLogMessage
939+ | LoggingCopilot Log. CopilotLogMessage
940+ deriving stock Generic
941+ deriving anyclass ToJSON
942+
943+ copilotLoggingToSayWhat :: CopilotLogging -> Log. SayWhat
944+ copilotLoggingToSayWhat (LoggingCrux msg) = Log. cruxLogMessageToSayWhat msg
945+ copilotLoggingToSayWhat (LoggingCruxLLVM msg) = Log. cruxLLVMLogMessageToSayWhat msg
946+ copilotLoggingToSayWhat (LoggingCopilot msg) = Log. copilotLogMessageToSayWhat msg
947+
948+ withCopilotLogging ::
949+ ( ( Log. SupportsCruxLogMessage CopilotLogging
950+ , Log. SupportsCruxLLVMLogMessage CopilotLogging
951+ , Log. SupportsCopilotLogMessage CopilotLogging
952+ ) => computation ) ->
953+ computation
954+ withCopilotLogging computation = do
955+ let ? injectCruxLogMessage = LoggingCrux
956+ ? injectCruxLLVMLogMessage = LoggingCruxLLVM
957+ ? injectCopilotLogMessage = LoggingCopilot
958+ in computation
0 commit comments