Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env cabal
{- cabal:
build-depends: base, primitive
-}
{-# language BangPatterns #-}
import Data.IORef (IORef, newIORef, writeIORef, readIORef, atomicModifyIORef)
import System.IO.Unsafe (unsafePerformIO)
import Data.Primitive.PrimArray (MutablePrimArray, newPrimArray, writePrimArray, readPrimArray, replicatePrimArray, thawPrimArray)
import Control.Monad (zipWithM_, when, mapM)
@adamse
adamse / shift_dfa.md
Created December 21, 2022 10:01 — forked from pervognsen/shift_dfa.md
Shift-based DFAs

A traditional table-based DFA implementation looks like this:

uint8_t table[NUM_STATES][256]

uint8_t run(const uint8_t *start, const uint8_t *end, uint8_t state) {
    for (const uint8_t *s = start; s != end; s++)
        state = table[state][*s];
    return state;
}
; NOTE: all just a quick sketch
; also I'm using xmm/ymm/zmm0-5 and then 16-31
; because Win64 ABI expects me to preserve ymm6-ymm15
; and I couldn't be bothered to set up a stack frame and save them :)
;
; Meant to be assembled with NASM
section .data
vone dd 1
@adamse
adamse / consume.rs
Last active November 15, 2022 23:47
#![feature(split_array)]
// https://rust.godbolt.org/z/6o5T55K9v
/// Consume a value which implements `from_le_bytes` from a buffer, advancing
/// the buffer beyond the bytes that were consumed
#[macro_export]
macro_rules! consume {
($buf:expr, $ty:ty) => {{
const SIZE: usize = std::mem::size_of::<$ty>();
@adamse
adamse / .gitignore
Last active December 5, 2021 19:56
AOC 21
/d1
*.o
*.exe
{-# language OverloadedStrings #-}
import Data.List (sort, nub, transpose)
import Data.String
-- keep:
--
-- empty -- empty string
-- a b -- sequence of regexes
-- <char> -- match char
@adamse
adamse / HS.hs
Created June 4, 2021 12:54
FFI with exceptions
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Exception
import Control.Concurrent
import Foreign.Ptr
import System.Exit
import System.IO
import Foreign.StablePtr
module CaseOfCase (f2) where
data What = Int Int | TT | FF
{-# inline f1 #-}
f1 :: Word -> What
f1 x = case x of
1 -> Int 1
2 -> TT
3 -> FF
$ sudo perf probe -x /nix/store/3wa1xwnfv8ada1za1r8m4vmsiz1jifqq-glibc-2.32-35/lib/libc.so.6 '--add=mmap%return $retval'
Added new event:
probe_libc:mmap__return (on mmap%return in /nix/store/3wa1xwnfv8ada1za1r8m4vmsiz1jifqq-glibc-2.32-35/lib/libc-2.32.so with $retval)
You can now use it in all perf tools, such as:
perf record -e probe_libc:mmap__return -aR sleep 1
$ sudo perf record -e probe_libc:mmap__return -g -aR `which ghc` -e 'putStrLn "hej"'
import gdb
class BP(gdb.Breakpoint):
def __init__(self):
super().__init__('*(main+431)')
def stop(self):
return True
valid_chars = list(map(chr, list(range(ord('a'), ord('f')+1)) + list(range(ord('0'), ord('9')+1))))
target = 'adpjkoadapekldmpbjhjhbaghlfldbhjdalgnbeedheenfoeddabpmdnliokcahomdphbcleipfgibjdcgmjcmadaomiakpdjcni'