diff options
author | Sam Clegg <sbc@chromium.org> | 2016-11-18 16:20:14 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-18 16:20:14 -0800 |
commit | 3382c7f9835fef7a3effa81d97d79bb48269ea0f (patch) | |
tree | 2a10396cc476904a8bb2d6c0d994ca46a96c911d /src | |
parent | efc81f5be6ce999c46681d46dd169a845ad670a7 (diff) | |
download | wabt-3382c7f9835fef7a3effa81d97d79bb48269ea0f.tar.gz wabt-3382c7f9835fef7a3effa81d97d79bb48269ea0f.tar.bz2 wabt-3382c7f9835fef7a3effa81d97d79bb48269ea0f.zip |
Parse arbitrary user sections (#220)
Handle user sections interspersed with other section
types and add tests for these.
Switch from calling these sections "Unknown" to
"User".
Diffstat (limited to 'src')
-rw-r--r-- | src/binary-reader-objdump.c | 33 | ||||
-rw-r--r-- | src/binary-reader.c | 39 | ||||
-rw-r--r-- | src/binary-reader.h | 6 | ||||
-rw-r--r-- | src/binary-writer.c | 2 | ||||
-rw-r--r-- | src/binary.h | 2 |
5 files changed, 56 insertions, 26 deletions
diff --git a/src/binary-reader-objdump.c b/src/binary-reader-objdump.c index f00e0cc1..50141062 100644 --- a/src/binary-reader-objdump.c +++ b/src/binary-reader-objdump.c @@ -90,6 +90,17 @@ static WasmResult begin_section(Context* ctx, return WASM_OK; } +static WasmResult begin_user_section(WasmBinaryReaderContext* ctx, + uint32_t size, + WasmStringSlice section_name) { + Context* context = ctx->user_data; + if (begin_section(context, "USER", ctx->offset, size)) + return WASM_ERROR; + if (context->options->mode == WASM_DUMP_DETAILS) + printf(" - name: \"" PRIstringslice "\"\n", WASM_PRINTF_STRING_SLICE_ARG(section_name)); + return WASM_OK; +} + SEGSTART(signature, "TYPE") SEGSTART(import, "IMPORT") SEGSTART(function_signatures, "FUNCTION") @@ -101,7 +112,6 @@ SEGSTART(start, "START") SEGSTART(function_bodies, "CODE") SEGSTART(elem, "ELEM") SEGSTART(data, "DATA") -SEGSTART(names, "NAMES") static WasmResult on_count(uint32_t count, void* user_data) { Context* ctx = user_data; @@ -365,7 +375,7 @@ static WasmResult on_import(uint32_t index, static WasmResult on_import_func(uint32_t index, uint32_t sig_index, void* user_data) { - print_details(user_data, " - func sig=%d\n", sig_index); + print_details(user_data, " - func sig=%d\n", sig_index); return WASM_OK; } @@ -374,7 +384,7 @@ static WasmResult on_import_table(uint32_t index, const WasmLimits* elem_limits, void* user_data) { print_details(user_data, - " - table elem_type=%s init=%" PRId64 " max=%" PRId64 "\n", + " - table elem_type=%s init=%" PRId64 " max=%" PRId64 "\n", wasm_get_type_name(elem_type), elem_limits->initial, elem_limits->max); @@ -384,7 +394,7 @@ static WasmResult on_import_table(uint32_t index, static WasmResult on_import_memory(uint32_t index, const WasmLimits* page_limits, void* user_data) { - print_details(user_data, " - memory\n"); + print_details(user_data, " - memory\n"); return WASM_OK; } @@ -392,14 +402,14 @@ static WasmResult on_import_global(uint32_t index, WasmType type, WasmBool mutable_, void* user_data) { - print_details(user_data, "- global\n"); + print_details(user_data, " - global\n"); return WASM_OK; } static WasmResult on_memory(uint32_t index, const WasmLimits* page_limits, void* user_data) { - print_details(user_data, "- memory %d\n", index); + print_details(user_data, " - memory %d\n", index); return WASM_OK; } @@ -408,7 +418,7 @@ static WasmResult on_table(uint32_t index, const WasmLimits* elem_limits, void* user_data) { print_details(user_data, - " - [%d] type=%#x init=%" PRId64 " max=%" PRId64 " \n", + " - [%d] type=%#x init=%" PRId64 " max=%" PRId64 " \n", index, elem_type, elem_limits->initial, @@ -487,7 +497,10 @@ static WasmBinaryReader s_binary_reader = { .end_module = end_module, .on_error = on_error, - // Signature sections + // User section + .begin_user_section = begin_user_section, + + // Signature section .begin_signature_section = begin_signature_section, .on_signature_count = on_count, .on_signature = on_signature, @@ -543,8 +556,8 @@ static WasmBinaryReader s_binary_reader = { .begin_data_section = begin_data_section, .on_data_segment_count = on_count, - // Names section - .begin_names_section = begin_names_section, + // Known "User" sections: + // - Names section .on_function_names_count = on_count, .on_init_expr_i32_const_expr = on_init_expr_i32_const_expr, diff --git a/src/binary-reader.c b/src/binary-reader.c index 9e148ce5..8edec68e 100644 --- a/src/binary-reader.c +++ b/src/binary-reader.c @@ -410,9 +410,10 @@ static uint32_t num_total_globals(Context* ctx) { return ctx->num_global_imports + ctx->num_globals; } -static WasmBool handle_unknown_section(Context* ctx, - WasmStringSlice* section_name, - uint32_t section_size) { +static void handle_user_section(Context* ctx, + WasmStringSlice* section_name, + uint32_t section_size) { + CALLBACK_CTX(begin_user_section, section_size, *section_name); if (ctx->options->read_debug_names && ctx->name_section_ok && strncmp(section_name->start, WASM_BINARY_SECTION_NAME, section_name->length) == 0) { @@ -438,10 +439,8 @@ static WasmBool handle_unknown_section(Context* ctx, } } CALLBACK0(end_names_section); - return WASM_TRUE; } - - return WASM_FALSE; + CALLBACK_CTX0(end_user_section); } static WasmBool skip_until_section(Context* ctx, @@ -465,15 +464,12 @@ static WasmBool skip_until_section(Context* ctx, if (ctx->section_end > ctx->data_size) RAISE_ERROR("invalid section size: extends past end"); - if (section_code == WASM_BINARY_SECTION_UNKNOWN) { + if (section_code == WASM_BINARY_SECTION_USER) { WasmStringSlice section_name; in_str(ctx, §ion_name, "section name"); - if (!handle_unknown_section(ctx, §ion_name, *section_size)) { - /* section wasn't handled, so skip it and try again. */ - ctx->offset = ctx->section_end; - return skip_until_section(ctx, expected_code, section_size); - } - return WASM_FALSE; + handle_user_section(ctx, §ion_name, *section_size); + ctx->offset = ctx->section_end; + return skip_until_section(ctx, expected_code, section_size); } else { /* section is known, check if it is valid. */ if (section_code >= WASM_NUM_BINARY_SECTIONS) { @@ -509,6 +505,7 @@ static void dedent(LoggingContext* ctx) { ctx->indent -= INDENT_SIZE; assert(ctx->indent >= 0); } + static void write_indent(LoggingContext* ctx) { static char s_indent[] = " " @@ -542,6 +539,16 @@ static void logging_on_error(WasmBinaryReaderContext* ctx, } } +static WasmResult logging_begin_user_section(WasmBinaryReaderContext* context, + uint32_t size, + WasmStringSlice section_name) { + LoggingContext* ctx = context->user_data; + LOGF("begin_user_section: '" PRIstringslice "' size=%d\n", + WASM_PRINTF_STRING_SLICE_ARG(section_name), size); + indent(ctx); + FORWARD_CTX(begin_user_section, size, section_name); +} + #define LOGGING_BEGIN(name) \ static WasmResult logging_begin_##name(WasmBinaryReaderContext* context, \ uint32_t size) { \ @@ -597,6 +604,7 @@ static void logging_on_error(WasmBinaryReaderContext* ctx, LOGGING_UINT32(begin_module) LOGGING0(end_module) +LOGGING_END(user_section) LOGGING_BEGIN(signature_section) LOGGING_UINT32(on_signature_count) LOGGING_END(signature_section) @@ -1016,6 +1024,9 @@ static WasmBinaryReader s_logging_binary_reader = { .begin_module = logging_begin_module, .end_module = logging_end_module, + .begin_user_section = logging_begin_user_section, + .end_user_section = logging_end_user_section, + .begin_signature_section = logging_begin_signature_section, .on_signature_count = logging_on_signature_count, .on_signature = logging_on_signature, @@ -2050,7 +2061,7 @@ WasmResult wasm_read_binary(WasmAllocator* allocator, } /* Handle supported unnamed sections at the end. */ - skip_until_section(ctx, WASM_BINARY_SECTION_UNKNOWN, §ion_size); + skip_until_section(ctx, WASM_BINARY_SECTION_USER, §ion_size); CALLBACK0(end_module); destroy_context(allocator, ctx); diff --git a/src/binary-reader.h b/src/binary-reader.h index feef44f5..617dec4c 100644 --- a/src/binary-reader.h +++ b/src/binary-reader.h @@ -49,6 +49,12 @@ typedef struct WasmBinaryReader { WasmResult (*begin_module)(uint32_t version, void* user_data); WasmResult (*end_module)(void* user_data); + /* user section */ + WasmResult (*begin_user_section)(WasmBinaryReaderContext* ctx, + uint32_t size, + WasmStringSlice section_name); + WasmResult (*end_user_section)(WasmBinaryReaderContext* ctx); + /* signatures section */ /* TODO(binji): rename to "type" section */ WasmResult (*begin_signature_section)(WasmBinaryReaderContext* ctx, diff --git a/src/binary-writer.c b/src/binary-writer.c index b9d9f27f..d64dc734 100644 --- a/src/binary-writer.c +++ b/src/binary-writer.c @@ -243,7 +243,7 @@ static void begin_user_section(Context* ctx, char desc[100]; wasm_snprintf(desc, sizeof(desc), "section \"%s\"", name); write_header(ctx, desc, PRINT_HEADER_NO_INDEX); - wasm_write_u8(&ctx->stream, WASM_BINARY_SECTION_UNKNOWN, "user section code"); + wasm_write_u8(&ctx->stream, WASM_BINARY_SECTION_USER, "user section code"); ctx->last_section_leb_size_guess = leb_size_guess; ctx->last_section_offset = write_u32_leb128_space(ctx, leb_size_guess, "section size (guess)"); diff --git a/src/binary.h b/src/binary.h index ff42b4fe..3d051837 100644 --- a/src/binary.h +++ b/src/binary.h @@ -24,7 +24,7 @@ #define WASM_BINARY_SECTION_NAME "name" #define WASM_FOREACH_BINARY_SECTION(V) \ - V(UNKNOWN, 0) \ + V(USER, 0) \ V(TYPE, 1) \ V(IMPORT, 2) \ V(FUNCTION, 3) \ |