Skip to content

A non-function coerced to a function can cause a stg_ap_v_ret

If you create an function by unsafe coercing a non-function you can get some strange functions. If you then take one of these function and apply it something it crashes with a stg_ap_v_ret. Here is a pretty minimal example.

import Unsafe.Coerce
main=(unsafeCoerce()::a->IO())1

You can reproduce it online here: https://tio.run/##y0gszk7Nyfn/PzO3IL@oRCE0rzgxLVXPOT@1KDmVKzcxM89WoxQsBhHS0LSyStS18/TX0NQ0/P8fAA

Here is another one:

import Unsafe.Coerce
g=unsafeCoerce()::a->a
main=print$g(g)1

If you coerce functions this doesn't appear to happen.

For example:

import Unsafe.Coerce
main=(unsafeCoerce id::a->IO())1

is a fine program. Sometimes this causes a segmentation fault (particularly with higher airity functions):

import Unsafe.Coerce
main=(unsafeCoerce(+)::a->IO())1

but I've never gotten it to make a stg_ap_v_ret.

Edited by WheatWizard
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information