From 03d23d03682616340f9b631b2ab7d489cddd68fe Mon Sep 17 00:00:00 2001 From: Alexey Kuleshevich Date: Tue, 19 Nov 2024 20:08:09 -0700 Subject: [PATCH] Add a comment about safety of `accursedUnutterablePerformIO` usage --- cardano-crypto-class/src/Cardano/Crypto/PackedBytes.hs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cardano-crypto-class/src/Cardano/Crypto/PackedBytes.hs b/cardano-crypto-class/src/Cardano/Crypto/PackedBytes.hs index b443d90f9..42e555072 100644 --- a/cardano-crypto-class/src/Cardano/Crypto/PackedBytes.hs +++ b/cardano-crypto-class/src/Cardano/Crypto/PackedBytes.hs @@ -123,6 +123,12 @@ instance KnownNat n => MemPack (PackedBytes n) where buf <- ask pure $! buffer buf (\ba# -> packBytes (SBS.SBS ba#) curPos) + -- Usage of `accursedUnutterablePerformIO` is safe below because there are no memory + -- allocations happening that depend on the IO monad that we are excaping here. All + -- IO actions are morally pure reads using pointers into the immutable + -- memory. Furthermore, in the place where ByteArray is allocated in + -- `packPinnedPtrN`, mutation and freezing are encapsulated with `runST` and is not + -- related to the `IO` we are escaping. (\addr# -> accursedUnutterablePerformIO $ packPinnedPtr (Ptr (addr# `plusAddr#` curPos#))) {-# INLINE unpackM #-}