Commit 926c3265 authored by Julian Kranz's avatar Julian Kranz
Browse files

Liveness

parent 20b468ab
......@@ -17,7 +17,7 @@ clang-sweep:
clang -pipe -O3 -Wall -static -I. -I../../.. -Wfatal-errors sweep.c ../../../dis.o -DRELAXEDFATAL -o clang-sweep
liveness-sweep:
clang -pipe -O3 -Wall -static -I. -I../../.. -Wfatal-errors liveness-sweep.c ../../../dis.o -DRELAXEDFATAL -o liveness-sweep
clang -pipe -O3 -Wall -static -I. -I../../.. -Wfatal-errors liveness-sweep.c ../../../dis.o -lelf -DRELAXEDFATAL -o liveness-sweep
gcc-sweep:
gcc -pipe -O3 -Wall -static -I. -I../../.. -Wfatal-errors sweep.c ../../../dis.o -DRELAXEDFATAL -o gcc-sweep
......
/* vim:cindent:ts=2:sw=2:expandtab */
#include <stdio.h>
......@@ -7,89 +6,177 @@
#include <dis.h>
#include <sys/resource.h>
int main (int argc, char** argv) {
const rlim_t kStackSize = 512L * 1024L * 1024L; // min stack size = 64 Mb
struct rlimit rl;
int result;
result = getrlimit(RLIMIT_STACK, &rl);
if (result == 0)
{
if (rl.rlim_cur < kStackSize)
{
rl.rlim_cur = kStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result != 0)
{
fprintf(stderr, "setrlimit returned result = %d\n", result);
}
}
}
size_t size = 16*1024*1024;
#include <err.h>
#include <fcntl.h>
#include <gelf.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
char elf_section_boundary_get(char *path, size_t *offset, size_t *size) {
char retval = 0;
int fd = open(path, O_RDONLY);
if(!fd) {
retval = 8;
goto end_0;
}
if(elf_version(EV_CURRENT) == EV_NONE) {
retval = 2;
goto end_0;
}
Elf *e = elf_begin(fd, ELF_C_READ, NULL);
if(!e) {
retval = 3;
goto end_0;
}
if(elf_kind(e) != ELF_K_ELF) {
retval = 4;
goto end_1;
}
size_t shstrndx;
if(elf_getshdrstrndx(e, &shstrndx) != 0) {
retval = 5;
goto end_1;
}
Elf_Scn *scn = NULL;
char found = 0;
while((scn = elf_nextscn(e, scn)) != NULL) {
GElf_Shdr shdr;
if(gelf_getshdr(scn, &shdr) != &shdr) {
retval = 6;
goto end_1;
}
char *name = elf_strptr(e, shstrndx, shdr.sh_name);
if(!name) {
retval = 7;
goto end_1;
}
if(!strcmp(name, ".text")) {
*offset = shdr.sh_offset;
*size = shdr.sh_size;
found = 1;
break;
}
// printf("%s - %zu:%zu\n", name, shdr.sh_offset, shdr.sh_size);
}
if(!found)
retval = 1;
end_1: elf_end(e);
end_0: close(fd);
return retval;
}
int main(int argc, char** argv) {
const rlim_t kStackSize = 1024L * 1024L * 1024L;
struct rlimit rl;
int result;
result = getrlimit(RLIMIT_STACK, &rl);
if(result == 0) {
if(rl.rlim_cur < kStackSize) {
rl.rlim_cur = kStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if(result != 0) {
fprintf(stderr, "setrlimit returned result = %d\n", result);
}
}
}
size_t size = 16 * 1024 * 1024;
char *fmt = (char*)malloc(size);
if(argc != 2) {
printf("Usage: liveness-sweep file\n");
return 1;
}
size_t offset;
size_t size_max;
if(argc == 3) {
if(strcmp(argv[1], "--elf"))
return 1;
char e = elf_section_boundary_get(argv[2], &offset, &size_max);
if(e)
return 2;
} else if(argc != 2) {
printf("Usage: liveness-sweep [--elf] file\n");
return 1;
} else {
offset = 0;
size_max = 0;
}
FILE *f = fopen(argv[1], "r");
if(!f) {
printf("Unable to open file.\n");
return 1;
}
FILE *f = fopen(argv[1 + (argc == 3)], "r");
if(!f) {
printf("Unable to open file.\n");
return 1;
}
fseek(f, offset, SEEK_SET);
size_t buffer_size = 128;
unsigned char *buffer = NULL;
size_t buffer_size = 128;
unsigned char *buffer = NULL;
size_t buffer_length = 0;
do {
buffer_size *= 2;
buffer = (unsigned char*)realloc(buffer, buffer_size);
buffer_length += fread(buffer + buffer_length, 1, buffer_size - buffer_length, f);
} while(!feof(f));
buffer_length += fread(buffer + buffer_length, 1,
buffer_size - buffer_length, f);
} while(!feof(f) && (!size_max || buffer_length < size_max));
fclose(f);
__obj state = __createState(buffer, buffer_length, 0, 0);
if(buffer_length > size_max)
buffer_length = size_max;
__obj rreil_instructions = __runMonadicNoArg(__translateBlock__, &state);
__obj state = __createState(buffer, buffer_length, 0, 0);
__obj rreil_instructions = __runMonadicNoArg(__translateBlock__, &state);
if(!__isNil(rreil_instructions)) {
__fatal("TranslateBlock failed");
__fatal("TranslateBlock failed");
goto end;
}
__obj greedy_state = __runMonadicOneArg(__liveness__, &state, rreil_instructions);
__obj greedy_state = __runMonadicOneArg(__liveness__, &state,
rreil_instructions);
if(!__isNil(greedy_state)) {
__fatal("Liveness failed");
__fatal("Liveness failed");
goto end;
}
__obj rreil_instructions_greedy = __RECORD_SELECT(state, ___live);
__obj rreil_instructions_greedy = __RECORD_SELECT(state, ___live);
if(!__isNil(rreil_instructions_greedy)) {
__fatal("Liveness failed (no greedy instructions)");
__fatal("Liveness failed (no greedy instructions)");
goto end;
}
printf("Liveness greedy state:\n");
__pretty(__lv_pretty__,greedy_state,fmt,size);
puts(fmt);
__pretty(__lv_pretty__, greedy_state, fmt, size);
puts(fmt);
printf("\n");
printf("Initial RREIL instructions:\n");
__pretty(__rreil_pretty__,rreil_instructions,fmt,size);
puts(fmt);
__pretty(__rreil_pretty__, rreil_instructions, fmt, size);
puts(fmt);
printf("\n");
size_t lines = 0;
for(size_t i = 0; fmt[i]; i++)
if(fmt[i] == '\n')
lines++;
printf("RREIL instructions after LV (greedy):\n");
__pretty(__rreil_pretty__,rreil_instructions_greedy,fmt,size);
puts(fmt);
__pretty(__rreil_pretty__, rreil_instructions_greedy, fmt, size);
puts(fmt);
printf("\n");
size_t lines_greedy = 0;
......@@ -100,16 +187,14 @@ int main (int argc, char** argv) {
printf("Statistics:\n");
printf("Number of lines without LV analysis: %zu\n", lines);
printf("Number of lines with LV analysis: %zu\n", lines_greedy);
double reduction = 1 - (lines_greedy/(double)lines);
printf("Reduction: %lf%%\n", 100*reduction);
double reduction = 1 - (lines_greedy / (double)lines);
printf("Reduction: %lf%%\n", 100 * reduction);
end:
free(buffer);
end: free(buffer);
free(fmt);
return (1);
return (1);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment