Spawn functions

This commit is contained in:
Jeremiah Orians 2021-11-23 05:58:45 -05:00
parent 9de58d6d9c
commit 668c5b7c4c
No known key found for this signature in database
GPG Key ID: 6B3A3F198708F894
5 changed files with 150 additions and 37 deletions

4
cc.c
View File

@ -42,6 +42,7 @@ int main(int argc, char** argv, char** envp)
FUZZING = FALSE; FUZZING = FALSE;
MAX_STRING = 4096; MAX_STRING = 4096;
PREPROCESSOR_MODE = FALSE; PREPROCESSOR_MODE = FALSE;
STDIO_USED = FALSE;
int debug_flag = TRUE; int debug_flag = TRUE;
FILE* in = stdin; FILE* in = stdin;
FILE* tempfile; FILE* tempfile;
@ -92,7 +93,8 @@ int main(int argc, char** argv, char** envp)
} }
else if(match(argv[i], "-o") || match(argv[i], "--output")) else if(match(argv[i], "-o") || match(argv[i], "--output"))
{ {
destination_file = fopen(argv[i + 1], "w"); destination_name = argv[i + 1];
destination_file = fopen(destination_name, "w");
if(NULL == destination_file) if(NULL == destination_file)
{ {
fputs("Unable to open for writing file: ", stderr); fputs("Unable to open for writing file: ", stderr);

View File

@ -22,9 +22,6 @@
void init_macro_env(char* sym, char* value, char* source, int num); void init_macro_env(char* sym, char* value, char* source, int num);
char* env_lookup(char* variable); char* env_lookup(char* variable);
char* Architecture;
int WORDSIZE;
void setup_env() void setup_env()
{ {
char* ARCH = NULL; char* ARCH = NULL;
@ -45,32 +42,47 @@ void setup_env()
/* Set desired architecture */ /* Set desired architecture */
WORDSIZE = 32; WORDSIZE = 32;
if(match("knight-native", ARCH)) Architecture = "knight-native"; ENDIAN = FALSE;
else if(match("knight-posix", ARCH)) Architecture = "knight-posix"; BASEADDRESS = "0x0";
if(match("knight-native", ARCH))
{
ENDIAN = TRUE;
Architecture = "knight-native";
}
else if(match("knight-posix", ARCH))
{
ENDIAN = TRUE;
Architecture = "knight-posix";
}
else if(match("x86", ARCH)) else if(match("x86", ARCH))
{ {
BASEADDRESS = "0x8048000";
Architecture = "x86"; Architecture = "x86";
init_macro_env("__i386__", "1", "--architecture", 0); init_macro_env("__i386__", "1", "--architecture", 0);
} }
else if(match("amd64", ARCH)) else if(match("amd64", ARCH))
{ {
BASEADDRESS = "0x00600000";
Architecture = "amd64"; Architecture = "amd64";
WORDSIZE = 64; WORDSIZE = 64;
init_macro_env("__x86_64__", "1", "--architecture", 0); init_macro_env("__x86_64__", "1", "--architecture", 0);
} }
else if(match("armv7l", ARCH)) else if(match("armv7l", ARCH))
{ {
BASEADDRESS = "0x10000";
Architecture = "armv7l"; Architecture = "armv7l";
init_macro_env("__arm__", "1", "--architecture", 0); init_macro_env("__arm__", "1", "--architecture", 0);
} }
else if(match("aarch64", ARCH)) else if(match("aarch64", ARCH))
{ {
BASEADDRESS = "0x400000";
Architecture = "aarch64"; Architecture = "aarch64";
WORDSIZE = 64; WORDSIZE = 64;
init_macro_env("__aarch64__", "1", "--architecture", 0); init_macro_env("__aarch64__", "1", "--architecture", 0);
} }
else if(match("riscv64", ARCH)) else if(match("riscv64", ARCH))
{ {
BASEADDRESS = "0x600000";
Architecture = "riscv64"; Architecture = "riscv64";
WORDSIZE = 64; WORDSIZE = 64;
init_macro_env("__riscv", "1", "--architecture", 0); init_macro_env("__riscv", "1", "--architecture", 0);

View File

@ -39,6 +39,11 @@ int PREPROCESSOR_MODE;
/* enable spawn behavior to be effective */ /* enable spawn behavior to be effective */
char* M2LIBC_PATH; char* M2LIBC_PATH;
char* Architecture;
int WORDSIZE;
int ENDIAN;
char* BASEADDRESS;
int STDIO_USED;
/* So we don't shoot ourself in the face */ /* So we don't shoot ourself in the face */
int FUZZING; int FUZZING;

View File

@ -42,6 +42,9 @@ extern int PREPROCESSOR_MODE;
extern char* M2LIBC_PATH; extern char* M2LIBC_PATH;
extern char* Architecture; extern char* Architecture;
extern int WORDSIZE; extern int WORDSIZE;
extern int ENDIAN;
extern char* BASEADDRESS;
extern int STDIO_USED;
/* So we don't shoot ourself in the face */ /* So we don't shoot ourself in the face */
extern int FUZZING; extern int FUZZING;

View File

@ -120,6 +120,20 @@ char* find_executable(char* name)
return NULL; return NULL;
} }
void sanity_command_check(char** array)
{
int i = 0;
char* s = array[0];
while(NULL != s)
{
fputs(s, stderr);
fputc(' ', stderr);
i = i + 1;
s = array[i];
}
fputc('\n', stderr);
}
int _execute(char* name, char** array, char** envp) int _execute(char* name, char** array, char** envp)
{ {
int status; /* i.e. return code */ int status; /* i.e. return code */
@ -135,6 +149,8 @@ int _execute(char* name, char** array, char** envp)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
sanity_command_check(array);
int f = fork(); int f = fork();
/* Ensure fork succeeded */ /* Ensure fork succeeded */
@ -186,15 +202,42 @@ void insert_array(char** array, int index, char* string)
int spawn_hex2(char* input, char* output, char* architecture, char** envp, int debug) int spawn_hex2(char* input, char* output, char* architecture, char** envp, int debug)
{ {
/* TODO FINISH */ char* elf_header = calloc(MAX_STRING, sizeof(char));
elf_header = strcat(elf_header, M2LIBC_PATH);
elf_header = strcat(elf_header, "/");
elf_header = strcat(elf_header, architecture);
elf_header = strcat(elf_header, "/ELF-");
elf_header = strcat(elf_header, architecture);
if(debug)
{
elf_header = strcat(elf_header, "-debug.hex2");
}
else
{
elf_header = strcat(elf_header, ".hex2");
}
fputs("# starting hex2 linking\n", stdout);
char** array = calloc(MAX_ARRAY, sizeof(char*)); char** array = calloc(MAX_ARRAY, sizeof(char*));
insert_array(array, 0, "hex2"); insert_array(array, 0, "hex2");
insert_array(array, 1, "--file"); insert_array(array, 1, "--file");
insert_array(array, 2, input); insert_array(array, 2, elf_header);
insert_array(array, 3, "--output"); insert_array(array, 3, "--file");
insert_array(array, 4, output); insert_array(array, 4, input);
insert_array(array, 5, "--architecture"); insert_array(array, 5, "--output");
insert_array(array, 6, architecture); insert_array(array, 6, output);
insert_array(array, 7, "--architecture");
insert_array(array, 8, architecture);
insert_array(array, 9, "--base-address");
insert_array(array, 10, BASEADDRESS);
if(ENDIAN)
{
insert_array(array, 11, "--big-endian");
}
else
{
insert_array(array, 11, "--little-endian");
}
int r = _execute("hex2", array, envp); int r = _execute("hex2", array, envp);
return r; return r;
} }
@ -202,48 +245,89 @@ int spawn_hex2(char* input, char* output, char* architecture, char** envp, int d
int spawn_M1(char* input, char* debug_file, char* output, char* architecture, char** envp, int debug_flag) int spawn_M1(char* input, char* debug_file, char* output, char* architecture, char** envp, int debug_flag)
{ {
char** array = calloc(MAX_ARRAY, sizeof(char*)); fputs("# starting M1 assembly\n", stdout);
insert_array(array, 0, "M1"); char* definitions = calloc(MAX_STRING, sizeof(char));
insert_array(array, 1, "--file"); definitions = strcat(definitions, M2LIBC_PATH);
insert_array(array, 2, input); definitions = strcat(definitions, "/");
if(debug_flag) definitions = strcat(definitions, architecture);
definitions = strcat(definitions, "/");
definitions = strcat(definitions, architecture);
definitions = strcat(definitions, "_defs.M1");
char* libc = calloc(MAX_STRING, sizeof(char));
libc = strcat(libc, M2LIBC_PATH);
libc = strcat(libc, "/");
libc = strcat(libc, architecture);
if(STDIO_USED)
{ {
insert_array(array, 3, "--file"); libc = strcat(libc, "/libc-full.M1");
insert_array(array, 4, debug_file);
insert_array(array, 5, "--output");
insert_array(array, 6, output);
insert_array(array, 7, "--architecture");
insert_array(array, 8, architecture);
} }
else else
{ {
insert_array(array, 3, "--output"); libc = strcat(libc, "/libc-core.M1");
insert_array(array, 4, output); }
insert_array(array, 5, "--architecture");
insert_array(array, 6, architecture); char** array = calloc(MAX_ARRAY, sizeof(char*));
insert_array(array, 0, "M1");
insert_array(array, 1, "--file");
insert_array(array, 2, definitions);
insert_array(array, 3, "--file");
insert_array(array, 4, libc);
insert_array(array, 5, "--file");
insert_array(array, 6, input);
if(ENDIAN)
{
insert_array(array, 7, "--big-endian");
}
else
{
insert_array(array, 7, "--little-endian");
}
insert_array(array, 8, "--architecture");
insert_array(array, 9, architecture);
if(debug_flag)
{
insert_array(array, 10, "--file");
insert_array(array, 11, debug_file);
insert_array(array, 12, "--output");
insert_array(array, 13, output);
}
else
{
insert_array(array, 10, "--output");
insert_array(array, 11, output);
} }
int r = _execute("M1", array, envp); int r = _execute("M1", array, envp);
return r; return r;
} }
int spawn_blood_elf(char* input, char* output, char* architecture, char** envp, int large_flag) int spawn_blood_elf(char* input, char* output, char** envp, int large_flag)
{ {
fputs("# starting Blood-elf stub generation\n", stdout);
char** array = calloc(MAX_ARRAY, sizeof(char*)); char** array = calloc(MAX_ARRAY, sizeof(char*));
insert_array(array, 0, "blood-elf"); insert_array(array, 0, "blood-elf");
insert_array(array, 1, "--file"); insert_array(array, 1, "--file");
insert_array(array, 2, input); insert_array(array, 2, input);
insert_array(array, 3, "--output"); if(ENDIAN)
insert_array(array, 4, output); {
insert_array(array, 5, "--architecture"); insert_array(array, 3, "--big-endian");
insert_array(array, 6, architecture); }
if(large_flag) insert_array(array, 7, "--64"); else
{
insert_array(array, 3, "--little-endian");
}
insert_array(array, 4, "--output");
insert_array(array, 5, output);
if(large_flag) insert_array(array, 6, "--64");
int r = _execute("blood-elf", array, envp); int r = _execute("blood-elf", array, envp);
return r; return r;
} }
int spawn_M2(char* input, char* output, char* architecture, char** envp, int debug_flag) int spawn_M2(char* input, char* output, char* architecture, char** envp, int debug_flag)
{ {
fputs("# starting M2-Planet build\n", stdout);
char** array = calloc(MAX_ARRAY, sizeof(char*)); char** array = calloc(MAX_ARRAY, sizeof(char*));
insert_array(array, 0, "M2-Planet"); insert_array(array, 0, "M2-Planet");
insert_array(array, 1, "--file"); insert_array(array, 1, "--file");
@ -267,7 +351,9 @@ void spawn_processes(int debug_flag, char* preprocessed_file, char* destination,
int i = mkstemp(M2_output); int i = mkstemp(M2_output);
if(-1 != i) if(-1 != i)
{ {
spawn_M2(preprocessed_file, M2_output, Architecture, envp, debug_flag); close(i);
i = spawn_M2(preprocessed_file, M2_output, Architecture, envp, debug_flag);
if(0 != i) exit(EXIT_FAILURE);
} }
else else
{ {
@ -283,7 +369,9 @@ void spawn_processes(int debug_flag, char* preprocessed_file, char* destination,
i = mkstemp(blood_output); i = mkstemp(blood_output);
if(-1 != i) if(-1 != i)
{ {
spawn_blood_elf(M2_output, blood_output, Architecture, envp, large_flag); close(i);
i = spawn_blood_elf(M2_output, blood_output, envp, large_flag);
if(0 != i) exit(EXIT_FAILURE);
} }
else else
{ {
@ -297,7 +385,9 @@ void spawn_processes(int debug_flag, char* preprocessed_file, char* destination,
i = mkstemp(M1_output); i = mkstemp(M1_output);
if(-1 != i) if(-1 != i)
{ {
spawn_M1(M2_output, blood_output, M1_output, Architecture, envp, debug_flag); close(i);
i = spawn_M1(M2_output, blood_output, M1_output, Architecture, envp, debug_flag);
if(0 != i) exit(EXIT_FAILURE);
} }
else else
{ {
@ -311,7 +401,8 @@ void spawn_processes(int debug_flag, char* preprocessed_file, char* destination,
if(!match("", blood_output)) remove(blood_output); if(!match("", blood_output)) remove(blood_output);
/* Build the final binary */ /* Build the final binary */
spawn_hex2(M1_output, destination, Architecture, envp, debug_flag); i = spawn_hex2(M1_output, destination, Architecture, envp, debug_flag);
if(0 != i) exit(EXIT_FAILURE);
/* clean up after ourselves*/ /* clean up after ourselves*/
remove(M1_output); remove(M1_output);