From 1503da32d26fb59fb6ebb620bfd0f8c08638f627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Fri, 24 May 2019 11:23:01 +0300 Subject: Fix rewriting invalid shifts to errors Fixes #16449. 5341edf3 removed a code in rewrite rules for bit shifts, which broke the "silly shift guard", causing generating invalid bit shifts or heap overflow in compile time while trying to evaluate those invalid bit shifts. The "guard" is explained in Note [Guarding against silly shifts] in PrelRules.hs. More specifically, this was the breaking change: --- a/compiler/prelude/PrelRules.hs +++ b/compiler/prelude/PrelRules.hs @@ -474,12 +474,11 @@ shiftRule shift_op ; case e1 of _ | shift_len == 0 -> return e1 - | shift_len < 0 || wordSizeInBits dflags < shift_len - -> return (mkRuntimeErrorApp rUNTIME_ERROR_ID wordPrimTy - ("Bad shift length" ++ show shift_len)) This patch reverts this change. Two new tests added: - T16449_1: The original reproducer in #16449. This was previously casing a heap overflow in compile time when CmmOpt tries to evaluate the large (invalid) bit shift in compile time, using `Integer` as the result type. Now it builds as expected. We now generate an error for the shift as expected. - T16449_2: Tests code generator for large (invalid) bit shifts. --- compiler/prelude/PrelRules.hs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'compiler/prelude/PrelRules.hs') diff --git a/compiler/prelude/PrelRules.hs b/compiler/prelude/PrelRules.hs index d9a1f8a023..bd18bfec63 100644 --- a/compiler/prelude/PrelRules.hs +++ b/compiler/prelude/PrelRules.hs @@ -467,13 +467,16 @@ shiftRule :: (DynFlags -> Integer -> Int -> Integer) -> RuleM CoreExpr -- Used for shift primops -- ISllOp, ISraOp, ISrlOp :: Word# -> Int# -> Word# -- SllOp, SrlOp :: Word# -> Int# -> Word# --- See Note [Guarding against silly shifts] shiftRule shift_op = do { dflags <- getDynFlags ; [e1, Lit (LitNumber LitNumInt shift_len _)] <- getArgs ; case e1 of _ | shift_len == 0 -> return e1 + -- See Note [Guarding against silly shifts] + | shift_len < 0 || shift_len > wordSizeInBits dflags + -> return $ mkRuntimeErrorApp rUNTIME_ERROR_ID wordPrimTy + ("Bad shift length " ++ show shift_len) -- Do the shift at type Integer, but shift length is Int Lit (LitNumber nt x t) -- cgit v1.2.1