aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xmain.py6
-rwxr-xr-xrun.sh1
-rw-r--r--test/autowasm.c72
-rw-r--r--text.py47
4 files changed, 124 insertions, 2 deletions
diff --git a/main.py b/main.py
index 6e71d9a..e331e1c 100755
--- a/main.py
+++ b/main.py
@@ -286,11 +286,14 @@ class CodeGen(object):
if self.argparser.args.datetime: struct_source.write("// " + self.dnt + "\n")
struct_source.write(text.header_guard_begin.replace("XXX", "structs".upper()))
struct_source.write(text.header_inttype)
+ struct_source.write(text.c_read_leb_u_def + "\n")
+ struct_source.write(text.c_read_leb_s_def + "\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)
pos = self.argparser.args.structsinclude.rfind("/")
sub = self.argparser.args.structsinclude[pos+1:]
- struct_source.write('#include "' + sub + '"\n')
+ struct_source.write('#include "' + sub + '"\n\n')
for child in self.def_elems + self.read_elems:
struct_source.write("typedef struct {\n")
for childer in child:
@@ -308,7 +311,6 @@ class CodeGen(object):
struct_source.write(text.pragma_endif)
struct_source.write(text.last_comment)
-
def gen_struct_header(self):
struct_source = open(get_full_path(self.argparser.args.outdir, "structs.h"), "w")
struct_source_c = open(get_full_path(self.argparser.args.outdir, "structs.c"), "w")
diff --git a/run.sh b/run.sh
index 9f4aa0d..2266539 100755
--- a/run.sh
+++ b/run.sh
@@ -1,3 +1,4 @@
#!/bin/sh
cd $(dirname $0)
"./faultreiber.py" --targetname autowasm --outdir ./test/ --structs ./test/struct.json --datetime --structsinclude ./resources/structsinclude.h --xml ./resources/wasm.xml --inline
+"less" ./test/structs.h
diff --git a/test/autowasm.c b/test/autowasm.c
new file mode 100644
index 0000000..5151898
--- /dev/null
+++ b/test/autowasm.c
@@ -0,0 +1,72 @@
+
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+uint64_t read_leb128_u(int _fd, int max_size) {
+ uint8_t read_bytes = 0U;
+ uint8_t byte = 0;
+ uint64_t result = 0U;
+ uint32_t shift = 0U;
+ do {
+ read(_fd, &byte, 1);read_bytes++;read_bytes++;
+ result |= (byte & 0x7f) << shift;
+ shift += 7;
+ } while(((byte & 0x80) != 0) && (read_bytes < max_size));
+ return result;
+}
+
+int64_t read_leb128_s(int _fd, int max_size) {
+ uint8_t byte;
+ uint8_t read_bytes = 0U;
+ uint8_t last_byte;
+ int64_t result = 0;
+ uint32_t shift = 0U;
+ read(_fd, &byte, 1);
+ do {
+ read(_fd, &byte, 1);read_bytes++;
+ result |= (byte & 0x7f) << shift;
+ last_byte = byte;
+ shift += 7;
+ } while(((byte & 0x80) != 0) && read_bytes < max_size);
+ if ((last_byte & 0x40) != 0) result |= -(1 << shift);
+ return result;
+}
+
+#define READ_VAR_UINT_1(FD) read_leb128_u(FD, 1)
+#define READ_VAR_UINT_7(FD) read_leb128_u(FD, 1)
+#define READ_VAR_UINT_32(FD) read_leb128_u(FD, 5)
+#define READ_VAR_INT_1(FD) read_leb128_s(FD, 1)
+#define READ_VAR_INT_7(FD) read_leb128_s(FD, 1)
+#define READ_VAR_INT_32(FD) read_leb128_s(FD, 5)
+
+#pragma weak main
+int main (int argc, char** argv) {
+ int wasm = open("./test.wasm", O_RDONLY);
+ uint64_t test_u = 0U;
+ int64_t test_s = 0;
+ unsigned char test_byte;
+ unsigned char byte;
+ uint32_t word;
+ uint32_t counter = 0U;
+
+ read(wasm, &word, 8);
+ printf("test_byte:%08x\n", word);
+
+ lseek(wasm, 9, SEEK_SET);
+ read(wasm, &word, 8);
+ printf("test_byte:%08x\n", word);
+
+ lseek(wasm, 9, SEEK_SET);
+ test_u = READ_VAR_UINT_32(wasm);
+ printf("read u res is: %lu.\n", test_u);
+ lseek(wasm, 0, SEEK_SET);
+ while(read(wasm, &word, sizeof(uint32_t))) {
+ //printf("%d:%02x\t", counter, word);
+ counter++;
+ }
+ printf("\n");
+ return 0;
+}
diff --git a/text.py b/text.py
index 6a46c39..0de2940 100644
--- a/text.py
+++ b/text.py
@@ -29,3 +29,50 @@ class text():
c_read_gen_2 = "read(_fd, &XXX, YYY);\n"
c_assign_struct = "XXX.YYY = ZZZ;\n"
simple_loop = "for (int i = 0; i < XXX; ++i) {\nYYY}\n"
+ c_read_leb_u_def = """
+uint64_t read_leb128_u(int _fd, int max_size) {
+ uint8_t read_bytes = 0U;
+ uint8_t byte = 0;
+ uint64_t result = 0U;
+ uint32_t shift = 0U;
+ do {
+ read(_fd, &byte, 1);read_bytes++;read_bytes++;
+ result |= (byte & 0x7f) << shift;
+ shift += 7;
+ } while(((byte & 0x80) != 0) && (read_bytes < max_size));
+ return result;
+}"""
+
+ c_read_leb_s_def = """
+int64_t read_leb128_s(int _fd, int max_size) {
+ uint8_t byte;
+ uint8_t read_bytes = 0U;
+ uint8_t last_byte;
+ int64_t result = 0;
+ uint32_t shift = 0U;
+ read(_fd, &byte, 1);
+ do {
+ read(_fd, &byte, 1);read_bytes++;
+ result |= (byte & 0x7f) << shift;
+ last_byte = byte;
+ shift += 7;
+ } while(((byte & 0x80) != 0) && read_bytes < max_size);
+ if ((last_byte & 0x40) != 0) result |= -(1 << shift);
+ return result;
+}"""
+
+ c_read_leb_macro_defs = """
+#define READ_VAR_UINT_1(FD) read_leb128_u(FD, 1)
+#define READ_VAR_UINT_7(FD) read_leb128_u(FD, 1)
+#define READ_VAR_UINT_32(FD) read_leb128_u(FD, 5)
+#define READ_VAR_INT_1(FD) read_leb128_s(FD, 1)
+#define READ_VAR_INT_7(FD) read_leb128_s(FD, 1)
+"""
+
+ c_read_leb_macro_varuin1 = "READ_VAR_UINT_1(XXX)"
+ c_read_leb_macro_varuin7 = "READ_VAR_UINT_7(XXX)"
+ c_read_leb_macro_varuin32 = "READ_VAR_UINT_32(XXX)"
+ c_read_leb_macro_varin1 = "READ_VAR_INT_1(XXX)"
+ c_read_leb_macro_varin7 = "READ_VAR_INT_7(XXX)"
+ c_read_leb_macro_varin32 = "READ_VAR_INT_32(XXX)"
+