Opened 10 years ago

Closed 10 years ago

Last modified 9 years ago

#2154 closed merge (fixed)

Stack overflow due to unsafePerformIO

Reported by: dons Owned by: igloo
Priority: normal Milestone: 6.8.3
Component: Runtime System Version: 6.8.2
Keywords: Cc: dons@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


The following program, extracted from a utf8 decoder for ByteStrings, overflows the stack when run with rules on. Speaking to SimonM, this is due to the stack squeezing optimisation not working for unsafePerformIO, meaning the resulting loop actually uses up stack.

import qualified Data.ByteString.Char8 as S 

main = putStrLn $ filter (\x -> enc x /= [x]) list
  where list = ['\0'..'\x10ffff'] ++ ['\0'..'\10']

{-# NOINLINE enc #-}
enc :: Char -> String
enc x = S.singleton x `seq` [x]

After fusing and optimising, S.singleton here is allocated using unsafePerformIO to wrap the allocation, and seems to run out of stack.

However, I've confirmed this code does work if its changed to use unsafeDupablePerformIO.

I'm filing this ticket to get documentation on what is going on here, so we can have something to point to if it comes up again, and to clarify that unsafeDupablePerformIO is the correct fix.

SimonM, could you summarise what the issue is?

Change History (4)

comment:1 Changed 10 years ago by simonmar

difficulty: Unknown
Milestone: 6.8.3
Owner: set to igloo
Type: bugmerge

Fixed in this patch I committed yesterday:

Thu Feb  7 04:24:45 PST 2008  Simon Marlow <>
  * Tweaks to stack squeezing
  1. We weren't squeezing two frames if one of them was a marked update
     frame.  This is easy to fix.
  2. The heuristic to decide whether to squeeze was a little
     conservative.  It's worth copying 3 words to save an update frame.

comment:2 Changed 10 years ago by igloo

Resolution: fixed
Status: newclosed


comment:3 Changed 9 years ago by simonmar

Architecture: UnknownUnknown/Multiple

comment:4 Changed 9 years ago by simonmar

Operating System: UnknownUnknown/Multiple
Note: See TracTickets for help on using tickets.