Skip to content

Commit

Permalink
macaw-aarch32-syntax: Syntactic sugar for for AArch32 CFGs
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Nov 19, 2024
1 parent d7470c1 commit de36027
Show file tree
Hide file tree
Showing 11 changed files with 532 additions and 0 deletions.
1 change: 1 addition & 0 deletions cabal.project.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
packages: base/
macaw-aarch32/
macaw-aarch32-symbolic/
macaw-aarch32-syntax/
macaw-dump/
macaw-semmc/
macaw-ppc/
Expand Down
30 changes: 30 additions & 0 deletions macaw-aarch32-syntax/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright (c) 2024 Galois Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.

* Neither the name of Galois, Inc. nor the names of its contributors
may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 changes: 21 additions & 0 deletions macaw-aarch32-syntax/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# macaw-aarch32-syntax

This package provides concrete syntax for macaw-aarch32-symbolic types and
operations.

Concretely, it implements a `ParserHooks` for use with [`crucible-syntax`][syn].
This `ParserHooks` supports the following types and operations:

**Types**:

- `AArch32Regs`: the struct of all AArch32 registers

**Operations**:

- `get-reg :: AArch32Reg -> AArch32Regs -> t`: extract an x86 register
- `set-reg :: AArch32Reg -> t -> AArch32Regs -> AArch32Regs`: set an x86 register
- Registers:
- `r1 :: AArch32Reg`: ???
- TODO

[syn]: https://github.com/GaloisInc/crucible/tree/master/crucible-syntax
127 changes: 127 additions & 0 deletions macaw-aarch32-syntax/macaw-aarch32-syntax.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
Cabal-version: 2.2
Name: macaw-aarch32-syntax
Version: 0.1
Author: Galois Inc.
Maintainer: langston@galois.com
Build-type: Simple
License: BSD-3-Clause
License-file: LICENSE
Category: Language
Synopsis: A syntax for macaw-aarch32-symbolic control-flow graphs
-- Description:

extra-doc-files: README.md
extra-source-files:
test-data/*.cbl
test-data/*.out.good

common shared
-- Specifying -Wall and -Werror can cause the project to fail to build on
-- newer versions of GHC simply due to new warnings being added to -Wall. To
-- prevent this from happening we manually list which warnings should be
-- considered errors. We also list some warnings that are not in -Wall, though
-- try to avoid "opinionated" warnings (though this judgement is clearly
-- subjective).
--
-- Warnings are grouped by the GHC version that introduced them, and then
-- alphabetically.
--
-- A list of warnings and the GHC version in which they were introduced is
-- available here:
-- https://ghc.gitlab.haskell.org/ghc/doc/users_guide/using-warnings.html

-- Since GHC 9.4 or earlier:
ghc-options:
-Wall
-Werror=ambiguous-fields
-Werror=compat-unqualified-imports
-Werror=deferred-type-errors
-Werror=deprecated-flags
-Werror=deprecations
-Werror=deriving-defaults
-Werror=dodgy-foreign-imports
-Werror=duplicate-exports
-Werror=empty-enumerations
-Werror=forall-identifier
-Werror=identities
-Werror=inaccessible-code
-Werror=incomplete-patterns
-Werror=incomplete-record-updates
-Werror=incomplete-uni-patterns
-Werror=inline-rule-shadowing
-Werror=misplaced-pragmas
-Werror=missed-extra-shared-lib
-Werror=missing-exported-signatures
-Werror=missing-fields
-Werror=missing-home-modules
-Werror=missing-methods
-Werror=operator-whitespace
-Werror=operator-whitespace-ext-conflict
-Werror=overflowed-literals
-Werror=overlapping-patterns
-Werror=partial-fields
-Werror=partial-type-signatures
-Werror=redundant-bang-patterns
-Werror=redundant-strictness-flags
-Werror=simplifiable-class-constraints
-Werror=star-binder
-Werror=star-is-type
-Werror=tabs
-Werror=typed-holes
-Werror=type-equality-out-of-scope
-Werror=type-equality-requires-operators
-Werror=unrecognised-pragmas
-Werror=unrecognised-warning-flags
-Werror=unsupported-calling-conventions
-Werror=unsupported-llvm-version
-Werror=unticked-promoted-constructors
-Werror=unused-imports
-Werror=warnings-deprecations
-Werror=wrong-do-bind

ghc-prof-options: -O2 -fprof-auto-top
default-language: Haskell2010

library
import: shared

build-depends:
base >= 4.13,
containers,
crucible,
crucible-syntax,
macaw-aarch32,
macaw-aarch32-symbolic,
macaw-base,
macaw-symbolic,
mtl,
parameterized-utils,
text,
what4,

hs-source-dirs: src

exposed-modules:
Data.Macaw.AArch32.Symbolic.Syntax

test-suite macaw-aarch32-syntax-tests
import: shared
type: exitcode-stdio-1.0
main-is: Test.hs
hs-source-dirs: test
build-depends:
base,
containers,
crucible >= 0.1,
crucible-syntax,
crucible-llvm-syntax,
filepath,
macaw-aarch32,
macaw-aarch32-symbolic,
macaw-aarch32-syntax,
macaw-symbolic,
macaw-symbolic-syntax,
parameterized-utils >= 0.1.7,
tasty,
tasty-golden,
text,
175 changes: 175 additions & 0 deletions macaw-aarch32-syntax/src/Data/Macaw/AArch32/Symbolic/Syntax.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE OverloadedStrings #-}

-- | 'LCSC.ParserHooks' for parsing macaw-aarch32-symbolic CFGs.
module Data.Macaw.AArch32.Symbolic.Syntax
( aarch32ParserHooks
-- * Types
, aarch32TypeParser
-- * Operations
, parseRegs
, parseReg
, aarch32AtomParser
) where

import Control.Applicative ( empty )
import Control.Monad qualified as Monad
import Control.Monad.IO.Class (MonadIO)
import Control.Monad.State.Strict (MonadState)
import Control.Monad.Writer.Strict (MonadWriter)
import Data.Text qualified as Text

import Data.Macaw.Symbolic qualified as DMS
import Data.Macaw.ARM qualified as ARM
import Data.Macaw.AArch32.Symbolic qualified as AArch32
import Data.Macaw.AArch32.Symbolic.Regs qualified as AArch32R
import Data.Parameterized.Context qualified as Ctx
import Data.Parameterized.Some (Some(..))
import Lang.Crucible.CFG.Expr as Expr
import Lang.Crucible.CFG.Reg (Atom, Stmt)
import Lang.Crucible.CFG.Reg qualified as Reg
import Lang.Crucible.Syntax.Atoms qualified as LCSA
import Lang.Crucible.Syntax.Concrete qualified as LCSC
import Lang.Crucible.Syntax.Monad qualified as LCSM
import Lang.Crucible.Types qualified as LCT
import What4.ProgramLoc (Posd(..))

-- | Parser hooks for macaw-aarch32-symbolic CFGs
aarch32ParserHooks :: LCSC.ParserHooks ext
aarch32ParserHooks =
LCSC.ParserHooks
{ LCSC.extensionTypeParser = aarch32TypeParser
, LCSC.extensionParser = aarch32AtomParser
}

---------------------------------------------------------------------
-- Types

-- Helper, not exported
namedAtom :: LCSM.MonadSyntax LCSA.Atomic m => Text.Text -> m ()
namedAtom expectName = do
name <- LCSC.atomName
Monad.unless (name == LCSA.AtomName expectName) LCSM.cut

-- Helper, not exported
aarch32RegTypes :: Ctx.Assignment LCT.TypeRepr (DMS.MacawCrucibleRegTypes ARM.ARM)
aarch32RegTypes = DMS.crucArchRegTypes AArch32.aarch32MacawSymbolicFns

-- Helper, not exported
aarch32RegStructType :: LCT.TypeRepr (DMS.ArchRegStruct ARM.ARM)
aarch32RegStructType = LCT.StructRepr aarch32RegTypes

-- | Parser for type extensions to Crucible syntax
aarch32TypeParser ::
LCSM.MonadSyntax LCSA.Atomic m =>
m (Some LCT.TypeRepr)
aarch32TypeParser = do
namedAtom "AArch32Regs"
pure (Some aarch32RegStructType)

---------------------------------------------------------------------
-- Operations

parseRegs ::
( LCSM.MonadSyntax LCSA.Atomic m
, MonadIO m
, MonadState (LCSC.SyntaxState s) m
, MonadWriter [Posd (Stmt ext s)] m
, IsSyntaxExtension ext
, ?parserHooks :: LCSC.ParserHooks ext
) =>
m (Atom s (DMS.ArchRegStruct ARM.ARM))
parseRegs =
LCSM.describe "a struct of AArch32 register values" $ do
assign <- LCSC.operands (Ctx.Empty Ctx.:> aarch32RegStructType)
pure (assign Ctx.! Ctx.baseIndex)

parseReg :: LCSM.MonadSyntax LCSA.Atomic m => m (Some (Ctx.Index (DMS.MacawCrucibleRegTypes ARM.ARM)))
parseReg =
LCSM.describe "an AArch32 register" $ do
name <- LCSC.atomName
case name of
LCSA.AtomName "r0" -> pure (Some AArch32R.r0)
LCSA.AtomName "r1" -> pure (Some AArch32R.r1)
LCSA.AtomName "r2" -> pure (Some AArch32R.r2)
LCSA.AtomName "r3" -> pure (Some AArch32R.r3)
LCSA.AtomName "r4" -> pure (Some AArch32R.r4)
LCSA.AtomName "r5" -> pure (Some AArch32R.r5)
LCSA.AtomName "r6" -> pure (Some AArch32R.r6)
LCSA.AtomName "r7" -> pure (Some AArch32R.r7)
LCSA.AtomName "r8" -> pure (Some AArch32R.r8)
LCSA.AtomName "r9" -> pure (Some AArch32R.r9)
LCSA.AtomName "r10" -> pure (Some AArch32R.r10)
LCSA.AtomName "r11" -> pure (Some AArch32R.r11)
LCSA.AtomName "fp" -> pure (Some AArch32R.fp)
LCSA.AtomName "r12" -> pure (Some AArch32R.r12)
LCSA.AtomName "ip" -> pure (Some AArch32R.ip)
LCSA.AtomName "r13" -> pure (Some AArch32R.r13)
LCSA.AtomName "sp" -> pure (Some AArch32R.sp)
LCSA.AtomName "r14" -> pure (Some AArch32R.r14)
LCSA.AtomName "lr" -> pure (Some AArch32R.lr)
LCSA.AtomName "v0" -> pure (Some AArch32R.v0)
LCSA.AtomName "v1" -> pure (Some AArch32R.v1)
LCSA.AtomName "v2" -> pure (Some AArch32R.v2)
LCSA.AtomName "v3" -> pure (Some AArch32R.v3)
LCSA.AtomName "v4" -> pure (Some AArch32R.v4)
LCSA.AtomName "v5" -> pure (Some AArch32R.v5)
LCSA.AtomName "v6" -> pure (Some AArch32R.v6)
LCSA.AtomName "v7" -> pure (Some AArch32R.v7)
LCSA.AtomName "v8" -> pure (Some AArch32R.v8)
LCSA.AtomName "v9" -> pure (Some AArch32R.v9)
LCSA.AtomName "v10" -> pure (Some AArch32R.v10)
LCSA.AtomName "v11" -> pure (Some AArch32R.v11)
LCSA.AtomName "v12" -> pure (Some AArch32R.v12)
LCSA.AtomName "v13" -> pure (Some AArch32R.v13)
LCSA.AtomName "v14" -> pure (Some AArch32R.v14)
LCSA.AtomName "v15" -> pure (Some AArch32R.v15)
LCSA.AtomName "v16" -> pure (Some AArch32R.v16)
LCSA.AtomName "v17" -> pure (Some AArch32R.v17)
LCSA.AtomName "v18" -> pure (Some AArch32R.v18)
LCSA.AtomName "v19" -> pure (Some AArch32R.v19)
LCSA.AtomName "v20" -> pure (Some AArch32R.v20)
LCSA.AtomName "v21" -> pure (Some AArch32R.v21)
LCSA.AtomName "v22" -> pure (Some AArch32R.v22)
LCSA.AtomName "v23" -> pure (Some AArch32R.v23)
LCSA.AtomName "v24" -> pure (Some AArch32R.v24)
LCSA.AtomName "v25" -> pure (Some AArch32R.v25)
LCSA.AtomName "v26" -> pure (Some AArch32R.v26)
LCSA.AtomName "v27" -> pure (Some AArch32R.v27)
LCSA.AtomName "v28" -> pure (Some AArch32R.v28)
LCSA.AtomName "v29" -> pure (Some AArch32R.v29)
LCSA.AtomName "v30" -> pure (Some AArch32R.v30)
LCSA.AtomName "v31" -> pure (Some AArch32R.v31)
LCSA.AtomName _ -> empty

aarch32AtomParser ::
( LCSM.MonadSyntax LCSA.Atomic m
, MonadIO m
, MonadState (LCSC.SyntaxState s) m
, MonadWriter [Posd (Stmt ext s)] m
, IsSyntaxExtension ext
, ?parserHooks :: LCSC.ParserHooks ext
) =>
m (Some (Atom s))
aarch32AtomParser =
LCSM.depCons LCSC.atomName $
\case
LCSA.AtomName "get-reg" -> do
loc <- LCSM.position
(Some reg, regs) <- LCSM.cons parseReg parseRegs
let regTy = aarch32RegTypes Ctx.! reg
Some <$> LCSC.freshAtom loc (Reg.EvalApp (Expr.GetStruct regs reg regTy))
LCSA.AtomName "set-reg" -> do
loc <- LCSM.position
LCSM.depCons parseReg $ \(Some reg) -> do
let regTy = aarch32RegTypes Ctx.! reg
assign <- LCSC.operands (Ctx.Empty Ctx.:> regTy Ctx.:> aarch32RegStructType)
let (rest, regs) = Ctx.decompose assign
let (Ctx.Empty, val) = Ctx.decompose rest
Some <$> LCSC.freshAtom loc (Reg.EvalApp (Expr.SetStruct aarch32RegTypes regs reg val))
LCSA.AtomName _ -> empty
1 change: 1 addition & 0 deletions macaw-aarch32-syntax/test-data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.out
Loading

0 comments on commit de36027

Please sign in to comment.