Skip to content

Instantly share code, notes, and snippets.

@NeatMonster
Created June 28, 2017 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeatMonster/10bb33694bbbf0f812510051c296dcdb to your computer and use it in GitHub Desktop.
Save NeatMonster/10bb33694bbbf0f812510051c296dcdb to your computer and use it in GitHub Desktop.
Sample program for testing value profiling.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
typedef void (*F)();
static F t[256];
void f() {
printf("passed ind\n");
abort();
}
void f1() {
t['a'] = f;
printf("passed ind (1)\n");
}
void f2() {
t['f'] = f1;
printf("passed ind (2)\n");
}
void f3() {
t['l'] = f2;
printf("passed ind (3)\n");
}
void f0() {}
static F t0[256] = {
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0, f0,
};
void test_ind(char *buf) {
memcpy(t, t0, sizeof(t));
t['.'] = f3;
t[buf[0] & 0xff]();
t[buf[1] & 0xff]();
t[buf[2] & 0xff]();
t[buf[3] & 0xff]();
}
void test_cmp32(char *buf) {
uint32_t x;
x = *((uint32_t*) buf);
if (x == 0x01234567) {
printf("passed cmp32\n");
abort();
} else
printf("failed cmp32\n");
}
void test_cmp64(char *buf) {
uint64_t x;
x = *((uint64_t*) buf);
if (x == 0x0123456789abcdef) {
printf("passed cmp64\n");
abort();
} else
printf("failed cmp64\n");
}
void test_switch32(char *buf) {
uint32_t x;
x = *((uint32_t*) buf);
switch (x) {
case 0x12345670:
printf("passed switch32 (1)\n");
abort();
break;
case 0x23456701:
printf("passed switch32 (2)\n");
abort();
break;
default:
printf("failed switch32\n");
break;
}
}
void test_switch64(char *buf) {
uint64_t x;
x = *((uint64_t*) buf);
switch (x) {
case 0x123456789abcdef0:
printf("passed switch64 (1)\n");
abort();
break;
case 0x23456789abcdef01:
printf("passed switch64 (2)\n");
abort();
break;
default:
printf("failed switch64\n");
break;
}
}
void test_div32(char *buf) {
uint32_t x, y;
x = *((uint32_t*) buf);
y = 0x01234567 / (0x76543210 - x);
printf("failed div32 (%d)\n", y);
}
void test_div64(char *buf) {
uint64_t x, y;
x = *((uint64_t*) buf);
y = 0x0123456789abcdef / (0xfedcba9876543210 - x);
printf("failed div64 (%ld)\n", y);
}
const int arrSize = 1234567;
int arr[arrSize];
void test_gep(char *buf) {
uint64_t x, y;
x = *((uint64_t*) buf);
y = arr[x % (arrSize + 1)];
printf("failed gep (%ld)\n", y);
}
void test_memcmp(char *buf) {
if (!memcmp("helloworld", buf, 10)) {
printf("passed memcmp\n");
abort();
} else
printf("failed memcmp\n");
}
void test_strcmp(char *buf) {
if (!strcmp("elloworldh", buf)) {
printf("passed strcmp\n");
abort();
} else
printf("failed strcmp\n");
}
void test_strncmp(char *buf) {
if (!strncmp("lloworldhe", buf, 10)) {
printf("passed strncmp\n");
abort();
} else
printf("failed strncmp\n");
}
void test_strcasecmp(char *buf) {
if (!strcasecmp("loworldhel", buf)) {
printf("passed strcasecmp\n");
abort();
} else
printf("failed strcasecmp\n");
}
void test_strncasecmp(char *buf) {
if (!strncasecmp("oworldhell", buf, 10)) {
printf("passed strncasecmp\n");
abort();
} else
printf("failed strncasecmp\n");
}
int main(int argc, char *argv[]) {
char buf[100];
while (__AFL_LOOP(1000)) {
memset(buf, 0, 100);
ssize_t len = read(0, buf, 100);
printf("read %ld bytes\n", len);
if (!strcmp(argv[1], "ind"))
test_ind(buf);
else if (!strcmp(argv[1], "cmp32"))
test_cmp32(buf);
else if (!strcmp(argv[1], "cmp64"))
test_cmp64(buf);
else if (!strcmp(argv[1], "switch32"))
test_switch32(buf);
else if (!strcmp(argv[1], "switch64"))
test_switch64(buf);
else if (!strcmp(argv[1], "div32"))
test_div32(buf);
else if (!strcmp(argv[1], "div64"))
test_div64(buf);
else if (!strcmp(argv[1], "gep"))
test_gep(buf);
else if (!strcmp(argv[1], "memcmp"))
test_memcmp(buf);
else if (!strcmp(argv[1], "strcmp"))
test_strcmp(buf);
else if (!strcmp(argv[1], "strncmp"))
test_strncmp(buf);
else if (!strcmp(argv[1], "strcasecmp"))
test_strcasecmp(buf);
else if (!strcmp(argv[1], "strncasecmp"))
test_strncasecmp(buf);
else
printf("test not found\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment