Skip to content

Instantly share code, notes, and snippets.

View karthick18's full-sized avatar

A R Karthick karthick18

View GitHub Profile
@karthick18
karthick18 / large_fact.c
Created December 12, 2012 07:26
Solution for large factorial problem accepted at: http://hackerearth.com/hackerearth-practice-challenge/p-6/ Should compute any factorial. Tested up to fact(250000) with an output of 1.24 million digits. Runs slowly when over fact(10000)
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#define likely(expr) __builtin_expect(!!(expr), 1)
#define unlikely(expr) __builtin_expect(!!(expr), 0)
#define __EXTEND_MEM (256<<10)
@karthick18
karthick18 / root_shell.c
Created January 27, 2012 01:15
The mempodipper root shell exploit that was released recently for linux. A slightly modified variant to allow exploiting any setuid victim binary that emits the passed invalid arguments to gain root shell
/*
* Mempodipper
* by zx2c4
*
* Linux Local Root Exploit
*
* Rather than put my write up here, per usual, this time I've put it
* in a rather lengthy blog post: http://blog.zx2c4.com/749
*
* Enjoy.
@karthick18
karthick18 / sse2_load.c
Created November 13, 2011 08:42
memcmp with SSE2 16 byte loads and compares
/*
* word compare with SSE2 functions/instructions for faster 16 byte loads and compares.
*
* compile with:
* gcc -o sse_load_test sse_load.c -Wall -O2 -g -DTEST_SSE2
*
*/
#if defined(TEST_SSE2) && !defined(__SSE2__)
#error "no SSE2 set"
#endif
@karthick18
karthick18 / splice.c
Created September 22, 2011 06:31
An example copy of a file to output file using the splice syscall that avoids copying to/from user space buffers to kernel space by employing pipe buffers allocated in kernel space for fast data transfers between 2 files
/*
* An example using splice syscall which avoids copying to/from user space buffers to kernel space
* and uses the pipe buffers allocated in kernel space as an intermediate to directly xfer from one file to another
*
* gcc -o splice splice.c -g
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
@karthick18
karthick18 / closure_malloc.c
Created July 26, 2011 06:13
Ressurection of Gopal.V's 2006 excellent post on closures for posterity. Example of closures for wrapping memory allocations to aid in debugging malloc leaks.
/*
* Another closure example using Gopal.V's (a.k.a @t3rmin4t0r the great)
* excellent blogpost on closure dating all the way back to 2006!
*
* http://notmysock.org/blog/hacks/when-macros-arent-enough
*
* Resurrecting it with a useless use-case for the sake of posterity
* and to remind the myriads of people who say
* they know "C" in their resumes since they think its "Let us C"
* even if they work with dot NET or web tech. in their day to day life
@karthick18
karthick18 / continuation.c
Created July 7, 2011 04:58
Now a continuation example using the eg: closure that I had created with the last gist at: https://gist.github.com/1057979
/*
* A continuation example with closure.
* To run:
*
* gcc -o continuation continuation.c -Wall -g -lpthread
* ./continuation
* would test with creating 10 continuations each stacking 3 levels of continuations before unwinding
* ./continuation 20
* would test with 20 continuations
*
@karthick18
karthick18 / thunk.c
Created July 1, 2011 06:27
An example closure in C that requires MAP_32BIT to work with 64 bit
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#ifndef MAP_32BIT
#error "MAP_32BIT not defined"
#endif
struct scope
{
@karthick18
karthick18 / brk_cow.c
Created June 17, 2011 23:08
Just an example to remind that its futile to free memory in the child to avoid taking a break COW perf. hit. Check the header comments for more details.
/*
* Just an example to remind that its futile to free memory in the child
* allocated by the parent to avoid taking a break COW perf. hit.
* Makes sense only to free large chunk sizes in the child. Smaller chunk sizes
* aren't really trimmed by malloc and only end up causing perf hits with
* break COW pages (copy on write) when freeing in the child.
* A break COW is when free results in malloc lib. touching the freed chunk
* of memory resulting in a write protection page fault for the child that ends up
* unmapping the shared page table entry and then maps a writable page copy to the child.
* The net effect for RSS(resident set size) is the same as in the parent but
@karthick18
karthick18 / vacation.mail
Created May 13, 2011 20:57
Vacation response to test mailers patience
## OUT OF OFFICE REPLY
#!/usr/bin/env bash
(gcc -x c - -o vacation && ./vacation) <<EOF
#include <stdio.h>
#include <ctype.h>
static char *rot(char*);
int main()
{
@karthick18
karthick18 / vacation.mail
Created May 13, 2011 20:57
Vacation response to test mailers patience
## OUT OF OFFICE REPLY
#!/usr/bin/env bash
(gcc -x c - -o vacation && ./vacation) <<EOF
#include <stdio.h>
#include <ctype.h>
static char *rot(char*);
int main()
{