Skip to content

Instantly share code, notes, and snippets.

@msullivan
msullivan / bsmake
Created February 25, 2015 18:37
Hacky wrapper to run make in parallel until linking starts
#!/bin/bash
# Terrible hack to run a build in parallel until it starts linking
# things and then switch to running it in serial to cut down on memory usage.
# This could be better and more robust in a *lot* of ways.
# I use this for building llvm/clang.
# The first argument gets passed to the initial make that runs until
# linking starts and should be -j4 or whatever. The rest of the
# arguments get passed to both makes.
@msullivan
msullivan / thin-air.c
Created December 15, 2014 20:12
Thin-air-read bad case
// Out of thin air writes are really scary.
// Example adapted from http://dl.acm.org/citation.cfm?id=2618134 to
// work under http://svr-pes20-cppmem.cl.cam.ac.uk/cppmem/index.html
// Can wind up with an = &b, bn = &a!
int main() {
int *p1, *p2;
atomic_int a, an, b, bn;
a = &an;
@msullivan
msullivan / indexed_list.agda
Created November 14, 2014 22:36
Agda vs. Coq
(* Indexed list indexing in Agda *)
data IList {a} (A : Set a) : Nat → Set a where
Nil : IList A 0
Cons : {n : Nat} -> A -> IList A n -> IList A (S n)
data Fin : Nat -> Set where
Z : {n : Nat} -> Fin (S n)
S : {n : Nat} -> Fin n -> Fin (S n)
@msullivan
msullivan / IO.hs
Last active August 29, 2015 14:08
Exploration of pre-monadic IO features
-- Exploration of pre monadic IO facilities and how they relate to
-- each other and with monadic IO.
-- See: http://research.microsoft.com/en-us/um/people/simonpj/papers/history-of-haskell/
-- and http://cpsc.yale.edu/sites/default/files/files/tr665.pdf
import System.IO
import System.IO.Unsafe
import System.IO.Error
import System.Exit
import Data.Functor
@msullivan
msullivan / integer_overflow.c
Last active August 29, 2015 14:07
Some code that can get rekt by integer overflow undefined behavior
// Adapated from some code at
// http://stackoverflow.com/questions/3948479/integer-overflow-and-undefined-behavior
#include <stdio.h>
#include <stdlib.h>
// gcc starts optimizing the branch out at -O2; clang never does
void f(int a, int b) {
if (a > 0 && b > 0) {
if (a + b <= 0) {
@msullivan
msullivan / list_build.ml
Last active August 29, 2015 14:03
Some fairly awful code for building up lists
(* Third order function for imperatively building sequences in safe way,
* even if the underlying implementation uses unsafety.
*
* Type is:
* build : (('a -> unit) -> 'b) -> 'a list * 'b
*
* "build f" calls "f push", where push is a function that adds its argument
* to the sequence being built under the hood.
*
* Inliner needs to be pretty good for this to own.
@msullivan
msullivan / fixed_point.h
Last active August 29, 2015 13:58
Silly fixed point number macros
typedef int fixed_t;
#define SIGN(n) ((n) < 0 ? -1 : 1)
#define FRACTIONAL_BITS 24 // Number of fixed point fractional bits
// A bunch of macros for constructing fixed point numbers.
#define INT_TO_FIXED(n) ((n) << FRACTIONAL_BITS)
#define FRAC_TO_FIXED(n, d) ((fixed_t)(INT_TO_FIXED((long long)(n)) / (d)))
#define IMPROPER_TO_FIXED(i, n, d) FRAC_TO_FIXED((i)*(d) + SIGN(i)*(n), (d))
@msullivan
msullivan / mandelbrot.c
Last active August 29, 2015 13:57
A much cooler version of the CMU 15-410 mandelbrot test
/*!
* @file mandelbrot.c
*
* @brief A very cool, fractal test.
*
* Written by Keith Bare sometime aeons ago, improved by sully
* to support panning and zooming in Spring 2014.
*
* This version compromises on some of the original behaviors of mandelbrot
* in order to get panning/zooming to perform better in simics.
@msullivan
msullivan / gist:9359405
Last active August 29, 2015 13:57
Some of the worst code I have ever written
// setcontext pushes the return address onto the stack in order to
// restore it by running ret. It would be bad if we wrote to the
// parent's thread stack, for obvious reasons. Instead we set up
// our own stack for it to go on and we generate code that restores
// the real %esp and then does a jmp to the real %eip.
size_t bullshit_ministack[2];
bullshit_ministack[1] = ctx.uc_mcontext.gregs[REG_ESP];
char ret_code[6] = { 0x5c /* pop %esp */, 0xe9 /* jmp rel32 */ };
uint32_t target_eip = ctx.uc_mcontext.gregs[REG_EIP] + 2; // skip the int
@msullivan
msullivan / universal.rs
Last active February 3, 2020 19:18
Universal type in rust
// Safe implementation based on the existential power of closures.
mod ClosureUniversal {
// A value of universal type is a pair of functions. store will
// write the underlying value into the associated tag, while clear
// will erase the data in the tag to prevent space leaks.
pub struct Univ {
priv store: @fn(),
priv clear: @fn()
}