Commit graph

841 commits

Author SHA1 Message Date
Florian Märkl
a3c35a88a1 Remove rz_hex_from_js()
It is untested and segfaults in almost all cases. It is also not worth
fixing because the implementation of converting from base64 is not very
useful.
2026-07-11 15:57:50 +02:00
Florian Märkl
f430f28c02
Make rz_interval_tree_insert return the node (#6613)
There are APIs for which the node is needed, so it makes sense to return
it directly on insertion instead of only the boolean success state.
2026-07-11 14:08:35 +02:00
wargio
674cdbc25e Remove unused rz_str_word_get0set & rz_str_word_set0_stack 2026-07-07 23:14:47 +08:00
wargio
1dfba2cdb8 Harden rz_str_append to prevent sum overflow 2026-07-07 23:14:47 +08:00
Florian Märkl
ff4d6608c0
Add rz_bv_append_inplace() (#6592)
Warning: this also swaps the arguments of the old rz_bv_append() to be
consistend with the new inplace variant.
The reason why the inplace function has the low as the first operand is
that it can be more efficient to append to an existing vector inplace
than to prepend to it. Then, the first argument is being used as the
in-out one in all other inplace functions.
2026-07-03 23:31:30 +08:00
MrQuantum1915
893ff4e380
librz/util/pj: Fix JSON depth limit handling (#6533) 2026-07-02 15:21:36 +08:00
NOT XVilka
ca07131f66
Fix memory leaks across arch, core, and util (#6581)
Co-authored-by: Anton Kochkov <anton.kochkov@gmail.com>
2026-07-02 11:10:03 +08:00
Dmitry Opokin
9d37b7cdf2
Add MediaTek md1img and GFH firmware image parsers (#5974)
- Introduced md1img.h and md1img.c for parsing MediaTek md1img container format.
- Implemented mtk.h and mtk.c for parsing MediaTek GFH firmware images (md1rom).
- Added plugin support for md1img and mtk formats in bin_md1img.c and bin_mtk.c.
- Updated meson.build to include new source files and plugins.
- Enhanced RzBuffer utility with LZMA alone decompression support.

---------

Co-authored-by: Giovanni <561184+wargio@users.noreply.github.com>
2026-06-22 20:25:48 +00:00
billow
bb3b7cc7b1
Add JSON projection grep (#6522) 2026-06-22 17:17:20 +00:00
مصطفي محمود كمال الدين
59d8c998e5 fix positive zero comparing inequal with negative zero, IEEE754 mandates equality 2026-06-21 03:48:49 +08:00
مصطفي محمود كمال الدين
afd6607fe2
Fix error handling and a percision issue in float core (#6469)
* fix error handling and percision issues in float core
* fix rz_float_cast_float on zero input and add tests
2026-06-14 22:48:44 +08:00
NOT XVilka
80f14bf6fc
librz/util/vector: minor RzVector/RzPVector performance optimizations (#6467)
* util/vector: hoist quicksort scratch buffers out of the recursion

vector_quick_sort allocated its two element-sized scratch buffers (t and
pivot) with malloc/free on every recursive call. For a vector of n elements
the sort makes O(n) recursive calls, i.e. O(n) malloc/free pairs purely for
scratch space, and each call could also fail half-way through the sort.

Split the function into a small entry point that allocates the two buffers
once and a recursive worker that receives them as scratch. The buffers are
reused across the whole recursion (each partition step finishes using them
before recursing, and the recursion is sequential, so sharing one pair is
safe). Small elements -- the common case, including every RzPVector-backed
sort -- use stack buffers and allocate nothing at all; only elements larger
than 256 bytes fall back to a single heap allocation for the whole sort.

The element movement and rand()-based pivot selection are unchanged, so the
result is identical for any input (verified byte-for-byte against the previous
implementation for ascending and descending orders over many random arrays).

* util/vector: evaluate the comparator once per element in the quicksort

The partition loop tested the element against the pivot with two separate
calls to the comparator:

    if ((cmp(VEC_INDEX(a, i), pivot, user) < 0 && !reverse) ||
        (cmp(VEC_INDEX(a, i), pivot, user) > 0 && reverse)) {

Because cmp is an opaque function pointer the compiler cannot common up the
two calls, so depending on the result and the reverse flag the comparator was
invoked up to twice per element. Compute the result once into a local and test
that:

    int c = cmp(VEC_INDEX(a, i), pivot, user);
    if ((c < 0 && !reverse) || (c > 0 && reverse)) {

This halves comparator calls in the worst case and is a clear win whenever the
comparator is non-trivial (the common case for struct elements). Measured on a
shared host: ~12-14% faster for int sorting and ~30% faster with a moderately
expensive comparator. The ordering is unchanged (verified byte-for-byte).

* util/vector: simplify rz_pvector_remove_data index computation

The index of the located slot was computed as

    size_t index = (el - (void **)vec->v.a) * sizeof(void **) / vec->v.elem_size;

For an RzPVector the element size is always sizeof(void *), so the
`* sizeof(void **) / vec->v.elem_size` factor is identically 1 and the pointer
difference `el - (void **)vec->v.a` already yields the index directly. Drop the
redundant scaling, which removes a multiply and a divide and makes the intent
clear. Behaviour is unchanged.

* test/unit: add RzVector sort and rz_pvector_remove_data regression tests

The existing sort tests only sort 4-5 small elements and there was no test for
rz_pvector_remove_data. Add coverage for the code paths exercised by the sort
changes and the remove_data cleanup:

  - test_vector_sort_large       sort 2000 heavily-duplicated ut32 values
                                 ascending and descending, verifying the result
                                 is ordered and a permutation of the input (vs a
                                 reference qsort). Drives the recursion deeply
                                 and the shared scratch buffers.
  - test_vector_sort_large_elem  sort 400 elements of 304 bytes each, taking the
                                 heap-allocated scratch fallback, and check the
                                 full payload (not just the key) stays consistent
                                 through all the element moves.
  - test_pvector_remove_data     remove interior, first and last elements by
                                 value while preserving order, and confirm
                                 removing an absent value is a no-op.

All pass on both the previous and the optimized implementation (the sort and
remove_data changes are behaviour-preserving).

* test/bench: benchmark rz_vector_sort and rz_pvector_sort

bench_vector.c benchmarked only remove_at and swap. Add sort benchmarks so the
suite covers the functions touched by the sort optimizations and can be run
against the old and new librz for before/after numbers:

  - rz_vector_sort over 4k ut64 with a cheap comparator
  - rz_vector_sort over 4k ut64 with a deliberately expensive comparator
    (shows the effect of evaluating the comparator once per element)
  - rz_pvector_sort over 4k pointers (reference; pvector sort is unchanged)

Each iteration refills the buffer from an unsorted master copy via a single
memcpy before sorting; that overhead is identical across builds so the measured
delta reflects the sort.

---------

Co-authored-by: Anton Kochkov <anton.kochkov@gmail.com>
2026-06-10 00:22:26 +08:00
Rot127
6d7c38cf5b
Graph - (tiny) performance improvements (#6382)
* Fix revert if adding a node failed.

* Reduce allocations by keeping only a single RzGraphEdge object per edge around.

* Add benchmark for graph deletion and addition of nodes/edges

* Use rz_pvector_remove_at_unsorted to save some runtime.

* Use realloc and memmove for matrix graphs on capacity increase.

* Add helper to determine memory usage.

* Add benchmark

* Revert matrix capacity extension to simple and jsut as fast loop.

* Missing type annotations
2026-05-31 22:00:25 +00:00
Rot127
2de65410ef
Return early from sleep() functions in case of 0 sleep time. (#6429) 2026-06-01 01:47:39 +08:00
wargio
f366095030 Implement special compare for bytes and always compare mem-aligned. 2026-06-01 01:45:59 +08:00
Rot127
691bd504c6
Add missing NULL check before passing the edge to cb(). (#6428) 2026-05-29 22:53:49 +02:00
NOT XVilka
d3860590ee
librz/util: shared Unicode subscript formatting for bit-vectors and floats (#6418)
Consolidate the Unicode subscript notation used when rendering
bit-vector and float values (the subscript width on a bit-vector
constant, e.g. 0x2c followed by a subscript 8, and the format width
on a float, e.g. .f followed by a subscript 32) into one place, so
the RzIL Unicode exporter, the RzNum value printer, and the
RzNum->RzIL lift cannot drift apart.

RzUtil gains the single source of truth:

  * rz_str_append_subscript() / rz_str_append_superscript() /
    rz_str_subscript() render a number as Unicode subscript or
    superscript digits;
  * rz_bv_width_subscript() / rz_bv_as_unicode_string() build a
    bit-vector's width subscript on top of the str helper;
  * rz_float_format_subscript() renders a float format's width
    subscript (16/32/64/80/128, with the decimal-format marker),
    reusing the same digit renderer.

The RzIL Unicode exporter (il_export_string_unicode.c) is switched
fully onto these: every append_subscript() call site (bit-vector
constant width, cast length, memory indices) now goes through
rz_str_append_subscript(), and the hardcoded per-format subscript
macro is replaced by rz_float_format_subscript(). The exporter's
private subscript-digit table and ut32 glyph helper are removed, so
there is no longer a second, parallel implementation to keep in sync.

Signed-off-by: Anton Kochkov <anton.kochkov@gmail.com>
Co-authored-by: Anton Kochkov <anton.kochkov@gmail.com>
2026-05-29 02:02:58 +08:00
Florian Märkl
7e97635726
Avoid overlapping memcpy in rz_vector_sort() (#6411)
Detected with valgrind, some element assignments could memcpy with
identical addresses. This is usually a no-op in practice, but
theoretically undefined behavior.
2026-05-28 06:33:45 +02:00
Khairul Azhar Kasmiran
1abe2dce99
Prevent RzBuffer's Oxff_priv from overriding io.0xff (#6371)
* Prevent RzBuffer's `Oxff_priv` from overriding `io.0xff`
* Use io from RZ_BUFFER_IO and RZ_BUFFER_IO_FD buffers
* Add `RzIO *` param to rz_buf_new_mmap()
* pe: Discard incomplete import directory
2026-05-27 06:18:45 +08:00
Rot127
c6820479c6
test/bench: add geometric stats to benchmarks (#6403)
* Add option to replace (geometric) invalid values with another value.
* Add geometric Mean and Std Dev to benchmarks.
* Add Doxygen documentation
* Add a README to the bench dir as intro.
2026-05-26 16:15:02 +08:00
Florian Märkl
a72a275ca2
Fix deadlock in rz_th_queue_close_when_empty() (#6383)
empty_cond was never signalled and the termination depended only on
the timeout in rz_th_queue_close_when_empty() causing a re-check of
emptiness.
However, the rz_th_cond_timed_wait() implementation, which was used
there, was flawed because it expected a relative timeout but passed that
directly to pthread_cond_timedwait() which expected an absolute time
value, practically causing it to time out immediately. Depending on the
pthread_cond implementation, this possibly created a situation where the
mutex could never be acquired by another thread, effectively causing a
deadlock. This behavior was observed on Mac OS X 10.5 (ppc) when running
the test_core_bin test.
We solve this by not using a timeout at all and signalling the condition
variable for all waiting threads at the appropriate time.
2026-05-25 09:25:29 +02:00
Rot127
702250eb4f
test/bench: measure standard deviation for benchmarks (#6390)
* Add Welfords square of sums algorithm for variance and std deviation calculations.
* Add standard deviation to benchmarks
* Simplify Welford
* Add geometric mean and standard deviation to Welford Sums
2026-05-25 05:08:06 +08:00
Rot127
12a16c812b
Add order ignoring remove_at version with better performance. (#6389)
* Add order ignoring remove_at version with better performance.

* Optimize rz_vector_swap by using stack memory for small elements.

* Add benchmark for rz_vector_swap
2026-05-22 20:54:49 +00:00
Rot127
75cd389b5c
Graph - API changes to enum (#6349)
* Refactor del_edges to use RzGraphStatus.

* Refactor del_edge() to use RzGraphStatus.

* Refactor update_edge() to use RzGraphStatus.

* Refactor has_edge() to use RzGraphStatus.

* Refactor add_edge() to use RzGraphStatus.

* Fix leak of b

* Fix leak of xref list

* Fix leaks of analysis ops

* Address review comments

* Inlcude clean up

* Fix invalid free

* Add tests with node and edge data.

* Extend tests
2026-05-22 13:45:18 +00:00
Rot127
1adba3227a
librz/util/graph: various improvements
* GRAPH: Add rz_graph_add_get_node() and refine API.
* GRAPH: Remove unused, and rename parameters.
* Improve doxygen.
* Implement rz_graph_del_edges()
* Rename rz_graph_node_get_hash_id() to rz_graph_node_get_hash_id() to make clear what identifier is returned.
* Simplify node identification.

Removes the option to have two sources of identifiers (node data or other identifier data).
Changes the API to use the hash id instead of a pointer to data.

* Fix type annotations.
* Remove duplicate function.
* Fix invalid asserts.
* Set flag if node was present.
* Grow by a factor of 1.25. Exponential growth quickly leads to OOM.
* Remove the edge index again to not remove reduce the main advantage of an adjacency matrix
* Refactor list based graph to use vectors instead of hash maps for edges.
* Fix heap-use-after-free
* Add an rz_graph_update_edge function.
* Add function to print graph as dot graph.
* Add warning about del_edges runtime.
* Refactor add_node to use RzGraphStatus.
* Refactor del_node to use RzGraphStatus.
* Use cast-macro to prevent ASAN issues.
2026-05-16 19:12:01 +08:00
Riccardo Schirone
4fe99052f4
build: propagate OpenSSL in CMake exports (#6284)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: NOT XVilka <notxvilka@proton.me>
2026-05-06 10:04:34 +00:00
Giovanni
2f0dcd65b2
Revert "Implement Pool Node allocation for RzList (#6203)" (#6313)
This reverts commit d3a97d5ef8.
2026-05-04 22:45:00 +08:00
Khairul Azhar Kasmiran
b5be64f6df
Remove rz_range (#6303) 2026-05-03 20:10:35 +08:00
Giovanni
bd44250fd6
Add new implementation of RzConfig (#5820) 2026-05-03 15:34:48 +08:00
Farhan Saiyed
d3a97d5ef8
Implement Pool Node allocation for RzList (#6203) 2026-05-03 08:52:31 +08:00
Cheese Cake
a99cc738c2
util: extend string search with user-defined printable characters (#6161)
* util: extend string search with user-defined printable characters
* util: use RzVector for user_unprintable options
* util: address review for configurable unprintable chars
* core/cconfig: use goto error_free pattern in cb_str_unprintable
* test/cmd_search_z: merge duplicate Armenian utf16le tests
2026-05-01 22:47:14 +08:00
Rot127
82f028018a
Fix rz_vector_set() behavior to match the rz_pvector version one. (#6274)
* Fix rz_vector_set() behavior to match the rz_pvector version one.
* Use existing vector functions for setting elements.
2026-04-26 22:20:22 +08:00
NOT XVilka
cfc9d4a740
librz/util/vector: fix pointer size calculation (#6277)
Co-authored-by: Anton Kochkov <anton.kochkov@gmail.com>
2026-04-23 10:48:44 +08:00
Khairul Azhar Kasmiran
3204f0c405
Use stdint types for ut64 and friends (#6276)
* Use `stdint` types for `ut64` and friends
* Use `inttypes.h` format specifiers
2026-04-22 21:43:04 +08:00
Rot127
a74ed707ea
Several rz_vector improvements. (#6250)
* Add getter for vector capacity.

* Make rz_(p)vector_assign_at consistent in behavior.

* Document rz_vector_clear

* Move doxygen for rz_vector_flush to definition.

* Add rz_vector_purge for clearing, but capacity keeping.

* Rename flush -> take_array to signal ownership transfer.
2026-04-22 08:20:03 +00:00
Khairul Azhar Kasmiran
c8878d3139
Convert %ll format specifiers to PFMT64 (#6267)
* Convert `%ll` format specifiers to PFMT64
* Remove ` ""` at end of some PFMT64
2026-04-22 05:57:04 +08:00
billow
71aa8bf1dd
Fix memory leaks related to DWARF (#6245)
* fix: memory leak rz_analysis_op_fini op->src

* fix: memory leak, replaced `rz_type_clone` with `rz_type_clone_shallow` to resolve the memory leak in `rz_type_free(type.callable)`

* fix: memory leak in try_create_var_global

* fix: memory leak in dwarf

* fix: remove redundant memset calls and add documentation for rz_type_clone_shallow

* Update librz/arch/op.c

Co-authored-by: Giovanni <561184+wargio@users.noreply.github.com>

* fix: set free and free_user to NULL in vector copy when item_cpy is not provided

---------

Co-authored-by: Giovanni <561184+wargio@users.noreply.github.com>
2026-04-17 17:33:11 +00:00
Nicolas Dias
874c85beee
librz/util/vector: use binary search in rz_vector_insert_sorted() (#6206)
* perf: use binary search in `rz_vector_insert_sorted`
* Refactor bin_search_range to return exact insertion index
* Fix redundant `cmp` calls (reduced from 3x to 1x per iteration)
* Add null guard on `*i` pointer
* Return insertion point on miss to support `rz_vector_insert_sorted`
* Preserve O(log n) complexity without duplicating search logic
2026-04-15 23:07:11 +08:00
Rot127
d0c21c67d5
Implement ring buffer (#6081)
* Add first ring buffer implementation.
* Add likely and unlikely macros.
* Implement take and put
* Block writes to buffer if it is full.
* Make Is_open return bool and add unsafe fcn for is_empty
* Add ring buffer tests.
* Add blocking take() function to ring buffer.
* Add open() function to ring buffer
2026-04-13 10:15:49 +00:00
Alok Kumar Mishra
1c22e9d6a0
Bit-Vector: fail when cast growth cant resize storage (#6219) 2026-04-13 08:50:39 +00:00
Alok Kumar Mishra
0bd98c9628
Harden diff formatting and row offset growth (#6218)
* use actual fmt buff size instead of 64
2026-04-13 08:49:05 +00:00
Alok Kumar Mishra
b46029ddec
librz/util: guard allocation failure paths in string and vector helpers (#6217) 2026-04-12 17:16:24 +08:00
Rot127
cc49fd52cd
Add rz_vector_find_sorted() for O(log n) search in vectors. (#6190) 2026-04-11 10:30:25 +00:00
Giovanni
1a57d18cc4
Move ESIL into its own namespace (#6199)
* Move ESIL into its own namespace
* Remove RzAnalysisEsilInterState from RzAnalysis
* More cleanup to split cil.c from ESIL code
* Rename RzAnalysisRzilTrace to RzAnalysisILTrace
* Fix various null-derefs.
* Remove rz_analysis_get/set_esil_inter_state
* Fix use after free and leaks
2026-04-11 16:26:01 +08:00
Farhan Saiyed
183a0376c7
librz/util: rz_list_purge speedup (#6179)
* Add RzList testbench
* Optimize the rz_list_purge() function
2026-04-09 02:51:44 +08:00
Giovanni
daeee9ac1a
Fix ISO C23 warnings related the usage of strstr & strchr (#6182) 2026-04-08 22:04:39 +08:00
Giovanni
6131a0e187
Fix RZ_IPI usage on public headers & fix memory leak in the ROP code. (#6171)
* Fix RZ_IPI declaration on public api for rz_pdb

* Fix RZ_IPI declaration on public api for jemalloc/glibc

* Fix RZ_IPI declaration on public api for rz_io

* Remove functions from public header for rz_panels

* Fix public functions not following rizin nomenclature in rz_graph

* Fix public functions not following rizin nomenclature in rz_rop

* move resolve_fcn_name into rz_analysis

* Fix public functions not following rizin nomenclature in rz_ascii_table

* Fix leak in rop code

* Rewrite parser for rop

* Add doxygen comment
2026-04-08 13:37:17 +08:00
Alok Kumar Mishra
300af08287
librz/util/table: fix shared query filtering (#6166) 2026-04-08 11:48:04 +08:00
Rot127
b11bb9d23a
refactor: add hashtable-based RzGraph implementation (#6152)
* Basic implement of list based and matrix based graph

Add comments

Basic support for list and matrix based graph refactor

Add rz_graph_*_new node and edge API

Modify calling for rz graph edge data

Add dfs and visitor mode

Add new get nth neighbours

Add unit test and wrapper for get edges

Bug fixed and unit test

Solve TODO about better semantic of get nodes

Doc RZ_API and other functions, split impl to new files

[cannot build] remove old graph impl and rename new graph API

* Rewrite and replace agrach, cgraph, drawable_graph, il_graph with new API

* Refactor graph API and fix unit test and multiple leaks

Update Wrapper for graph identifier

Ordered Nodes in graph drawable

Fix icfg and several agraph refactor issues

Use kahn to get topo sort in assign_layers

Introduce khan for assign_layer and find DAG cycle and backedges in algorithm

* Fix self loop and update db test

* Solve request changes

* Fix cbpf db

* Fix test graph warning in testing NULL deletion

* Fix mismatched rz_agraph_compute_layout nonnull

* Fix memleak

* Clean and fix code

* Fix memleak in graph free

* Fix memleak in matrix implement

* Add bindgen doc

* Fix memleak in early exit

* Fix rz-bindgen warning comments

* Make graph structures private.

* Fix Windows C2036

* Fix typo in type annotation.

* Add new type annotations for RzGraph and HtPP

---------

Co-authored-by: Heersin <teablearcher@gmail.com>
2026-04-08 04:01:40 +08:00
Florian Märkl
995f73b1ad
Add inplace variants for rz_bv_(un)signed_cast() (#6164) 2026-04-06 17:09:05 +02:00