Skip to content

Instantly share code, notes, and snippets.

@bgaff
bgaff / pkru.c
Created Nov 9, 2021
11th Gen Core CPU PKRU
View pkru.c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/mman.h>
static uint32_t rdpkru(void)
{
uint32_t ecx = 0;
uint32_t edx, pkru;
@bgaff
bgaff / repro.c
Created Nov 8, 2021
rdpkru - Intel 11th Gen Core CPU bug
View repro.c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/mman.h>
#define BITS_PER_PKEY 2
static uint32_t rdpkru(void)
{
uint32_t ecx = 0;
View accounting_underflow.c
#define _GNU_SOURCE
#include <sys/mman.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#ifndef MREMAP_DONTUNMAP
View userfaultfd_read_block.c
#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
View userfaultfd_close.c
#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
View controlflow.c
#include <iostream>
#include <functional>
#include <memory>
#include <cstdio>
void* _saved_return;
void* _saved_bp;
#define NO_INLINE __attribute__((noinline))
#define SET_OLD_RBP() \
View defer.cpp
#include <iostream>
#include <functional>
#include <memory>
#define GET_RETURN_VALUE64(VAR) \
asm ("nop\n nop\n movq %%rax,%0 \n nop\n" \
: "=r" (VAR) \
: \
: "rax" \
);
@bgaff
bgaff / userfaultfd_race.c
Created Feb 14, 2020
Example of SIGBUS caused by race handling userfaults
View userfaultfd_race.c
#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
@bgaff
bgaff / fizzbuzz.cpp
Created Apr 26, 2012
FizzBuzz C++: Template Recursion
View fizzbuzz.cpp
/*
* fizzbuzz.cpp
*
* Created on: Apr 25, 2012
* Author: Brian Geffon
*
* fizzbuzz solved without looping or conditionals using only template recursion.
*/
#include <iostream>