Skip to content

Instantly share code, notes, and snippets.

@wavewave
Created August 10, 2023 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wavewave/662fcb687315002c2d3600c9c95cedbf to your computer and use it in GitHub Desktop.
Save wavewave/662fcb687315002c2d3600c9c95cedbf to your computer and use it in GitHub Desktop.
ghc-debug on HeapOverflow
#!/bin/bash
rm test.o
ghc -rtsopts -threaded test.hs
# run like the following
# GHC_DEBUG_SOCKET=/tmp/ghc-debug ./test +RTS -M1G
{
description = "heapoverflow-ghc-debug-test";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/master";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = {self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let pkgs = import nixpkgs { inherit system; };
hspkgs = pkgs.haskell.packages.ghc962.override (old: {
overrides = hself: hsuper: {
ghc-debug-stub = pkgs.haskell.lib.doJailbreak (hself.callHackage "ghc-debug-stub" "0.4.0.0" {});
ghc-debug-convention = hself.callHackage "ghc-debug-convention" "0.4.0.0" {};
};
});
hsenv = hspkgs.ghcWithPackages (p: [
p.ghc-debug-stub
p.cabal-install
]);
in {
devShells.default =
pkgs.mkShell {
buildInputs = [hsenv];
};
}
);
}
{-# LANGUAGE NumericUnderscores #-}
module Main where
import Control.Concurrent
( forkIO,
threadDelay,
)
import Control.Exception
( AsyncException (HeapOverflow),
catch,
)
import Data.List (foldl)
import GHC.Debug.Stub
( pause,
withGhcDebug,
)
main' :: IO ()
main' = do
forkIO $ do
let x = foldl (+) 0 [0..1_000_000_000]
print x
threadDelay 10_000_000
main :: IO ()
main =
withGhcDebug $
catch main' $ \(e :: AsyncException) ->
case e of
HeapOverflow -> do
print "yay! HeapOverflow!"
print "i am pausing"
pause
_ -> pure ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment