Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Official C Challenge ①

Name: Anonymous 2010-07-18 12:23

Task:  Come up with an extension to the C programming language.  Provide a clear description of the extension, and a code sample that clearly shows what the extension is intended to do.

Prize:  (Winning criteria are not specified.)  The winner will have his extension featured in the C11 language standard.

My entry:  ... Sorry, I think C is perfect in every way already.

Name: Anonymous 2010-07-19 14:54

Here's as far as I got, I couldn't get the mprotect call working though... I think I need to align it to the page size, and the code I copied didn't work ;_;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <sys/mman.h>
#include <errno.h>
#include <unistd.h>

typedef int (*goo_ptr)(int);

static int goo(int n, int i) {
    return n + i;
}

/* This one is copied and replaced. */
static int goo_wrapper(int i)
{
    const int n = 0xdeadbeef;
    return goo(n, i);
}

static goo_ptr foo(int n) {
    const int deadbeef = 0xdeadbeef;
    ptrdiff_t size = (void*)foo - (void*)goo_wrapper;
    void* function = malloc(size);
    memcpy(function, goo_wrapper, size);

    void* deadbeef_p = memmem((const void*)function, size, (const void*)&deadbeef, sizeof(int));
    memcpy(deadbeef_p, &n, sizeof(int));

    /* mprotect HEEEEEEEEEEEEEEEEEELP */
    return (goo_ptr)function;
}

int main(int argc, char* argv[]) {
    int n = 1;
    int i = 2;
    goo_ptr goop = foo(n);
    int r = goop(i);
    printf("%d\n", r);
    return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List