[GHC] #13457: unsafeInterleaveST is too unsafe

#13457: unsafeInterleaveST is too unsafe -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: 8.4.1 Component: Core | Version: 8.1 Libraries | Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: Incorrect result Unknown/Multiple | at runtime Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- Several years ago, Edward Kmett wrote a [https://www.schoolofhaskell.com/school/to-infinity-and-beyond/older-but- still-interesting/deamortized-st#newsflash--unsafeinterleavest-is-unsafe- blog post] that (among other things) explains that `unsafeInterleaveST` is not thread-safe; actions may be performed multiple times. The fix that would seem to match everything else best would be to rename `unsafeInterleaveST` to `unsafeDupableInterleaveST` and define `unsafeInterleaveST m = unsafeDupableInterleaveST (noDuplicate >> m)` using an `ST`ish version of `noDuplicate`. As Kmett explains, the dupable version is useful for high performance when the suspended action is known to be idempotent. Actually, the action being idempotent is insufficient, but I believe a sufficient condition is that the action does not both read and write any reference or array element. While we're at it, we need to document all the functions in `Control.Monad.ST.Unsafe`. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13457 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13457: unsafeInterleaveST is too unsafe -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: dfeuer Type: bug | Status: new Priority: normal | Milestone: 8.4.1 Component: Core Libraries | Version: 8.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by dfeuer): * owner: (none) => dfeuer -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13457#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13457: unsafeInterleaveST is too unsafe -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: dfeuer Type: bug | Status: new Priority: normal | Milestone: 8.4.1 Component: Core Libraries | Version: 8.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by ekmett): If you don't read or write to the same STRef (or STArray element) like you state (and don't use unsafeIOToST, etc.) then this is a sufficient condition for your operation to be idempotent. =) You can actually strengthen to not reading from a ref that you write to before you write to it. The write washes away your sins by destroying any information leaked. I actually exploit that extra structure in my monadic revisions code. But again, this is just a weak way to state a demand for idempotence. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13457#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13457: unsafeInterleaveST is too unsafe -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: dfeuer Type: bug | Status: new Priority: normal | Milestone: 8.4.1 Component: Core Libraries | Version: 8.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by dfeuer): ekmett, what I was trying to get at is that as well as being run twice by different threads, the action could even be run twice ''at the same time'', interleaving reads and writes in an unspecified order. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13457#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13457: unsafeInterleaveST is too unsafe -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: dfeuer Type: bug | Status: patch Priority: normal | Milestone: 8.4.1 Component: Core Libraries | Version: 8.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D3370 Wiki Page: | -------------------------------------+------------------------------------- Changes (by dfeuer): * status: new => patch * differential: => Phab:D3370 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13457#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13457: unsafeInterleaveST is too unsafe
-------------------------------------+-------------------------------------
Reporter: dfeuer | Owner: dfeuer
Type: bug | Status: patch
Priority: normal | Milestone: 8.4.1
Component: Core Libraries | Version: 8.1
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture:
Type of failure: Incorrect result | Unknown/Multiple
at runtime | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s): Phab:D3370
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by David Feuer

#13457: unsafeInterleaveST is too unsafe -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: dfeuer Type: bug | Status: merge Priority: normal | Milestone: 8.4.1 Component: Core Libraries | Version: 8.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D3370 Wiki Page: | -------------------------------------+------------------------------------- Changes (by dfeuer): * status: patch => merge -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13457#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13457: unsafeInterleaveST is too unsafe -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: dfeuer Type: bug | Status: merge Priority: normal | Milestone: 8.2.1 Component: Core Libraries | Version: 8.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D3370 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * milestone: 8.4.1 => 8.2.1 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13457#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13457: unsafeInterleaveST is too unsafe -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: dfeuer Type: bug | Status: closed Priority: normal | Milestone: 8.2.1 Component: Core Libraries | Version: 8.1 Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D3370 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * status: merge => closed * resolution: => fixed Comment: Merged with 167548f4cd087535a6c4c9fc642b60669b4dc019. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13457#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC