-
Notifications
You must be signed in to change notification settings - Fork 5
/
Async.hs
359 lines (277 loc) · 11 KB
/
Async.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
{-# LANGUAGE ImplicitParams, ScopedTypeVariables, Rank2Types,
ConstraintKinds
#-}
-- Asynchronous clocks
module Async where
import ProcessIO
import StaticCorruptions
import Multisession
import Safe
import Control.Concurrent.MonadIO
import Control.Monad (forever)
import Data.IORef.MonadIO
import Data.Map.Strict (member, empty, insert, Map)
import qualified Data.Map.Strict as Map
{-- Program abstractions for asynchronous functionalities --}
{--
"Asynchronous" network models allow the adversary to have full control
over the message delivery order.
This is captured by extending the Functionality syntax with
"eventually" blocks. The expression `eventually m` registers
the action `m` to be delivered later.
m is a monad action, `eventually` returns nothing. The type is
eventually :: MonadFunctionality m => m a -> m ()`
--}
type MonadFunctionalityAsync m l = (MonadFunctionality m,
?eventually :: m () -> m (),
?leak :: l -> m ())
eventually :: MonadFunctionalityAsync m l => m () -> m ()
eventually = ?eventually
leak :: MonadFunctionalityAsync m l => l -> m ()
leak = ?leak
{-- The Asynchronous functionality wrapper --}
{--
Lets other functionalities schedule events to be delivered in the future, in any order chosen by the adversary.
Environment can "force" progress to move along, thus the adversary is thus unable to stall forever.
--}
type CallbackID = Int
type Round = Int
{-- Types of messages exchanged with the clock --}
data ClockA2F = ClockA2F_GetCount | ClockA2F_Deliver Int | ClockA2F_GetLeaks deriving Show
data ClockF2A l = ClockF2A_Pass | ClockF2A_Count Int | ClockF2A_Leaks [l] deriving (Show, Eq)
data ClockZ2F = ClockZ2F_MakeProgress deriving Show
data ClockP2F a = ClockP2F_Pass | ClockP2F_Through a deriving Show
{-- Implementation of MonadAsync --}
deleteNth i xs = l ++ r where (l,(_:r)) = splitAt i xs
runAsyncF :: MonadFunctionality m =>
(MonadFunctionalityAsync m l => Functionality p2f f2p a2f f2a Void Void m)
-> Functionality (ClockP2F p2f) f2p (Either ClockA2F a2f) (Either (ClockF2A l) f2a) ClockZ2F Void m
runAsyncF f (p2f, f2p) (a2f, f2a) (z2f, f2z) = do
-- Store state for the leakage buffer
leaks <- newIORef []
-- how to handle "leak"
let _leak l = do
modifyIORef leaks (++ [(l)])
return ()
-- Store state for the clock
runqueue <- newIORef []
a2f' <- newChan
f2a' <- wrapWrite Right f2a
z2f' <- newChan
-- Allow protocols to pass
p2f' <- newChan
fork $ forever $ do
mf <- readChan p2f
case mf of
(_, ClockP2F_Pass) -> writeChan f2a $ Left ClockF2A_Pass
(pid, ClockP2F_Through m) -> writeChan p2f' (pid, m)
-- Adversary can query the current state, and deliver messages early
fork $ forever $ do
mf <- readChan a2f
case mf of
Left ClockA2F_GetCount -> do
r <- readIORef runqueue
writeChan f2a $ (Left $ ClockF2A_Count (length r))
Left ClockA2F_GetLeaks -> do
l <- readIORef leaks
writeChan f2a $ (Left $ ClockF2A_Leaks l)
Left (ClockA2F_Deliver idx) -> do
q <- readIORef runqueue
modifyIORef runqueue (deleteNth idx)
writeChan (q !! idx) ()
Right msg -> writeChan a2f' msg
-- how to handle "eventually"
let _eventually m = do
c :: Chan () <- newChan
modifyIORef runqueue (++ [c])
fork $ readChan c >> m
return ()
-- TODO: add the "delay" option to the environment
-- Allow the environment to force progress along
fork $ forever $ do
ClockZ2F_MakeProgress <- readChan z2f
liftIO $ putStrLn $ "[fAsync] MakeProgress"
rq <- readIORef runqueue
if length rq > 0 then do
-- Deliver the first message, remove it from buffer
modifyIORef runqueue (deleteNth 0)
liftIO $ putStrLn $ "[fAsync] sending callback"
writeChan (rq !! 0) ()
else error "underflow"
let ?eventually = _eventually; ?leak = _leak in
f (p2f', f2p) (a2f', f2a') (z2f', f2z)
return ()
{-
-- Example: fAuth
fAuth is an asynchronous, one-shot, authenticated communication channel.
The PIDs of the Sender and Receiver are encoded in the SID
The sender can set the message (once).
After one (asynchronous) round, the receiver receives the message
-}
data FAuthF2P a = FAuthF2P_OK | FAuthF2P_Deliver a deriving (Eq, Read, Show)
fAuth :: MonadFunctionalityAsync m msg => Functionality msg (FAuthF2P msg) Void Void Void Void m
fAuth (p2f, f2p) (a2f, f2a) (z2f, f2z) = do
-- Parse SID as sender, recipient, ssid
-- liftIO $ putStrLn $ "fauth sid: " ++ show ?sid
let (pidS :: PID, pidR :: PID, ssid :: String) = readNote "fAuth" $ snd ?sid
-- Store an optional message
recorded <- newIORef False
fork $ forever $ do
(pid, m) <- readChan p2f
liftIO $ putStrLn $ "[fAuth sid]: " ++ show ?sid
liftIO $ putStrLn $ "[fAuth] message received: " ++ show (pidS, pidR, ssid)
-- Sender can only send message once
_ <- readIORef recorded
-- if _ == False then error "recorded problem" else do
if not (pid == pidS) then error "Invalid sender to fAuth"
else do
writeIORef recorded True
leak m
eventually $ writeChan f2p (pidR, FAuthF2P_Deliver m)
writeChan f2p (pidS, FAuthF2P_OK)
return ()
{-- Example environment using fAuth --}
testEnvAuthAsync z2exec (p2z, z2p) (a2z, z2a) (f2z, z2f) pump outp = do
let sid = ("sidTestAuthAsync", show ("Alice", "Bob", ""))
writeChan z2exec $ SttCrupt_SidCrupt sid empty
fork $ forever $ do
x <- readChan p2z
liftIO $ putStrLn $ "Z: p sent " ++ show x
?pass
fork $ forever $ do
m <- readChan a2z
liftIO $ putStrLn $ "Z: a sent " ++ show m
?pass
fork $ forever $ do
f <- readChan f2z
liftIO $ putStrLn $ "Z: f sent " ++ show f
?pass
-- Have Alice write a message
() <- readChan pump
writeChan z2p ("Alice", ClockP2F_Through ("hi Bob"))
-- Let the adversary see
() <- readChan pump
writeChan z2a $ SttCruptZ2A_A2F $ Left ClockA2F_GetLeaks
-- Let the adversary deliver it
() <- readChan pump
writeChan z2a $ SttCruptZ2A_A2F $ Left (ClockA2F_Deliver 0)
-- [TODO] Let the environment force deliver
-- () <- readChan pump
-- writeChan z2a $ SttCruptZ2A_A2F $ DuplexA2F_Right $ DuplexA2F_Left $ LeakA2F_Get
() <- readChan pump
writeChan outp "environment output: 1"
testAuthAsync :: IO String
testAuthAsync = runITMinIO 120 $ execUC testEnvAuthAsync (idealProtocol) (runAsyncF fAuth) dummyAdversary
{----------------------------------}
{- runAsyncF !F vs !(runAsyncF F) -}
{----------------------------------}
{-
There are two approaches to compose async and bang. Regardless of which we choose,
we have to handle updating the session ID, since "leak" doesn't normally include it.
(Option 1): bangF (runAsyncF F)
this isn't generally what we want, since we think there being a single
queue of scheduled tasks and a single queue of leaks
(Option 2): runAsyncF (bangF)
However, the generalized version of theories like JUC don't automatically follow,
so the following is new and must be proven.
async(F) --p-> async(!F)
async(F) --q-> async(G)
--------------------------------
async(F) --!q.p-> async(!G)
-}
_bangFAsyncInstance :: MonadFunctionality m => Chan (SID, l) -> Chan (Chan (), Chan ()) -> (forall m. MonadFunctionalityAsync m l => Functionality p2f f2p a2f f2a Void Void m) -> Functionality p2f f2p a2f f2a Void Void m
_bangFAsyncInstance _leak _eventually f = f
where
?leak = \l -> writeChan _leak (?sid, l)
?eventually = \m -> do
cb :: Chan () <- newChan
ok :: Chan () <- newChan
writeChan _eventually (cb, ok)
fork $ readChan cb >> m
readChan ok
bangFAsync
:: MonadFunctionalityAsync m (SID, l) =>
(forall m'. MonadFunctionalityAsync m' l => Functionality p2f f2p a2f f2a Void Void m') ->
Functionality (SID, p2f) (SID, f2p) (SID, a2f) (SID, f2a) Void Void m
bangFAsync f (p2f, f2p) (a2f, f2a) (z2f, f2z) = do
_leak <- newChan
_eventually <- newChan
fork $ forever $ do
(cb,ok) <- readChan _eventually
?eventually $ do
writeChan cb ()
writeChan ok ()
fork $ forever $ do
l <- readChan _leak
leak l
bangF (_bangFAsyncInstance _leak _eventually f) (p2f, f2p) (a2f, f2a) (z2f, f2z)
{-- Example environments using !fAuth --}
--testEnvAsyncBang ::
testEnvAsyncBang z2exec (p2z, z2p) (a2z, z2a) (f2z, z2f) pump outp = do
let sid = ("sidTestAuthAsync", "")
writeChan z2exec $ SttCrupt_SidCrupt sid empty
fork $ forever $ do
(pid, m) <- readChan p2z
liftIO $ putStrLn $ "Z: Party[" ++ pid ++ "] output " ++ show m
?pass
fork $ forever $ do
m <- readChan a2z
liftIO $ putStrLn $ "Z: a sent " ++ show m
?pass
-- Have Alice write a message
() <- readChan pump
let ssid1 = ("ssidX", show ("Alice","Bob",""))
writeChan z2p ("Alice", ClockP2F_Through (ssid1, "hi Bob"))
-- Let the adversary read the buffer
() <- readChan pump
writeChan z2a $ SttCruptZ2A_A2F $ Left ClockA2F_GetLeaks
-- Let the adversary deliver it
() <- readChan pump
writeChan z2a $ SttCruptZ2A_A2F $ Left (ClockA2F_Deliver 0)
() <- readChan pump
writeChan outp "environment output: 1"
testAsyncBang :: IO String
testAsyncBang = runITMinIO 120 $ execUC testEnvAsyncBang (idealProtocol) (runAsyncF $ bangFAsync fAuth) dummyAdversary
{-- Counter example of why !Async(F) does not work, even though Async(!F) is OK --}
{--
testEnvBangAsync z2exec (p2z, z2p) (a2z, z2a) (f2z, z2f) pump outp = do
let sid = ("sidTestAuthAsync", "")
writeChan z2exec $ SttCrupt_SidCrupt sid empty
fork $ forever $ do
(pid, m) <- readChan p2z
liftIO $ putStrLn $ "Z: Party[" ++ pid ++ "] output " ++ show m
?pass
fork $ forever $ do
m <- readChan a2z
liftIO $ putStrLn $ "Z: a sent " ++ show m
?pass
fork $ forever $ do
(ssid, f) <- readChan f2z
liftIO $ putStrLn $ "Z: f sent " -- ++ show f
?pass
-- Have Alice write a message
() <- readChan pump
let ssid1 = ("ssidX", show ("Alice","Bob",""))
writeChan z2p ("Alice", (ssid1, "hi Bob"))
-- Let the adversary read the buffer
() <- readChan pump
writeChan z2a $ SttCruptZ2A_A2F $ (ssid1, Left ClockA2F_GetLeaks)
-- Let the adversary deliver it
() <- readChan pump
writeChan z2a $ SttCruptZ2A_A2F $ (ssid1, Left (ClockA2F_Deliver 0))
() <- readChan pump
writeChan outp "environment output: 1"
testBangAsync :: IO String
testBangAsync = runITMinIO 120 $ execUC testEnvBangAsync (idealProtocol) (bangF $ runAsyncF fAuth) dummyAdversary
--}
type MonadAsyncP m = (MonadProtocol m,
?pass :: m ())
runAsyncP :: MonadProtocol m =>
(MonadAsyncP m => Protocol z2p p2z f2p p2f m) ->
Protocol z2p p2z f2p (ClockP2F p2f) m
runAsyncP prot (z2p, p2z) (f2p, p2f) = do
let pass = do
writeChan p2f ClockP2F_Pass
p2f' <- wrapWrite ClockP2F_Through p2f
let ?pass = pass in
prot (z2p, p2z) (f2p,p2f')