[GHC] #12433: GHCi produces incorrect results when evaluating with compiled code

GHC ghc-devs at haskell.org
Mon Jul 25 21:57:06 UTC 2016


#12433: GHCi produces incorrect results when evaluating with compiled code
-------------------------------------+-------------------------------------
           Reporter:  diatchki       |             Owner:
               Type:  bug            |            Status:  new
           Priority:  normal         |         Milestone:
          Component:  Compiler       |           Version:  8.0.1
           Keywords:  ghci, dynamic  |  Operating System:  Linux
  linking, compiled code             |
       Architecture:  x86_64         |   Type of failure:  Incorrect result
  (amd64)                            |  at runtime
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 When GHCi evaluates in non-interpreted mode, it sometimes produces
 incorrect results.  The following example---extracted from a much larger
 program---illustrates the problem:
 {{{#!hs
 import Lex

 num  = 100000
 txt1 = "aaa,aaa"

 main :: IO ()
 main = print $ sum $ map (length . haskellLex) $ replicate num txt1
 }}}

 This program lexes the same string 100000 times, and prints the total
 number of tokens.  Since we are lexing the same string, we'd expect to
 always get the same result, but due to this bug, this is not the case!  We
 repeat the process 100000 times so that we can encounter the bug somewhat
 reliably---the problem does not occur every time.

 To reproduce the problem, we need to load `Lex` in compiled form, not
 interpreted and with some optimizations:
 {{{
 ghc -c -O1 -dynamic Lex.hs
 }}}

 The source code for the "lexer" is as follows:
 {{{#!haskell
 module Lex (haskellLex) where

 cclass :: Char -> Int
 cclass c =
   case c of
     'a' -> 10
     ',' -> 11

     'A' -> 0
     'B' -> 0
     'C' -> 0
     'D' -> 0
     'E' -> 0
     'F' -> 0

 haskellLex :: String -> [()]
 haskellLex [] = []
 haskellLex (i:is) =
   case cclass i of
     10 -> haskellLex62 is
     11 -> () : haskellLex is

 haskellLex62 :: String -> [()]
 haskellLex62 [] = [()]
 haskellLex62 (i:is) =
   case cclass i of
     0   -> [()]
     1   -> [()]
     2   -> [()]
     3   -> [()]
     4   -> [()]

     10  -> haskellLex62 is
     11  -> () : haskellLex (i:is)

     x   -> error ("[GHC BUG] cclass returned: " ++ show (i,x)
 }}}

 This is a minimized version from a full lexer.  As odd as it looks,
 removing pretty much anything seems to cause the bug to go away.  This is
 what happens when we run the program:
 {{{
 runhaskell test.hs
 test.hs: [GHC BUG] cclass returned: (',',-556)
 CallStack (from HasCallStack):
   error, called at Lex.hs:36:12 in main:Lex
 }}}

 The problem is that after many evaluations, the function `cclass` returned
 `-556` for character `,`, when it should have returned `11`.

 I've been able to reproduce this on two Linux machines, both running
 64-bit Ubuntu (16.04).   The issue does not seem to happen on Mac OS.

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12433>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list