Skip to content

Instantly share code, notes, and snippets.

View regehr's full-sized avatar
🎯
Focusing

John Regehr regehr

🎯
Focusing
View GitHub Profile
@regehr
regehr / gist:d45247f36106a65e9479
Last active March 24, 2016 19:47
implementation sketch for faster integer overflow checks in clang/llvm
I want to speed up integer overflow checking for hardening purposes by keeping a sticky overflow flag and only trapping when necessary. I want the implementation to be simple but hopefully amenable to optimizations. In the codegen part of clang:
- each function gets an i1 for storing overflow information, initialized to 0
- each integer overflow check ORs its result into the overflow flag
- before each function call, return instruction, or other side-effecting operation, execude ud2 if overflow is set
Reasonable?
@regehr
regehr / overflow.md
Last active April 13, 2016 22:56
implementation sketch for faster integer overflow checking in clang/llvm

I want to speed up integer overflow checking for hardening purposes by keeping a sticky overflow flag and only trapping when necessary. I want to keep it super simple while hopefully giving the optimizers room to do their thing.

In the codegen part of clang:

  • each function gets an i1 for storing overflow information, initialized to 0
  • each integer overflow check ORs its result into the overflow flag
  • before each function call, return instruction, or other side-effecting operation, execude ud2 if overflow is set

Reasonable?

int a1[10];
int *a2;
int foo1(int i) {
return a1[i];
}
int foo2(int *a, int i) {
return a[i];
}
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
volatile int x, y, tmp1, tmp2;
void *t0(void *arg) {
x = 1;
tmp1 = y;
return 0;
#!/usr/bin/python3
import random
# dimensions should be odd and >= 5
W = 25
H = 25
q = []
m = []
@regehr
regehr / random_maze.py
Created April 3, 2017 02:39
mst-based random maze generator
#!/usr/bin/python3
import random
# dimensions should be odd and >= 5
W = 25
H = 25
q = []
m = []
#include <cassert>
#include <iostream>
#include <vector>
#include <random>
namespace {
template <typename Key, typename Value> class RBTree {
private:
class RBTreeNode {
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
enum dir { N, S, W, E };
static enum dir getcmd(void) {
int in;
do {
in = getchar();
@regehr
regehr / gist:a428f164ae60ff783e5872d5efc5416e
Created April 24, 2018 21:55
souper constants from wasm_lua_scimark.c.wasm
; RHS inferred successfully
%0:i32 = var
%1:i32 = add 8:i32, %0
%2:i1 = eq 0:i32, %1
pc %2 1:i1
%3:i32 = add 4:i32, %0
cand %3 4294967292:i32
; RHS inferred successfully
%0:i32 = var
-----------------------------------------------------------
; RHS inferred successfully
result 7:i32
Input was:
; start LHS
%0 = block 2
%1 = phi %0, 7:i32, 7:i32
infer %1