Skip to content

Instantly share code, notes, and snippets.

Avatar
Taking some time off personal open source work

Leandro A. F. Pereira lpereira

Taking some time off personal open source work
View GitHub Profile
@lpereira
lpereira / coro.c
Created Mar 22, 2012
Simple coroutine implementation in C
View coro.c
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <ucontext.h>
typedef struct coro_t_ coro_t;
typedef struct thread_t_ thread_t;
typedef int (*coro_function_t)(coro_t *coro);
typedef enum {
CORO_NEW,
View is-process-waiting-for-terminal.py
#!/usr/bin/python
import sys
import time
def is_sleeping(pid):
with open("/proc/%d/stat" % pid) as stat:
state = stat.read().strip().split()
return state[2] == 'S'
View autotmp.c
#include <alloca.h>
#include <stdio.h>
#include <stdlib.h>
struct tmpvar_hook {
void *ptr;
struct tmpvar_hook *next;
};
static void tmpvar_hook_free(struct tmpvar_hook **hook) {
View nm2dot.py
#!/usr/bin/python
import sys
def parse_nm_output(handle):
current_object = None
symbols = {}
symbol_to_object = {}
for line in handle.readlines():
@lpereira
lpereira / WebServerHelloWorld.cs
Last active Feb 19, 2021
C# Web Server Hello World
View WebServerHelloWorld.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
View find-potentially-unmappable.py
#!/usr/bin/python
import subprocess
unmappable_funcs = set()
out, err = subprocess.Popen(["objdump", "-t", "Build/Kernel/Kernel"], stdout=subprocess.PIPE).communicate()
for line in out.splitlines():
line = line.strip()
line = line.decode('ascii')
View jsonjit.c
/* Maps a page of the request size near the text page that contains
* a symbol. Useful for hand-rolled JIT compilers to avoid indirect
* calls to symbols in a program on x86-64. (Part of the code here is
* a JIT compiler to encode JSON based on a struct descriptor, but it's
* not finished yet.)
*
* Written by Leandro Pereira <leandro@tia.mat.br>
*
* Thanks to Ole André V. Ravnås for the idea to use the NOREPLACE flag
* in mmap(), and to Paul Khuong for helping me make it more robust in
View error-codes.c
#include <stdio.h>
struct error_code {
int code;
};
struct error_context {
const struct error_code *code;
const char *msg;
const char *file;
@lpereira
lpereira / partial.c
Last active Nov 27, 2020
Partial functions in C This program illustrates a hack to create partial functions in C. The way it works is that it generates a template function (partial_template_function) with known pointers, that is later copied to a region of memory obtained with mmap(), patched up with the address and data to be passed to the real function, and then made …
View partial.c
/*
* Partial applied functions in C
* Leandro Pereira <leandro@tia.mat.br>
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
View co.c
#include <stdio.h>
#include <stdbool.h>
struct coro {
void *state;
};
struct range_coro {
struct coro base;
int cur, max, step;