From 1eb3f6790388c1e5b990408bb57604aad3f03f03 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Sat, 11 Aug 2018 21:18:53 +0430 Subject: update --- main.py | 20 +++++++++++++++----- resources/wasm.xml | 9 +++------ run.sh | 2 +- test/autowasm.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ text.py | 15 +++++++++++++++ 5 files changed, 85 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index 45ee906..695599d 100755 --- a/main.py +++ b/main.py @@ -307,8 +307,10 @@ class CodeGen(object): if size > 0: read_size_replacement = str(size) if size == -1: - ref_size = "dummy->" + get_node_name(child.attrib["size"][6:], elem) - + if "delimiter" in child.attrib: + ref_size = "" + else: + ref_size = "dummy->" + get_node_name(child.attrib["size"][6:], elem) if "conditional" in child.attrib: cond_name = get_node_name(child.attrib["condition"][6:], elem) for cond in child: @@ -393,9 +395,15 @@ class CodeGen(object): for_read = "dummy->" + child.attrib["name"] + array_subscript + "=" + get_encoding_read(child.attrib["encoding"]) else: if child.attrib["type"] == "string": - for_read = "dummy->" + child.attrib["name"] + " = " + "malloc(" + ref_size + "+1);\n" - for_read += "dummy->" + child.attrib["name"] + "["+ref_size+"]=" + "0;\n" - for_read += text.c_read_gen_2_no.replace("XXX", "dummy" + "->"+ child.attrib["name"] + array_subscript).replace("YYY", ref_size) + if "delimiter" in child.attrib: + delimiter = child.attrib["delimiter"] + for_read = "int32_t " + child.attrib["name"] + "_del_pos =" + text.c_read_until_delimiter_proto.replace("XXX", delimiter) + ";\n" + for_read += "dummy->" + child.attrib["name"] + "=" + "malloc(" + child.attrib["name"] + "_del_pos);\n" + for_read += text.c_read_gen_2_no.replace("XXX", "dummy" + "->"+ child.attrib["name"] + array_subscript).replace("YYY", child.attrib["name"]+"_del_pos") + else: + for_read = "dummy->" + child.attrib["name"] + " = " + "malloc(" + ref_size + "+1);\n" + for_read += "dummy->" + child.attrib["name"] + "["+ref_size+"]=" + "0;\n" + for_read += text.c_read_gen_2_no.replace("XXX", "dummy" + "->"+ child.attrib["name"] + array_subscript).replace("YYY", ref_size) else: for_read = text.c_read_gen_2.replace("XXX", "dummy" + "->"+ child.attrib["name"] + array_subscript).replace("YYY", ref_size) else: @@ -637,8 +645,10 @@ class CodeGen(object): struct_source.write(text.header_inttype) struct_source_c.write(text.c_read_leb_u_def + "\n") struct_source_c.write(text.c_read_leb_s_def + "\n") + struct_source_c.write(text.c_read_until_delimiter + "\n") struct_source.write(text.c_read_leb_128_u_sig + "\n") struct_source.write(text.c_read_leb_128_s_sig + "\n") + struct_source.write(text.c_read_until_delimiter_sig + "\n") #struct_source.write(text.c_read_leb_macro_defs + "\n") if self.argparser.args.structsinclude: copy(self.argparser.args.structsinclude, self.argparser.args.outdir) diff --git a/resources/wasm.xml b/resources/wasm.xml index 40bd7d4..dfae936 100644 --- a/resources/wasm.xml +++ b/resources/wasm.xml @@ -40,9 +40,7 @@ - + @@ -102,8 +100,7 @@ - - + @@ -159,7 +156,7 @@ - + diff --git a/run.sh b/run.sh index d95979d..da34fd7 100755 --- a/run.sh +++ b/run.sh @@ -1,5 +1,5 @@ #!/bin/sh cd $(dirname $0) "./faultreiber.py" --targetname autowasm --outdir ./test/ --structs ./test/struct.json --datetime --structsinclude ./resources/structsinclude.h --xml ./resources/wasm.xml -"clang-format" -i ./test/read.c ./test/structs.h ./test/aggregate.c ./test/aggregate.h ./test/read.h +"clang-format" -i ./test/read.c ./test/structs.c ./test/structs.h ./test/aggregate.c ./test/aggregate.h ./test/read.h #"less" ./test/structs.h diff --git a/test/autowasm.c b/test/autowasm.c index 460856d..4054f48 100644 --- a/test/autowasm.c +++ b/test/autowasm.c @@ -76,6 +76,57 @@ int main (int argc, char** argv) { printf("index:%d\n", W_Export_Section_container->entries[i]->index); } + if (W_Start_Section_container == NULL) printf("start section doesnt exist.\n"); + + printf("element_seciton_id:%d\n", W_Element_Section_container->id); + printf("element_section_payloadlength:%d\n", W_Element_Section_container->payloadlength); + printf("entry count:%d\n", W_Element_Section_container->count); + + for (int i = 0; i < W_Element_Section_container->count; ++i) { + printf("index:%d\n", W_Element_Section_container->entries[i]->index); + for (int j = 0; j < 3; ++j) { + printf("code:%d\n", W_Element_Section_container->entries[i]->init->code[j]); + } + printf("num_length:%d\n", W_Element_Section_container->entries[i]->num_length); + for (int j = 0; j < W_Element_Section_container->entries[i]->num_length; ++j) { + printf("elems:%d\n", W_Element_Section_container->entries[i]->elems[j]); + } + } + + printf("code_section_id:%d\n", W_Code_Section_container->id); + printf("code_section_payloadlength:%d\n", W_Code_Section_container->payloadlength); + printf("count:%d\n", W_Code_Section_container->count); + + for (int i = 0; i < W_Code_Section_container->count; ++i) { + printf("body_size:%d\n", W_Code_Section_container->bodies[i]->body_size); + printf("local_count:%d\n", W_Code_Section_container->bodies[i]->local_count); + if (W_Code_Section_container->bodies[i]->local_count > 0) { + for (int j =0; j < W_Code_Section_container->bodies[i]->local_count; ++j) { + for (int k = 0; k < W_Code_Section_container->bodies[i]->locals[j]->count; ++k) { + } + } + } + printf("code:\n"); + for (int j = 0; j < W_Code_Section_container->bodies[i]->body_size; ++j) { + printf("%02x ", W_Code_Section_container->bodies[i]->code[j]); + } + printf("\n"); + } + + printf("data_section_id:%d\n", W_Data_Section_container->id); + printf("data_section_payloadlength:%d\n", W_Data_Section_container->payloadlength); + printf("data seg count:%d\n", W_Data_Section_container->count); + + for (int i = 0; i < W_Data_Section_container->count; ++i) { + printf("index:%d\n", W_Data_Section_container->entries[i]->index); + printf("size:%d\n", W_Data_Section_container->entries[i]->size); + printf("code:\n"); + for (int j = 0; j < W_Data_Section_container->entries[i]->size; ++j) { + printf("%c ", W_Data_Section_container->entries[i]->data[j]); + } + printf("\n"); + } + release_all(); return 0; } diff --git a/text.py b/text.py index 2562311..d70cb09 100644 --- a/text.py +++ b/text.py @@ -69,8 +69,23 @@ int64_t read_leb_128_s(int _fd, int max_size) { if ((last_byte & 0x40) != 0) result |= -(1 << shift); return result; }""" + + c_read_until_delimiter = """ +int32_t read_until_delimiter(int _fd, uint8_t delimiter) { + uint8_t dummy = 0; + int32_t pos = 0; + while(1) { + read(_fd, &dummy, 1); + pos++; + if (dummy == delimiter) { + lseek(_fd, -pos, SEEK_CUR); + return pos; + }} +}""" c_read_leb_128_s_sig = "int64_t read_leb_128_s(int _fd, int max_size);\n" c_read_leb_128_u_sig = "uint64_t read_leb_128_u(int _fd, int max_size);\n" + c_read_until_delimiter_sig = "int32_t read_until_delimiter(int _fd, uint8_t delimiter);\n" + c_read_until_delimiter_proto = "read_until_delimiter(_fd, XXX)" c_read_leb_macro_defs = """ #define READ_VAR_UINT_1(FD) read_leb128_u(FD, 1) -- cgit v1.2.3