Quantcast
Channel: Nikolay Igotti
Viewing all articles
Browse latest Browse all 30

VTBL games

$
0
0
Little game with changing vtbl entry for an instance. Probably more interesting part would be class-wide modification, but it's usually in read only area (text segment). For completeness of demo we'll dispatch calls to regular function, not member function. On Windows this means this is not passed, as it uses a bit different calling convention between member and regular functions (this in ECX).
#include<stdio.h>
#include<stdlib.h>class A {public:virtualvoid foo() {
    printf("A::foo(): %p\n", this);
  }
};class B : public A {public:virtualvoid foo() {
    printf("B::foo(): %p\n", this);
  }
};

A* get() {if (rand() & 1) {returnnew A();
  } else {returnnew B();
  }
}voidbar(void* thiz) {
  printf("bar: %p\n", thiz);
}intmain() {
  A* a = get();void* vt[] = { (void*)bar };

  a->foo();

  *(void**)a = &vt;

  a->foo();return 0;
}

Viewing all articles
Browse latest Browse all 30

Trending Articles