Skip to content

Commit

Permalink
Add some more Safe tests
Browse files Browse the repository at this point in the history
  • Loading branch information
David Terei committed Jun 30, 2011
1 parent 0947668 commit 39e06ea
Show file tree
Hide file tree
Showing 27 changed files with 276 additions and 0 deletions.
14 changes: 14 additions & 0 deletions deprecate/ControlMonadST.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module ControlMonadST where

import Control.Monad
import Control.Monad.ST
-- import Control.Monad.ST.Unsafe
import Data.STRef

sumST :: Num a => [a] -> IO a
sumST xs = unsafeSTToIO $ do
n <- newSTRef 0
forM_ xs $ \x -> do
modifySTRef n (+x)
readSTRef n

17 changes: 17 additions & 0 deletions deprecate/ControlMonadSTLazy.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module ControlMonadSTLazy where

import Control.Monad
import Control.Monad.ST.Lazy
-- import Control.Monad.ST.Unsafe
import Data.STRef.Lazy

sumST :: Num a => [a] -> a
sumST xs = runST $ do
n <- newSTRef 0
forM_ xs $ \x -> do
modifySTRef n (+x)
readSTRef n

badST :: ()
badST = runST $ unsafeIOToST $ putStrLn "Hello World"

7 changes: 7 additions & 0 deletions deprecate/Foreign2.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Foreign2 where

import Foreign

bad :: IO a -> a
bad = unsafePerformIO

8 changes: 8 additions & 0 deletions deprecate/ForeignForeignPtr.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module ForeignForeignPtr where

import Foreign.Ptr
import Foreign.ForeignPtr

bad :: ForeignPtr a -> Ptr a
bad = unsafeForeignPtrToPtr

12 changes: 12 additions & 0 deletions deprecate/GHCArr.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module GHCArr where

import GHC.Arr

bad1 = unsafeArray

bad2 = fill

bad3 = done

bad4 = unsafeThawSTArray

6 changes: 6 additions & 0 deletions deprecate/GHCConc.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module GHCConc where

import GHC.Conc

bad1 = unsafeIOToSTM

6 changes: 6 additions & 0 deletions deprecate/GHCForeignPtr.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module GHCForeignPtr where

import GHC.ForeignPtr

bad1 = unsafeForeignPtrToPtr

8 changes: 8 additions & 0 deletions deprecate/GHCIOArray.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module GHCIOArray where

import GHC.IOArray

bad1 = unsafeReadIOArray

bad2 = unsafeWriteIOArray

6 changes: 6 additions & 0 deletions deprecate/GHCPtr.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module GHCPtr where

import GHC.Ptr

bad1 = castFunPtrToPtr

8 changes: 8 additions & 0 deletions deprecate/GHCST.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module GHCST where

import GHC.ST

bad1 = liftST

bad2 = unsafeInterleaveST

13 changes: 13 additions & 0 deletions pkgs/ImpSafe.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{-# LANGUAGE Safe #-}
{-# LANGUAGE NoImplicitPrelude #-}
module ImpSafe ( MyWord ) where

-- Data.Word is safe so shouldn't requrie base be trusted.
-- (No wrong as while Data.Word is safe it imports trustworthy
-- modules in base, hence base needs to be trusted).
-- Note: Worthwhile giving out better error messages for cases
-- like this if I can.
import Data.Word

type MyWord = Word

8 changes: 8 additions & 0 deletions pkgs/ImpSafeOnly.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{-# LANGUAGE Safe #-}
module Main where

import M_Terei

main = do
putStrLn $ show bigInt

8 changes: 8 additions & 0 deletions pkgs/ImpSafeOnly2.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{-# LANGUAGE Safe #-}
module Main where

import M_Terei2

main = do
putStrLn $ show bigInt

8 changes: 8 additions & 0 deletions pkgs/ImpSafeOnly3.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{-# LANGUAGE Safe #-}
module Main where

import M_Terei3

main = do
putStrLn $ show bigInt

8 changes: 8 additions & 0 deletions pkgs/ImpTrustworthy.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{-# LANGUAGE Safe #-}
module ImpTrustworthy ( MyInt ) where

-- Data.Int is trustworthy so we need to trust base
import Data.Int

type MyInt = Int

6 changes: 6 additions & 0 deletions pkgs/p/M_Terei.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{-# LANGUAGE Safe #-}
module M_Terei where

bigInt :: Int
bigInt = 9

6 changes: 6 additions & 0 deletions pkgs/p/M_Terei2.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{-# LANGUAGE Trustworthy #-}
module M_Terei2 where

bigInt :: Int
bigInt = 9

8 changes: 8 additions & 0 deletions pkgs/p/M_Terei3.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{-# LANGUAGE Safe #-}
module M_Terei3 where

import qualified M_Terei2 as M2

bigInt :: Int
bigInt = M2.bigInt

11 changes: 11 additions & 0 deletions pkgs/p/M_Terei4.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{-# LANGUAGE Safe #-}
module M_Terei4 where

import qualified M_Terei3 as M3
import Data.Word

bigInt :: Int
bigInt = M3.bigInt

type MyWord = Word

4 changes: 4 additions & 0 deletions pkgs/p/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Distribution.Simple

main = defaultMain

18 changes: 18 additions & 0 deletions pkgs/p/p.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Name: p
Version: 1.0
Description: SafeHaskell Test Package
License: BSD3
Author: David Terei
Maintainer: davidterei@gmail.com
Build-Type: Simple
Cabal-Version: >= 1.2

Library {
Build-Depends: base >= 4
Exposed-Modules:
M_Terei
M_Terei2
M_Terei3
M_Terei4
}

15 changes: 15 additions & 0 deletions transitive/A.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{-# LANGUAGE Trustworthy #-}
module A (
trace
) where

import qualified Debug.Trace as D
import qualified Data.ByteString.Lazy.Char8 as BS

-- | Allowed declasification
trace :: String -> a -> a
trace s = D.trace $ s ++ show a3

a3 :: BS.ByteString
a3 = BS.take 3 $ BS.repeat 'a'

8 changes: 8 additions & 0 deletions transitive/B.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{-# LANGUAGE Safe #-}
module B where

import A

mainM :: Int -> Int
mainM n = trace "Allowed Leak" $ n * 2

9 changes: 9 additions & 0 deletions transitive/C.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{-# LANGUAGE SafeImports #-}
module C ( main' ) where

import safe B

main' = do
let n = mainM 1
print $ n

7 changes: 7 additions & 0 deletions transitive/C_2.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{-# LANGUAGE SafeImports #-}
module Main ( main ) where

import C

main = main'

8 changes: 8 additions & 0 deletions transitive/D.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Main where

import B

main = do
let n = mainM 1
print $ n

39 changes: 39 additions & 0 deletions transitive/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
GHC=/home/davidt/Ghc/safeHaskell2/inplace/bin/ghc-stage2

# should fail as we don't trust the base package when compiling C
# and so B isn't safe anymore since B imports a trustworthy
# module from base.
c:
${GHC} -c -fforce-recomp A.hs
${GHC} -c -fforce-recomp B.hs -trust base
${GHC} -c -fforce-recomp C.hs
echo "Should fail!"

c2:
${GHC} -c -fforce-recomp A.hs
${GHC} -c -fforce-recomp B.hs -trust base
${GHC} -c -fforce-recomp C.hs -trust base
${GHC} -c -fforce-recomp C_2.hs -o C2
echo "Should succeed!"

# should succeed as we do trust the base package when compiling C
# and so B is safe still.
c_trust:
${GHC} -c -fforce-recomp A.hs
${GHC} -c -fforce-recomp B.hs -trust base
${GHC} -c -fforce-recomp C.hs -trust base
echo "Should succeed!"

## should succeed as we aren't importing B as a safe module
d:
${GHC} -c -fforce-recomp A.hs
${GHC} -c -fforce-recomp B.hs -trust base
${GHC} -c -fforce-recomp D.hs -o d
echo "Should succeed!"

clean:
rm *.o
rm *.hi
rm C2
rm D

0 comments on commit 39e06ea

Please sign in to comment.