8.2.1 regression: heap corruption after safe foreign calls
Test case: (compile with ghc 8.2.1 and -threaded option)
```haskell
module Main where
import Control.Concurrent
import Control.Monad
import Data.Word
import Foreign.Marshal.Alloc
import Foreign.Ptr
import Foreign.Storable
foreign import ccall safe "test"
c_test :: Ptr Word32 -> IO ()
main :: IO ()
main = do
replicateM_ 1000 $ threadDelay 1000
_ <- forkIO $ forever $ threadDelay 100
allocaBytes 4 $ \p -> forever $ do
c_test p
x <- peek p
unless (x == 0xDEADBEEF) $ putStrLn "value mismatch"
```
```c
void test(unsigned int *buf) {
*buf = 0xDEADBEEF;
}
```
On my machine, it detects a few value mismatches before crashing with sigsegv.
```
$ time ./.stack-work/install/x86_64-linux-nopie/nightly-2017-10-10/8.2.1/bin/bug
value mismatch
value mismatch
value mismatch
value mismatch
zsh: segmentation fault (core dumped) ./.stack-work/install/x86_64-linux-nopie/nightly-2017-10-10/8.2.1/bin/bug
./.stack-work/install/x86_64-linux-nopie/nightly-2017-10-10/8.2.1/bin/bug 2.11s user 0.25s system 66% cpu 3.543 total
```
I believe this is what is causing crashes in xmobar. See discussion: https://github.com/jaor/xmobar/issues/310. Note that the crash in xmobar still happens without -threaded option, while this example only breaks when compiled with -threaded.
<details><summary>Trac metadata</summary>
| Trac field | Value |
| ---------------------- | -------------- |
| Version | 8.2.1 |
| Type | Bug |
| TypeOfFailure | OtherFailure |
| Priority | highest |
| Resolution | Unresolved |
| Component | Runtime System |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | |
| Operating system | |
| Architecture | |
</details>
<!-- {"blocked_by":[],"summary":"8.2.1 regression: heap corruption after safe foreign calls","status":"New","operating_system":"","component":"Runtime System","related":[],"milestone":"","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"8.2.1","keywords":[],"differentials":[],"test_case":"","architecture":"","cc":[""],"type":"Bug","description":"Test case: (compile with ghc 8.2.1 and -threaded option)\r\n{{{#!haskell\r\nmodule Main where\r\n\r\nimport Control.Concurrent\r\nimport Control.Monad\r\nimport Data.Word\r\nimport Foreign.Marshal.Alloc\r\nimport Foreign.Ptr\r\nimport Foreign.Storable\r\n\r\nforeign import ccall safe \"test\"\r\n c_test :: Ptr Word32 -> IO ()\r\n\r\nmain :: IO ()\r\nmain = do\r\n replicateM_ 1000 $ threadDelay 1000\r\n _ <- forkIO $ forever $ threadDelay 100\r\n allocaBytes 4 $ \\p -> forever $ do\r\n c_test p\r\n x <- peek p\r\n unless (x == 0xDEADBEEF) $ putStrLn \"value mismatch\"\r\n}}}\r\n{{{#!c\r\nvoid test(unsigned int *buf) {\r\n *buf = 0xDEADBEEF;\r\n}\r\n}}}\r\n\r\nOn my machine, it detects a few value mismatches before crashing with sigsegv.\r\n{{{\r\n$ time ./.stack-work/install/x86_64-linux-nopie/nightly-2017-10-10/8.2.1/bin/bug\r\nvalue mismatch\r\nvalue mismatch\r\nvalue mismatch\r\nvalue mismatch\r\nzsh: segmentation fault (core dumped) ./.stack-work/install/x86_64-linux-nopie/nightly-2017-10-10/8.2.1/bin/bug\r\n./.stack-work/install/x86_64-linux-nopie/nightly-2017-10-10/8.2.1/bin/bug 2.11s user 0.25s system 66% cpu 3.543 total\r\n}}}\r\n\r\nI believe this is what is causing crashes in xmobar. See discussion: https://github.com/jaor/xmobar/issues/310. Note that the crash in xmobar still happens without -threaded option, while this example only breaks when compiled with -threaded.","type_of_failure":"OtherFailure","blocking":[]} -->
issue