Merge pull request #1183 from unisonweb/fix/1042-big-list-crashes

fix #1042
This commit is contained in:
mergify[bot] 2020-01-29 22:21:23 +00:00 committed by GitHub
commit 44ed1db47c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 22 deletions

View File

@ -162,18 +162,13 @@ atd size i m = at size i m >>= \case
Data r id vs -> pure (r, id, vs)
v -> fail $ "type error, expecting Data, got " <> show v
-- | `push` doesn't return the new stack size (is it for efficiency?),
-- so make sure that you add +1 to it yourself, after this call.
push :: Size -> Value -> Stack -> IO Stack
push size v s0 = do
s1 <-
if size >= MV.length s0
then do
-- increase the size to fit
s1 <- MV.grow s0 (size `max` 128)
-- traceM $ "Grew stack size to: " <> show (MV.length s1)
pure s1
else pure s0
MV.write s1 size v
pure s1
push size v m = do
m <- ensureSize (size + 1) m
MV.write m size v
pure m
-- Values passed to pushMany* are already in stack order:
-- the first Value is deeper on the resulting stack than the final Value
@ -198,9 +193,11 @@ pushManyZ size zs m = do
size2 <- foldM pushArg size zs
pure (size2, m)
-- | Grow the physical stack to at least `size` slots
ensureSize :: Size -> Stack -> IO Stack
ensureSize size m =
if (size >= MV.length m) then MV.grow m size
if (size > MV.length m) then
MV.grow m size
else pure m
force :: Value -> IO Value
@ -426,7 +423,6 @@ run ioHandler env ir = do
-- traceM $ P.render 80 (pir ir)
supply <- newIORef 0
m0 <- MV.new 256
MV.set m0 (T "uninitialized")
let
fresh :: IO Int
fresh = atomicModifyIORef' supply (\n -> (n + 1, n))
@ -459,8 +455,20 @@ run ioHandler env ir = do
RDone v -> do
-- Garbage collect the stack occasionally
(size, m) <-
if size > MV.length m `div` 2
then gc size m (if Set.null freeInBody then 0 else Set.findMax freeInBody)
if size >= MV.length m
-- freeInBody just the set of de bruijn indices referenced in `body`
-- Examples:
-- a) let x = 1 in x, freeInBody = {0}
-- b) let x = 1 in 42, freeInBody = {}
-- We don't need anything from old stack in either of the above
--
-- c) let x = 1 in (let y = 2 in x + y), freeInBody = {0,1}
-- We need the top element of the old stack to be preserved
then let
maxSlot =
if Set.null freeInBody then -1
else Set.findMax freeInBody - 1
in gc size m maxSlot
else pure (size, m)
-- traceM . P.render 80 $ P.shown var <> " =" `P.hang` pvalue v
push size v m >>= \m -> go (size + 1) m body
@ -766,15 +774,16 @@ run ioHandler env ir = do
go size' m body
-- Garbage collect the elements of the stack that are more than `maxSlot`
-- below the top - this is done just by copying to a fresh stack.
-- from the top - this is done just by copying to a fresh stack.
gc :: Size -> Stack -> Int -> IO (Size, Stack)
-- when maxSlot = -1, nothing from the old stack is needed.
gc _ _ _maxSlot@(-1) = do m <- MV.new 256; pure (0, m)
gc size m maxSlot = do
when (maxSlot < 0) $ fail $ "invalid max slot for garbage collection: " <> show maxSlot
let size2 = maxSlot + 1
m2 = MV.slice (size - maxSlot - 1) size2 m
m <- MV.clone m2
m <- MV.grow m 256
pure (size2, m)
let start = size - maxSlot - 1
len = maxSlot + 1
m <- MV.clone $ MV.slice start len m
pure (len, m)
loop (RRequest (Req ref cid vs k)) = do
ioResult <- ioHandler ref cid vs
case ioResult of

View File

@ -0,0 +1,9 @@
#### Big list crash
Big lists have been observed to crash, while in the garbage collection step.
```unison
unique type Direction = U | D | L | R
x = [(R,1005),(U,563),(R,417),(U,509),(L,237),(U,555),(R,397),(U,414),(L,490),(U,336),(L,697),(D,682),(L,180),(U,951),(L,189),(D,547),(R,697),(U,583),(L,172),(D,859),(L,370),(D,114),(L,519),(U,829),(R,389),(U,608),(R,66),(D,634),(L,320),(D,49),(L,931),(U,137),(L,349),(D,689),(L,351),(D,829),(R,819),(D,138),(L,118),(D,849),(R,230),(U,858),(L,509),(D,311),(R,815),(U,217),(R,359),(U,840),(R,77),(U,230),(R,361),(U,322),(R,300),(D,646),(R,348),(U,815),(R,793),(D,752),(R,967),(U,128),(R,948),(D,499),(R,359),(U,572),(L,566),(U,815),(R,630),(D,290),(L,829),(D,736),(R,358),(U,778),(R,891),(U,941),(R,544),(U,889),(L,920),(U,913),(L,447),(D,604),(R,538),(U,818),(L,215),(D,437),(R,447),(U,576),(R,452),(D,794),(R,864),(U,269),(L,325),(D,35),(L,268),(D,639),(L,101),(U,777),(L,776),(U,958),(R,105),(U,517),(R,667),(D,423),(R,603),(U,469),(L,125),(D,919),(R,879),(U,994),(R,665),(D,377),(R,456),(D,570),(L,685),(U,291),(R,261),(U,846),(R,840),(U,418),(L,974),(D,270),(L,312),(D,426),(R,621),(D,334),(L,855),(D,378),(R,694),(U,845),(R,481),(U,895),(L,362),(D,840),(L,712),(U,57),(R,276),(D,643),(R,566),(U,348),(R,361),(D,144),(L,287),(D,864),(L,556),(U,610),(L,927),(U,322),(R,271),(D,90),(L,741),(U,446),(R,181),(D,527),(R,56),(U,805),(L,907),(D,406),(L,286),(U,873),(L,79),(D,280),(L,153),(D,377),(R,253),(D,61),(R,475),(D,804),(R,788),(U,393),(L,660),(U,314),(R,489),(D,491),(L,234),(D,712),(L,253),(U,651),(L,777),(D,726),(R,146),(U,47),(R,630),(U,517),(R,226),(U,624),(L,834),(D,153),(L,513),(U,799),(R,287),(D,868),(R,982),(U,390),(L,296),(D,373),(R,9),(U,994),(R,105),(D,673),(L,657),(D,868),(R,738),(D,277),(R,374),(U,828),(R,860),(U,247),(R,484),(U,986),(L,723),(D,847),(L,578),(U,487),(L,51),(D,865),(L,328),(D,199),(R,812),(D,726),(R,355),(D,463),(R,761),(U,69),(R,508),(D,753),(L,81),(D,50),(L,345),(D,66),(L,764),(D,466),(L,975),(U,619),(R,59),(D,788),(L,737),(D,360),(R,14),(D,253),(L,512),(D,417),(R,828),(D,188),(L,394),(U,212),(R,658),(U,369),(R,920),(U,927),(L,339),(U,552),(R,856),(D,458),(R,407),(U,41),(L,930),(D,460),(R,809),(U,467),(L,410),(D,800),(L,135),(D,596),(R,678),(D,4),(L,771),(D,637),(L,876),(U,192),(L,406),(D,136),(R,666),(U,730),(R,711),(D,291),(L,586),(U,845),(R,606),(U,2),(L,228),(D,759),(R,244),(U,946),(R,948),(U,160),(R,397),(U,134),(R,188),(U,850),(R,623),(D,315),(L,219),(D,450),(R,489),(U,374),(R,299),(D,474),(L,767),(D,679),(L,160),(D,403),(L,708)]
```

View File

@ -0,0 +1,25 @@
#### Big list crash
Big lists have been observed to crash, while in the garbage collection step.
```unison
unique type Direction = U | D | L | R
x = [(R,1005),(U,563),(R,417),(U,509),(L,237),(U,555),(R,397),(U,414),(L,490),(U,336),(L,697),(D,682),(L,180),(U,951),(L,189),(D,547),(R,697),(U,583),(L,172),(D,859),(L,370),(D,114),(L,519),(U,829),(R,389),(U,608),(R,66),(D,634),(L,320),(D,49),(L,931),(U,137),(L,349),(D,689),(L,351),(D,829),(R,819),(D,138),(L,118),(D,849),(R,230),(U,858),(L,509),(D,311),(R,815),(U,217),(R,359),(U,840),(R,77),(U,230),(R,361),(U,322),(R,300),(D,646),(R,348),(U,815),(R,793),(D,752),(R,967),(U,128),(R,948),(D,499),(R,359),(U,572),(L,566),(U,815),(R,630),(D,290),(L,829),(D,736),(R,358),(U,778),(R,891),(U,941),(R,544),(U,889),(L,920),(U,913),(L,447),(D,604),(R,538),(U,818),(L,215),(D,437),(R,447),(U,576),(R,452),(D,794),(R,864),(U,269),(L,325),(D,35),(L,268),(D,639),(L,101),(U,777),(L,776),(U,958),(R,105),(U,517),(R,667),(D,423),(R,603),(U,469),(L,125),(D,919),(R,879),(U,994),(R,665),(D,377),(R,456),(D,570),(L,685),(U,291),(R,261),(U,846),(R,840),(U,418),(L,974),(D,270),(L,312),(D,426),(R,621),(D,334),(L,855),(D,378),(R,694),(U,845),(R,481),(U,895),(L,362),(D,840),(L,712),(U,57),(R,276),(D,643),(R,566),(U,348),(R,361),(D,144),(L,287),(D,864),(L,556),(U,610),(L,927),(U,322),(R,271),(D,90),(L,741),(U,446),(R,181),(D,527),(R,56),(U,805),(L,907),(D,406),(L,286),(U,873),(L,79),(D,280),(L,153),(D,377),(R,253),(D,61),(R,475),(D,804),(R,788),(U,393),(L,660),(U,314),(R,489),(D,491),(L,234),(D,712),(L,253),(U,651),(L,777),(D,726),(R,146),(U,47),(R,630),(U,517),(R,226),(U,624),(L,834),(D,153),(L,513),(U,799),(R,287),(D,868),(R,982),(U,390),(L,296),(D,373),(R,9),(U,994),(R,105),(D,673),(L,657),(D,868),(R,738),(D,277),(R,374),(U,828),(R,860),(U,247),(R,484),(U,986),(L,723),(D,847),(L,578),(U,487),(L,51),(D,865),(L,328),(D,199),(R,812),(D,726),(R,355),(D,463),(R,761),(U,69),(R,508),(D,753),(L,81),(D,50),(L,345),(D,66),(L,764),(D,466),(L,975),(U,619),(R,59),(D,788),(L,737),(D,360),(R,14),(D,253),(L,512),(D,417),(R,828),(D,188),(L,394),(U,212),(R,658),(U,369),(R,920),(U,927),(L,339),(U,552),(R,856),(D,458),(R,407),(U,41),(L,930),(D,460),(R,809),(U,467),(L,410),(D,800),(L,135),(D,596),(R,678),(D,4),(L,771),(D,637),(L,876),(U,192),(L,406),(D,136),(R,666),(U,730),(R,711),(D,291),(L,586),(U,845),(R,606),(U,2),(L,228),(D,759),(R,244),(U,946),(R,948),(U,160),(R,397),(U,134),(R,188),(U,850),(R,623),(D,315),(L,219),(D,450),(R,489),(U,374),(R,299),(D,474),(L,767),(D,679),(L,160),(D,403),(L,708)]
```
```ucm
I found and typechecked these definitions in scratch.u. If you
do an `add` or `update`, here's how your codebase would
change:
⍟ These new definitions are ok to `add`:
unique type Direction
x : [(Direction, Nat)]
Now evaluating any watch expressions (lines starting with
`>`)... Ctrl+C cancels.
```