[Git][ghc/ghc][master] 2 commits: rts: Ensure that Array# card arrays are initialized

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Mon Aug 8 23:40:12 UTC 2022



Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
50912d68 by Ben Gamari at 2022-08-08T19:39:57-04:00
rts: Ensure that Array# card arrays are initialized

In #19143 I noticed that newArray# failed to initialize the card table
of newly-allocated arrays. However, embarrassingly, I then only fixed
the issue in newArrayArray# and, in so doing, introduced the potential
for an integer underflow on zero-length arrays (#21962).

Here I fix the issue in newArray#, this time ensuring that we do not
underflow in pathological cases.

Fixes #19143.

- - - - -
e5ceff56 by Ben Gamari at 2022-08-08T19:39:57-04:00
testsuite: Add test for #21962

- - - - -


4 changed files:

- rts/PrimOps.cmm
- rts/include/Cmm.h
- + testsuite/tests/array/should_run/T21962.hs
- testsuite/tests/array/should_run/all.T


Changes:

=====================================
rts/PrimOps.cmm
=====================================
@@ -350,6 +350,11 @@ stg_newArrayzh ( W_ n /* words */, gcptr init )
     StgMutArrPtrs_ptrs(arr) = n;
     StgMutArrPtrs_size(arr) = size;
 
+    /* Ensure that the card array is initialized */
+    if (n != 0) {
+        setCardsValue(arr, 0, n, 0);
+    }
+
     // Initialise all elements of the array with the value in R2
     p = arr + SIZEOF_StgMutArrPtrs;
   for:


=====================================
rts/include/Cmm.h
=====================================
@@ -870,10 +870,11 @@
 /*
  * Set the cards in the array pointed to by arr for an
  * update to n elements, starting at element dst_off to value (0 to indicate
- * clean, 1 to indicate dirty).
+ * clean, 1 to indicate dirty). n must be non-zero.
  */
 #define setCardsValue(arr, dst_off, n, value)                                    \
     W_ __start_card, __end_card, __cards, __dst_cards_p;                         \
+    ASSERT(n != 0); \
     __dst_cards_p = (arr) + SIZEOF_StgMutArrPtrs + WDS(StgMutArrPtrs_ptrs(arr)); \
     __start_card = mutArrPtrCardDown(dst_off);                                   \
     __end_card = mutArrPtrCardDown((dst_off) + (n) - 1);                         \


=====================================
testsuite/tests/array/should_run/T21962.hs
=====================================
@@ -0,0 +1,11 @@
+{-# LANGUAGE UnboxedTuples #-}
+{-# LANGUAGE MagicHash #-}
+
+module Main where
+
+import GHC.IO
+import GHC.Exts
+
+main :: IO ()
+main = do
+   IO $ \s0 -> case newArray# 0# () s0 of (# s1, arr #) -> (# s1, () #)


=====================================
testsuite/tests/array/should_run/all.T
=====================================
@@ -23,3 +23,4 @@ test('arr017', when(fast(), skip), compile_and_run, [''])
 test('arr018', when(fast(), skip), compile_and_run, [''])
 test('arr019', normal, compile_and_run, [''])
 test('arr020', normal, compile_and_run, [''])
+test('T21962', normal, compile_and_run, [''])



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/ae707762335dabe2bb7e40639fd2ab2c7d3234fd...e5ceff56a6f11e4adc17a7cc05645b3e3a66ab97

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/ae707762335dabe2bb7e40639fd2ab2c7d3234fd...e5ceff56a6f11e4adc17a7cc05645b3e3a66ab97
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20220808/b869ef19/attachment-0001.html>


More information about the ghc-commits mailing list