[GHC] #10245: panic in new integer switch logic with "out-of-range" literals

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: Type: bug | Status: new Priority: high | Milestone: Component: Compiler | Version: 7.11 (CodeGen) | Operating System: Unknown/Multiple Keywords: | Type of failure: Compile-time Architecture: x86_64 | crash (amd64) | Blocked By: Test Case: | Related Tickets: Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Compiling this module {{{ module D (f) where f :: Int -> String f n = case n of 0x8000000000000000 -> "yes" _ -> "no" }}} crashes with the error {{{ [1 of 1] Compiling D ( /tmp/D.hs, /tmp/D.o ) ghc-stage1: panic! (the 'impossible' happened) (GHC version 7.11.20150403 for x86_64-unknown-linux): Map.findMin: empty map has no minimal element }}} The constant does not have to be exactly `0x8000000000000000`, everything I tested from there up to `0xffffffffffffffff` yields the same crash. Also occurs with `Word` and negative literals. The bug seems to be tied to the target's word size somehow, though: a 64-bit compiler does not panic on `Int32` and `0x80000000`, but a 32-bit compiler does. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: nomeata Type: bug | Status: new Priority: high | Milestone: Component: Compiler | Version: 7.11 (CodeGen) | Keywords: Resolution: | Architecture: x86_64 Operating System: Unknown/Multiple | (amd64) Type of failure: Compile-time | Test Case: crash | Blocking: Blocked By: | Differential Revisions: Related Tickets: | -------------------------------------+------------------------------------- Changes (by nomeata): * owner: => nomeata Comment: Thanks, I’ll look into it. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: nomeata Type: bug | Status: new Priority: high | Milestone: Component: Compiler | Version: 7.11 (CodeGen) | Keywords: Resolution: | Architecture: x86_64 Operating System: Unknown/Multiple | (amd64) Type of failure: Compile-time | Test Case: crash | Blocking: Blocked By: | Differential Revisions: Related Tickets: | -------------------------------------+------------------------------------- Comment (by nomeata): Interesting, as a test case it fails with {{{ ghc-stage2: panic! (the 'impossible' happened) (GHC version 7.11.20150403 for x86_64-unknown-linux): ASSERT failed! file compiler/basicTypes/Literal.hs line 220 9223372036854775808 }}} due to {{{ mkMachInt :: DynFlags -> Integer -> Literal mkMachInt dflags x = ASSERT2( inIntRange dflags x, integer x ) MachInt x }}} This makes we wonder: Is that even legal Haskell, or should we fail much earlier? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: nomeata Type: bug | Status: closed Priority: high | Milestone: Component: Compiler | Version: 7.11 (CodeGen) | Keywords: Resolution: fixed | Architecture: x86_64 Operating System: Unknown/Multiple | (amd64) Type of failure: Compile-time | Test Case: crash | Blocking: Blocked By: | Differential Revisions: Related Tickets: | -------------------------------------+------------------------------------- Changes (by nomeata): * status: new => closed * resolution: => fixed Comment: Fixed. One could think about whether that is legal haskell or whether the ASSERT is wrong, but that’s a different story, and not one for Easter night. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10245: panic in new integer switch logic with "out-of-range" literals
-------------------------------------+-------------------------------------
Reporter: rwbarton | Owner: nomeata
Type: bug | Status: closed
Priority: high | Milestone:
Component: Compiler | Version: 7.11
(CodeGen) | Keywords:
Resolution: fixed | Architecture: x86_64
Operating System: Unknown/Multiple | (amd64)
Type of failure: Compile-time | Test Case:
crash | Blocking:
Blocked By: | Differential Revisions:
Related Tickets: |
-------------------------------------+-------------------------------------
Comment (by Joachim Breitner

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: nomeata Type: bug | Status: closed Priority: high | Milestone: Component: Compiler | Version: 7.11 (CodeGen) | Keywords: Resolution: fixed | Architecture: x86_64 Operating System: Unknown/Multiple | (amd64) Type of failure: Compile-time | Test Case: crash | Blocking: Blocked By: | Differential Revisions: Related Tickets: | -------------------------------------+------------------------------------- Comment (by rwbarton): FWIW I encountered this in the wild (https://github.com/ghc/packages- time/blob/master/lib/Data/Time/LocalTime/TimeZone.hs#L88) while trying to cross-compile GHC to x86. I'm pretty sure it is valid Haskell; what if the type signature was `f :: Num a => a -> String`? Should specializing `a` to `Int` turn a valid program into an invalid one? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: nomeata Type: bug | Status: closed Priority: high | Milestone: Component: Compiler | Version: 7.11 (CodeGen) | Keywords: Resolution: fixed | Architecture: x86_64 Operating System: Unknown/Multiple | (amd64) Type of failure: Compile-time | Test Case: crash | Blocking: Blocked By: | Differential Revisions: Related Tickets: | -------------------------------------+------------------------------------- Comment (by rwbarton): Curiously for a 32-bit compiler I can reproduce the issue with `Int` but not with `Int32`. Maybe the ASSERT is really checking an intended invariant, and something went wrong earlier (desugaring?) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: nomeata Type: bug | Status: closed Priority: high | Milestone: Component: Compiler | Version: 7.11 (CodeGen) | Keywords: Resolution: fixed | Architecture: x86_64 Operating System: Unknown/Multiple | (amd64) Type of failure: Compile-time | Test Case: crash | Blocking: Blocked By: | Differential Revisions: Related Tickets: | -------------------------------------+------------------------------------- Comment (by nomeata):
I'm pretty sure it is valid Haskell;
for some reason I was assuming you were writing unboxed literals. With regular literals, you are right of course! But I think we are implementing it wrongly. According to the report, your code should be treated like {{{ f n = if n == fromIntegral 0x8000000000000000 then "yes" else "no" }}} but my impression is that we produce a literal 0x8000000000000000 :: Int# internally, which is then dropped from the case. I’ll check. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: Type: bug | Status: new Priority: high | Milestone: Component: Compiler | Version: 7.11 (CodeGen) | Keywords: Resolution: | Architecture: x86_64 Operating System: Unknown/Multiple | (amd64) Type of failure: Compile-time | Test Case: crash | Blocking: Blocked By: | Differential Revisions: Related Tickets: | -------------------------------------+------------------------------------- Changes (by nomeata): * owner: nomeata => * status: closed => new * resolution: fixed => Comment: Ok, I think I broke something else since 7.8. If I add {{{ main = do let string = "0x8000000000000000" let i = read string :: Integer let i' = fromIntegral i :: Int print i print i' print (f i') }}} to your example, 7.8.4 produces {{{ 9223372036854775808 -9223372036854775808 "yes" }}} while HEAD produces {{{ 9223372036854775808 -9223372036854775808 "no" }}} I’ll investigate. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10245: panic in new integer switch logic with "out-of-range" literals
-------------------------------------+-------------------------------------
Reporter: rwbarton | Owner:
Type: bug | Status: new
Priority: high | Milestone:
Component: Compiler | Version: 7.11
(CodeGen) | Keywords:
Resolution: | Architecture: x86_64
Operating System: Unknown/Multiple | (amd64)
Type of failure: Compile-time | Test Case:
crash | Blocking:
Blocked By: | Differential Revisions:
Related Tickets: |
-------------------------------------+-------------------------------------
Comment (by Joachim Breitner

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: Type: bug | Status: new Priority: high | Milestone: Component: Compiler | Version: 7.11 (CodeGen) | Keywords: Resolution: | Architecture: x86_64 Operating System: Unknown/Multiple | (amd64) Type of failure: Compile-time | Test Case: crash | Blocking: Blocked By: | Differential Revisions: Related Tickets: | -------------------------------------+------------------------------------- Comment (by nomeata): There is something larger at miss. I created #10246 for that, and left this for the (now fixed) partiality of the new switch logic. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245#comment:10 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: Type: bug | Status: new Priority: high | Milestone: Component: Compiler | Version: 7.11 (CodeGen) | Keywords: Resolution: | Architecture: x86_64 Operating System: Unknown/Multiple | (amd64) Type of failure: Compile-time | Test Case: crash | Blocking: Blocked By: | Differential Revisions: Related Tickets: | -------------------------------------+------------------------------------- Comment (by rwbarton): Good catch on the relation to #9533, I had totally forgotten about that ticket! -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245#comment:11 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10245: panic in new integer switch logic with "out-of-range" literals
-------------------------------------+-------------------------------------
Reporter: rwbarton | Owner:
Type: bug | Status: new
Priority: high | Milestone:
Component: Compiler | Version: 7.11
(CodeGen) |
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture: x86_64
Type of failure: Compile-time | (amd64)
crash | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Revisions:
-------------------------------------+-------------------------------------
Comment (by Thomas Miedema

#10245: panic in new integer switch logic with "out-of-range" literals
-------------------------------------+-------------------------------------
Reporter: rwbarton | Owner: (none)
Type: bug | Status: new
Priority: high | Milestone:
Component: Compiler | Version: 7.11
(CodeGen) |
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture: x86_64
Type of failure: Compile-time | (amd64)
crash | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s):
-------------------------------------+-------------------------------------
Comment (by Ben Gamari

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: (none) Type: bug | Status: closed Priority: high | Milestone: 8.2.1 Component: Compiler | Version: 7.11 (CodeGen) | Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: x86_64 Type of failure: Compile-time | (amd64) crash | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * status: new => closed * resolution: => fixed * milestone: => 8.2.1 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245#comment:14 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: (none) Type: bug | Status: new Priority: high | Milestone: 8.2.1 Component: Compiler | Version: 7.11 (CodeGen) | Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: x86_64 Type of failure: Incorrect result | (amd64) at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by AndreasK): * cc: AndreasK (added) * status: closed => new * resolution: fixed => * failure: Compile-time crash => Incorrect result at runtime Comment: In MatchLit [https://ghc.haskell.org/trac/ghc/browser/ghc/compiler/deSugar/MatchLit.hs#L7... dsLit] doesn't use the `mkMachInt` functions resulting in potentially faulty code. `y = I# 0x8000000000000000# :: Int` compiles without warning with -Wall for example. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245#comment:15 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: AndreasK Type: bug | Status: new Priority: normal | Milestone: 8.2.1 Component: Compiler | Version: 7.11 (CodeGen) | Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: x86_64 Type of failure: Incorrect result | (amd64) at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D4218 Wiki Page: | -------------------------------------+------------------------------------- Changes (by AndreasK): * owner: (none) => AndreasK * priority: high => normal * differential: => Phab:D4218 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245#comment:16 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: AndreasK Type: bug | Status: patch Priority: normal | Milestone: 8.2.1 Component: Compiler | Version: 7.11 (CodeGen) | Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: x86_64 Type of failure: Incorrect result | (amd64) at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D4218 Wiki Page: | -------------------------------------+------------------------------------- Changes (by AndreasK): * status: new => patch Comment: I have a patch up on Phab but it increases allocations. (Phab:D4218) This seems to be caused by desugaring of derived constants. While the constants should be safe I don't see a way to differentiate them from user written constants. So I would appreciate input on the issue. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245#comment:17 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10245: panic in new integer switch logic with "out-of-range" literals -------------------------------------+------------------------------------- Reporter: rwbarton | Owner: AndreasK Type: bug | Status: closed Priority: normal | Milestone: 8.2.1 Component: Compiler | Version: 7.11 (CodeGen) | Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: x86_64 Type of failure: Incorrect result | (amd64) at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D4218 Wiki Page: | -------------------------------------+------------------------------------- Changes (by AndreasK): * status: patch => closed * resolution: => fixed Comment: Replying to [comment:15 AndreasK]:
In MatchLit [https://ghc.haskell.org/trac/ghc/browser/ghc/compiler/deSugar/MatchLit.hs#L7... dsLit] doesn't use the `mkMachInt` functions resulting in potentially faulty code.
`y = I# 0x8000000000000000# :: Int` compiles without warning with -Wall for example.
Fixing this would incur a overhead when compiling Code which generates Prim Literals. As these use the same code Path as user specified Literals we can't skip the checks on these even when we know these will not be out of bounds. Closing this after a discussion on IRC and it's not deemed a big enough issue to warrant the overhead. (wontfix as to not confuse this issue with the original one) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10245#comment:18 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC