summaryrefslogtreecommitdiff
path: root/c/examples/zero_init
diff options
context:
space:
mode:
Diffstat (limited to 'c/examples/zero_init')
-rw-r--r--c/examples/zero_init/.gitignore2
-rw-r--r--c/examples/zero_init/Makefile14
-rw-r--r--c/examples/zero_init/dietpass.c25
-rw-r--r--c/examples/zero_init/test.c20
4 files changed, 61 insertions, 0 deletions
diff --git a/c/examples/zero_init/.gitignore b/c/examples/zero_init/.gitignore
new file mode 100644
index 0000000..b74bd7f
--- /dev/null
+++ b/c/examples/zero_init/.gitignore
@@ -0,0 +1,2 @@
+default
+instrumented
diff --git a/c/examples/zero_init/Makefile b/c/examples/zero_init/Makefile
new file mode 100644
index 0000000..79b5692
--- /dev/null
+++ b/c/examples/zero_init/Makefile
@@ -0,0 +1,14 @@
+all: default instrumented
+
+default: test.c
+ gcc -O0 $^ -o $@
+
+instrumented: test.c dietpass
+ dietcc -O0 test.c -o $@ --dietc-pass $(PWD)/dietpass
+
+dietpass: dietpass.c ../../libdietc.l ../../libdietc.h
+ make -C ../..
+ gcc -O3 dietpass.c ../../libdietc.o -I../../ -lfl -o $@
+
+clean:
+ rm -f default instrumented dietpass
diff --git a/c/examples/zero_init/dietpass.c b/c/examples/zero_init/dietpass.c
new file mode 100644
index 0000000..e145a91
--- /dev/null
+++ b/c/examples/zero_init/dietpass.c
@@ -0,0 +1,25 @@
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <libdietc.h>
+
+int main(int argc, char **argv) {
+ assert(argc == 2);
+ struct program program = libdietc_parse(argv[1]);
+
+ for (struct function *f = program.functions; f; f = f->next) {
+ for (struct instruction *i = f->instructions; i; i = i->next) {
+ if (strncmp(i->line, "\tType_", strlen("\tType_"))) continue;
+ char *name = libdietc_tokdup(strchrnul(i->line, ' ') + 1);
+ struct instruction *n = calloc(1, sizeof(*n));
+ n->line = malloc(strlen(name) + 512);
+ sprintf(n->line, "memset(&%s, 0, sizeof(%s));", name, name);
+ n->next = i->next;
+ i->next = n;
+ i = n;
+ }
+ }
+ libdietc_print(program);
+ return 0;
+}
diff --git a/c/examples/zero_init/test.c b/c/examples/zero_init/test.c
new file mode 100644
index 0000000..5db109a
--- /dev/null
+++ b/c/examples/zero_init/test.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+
+int set_xyz() {
+ int xyz = 1;
+ return xyz;
+}
+
+int foo() {
+ int xyz;
+ return xyz;
+}
+
+int main() {
+ printf("Foo return value: %d\n", foo());
+ set_xyz();
+ printf("Foo return value: %d\n", foo());
+ set_xyz();
+ printf("Foo return value: %d\n", foo());
+ return 0;
+}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback