Skip to content

Instantly share code, notes, and snippets.

View Aatch's full-sized avatar

James Miller Aatch

View GitHub Profile
@Aatch
Aatch / ast.rs
Created December 22, 2013 23:58
Rust-based Parser
use std::hashmap::HashMap;
#[deriving(Eq,Clone,IterBytes)]
pub struct Ident(u32);
#[deriving(Clone)]
pub struct Pos {
col: u16,
row: u16
}
@Aatch
Aatch / test.ll
Last active December 25, 2015 13:28
; ModuleID = 'test.rc'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
%struct.Foo = type { [10 x i64] }
%tydesc = type { i64, i64, void ({}*, i8*)*, void ({}*, i8*)*, void ({}*, i8*)*, void ({}*, i8*)*, i64, { i8*, i64 } }
@_rust_crate_map_toplevel = global { i32, i64, [2 x i64] } { i32 1, i64 ptrtoint ([1 x { i64, i64 }]* @_rust_mod_map to i64), [2 x i64] [i64 ptrtoint (i64* @_rust_crate_map_std_0.9-pre_6c65cf4b443341b1 to i64), i64 0] }
@_rust_crate_map_std_0.9-pre_6c65cf4b443341b1 = external global i64
@_rust_mod_map = internal global [1 x { i64, i64 }] zeroinitializer
@Aatch
Aatch / gist:6100515
Last active December 20, 2015 08:29
Memory Corruption
<?php
/*
Horrible Memory Corruption Bug.
So using PHPUnit, the file below (more-or-less) gets included inside a function as bootstrapping
script for PHPUnit. The first time it runs (i.e. no bytecode cache) it works fine. The second
causes a segfault in release mode. It asserts if assertions are turned on (-DALWAYS_ASSERT).
The assertion is:
@Aatch
Aatch / ml.rs
Created July 9, 2013 02:04
Multiple Lifetimes - The issue
/*
This is some hypothetical builder object that mutably
borrows a `Map` then does stuff to it. But the map itself
has a bounded lifetime.
What compiles is this:
*/
struct Builder<'self> {
extern mod std;
extern mod syntax;
use syntax::ast::*;
use std::sys::size_of;
macro_rules! type_size (
(S $t:ident) => (print_type_size::<$t>("struct", stringify!($t)));
(E $t:ident) => (print_type_size::<$t>("enum", stringify!($t)))
)
@Aatch
Aatch / ast_sizes.md
Last active December 19, 2015 11:09
AST Type sizes, before vs. after

Before

Structs

[struct]  	Lifetime: 48
[struct]  	Path: 104
[struct]  	def_id: 16
[struct]  	TyParam: 32
[struct]  	Generics: 16
@Aatch
Aatch / rt_exit.s
Last active December 19, 2015 04:38
_ZN7rt_exit17_96fb77fd6c6361db3_00E:
.cfi_startproc
cmpq %fs:112, %rsp
ja .LBB5_2
movabsq $8, %r10
movabsq $0, %r11
callq __morestack
ret
.LBB5_2:
pushq %rbp
@Aatch
Aatch / syscalls.rs
Last active December 19, 2017 13:14
x86-64 syscalls in rust, using asm!
#[inline(always)]
unsafe fn syscall0(n: int) -> int {
let mut ret : int = 0;
asm!("syscall" : "={rax}"(ret) : "{rax}"(n) : "rcx", "r11", "memory" : "volatile");
return ret;
}
#[inline(always)]
#[allow(default_methods)];
trait Foo {
pub fn visit_crate(&mut self) {
self.visit_mod([1,2,3])
}
pub fn visit_mod(&mut self, a: &[uint]) {
for a.iter().advance |i| {
self.visit_item(i);
execve("./test", ["./test"], [/* 54 vars */]) = 0
brk(0) = 0x6f8000
readlink("/proc/self/exe", "/home/james/tmp/test", 4096) = 20
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb03fd5d000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/home/james/tmp/../../../usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib/tls/x86_64/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/home/james/tmp/../../../usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib/tls/x86_64", 0x7fff360cf100) = -1 ENOENT (No such file or directory)
open("/home/james/tmp/../../../usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib/tls/librt.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/home/james/tmp/../../../usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib/tls", 0x7fff360cf100) = -1 ENOENT (No such file or directory)
open("/home/james/tmp/../../../usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib