rizin/test/db/cmd/cmd_string
Rot127 699c7d2dff
Improve performance of string search. (#5262)
* Make checks against defined Unicode points optional

* Allow to decode UTF-16 without writing the result.

* Remove PCRE2_NO_UTF_CHECK as default, since it can lead to undefined behavior.

* First refactor regex to support utf16 and utf32

* Add UTF-16-BE encoding function

* Add UTF-8 counting helper functions.

- One for counting the number of Unicode code points.
- The other to get the number of bytes required to represent the given UTF-8 string in UTF-16.

* Remove unused code

* Add UTF-8 to UTF-16 conversion function.

* Add type annotations

* Implement utf8 to utf32 string conversion

* Add UTF-16/32 versions of all other necessary regex functions for str search.

* Another regex refactor for utf16/32

* Add UTF16/32 regex matching tests.

* Implement still segfaulting (possibly JIT double usage) regex search.

* Duplicate match_first functions to reduce necessary branch predictions.

* Reduce number of required branches for encoding UTF16/32 to one.

* Duplicate match_all_internal functions to reduce necessary branch predictions.

* Fix too early free

* Only allocate match vector when needed.

* Fix: use code point size of buffer.

* Add missing return

* Normalize pointers to UTF16/32 strings to use proper code point with

* Also replace spaces with in utf16/32

* Add an additional host endian tests for UTF16/32 string encodings.

* Ensure thread savety.

JIT compiled patterns need to be owned by a single thread.
For the search we need to clone it.

JIT matching structures are optionally cloned as well.

* Enforce NO_UTF_CHECK in regex search.

This improves performance and currently is
default because we always match on binary data.

* Fix matching of UTF strings which are not suported by direct buffer matching.

PCRE2 only supports matching against memory which is aligned
to a code point width of the encoding.

These changes prevent taking the fast (direct matching with PCRE2) path
and use the slow string search path if the alignment doesn't match
the UTF string encoding.

To not complicate the change and additional alignment member
is added to each searched string in the search collection.

* Create search hit description on the stack

* Unset complete JIT matching if user provided custom jflags.

* Document what passing NULL to copy function pointers does.

* Replace the retarded idea of tracking offsets with a hashmap with a linear buffer.

This improves performance something like 10x.

* Remove const for the non-JIT builds.

* Remove additional flags for skip checking.

It is not needed because each decoded character is checked for printablity below anyways.

* Enfore no setup of IO mem with 0xff

* Fix: Set JIT complete flag for multi regex patterns

* Fix heap.

* Add note about worsed performance path.

* Fix unit test with string terminated by undefined code point.

* Run clang-format

* Fix order of arguments

* Add warning about string search with encoding=guess to tests.

* Fix string lengths, they no longer count the final invalid code point.

* Fix endian macro on Windows

* Fix NULL dereference

* Fix number tests

* Use endianness check not dependent on stdbit

* Fix type annotations.

* Reintroduce rz_str_len_utf8char

* Fix command description.

* Fix and unify RZ_SYS_ENDIAN macros.

- Don't allow unhandled architectures anymore.
- Check endianness for Sparc and PPC using non GCC/Clang compilers.
- Fix several endianness checks using the value instead of the macros.

* Fix tests

* Apply review comments.
2025-09-11 22:29:20 +08:00

90 lines
5.2 KiB
Text

NAME=izz
FILE=bins/elf/analysis/hello-linux-x86_64
CMDS=izz
EXPECT=<<EOF
paddr vaddr len size section type string
----------------------------------------------------------------------------------------
0x00000034 0x00400034 4 8 utf16le @8\b@
0x00000200 0x00400200 25 26 .interp ascii /lib/ld-linux-x86-64.so.2
0x000002e1 0x004002e1 14 15 .dynstr ascii __gmon_start__
0x000002f0 0x004002f0 9 10 .dynstr ascii libc.so.6
0x000002fa 0x004002fa 4 5 .dynstr ascii puts
0x000002ff 0x004002ff 17 18 .dynstr ascii __libc_start_main
0x00000311 0x00400311 11 12 .dynstr ascii GLIBC_2.2.5
0x00000588 0x00400588 4 4 .text ascii \$\bH
0x00000597 0x00400597 4 4 .text ascii l$ L
0x0000059c 0x0040059c 4 4 .text ascii t$(L
0x000005a1 0x004005a1 4 4 .text ascii |$0H
0x000005c4 0x004005c4 11 12 .rodata ascii Hello World
0x00000618 0x00400618 4 4 .eh_frame ascii \e\f\a\b
0x00000648 0x00400648 4 4 .eh_frame ascii \e\f\a\b
0x0000066f 0x0040066f 5 6 .eh_frame ascii ;*3$"
0x00000691 0x00400691 4 5 .eh_frame ascii P\f\a\b
0x00000910 ---------- 16 17 .comment ascii GCC: (GNU) 4.7.2
0x00000922 ---------- 7 8 .shstrtab ascii .symtab
0x0000092a ---------- 7 8 .shstrtab ascii .strtab
0x00000932 ---------- 9 10 .shstrtab ascii .shstrtab
0x0000093c ---------- 7 8 .shstrtab ascii .interp
0x00000944 ---------- 13 14 .shstrtab ascii .note.ABI-tag
0x00000952 ---------- 18 19 .shstrtab ascii .note.gnu.build-id
0x00000965 ---------- 9 10 .shstrtab ascii .gnu.hash
0x0000096f ---------- 7 8 .shstrtab ascii .dynsym
0x00000977 ---------- 7 8 .shstrtab ascii .dynstr
0x0000097f ---------- 12 13 .shstrtab ascii .gnu.version
0x0000098c ---------- 14 15 .shstrtab ascii .gnu.version_r
0x0000099b ---------- 9 10 .shstrtab ascii .rela.dyn
0x000009a5 ---------- 9 10 .shstrtab ascii .rela.plt
0x000009af ---------- 5 6 .shstrtab ascii .init
0x000009b5 ---------- 5 6 .shstrtab ascii .text
0x000009bb ---------- 5 6 .shstrtab ascii .fini
0x000009c1 ---------- 7 8 .shstrtab ascii .rodata
0x000009c9 ---------- 13 14 .shstrtab ascii .eh_frame_hdr
0x000009d7 ---------- 9 10 .shstrtab ascii .eh_frame
0x000009e1 ---------- 11 12 .shstrtab ascii .init_array
0x000009ed ---------- 11 12 .shstrtab ascii .fini_array
0x000009f9 ---------- 4 5 .shstrtab ascii .jcr
0x000009fe ---------- 8 9 .shstrtab ascii .dynamic
0x00000a07 ---------- 4 5 .shstrtab ascii .got
0x00000a0c ---------- 8 9 .shstrtab ascii .got.plt
0x00000a15 ---------- 5 6 .shstrtab ascii .data
0x00000a1b ---------- 4 5 .shstrtab ascii .bss
0x00000a20 ---------- 8 9 .shstrtab ascii .comment
0x000017f9 ---------- 6 7 .strtab ascii init.c
0x00001800 ---------- 10 11 .strtab ascii crtstuff.c
0x0000180b ---------- 12 13 .strtab ascii __JCR_LIST__
0x00001818 ---------- 20 21 .strtab ascii deregister_tm_clones
0x0000182d ---------- 18 19 .strtab ascii register_tm_clones
0x00001840 ---------- 21 22 .strtab ascii __do_global_dtors_aux
0x00001856 ---------- 14 15 .strtab ascii completed.6724
0x00001865 ---------- 38 39 .strtab ascii __do_global_dtors_aux_fini_array_entry
0x0000188c ---------- 11 12 .strtab ascii frame_dummy
0x00001898 ---------- 30 31 .strtab ascii __frame_dummy_init_array_entry
0x000018b7 ---------- 7 8 .strtab ascii hello.c
0x000018bf ---------- 13 14 .strtab ascii __FRAME_END__
0x000018cd ---------- 11 12 .strtab ascii __JCR_END__
0x000018d9 ---------- 16 17 .strtab ascii __init_array_end
0x000018ea ---------- 8 9 .strtab ascii _DYNAMIC
0x000018f3 ---------- 18 19 .strtab ascii __init_array_start
0x00001906 ---------- 21 22 .strtab ascii _GLOBAL_OFFSET_TABLE_
0x0000191c ---------- 15 16 .strtab ascii __libc_csu_fini
0x0000192c ---------- 27 28 .strtab ascii _ITM_deregisterTMCloneTable
0x00001948 ---------- 10 11 .strtab ascii data_start
0x00001953 ---------- 17 18 .strtab ascii puts@@GLIBC_2.2.5
0x00001965 ---------- 6 7 .strtab ascii _edata
0x0000196c ---------- 5 6 .strtab ascii _fini
0x00001972 ---------- 30 31 .strtab ascii __libc_start_main@@GLIBC_2.2.5
0x00001991 ---------- 12 13 .strtab ascii __data_start
0x0000199e ---------- 14 15 .strtab ascii __gmon_start__
0x000019ad ---------- 12 13 .strtab ascii __dso_handle
0x000019ba ---------- 14 15 .strtab ascii _IO_stdin_used
0x000019c9 ---------- 15 16 .strtab ascii __libc_csu_init
0x000019d9 ---------- 4 5 .strtab ascii _end
0x000019de ---------- 6 7 .strtab ascii _start
0x000019e5 ---------- 11 12 .strtab ascii __bss_start
0x000019f1 ---------- 4 5 .strtab ascii main
0x000019f6 ---------- 19 20 .strtab ascii _Jv_RegisterClasses
0x00001a0a ---------- 11 12 .strtab ascii __TMC_END__
0x00001a16 ---------- 25 26 .strtab ascii _ITM_registerTMCloneTable
0x00001a30 ---------- 5 6 .strtab ascii _init
EOF
RUN