-- | Failure statuses at the C data-layer boundary.---- Every C allocator reports failure through a sentinel or a status-- instead of aborting: a NULL pointer, @UINT32_MAX@ from the bytecode-- emitters, @NN_SYMBOL_INVALID@ from the symbol table, @-1@ from the-- list constructors and the attribute set insert. Each covers two-- causes a caller needs to tell apart: the call ran outside an-- evaluation session ('Nix.Eval.IO.withEvalSession'), so the sub-arena-- has no memory, or the arena is live and could not grow. Only the C-- side knows which, so the failure path asks it once, through-- @nn_arena_live@, and raises a 'CStatusError' that names the C entry-- point. The boundary asks only after a failure, so on the Haskell side-- the hot path pays the sentinel comparison and nothing more; the C side-- asks as well, as each tracked object joins its tracker-- (@cbits\/nn_tracker.h@).---- A third kind of failure is a defect in this library: an answer from-- the C side that its own invariants rule out. A thunk refuses a state-- transition the boundary had just made valid, a cell holds a state,-- tag, form or builtin index nothing produces, an index the compiler or-- resolver chose holds nothing, or a string stored from text holds bytes-- that are not UTF-8. Each is raised as 'CInvariantViolated' rather-- than carried on.module Nix.Eval.CStatus ( -- * Errors CStatusError (..), -- * Checks checkedCPtr, cStatusFailure, cInvariantViolated, -- * Arena state arenaLive, )whereimport Control.Exception (Exception (..), throwIO)import Foreign.C.Types (CInt (..))import Foreign.Ptr (Ptr, nullPtr)-- ----------------------------------------------------------------------------- FFI import-- ---------------------------------------------------------------------------foreign import ccall unsafe "nn_arena_live" c_nn_arena_live :: IO CInt-- ----------------------------------------------------------------------------- Errors-- ----------------------------------------------------------------------------- | Why a C data-layer call reported failure. Each constructor carries-- the C entry point that refused.data CStatusError = -- | The call ran outside an evaluation session -- ('Nix.Eval.IO.withEvalSession'). A setup diagnosis to act on by -- running the evaluation in a session, not an error to catch and -- retry in place: GHC updates a thunk whose evaluation raised with -- that exception, so a shared value whose evaluation reached C -- outside a session re-raises it inside a later one. ArenaNotInitialized !String | -- | The arena is live and the allocation itself failed: exhaustion, -- or a size the C side rejects. The second field is the detail. CAllocationFailed !String !String | -- | The C side gave an answer this library's invariants rule out -- (see the module header). A defect in this library, never a -- resource failure; the second field says what was wrong. CInvariantViolated !String !String deriving (Eq, Show)instance Exception CStatusError where displayException (ArenaNotInitialized site) = site ++ ": the C data layer is not initialized; evaluation must run in an " ++ "evaluation session (Nix.Eval.IO.withEvalSession)" displayException (CAllocationFailed site detail) = site ++ ": " ++ detail displayException (CInvariantViolated site detail) = site ++ ": internal invariant violated: " ++ detail-- ----------------------------------------------------------------------------- Checks-- ----------------------------------------------------------------------------- | Reject a NULL from a C allocator before it can flow onward as a-- pointer: the C side signals failure deliberately, and the next-- dereference would be undefined behaviour.checkedCPtr :: String -> Ptr a -> IO (Ptr a)checkedCPtr site ptr | ptr == nullPtr = cStatusFailure site "C allocation failed" | otherwise = pure ptr-- | Raise the 'CStatusError' for a sentinel @site@ just returned. Asked-- right after the failing call and before anything else touches the-- arena, so the answer describes the state that call saw.cStatusFailure :: String -> String -> IO acStatusFailure site detail = do live <- arenaLive throwIO (if live then CAllocationFailed site detail else ArenaNotInitialized site)-- | Raise 'CInvariantViolated' for an answer from @site@ that this-- library's invariants rule out. Unlike-- 'cStatusFailure' it does not ask whether the arena is live: the answer-- is a defect whatever the arena's state, so liveness is not the question.cInvariantViolated :: String -> String -> IO acInvariantViolated site detail = throwIO (CInvariantViolated site detail)-- ----------------------------------------------------------------------------- Arena state-- ----------------------------------------------------------------------------- | Whether every C sub-arena is inside its init .. destroy window.arenaLive :: IO BoolarenaLive = (/= 0) <$> c_nn_arena_live