Admin message

Due to a large amount of spam we do not allow new users to create repositories, they are "external" users. If you are a new user and want to create a repository, for example for forking GHC, open a new issue on ghc/ghc using the "get-verified" issue template

How untagged pointers sneak into banged fields
(*N.B.* I am writing this up from memory, and cannot verify it just now, maybe someone can lend a hand, otherwise I'll do it ASAP!) Here is a way how untagged pointers to strict data can be created in banged (strict) constructor fields. This reproduction recipe \*\*depends on the patch from #14677 applied\*\*. We have 3 modules `A`, `B` and `C` (SG: Note that the newtype wrapper has become redundant, probably in the aftermath of #15696. I could reproduce the bug without it): ```hs module A where data A = X | Y | Z a = Z ``` ```hs module B where import A newtype B = B A b = B a ``` ```hs {-# language MagicHash #-} module C where import A import B import GHC.Exts data C = C !B c = C b main = do print (I# (reallyUnsafePtrEquality# a (coerce b))) -- prints 0, b is softlink print (I# (dataToTag# c)) -- prints 0: not entered yet print (case c of C b' -> I# (dataToTag# b')) -- used to print 0? After #15696 it prints 2 print (case c of C (B a') -> I# (dataToTag# a')) -- used to print 3. After #15696 it prints 2 ``` ------------------- == Why this happens `B.b` is a newtype to `A.a` so one would expect that both alias the same memory location (a *hardlink* in filesystem parlance). But currently reexports are implemented with a special type of closure `IND_STATIC` (a *softlink*) which needs to be entered to obtain the actual (tagged pointer). The `IND_STATIC` closure's pointer is never tagged (otherwise it would never be entered, instead interpreted as a honest-to-goodness `A.A`, which causes the symptoms seen in #14677). With #14677 applied to GHC, the unfolding of `B.b` is correctly read when compiling `C` (with `-O1` and better) and thus the compiler knows that it should be a tagged pointer value. Thus the construction of `C.c` shortcuts the entering of `B.b` when filling the strict field, and (because `B.b` being a softlink, thus untagged) the field ends up carrying a 0 tag. ------------------------- == How can this be fixed? I see two possibilities one conservative and one invasive. ### Conservative When seeing a coercion unfolding of a tagged value being used to initialise a strict field, do not skip the evaluatedness check, but cater for the possibility of an `IND_STATIC` closure. Check the closure type, and if confirmed, steal the pointee and use that. ### Invasive Get rid of the `IND_STATIC` closures altogether. For *intra-module* softlinks we can have proper hardlinks (assembler `.equiv` directives, or LLVM `alias`es). *Inter-module* softlinks can also be eliminated by linker scripts. This would however cause more build artifacts, so I don't know how hairy it would turn out. OTOH, it would reduce binary size by eliminating indirection closures and potential dereferencing code. <details><summary>Trac metadata</summary> | Trac field | Value | | ---------------------- | ------------ | | Version | 8.4.2 | | Type | Bug | | TypeOfFailure | OtherFailure | | Priority | normal | | Resolution | Unresolved | | Component | Compiler | | Test case | | | Differential revisions | | | BlockedBy | | | Related | | | Blocking | | | CC | alexbiehl | | Operating system | | | Architecture | | </details> <!-- {"blocked_by":[],"summary":"How untagged pointers sneak into banged fields","status":"New","operating_system":"","component":"Compiler","related":[],"milestone":"8.6.1","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"8.4.2","keywords":[],"differentials":[],"test_case":"","architecture":"","cc":["alexbiehl"],"type":"Bug","description":"(''N.B.'' I am writing this up from memory, and cannot verify it just now, maybe someone can lend a hand, otherwise I'll do it ASAP!)\r\n\r\nHere is a way how untagged pointers to strict data can be created in banged (strict) constructor fields.\r\nThis reproduction recipe **depends on the patch from #14677 applied**.\r\n\r\nWe have 3 modules `A`, `B` and `C`:\r\n\r\n{{{#!hs\r\nmodule A where\r\ndata A = X | Y | Z\r\na = Z\r\n}}}\r\n\r\n{{{#!hs\r\nmodule B where\r\nimport A\r\nnewtype B = B A\r\nb = B a\r\n}}}\r\n\r\n{{{#!hs\r\n{-# language MagicHash #-}\r\n\r\nmodule C where\r\nimport A\r\nimport B\r\nimport GHC.Exts\r\n\r\ndata C = C !B\r\nc = C b\r\n\r\nmain = do print (I# (reallyUnsafePtrEquality# a (coerce b))) -- prints 0, b is softlink\r\n print (I# (dataToTag# c)) -- prints 0: not entered yet\r\n print (case c of C b' -> I# (dataToTag# b')) -- prints 0?\r\n print (case c of C (B a') -> I# (dataToTag# a')) -- prints 3\r\n}}}\r\n\r\n-------------------\r\n== Why this happens\r\n\r\n`B.b` is a newtype to `A.a` so one would expect that both alias the same memory location (a ''hardlink'' in filesystem parlance). But currently reexports are implemented with a special type of closure `IND_STATIC` (a ''softlink'') which needs to be entered to obtain the actual (tagged pointer). The `IND_STATIC` closure's pointer is never tagged (otherwise it would never be entered, instead interpreted as a honest-to-goodness `A.A`, which causes the symptoms seen in #14677).\r\n\r\nWith #14677 applied to GHC, the unfolding of `B.b` is correctly read when compiling `C` (with `-O1` and better) and thus the compiler knows that it should be a tagged pointer value. Thus the construction of `C.c` shortcuts the entering of `B.b` when filling the strict field, and (because `B.b` being a softlink, thus untagged) the field ends up carrying a 0 tag.\r\n\r\n-------------------------\r\n== How can this be fixed?\r\n\r\nI see two possibilities one conservative and one invasive.\r\n\r\n=== Conservative\r\n\r\nWhen seeing a coercion unfolding of a tagged value being used to initialise a strict field, do not skip the evaluatedness check, but cater for the possibility of an `IND_STATIC` closure. Check the closure type, and if confirmed, steal the pointee and use that.\r\n\r\n=== Invasive\r\n\r\nGet rid of the `IND_STATIC` closures altogether. For ''intra-module'' softlinks we can have proper hardlinks (assembler `.equiv` directives, or LLVM `alias`es). ''Inter-module'' softlinks can also be eliminated by linker scripts. This would however cause more build artifacts, so I don't know how hairy it would turn out.\r\n\r\nOTOH, it would reduce binary size by eliminating indirection closures and potential dereferencing code.\r\n\r\n","type_of_failure":"OtherFailure","blocking":[]} -->
issue