Skip to content

Commit 560e05a

Browse files
committed
added C and C++ example code
1 parent 2dafe21 commit 560e05a

23 files changed

+826
-12
lines changed

v3/c-example-code/array-overflow.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Example C code for OPT
2+
#include <stdio.h>
3+
4+
void foo(int* x) {
5+
printf("%d\n", x[3]);
6+
}
7+
8+
int main() {
9+
int arr[3];
10+
int overflow = 1000;
11+
arr[0] = 10;
12+
arr[1] = 20;
13+
arr[2] = 30;
14+
foo(arr);
15+
return 0;
16+
}

v3/c-example-code/array-param.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Example C code for OPT
2+
#include <stdlib.h>
3+
4+
int arrayParams(short short_array[5] /* incorrect bound */, char char_array[]) {
5+
printf(char_array);
6+
return 0;
7+
}
8+
9+
int main() {
10+
short stack_shorts[10];
11+
stack_shorts[1] = 1;
12+
stack_shorts[3] = 3;
13+
stack_shorts[5] = 5;
14+
stack_shorts[7] = 7;
15+
stack_shorts[9] = 9;
16+
17+
char* heap_str = (char*)malloc(7);
18+
heap_str[0] = 'B';
19+
heap_str[1] = 'o';
20+
heap_str[2] = 'b';
21+
heap_str[3] = '\0';
22+
23+
arrayParams(stack_shorts, heap_str);
24+
return 0;
25+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Example C code for OPT
2+
// From the test suite of https://github.com/codespecs/daikon
3+
// daikon/tests/kvasir-tests/
4+
5+
// Kvasir unit test for nested structs
6+
7+
// TODO: This is another test case that fails on AMD64 due to the
8+
// DynComp single tag per register issue. If (when) we fix this,
9+
// change all the 'long's below back to 'int'. (markro)
10+
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
14+
struct foo {
15+
long age;
16+
struct bar {
17+
long a;
18+
long b;
19+
} b;
20+
char* name;
21+
};
22+
23+
struct betterFoo {
24+
25+
long betterAge;
26+
27+
struct betterBar {
28+
long betterA;
29+
struct foo crappyFoo;
30+
31+
struct bazzz {
32+
char hello[100];
33+
char world[1000];
34+
} myBazzz;
35+
36+
long betterB;
37+
} namedBar;
38+
39+
char* betterName;
40+
};
41+
42+
struct foo globalFoo[4];
43+
struct betterFoo globalBetterFoo;
44+
45+
struct foo* returnF(struct foo* f, long* blah)
46+
{
47+
return f;
48+
}
49+
50+
int main() {
51+
struct foo fooArray[20];
52+
long intArray1[100];
53+
long* onHeap = (long*)calloc(69, sizeof(*onHeap));
54+
55+
globalFoo[0].age = 13;
56+
globalFoo[1].age = 23;
57+
globalFoo[2].age = 33;
58+
globalFoo[3].age = 43;
59+
60+
globalFoo[0].b.a = 0;
61+
globalFoo[1].b.a = 1;
62+
globalFoo[2].b.a = 2;
63+
globalFoo[3].b.a = 3;
64+
65+
globalFoo[0].b.b = 0;
66+
globalFoo[1].b.b = 100;
67+
globalFoo[2].b.b = 200;
68+
globalFoo[3].b.b = 300;
69+
70+
globalFoo[0].name = "globalFoo[0]";
71+
globalFoo[1].name = "globalFoo[1]";
72+
globalFoo[2].name = "globalFoo[2]";
73+
globalFoo[3].name = "globalFoo[3]";
74+
75+
returnF(fooArray, intArray1);
76+
printf("&globalFoo: %p, &globalBetterFoo: %p, &fooArray: %p\n", &globalFoo, &globalBetterFoo, fooArray);
77+
returnF(globalFoo, onHeap);
78+
return 0;
79+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Example C code for OPT
2+
// From the test suite of https://github.com/codespecs/daikon
3+
// daikon/tests/kvasir-tests/
4+
5+
int **global;
6+
7+
void f(int ***ppp) { return; }
8+
9+
int main()
10+
{
11+
long x = 1;
12+
int *ptr;
13+
int **ptrptr = &ptr;
14+
15+
global = (int **)&x;
16+
17+
f(&ptrptr);
18+
return 0;
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Example C code for OPT
2+
// From the test suite of https://github.com/codespecs/daikon
3+
// daikon/tests/kvasir-tests/
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
#include <ctype.h>
9+
10+
int f(char *arg, char **strings) {
11+
return 12;
12+
}
13+
14+
char *uppercase (char *s) {
15+
static char upcasestr[128];
16+
int i;
17+
18+
for (i = 0; i < strlen (s); i++)
19+
upcasestr[i] = toupper(s[i]);
20+
upcasestr[i] = '\0';
21+
22+
return (upcasestr);
23+
}
24+
25+
int main()
26+
{
27+
char *none = 0;
28+
static char *strings[] = {"apple", "banana", "carrot", "daikon",
29+
"eggplant", "fig", "grape", 0 };
30+
f(strings[1], strings);
31+
32+
uppercase("huckleberry");
33+
34+
return 0;
35+
}

v3/c-example-code/globals.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Example C code for OPT
2+
// adapted from pg's meng thesis
3+
4+
int globalInt = 42;
5+
char* globalStr = "hello\nworld\tagain!\n\n\n";
6+
char globalChar = 'x';
7+
double globalDouble = 3.14159;
8+
int globalIntArray[7];
9+
10+
int main() {
11+
int *a = &globalInt;
12+
char* aliasGlobalStr = globalStr;
13+
14+
globalIntArray[0] = 100;
15+
globalIntArray[2] = 200;
16+
globalIntArray[4] = 300;
17+
globalIntArray[6] = 400;
18+
19+
// increment it!
20+
aliasGlobalStr++;
21+
aliasGlobalStr++;
22+
aliasGlobalStr++;
23+
aliasGlobalStr++;
24+
aliasGlobalStr++;
25+
26+
return 0;
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Example C code for OPT
2+
#include <stdlib.h>
3+
4+
// from pg's meng thesis
5+
int globalInt = 42;
6+
7+
int main() {
8+
int localArray[10]; // contents uninitialized
9+
int *a, *b, *c, i, j; // c and j uninitialized, *c is meaningless
10+
a = &globalInt;
11+
b = (int*)malloc(15*sizeof(int));
12+
// Heap buffer overflow after i = 14
13+
for (i = 1; i < 100; i+=2) {
14+
b[i] = i; // Initialize only odd-indexed elements of b
15+
}
16+
return 0;
17+
}

v3/c-example-code/pointer-chain.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Example C code for OPT
2+
int main() {
3+
// inception?!?
4+
int x = 42;
5+
int *y = &x;
6+
int **z = &y;
7+
int ***wtf = &z;
8+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Example C code for OPT
2+
#include <stdlib.h>
3+
int* foo() {
4+
int local_x = 42;
5+
int* retval = &local_x; // uh oh
6+
return retval;
7+
}
8+
9+
int main() {
10+
int* null_p = NULL;
11+
int* random_p = (int*)123;
12+
13+
int* heap_p;
14+
int** uninit_pp = &heap_p;
15+
16+
heap_p = malloc(5 * sizeof(*heap_p));
17+
int* cur_p = heap_p;
18+
cur_p++;
19+
cur_p++;
20+
cur_p++;
21+
cur_p++;
22+
cur_p++;
23+
cur_p++;
24+
cur_p++;
25+
cur_p++;
26+
free(heap_p);
27+
heap_p = NULL;
28+
29+
int* stack_p = foo();
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Example C code for OPT
2+
// From: http://stackoverflow.com/questions/2124600/how-to-reverse-a-string-in-place-in-c-using-pointers
3+
#include <string.h>
4+
#include <stdlib.h>
5+
6+
void reverse(char *s) {
7+
char *end = s + (strlen(s) - 1);
8+
for(; end > s; --end, ++s) {
9+
(*s) ^= (*end);
10+
(*end) ^= (*s);
11+
(*s) ^= (*end);
12+
}
13+
}
14+
15+
int main() {
16+
char *x = malloc(20);
17+
strcpy(x, "Hello world!");
18+
reverse(x);
19+
return 0;
20+
}

0 commit comments

Comments
 (0)