diff options
-rwxr-xr-x | main.py | 20 | ||||
-rw-r--r-- | resources/wasm.xml | 9 | ||||
-rwxr-xr-x | run.sh | 2 | ||||
-rw-r--r-- | test/autowasm.c | 51 | ||||
-rw-r--r-- | text.py | 15 |
5 files changed, 85 insertions, 12 deletions
@@ -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 @@ <PayloadLength name="payloadlength" type="uint32" encoding="leb128u"/> <NameLength name="namelength" type="uint32" encoding="leb128u"/> <Name name="name" type="string" size="self::NameLength"/> - <!-- - <Count name="count" encoding="leb128u" type="uint32" count="1"/> - --> + <!--<Count name="count" encoding="leb128u" type="uint32" count="1"/>--> <Entries name="entries" type="self::Memory_Type" count="1"/> </Memory_Section> <Global_Section name="W_Global_Section" count="1" type="" isaggregate="true" unordered="true"> @@ -102,8 +100,7 @@ </Read> <Definition> <Init_Expr name="init_expr_t" isaggregate="true"> - <Size name="size" encoding="leb128u" type="varuint32" count="1"></Size> - <Code name="code" type="string" count="1" size="self::Size"></Code> + <Code name="code" type="string" count="1" size="-1" delimiter="11"></Code> </Init_Expr> <Resizable_Limit name="resizable_limit_t" isaggregate="true"> <Flags name="flags" encoding="leb128u" type="uint8" count="1"></Flags> @@ -159,7 +156,7 @@ </Element_Segment> <Local_Entry name="W_Local_Entry" isaggregate="true"> <Count name="count" encoding="leb128u" type="uint32" count="1"/> - <Type name="type" encoding="leb128u" type="int16" count="self::Count"/> + <Type name="type" encoding="leb128u" type="uint16" count="self::Count"/> </Local_Entry> <Function_Body name="W_Function_Body" isaggregate="true"> <Body_size name="body_size" encoding="leb128u" type="uint32" count="1"/> @@ -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; } @@ -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) |