diff options
-rw-r--r-- | src/decompiler-naming.h | 44 | ||||
-rw-r--r-- | test/decompile/basic.txt | 10 | ||||
-rw-r--r-- | test/decompile/loadstore.txt | 4 |
3 files changed, 53 insertions, 5 deletions
diff --git a/src/decompiler-naming.h b/src/decompiler-naming.h index 786c3ebb..41e0e3cb 100644 --- a/src/decompiler-naming.h +++ b/src/decompiler-naming.h @@ -110,6 +110,47 @@ void RenameToIdentifiers(std::vector<T*>& things, BindingHash& bh, } } +enum { + // This a bit arbitrary, change at will. + min_content_identifier_size = 7, + max_content_identifier_size = 30 +}; + +void RenameToContents(std::vector<DataSegment*>& segs, BindingHash& bh) { + std::string s; + for (auto seg : segs) { + s = "d_"; + for (auto c : seg->data) { + if (isalnum(c) || c == '_') { + s += static_cast<char>(c); + } + if (s.size() >= max_content_identifier_size) { + // We truncate any very long names, since those make for hard to + // format output. They can be somewhat long though, since data segment + // references tend to not occur that often. + break; + } + } + if (s.size() < min_content_identifier_size) { + // It is useful to have a minimum, since if there few printable characters + // in a data section, that is probably a sign of binary, and those few + // characters are not going to be very significant. + continue; + } + // We could do the same disambiguition as RenameToIdentifier and + // GenerateNames do, but if we come up with a clashing name here it is + // likely a sign of not very meaningful binary data, so it is easier to + // just keep the original generated name in that case. + if (bh.count(s) != 0) { + continue; + } + // Remove original entry. + bh.erase(seg->name); + seg->name = s; + bh.emplace(s, Binding(static_cast<Index>(&seg - &segs[0]))); + } +} + // Function names may contain arbitrary C++ syntax, so we want to // filter those to look like identifiers. A function name may be set // by a name section (applied in ReadBinaryIr, called before this function) @@ -119,6 +160,7 @@ void RenameToIdentifiers(std::vector<T*>& things, BindingHash& bh, // this function). // To not have to add too many decompiler-specific code into those systems // (using a callback??) we instead rename everything here. +// Also do data section renaming here. void RenameAll(Module& module) { // We also filter common C++ keywords/STL idents that make for huge // identifiers. @@ -144,6 +186,8 @@ void RenameAll(Module& module) { // Also do this for some other kinds of names. RenameToIdentifiers(module.globals, module.global_bindings, nullptr); RenameToIdentifiers(module.tables, module.table_bindings, nullptr); + + RenameToContents(module.data_segments, module.data_segment_bindings); } } // namespace wabt diff --git a/test/decompile/basic.txt b/test/decompile/basic.txt index 7d6088ea..66c05323 100644 --- a/test/decompile/basic.txt +++ b/test/decompile/basic.txt @@ -16,6 +16,8 @@ (data 0 (offset (i32.const 0)) "Hello, World!\n\00") (data 0 (offset (i32.const 100)) "abcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyz") + (data 0 (offset (i32.const 200)) "hi") ;; Too short for data derived name + (data 0 (offset (i32.const 300)) "Hello, World!\n\00") ;; Duplicate. (func $f (param i32 i32) (result i32) (local i64 f32 f64) i64.const 8 @@ -119,12 +121,14 @@ import table ns_tab3:funcref; table T_b:funcref(min: 0, max: 10); export table tab2:funcref(min: 0, max: 11); -data d_a(offset: 0) = "Hello, World!\0a\00"; -data d_b(offset: 100) = +data d_HelloWorld(offset: 0) = "Hello, World!\0a\00"; +data d_abcdefghijklmnoqrstuvwxyzabc(offset: 100) = "abcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstu" "vwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmno" "qrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghij" "klmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoqrstuvwxyz"; +data d_c(offset: 200) = "hi"; +data d_d(offset: 300) = "Hello, World!\0a\00"; import function ns_fi(); @@ -132,7 +136,7 @@ export function f(a:int, b:int):int { var c:long = 8L; var d:float = 6.0f; var e:double = 7.0; - if (e < 10.0) { d_a[5@4]:int = d_a[5]:int@1 + 5 } + if (e < 10.0) { d_HelloWorld[5@4]:int = d_HelloWorld[5]:int@1 + 5 } f(a + g_b, 9); loop L_b { if (if (0) { 1 } else { 2 }) goto B_c; diff --git a/test/decompile/loadstore.txt b/test/decompile/loadstore.txt index 65334cec..c02700a1 100644 --- a/test/decompile/loadstore.txt +++ b/test/decompile/loadstore.txt @@ -108,7 +108,7 @@ (;; STDOUT ;;; memory M_a(initial: 1, max: 0); -data d_a(offset: 10) = "Hello, World!\0a\00"; +data d_HelloWorld(offset: 10) = "Hello, World!\0a\00"; export function f(a:{ a:float, b:float }, b:{ a:ushort, b:long }) { var c:{ a:float, b:float } @@ -129,7 +129,7 @@ export function f(a:{ a:float, b:float }, b:{ a:ushort, b:long }) { a[b]:int = a[b]:int; a[b + 1]:int = a[b + 1]:int; (a + (b << 3))[1]:int = a[b + 1]:int; - d_a[7]:ubyte; + d_HelloWorld[7]:ubyte; } ;;; STDOUT ;;) |