Commit graph

49 commits

Author SHA1 Message Date
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
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
Alok Kumar Mishra
1c22e9d6a0
Bit-Vector: fail when cast growth cant resize storage (#6219) 2026-04-13 08:50:39 +00:00
Florian Märkl
995f73b1ad
Add inplace variants for rz_bv_(un)signed_cast() (#6164) 2026-04-06 17:09:05 +02:00
Florian Märkl
babba5f428
librz/util: fix fill_bit in rz_bv_cast_inplace for small vectors (#6163)
Resulting bitvector contents for fill_bit = true were incorrect. In
particular, when extending, the new bits were not set and when
shrinking, the cut off bits were set to the fill_bit, even though they
would be out of range.
2026-04-06 02:46:34 +08:00
Rot127
a1012b5305
Improve performance of rz_bv_set_from_Xt64() at least for the value 0. (#6147) 2026-04-04 12:01:44 +00:00
Anton Angelov
0ff186d8e2
Improve performance of mem.c::read_n_bits() (#5819)
* Add micro benchmark
* Add implementation for rz_bv_set_from_buffer_*()
* Implementation for BE host
* Fix incorrect size parameter when calling rz_bv_set_from_buffer
* Warning when reading beyond ST64_MAX
2026-01-26 12:19:21 +08:00
Florian Märkl
c594b413ff
Swap argument order of rz_bv_copy* (#5793)
This brings these copy functions in line with the more common order of
dst, src as used in memcpy and rz_bv_*_inplace functions.
This is a breaking change and callers of rz_bv_copy() and
rz_bv_copy_nbits() must adjust to the new order.
2026-01-14 23:18:34 +08:00
Anton Angelov
b2fb8bbec5
Improve performance of rz_bv_add_inplace() (#5751)
* Add rz_bv_add_inplace benchmark for 256-bit vectors

* Improve performance of rz_bv_add

* Simplify helper function and fix bug

* Minor changes
2026-01-11 00:14:19 +00:00
Rot127
960dc17b23
util/bitvector: return successfully for bit vector shift of 0. (#5747)
The result of shift with 0 is perfectly well defined.
2026-01-08 23:27:38 +08:00
Rot127
4213ddfecc
Fix reachable invalid state, for bitvector in-place casts from large to large. (#5746) 2026-01-08 13:47:53 +00:00
Rot127
a89bbe5cf2
librz/util: implement inplace rz_bv_div and rz_bv_mod. (#5742) 2026-01-07 12:26:06 +08:00
Arya H R
a6edb4ecc5
Fix various Coverity findings (#5668) 2025-12-30 02:02:56 +08:00
Rot127
472819aa92
librz/util: add in-place bitvector operations (#5569)
* Add an in-place addition of bitvectors.

* Decouple _elem_len from assumption it has just enough bytes to hold len in bits.

* Add in-place copy of bits.

* Use default copy bits bitvector function.

* Mark stack allocated bit vector.

* Add inplace variant of complement_1

* Implement inplace bitvecotr not.

* Add inplace variant of rz_bv_and

* Implement inplace variant of rz_bv_or

* Implement inplace variant of rz_bv_xor

* Constify rz_bv_lsb/msb

* Implement inplace variant of rz_bv_neg

* Fix inplace add. Add inplace SUB

* Remove prefix

* Remove invalid const

* Implement in-place casting of bit vectors.

* Add a hash test

* Fix unnecessary &

* Prevent OOB reads & writes by copying only the minimum of bytes.

* Add inplace MUL

* Add ble version for bitvector set/get with bytes.

* Add documentation for inplace bitvector functions.

* Add bench of add/sub() and sub/add_inplace().

* Remove rz_bv_copy_nbits_inplace because it was the same as rz_bv_copy_nbits.
2025-12-23 13:56:06 +08:00
Anton Angelov
0a14a7a1da
Improve performance of rz_bv_set_range() (#5562)
* Add microbenchmarks

* New implementation for rz_bv_set_range()

* Remove rz_ prefix from static function names
2025-12-04 14:44:19 +08:00
Anton Angelov
9d3109888e
Enhance performance of rz_bv_copy_nbits for large to small and small to large bv copies (#5551)
* Implement optimized functions for small to large and large to small

* Add benchmark instructions in testREADME.md

* Use the term unaligned consistently instead of nonaligned

* Slightly improve bitvector test error message

* Remove unnecessary annotations

* Edit readme

* Add explicit fallthrough comments

* Return statement for rz_bv_toggle_all()

* Extend tests
2025-11-30 00:48:36 +08:00
Anton Angelov
c186f1ec83
Enhance performance of rz_bv_copy_nbits (#5541)
* Add benchmark for rz_bv_copy_nbits
* Improve performance for large to large and small to small bitvector copy
* Fix bug with nbit=64 and simplify code
* Add test for same bitvector copy
* Move bit copy logic to separate function in rz_bits.h + improve comments
* Support same vector copy for unaligned case
* Expect non-null RzTable in bench utils and add comments
* Test against reference implementation instead of hardcoded values
2025-11-23 21:52:23 +08:00
billow
7a77b8f7bd
linter: update clang-format installation to version 20 (#5451)
* fix: format with clang-format-20
* fix: fix clang-format linter with rewrite '^#define.*/\*.*\\$'
* fix: update clang-format to version 20 in workflows and documentation
* fix: add SPDX license information to .git-blame-ignore-revs
2025-10-12 08:02:03 +08:00
Giovanni
6c8c2b56bb
Fix some coverity issues. (#5043)
* Fix CID 436263 Resource leak
* Refactor names and fix CID 316211 Res
* Fix CID 434354 Resource leak
* Fix CID 350586 Logically dead code
* Fix CID 317123 Logically dead code
* Refactoring and Fix CID 401538 Logically dead code
* Fix CID 471305 Logically dead code
* Fix CID 316396 Logically dead code
* Fix 316248 Logically dead code
* Fix CID 365932 Logically dead code
* Fix CID 365945 Logically dead code
* Fix CID 464683 Overflowed integer argument
* Fix CID 478061 Resource leak
* Fix CID 477908, 477950 Resource leak
* Fix CID 477882 Resource leak
* Fix CID 478030 Resource leak
* Fix CID 478058 Resource leak
* Fix CID 477829 Resource leak
* Fix CID 477973, 477991 Resource leak
* Fix CID 477970 Resource leak
* Fix CID 366822 Dereference after null check
* Fix CID 478059 Resource leak
* Fix CID 477874, 478012 Resource leak
* Fix CID 478074 Logically dead code
* Fix CID 408926 Dereference after null check
2025-03-27 12:38:42 +08:00
Rot127
8eec797a3b
doc: correct doxygen for rz_bv_copy() (#4715) 2024-11-14 11:41:26 +08:00
Heersin
48007b4ae0
Adding missing operation in bitvector lib and core theory (#4078)
* Add extra bitvector operations defined in core theory basic extra part

* Add extra bitvector operations test

* Fix bv64 init const overflow in windows x64 test

* Add test case for bv_pred and bv_succ near 0
2024-01-08 23:10:14 +08:00
wargio
7ef518f6b8 Adds const to rz_bv_as[_hex]_string 2022-12-04 16:53:58 +01:00
wargio
bf6d162909 Implement float80 and RzFloat to decimal string 2022-11-01 10:58:16 +08:00
Rot127
b46e7bd4f2
PPC uplifting to RzIL (#2823)
* Print a warning if config for VM was NULL.
* Add warning if register is not added to VM due to overlap.
* Update PPC register profile.
* Add vector and float registers. As well as some control and system registers.
* Enable to write values 4bit registers.
* PPC: Uplift most common instructions.
* Print warning if reserved SPR instruction is encountered.
* Update PC/LR addresses and add ca32 writes.
* Write cache needs to get flushed so load and store tests don't share the same memory.
* Add missing T/F branch mnemonics.
* Add Move to/from CR/CR0-7
* Fix TA address calculation for branch instructions.
* Add branch tests for branch mnemonics.
* Add XNOP
* NOP cache touch instructions.
* Add undocumented ATTN instruction to the not_implemented group.
* Add isel instruction.
* Implement CRCLR, CRSET, CROR.
* Add CNTLZ instructions.
* Add mcrf instructions.
* Add inacive test for cmpb.
* Add Load bytes reverse instructions.
* Add test and add address alignment to dcbz.
* Add eqv test
* Correct DIV and MUL operations
* Add div tests.
* Add tests and correct MT/MFXER
* Remove register ca32, ov32
* Remove explici setting of cr register because QEMU does not do it. Otherwise we get a mismatch in the trace
* Simplify carry set for add and sub. Sub instructions are exclusivly defined with addition. Hence no sub case needed.
* Unify BD and fix branch instructions. Fix: Check the single bit not the cr reg
* Document Conditional branches and replace NOPs with EMPTY
* Add mulli instruction to double word instructions.
* Fix ca set for shift instructions:
* ca value had to be determined before the shift happened. The wrong ca value was calculated if the src and target reg were the same.
* Replace NOP wit EMPTY.
* Use MSB isntead of SLT.
* Brought fixup of ADD and SUB instructions: The add and sub instructions had several issues which let to incorrect execution.
* The carry was incorrectly if three add operations happened (only the last add were checked, not both).
* The carry was incorrectly set if the src and target register matched.
* Same applies for the CR bit.
* It was too complex. Several local variables were introduced for this.
* Set result in local var, since it would change if src and target reg are the same.
* Remove MTMSR and MFMSR since it is too complex and untestable currently.
* Use unsigned int for shift. Otherwise the 0x1c shift produces a runtime error since 0xf is int as default.
* Fix mtxer: Only write flag bits.
* Let NOT_IMPLEMENTED macro return NULL.
* Mark st[wd]cx and l[wd]cx as not implemented.
* Increase dcache_line_size to 128 bytes.
* Fix cntlz for ppc32. m was set incorrectly, since it is 0 not 32 for 32bit cpus.
* Fix isel: Use op.crx reg instead of imm.
* Unify helper function names: Prependnig `ppc_` mark as IPI
* Add more "Move to SPR" cases.
* MULLI opeartes only on double word on 64bit CPUs.
* Most registers are now assigned the control register type and no longer show up in the ar command.
* Fix xor if dest and src registers match by saving result in local var.
* Fix BE/LE issue for Load BRX instructions.
* Fix shifts: Use only lower 6bits of n.
* User Pure local variables for ROT macros.
* Fix rldimi instructions.
* n was not inverted.
* more than 6 bits of n could be used
* Add 32bit emulateme tests.
* Add 64bit emulateme tests.
* Determine lg(v) in inline function.
* Calculate CR bit in C not in the VM.
* Check if `~mask = 0` and skip mask calculation if yes.
* Check for `sh == 0` and skip rotations where possible.
* Remove `la` instruction. `la` is a mnemonic for `addi`.
* Remove SPR instructions which are not supported by QEMU or not traced yet. For most set/read SPR instructions QEMU segfaults.


In case of SPR 1 (xer), 8 (lr) and 9 (ctr) the assembler resolves them to their mnemonics (mtxer, mtlr etc.). This means the code here is never reached.
To test the get_xer code MFXER was added again. The rz-tracetests will fail for this instructions (due to missing ca32, ov32). But this case is covert in an issue.
2022-08-11 08:41:15 +08:00
Heersin
a9c59ce1dd
Add rz_bv_range_set and rz_bv_is_all_one to bitvector lib (#2891) 2022-08-10 08:20:16 +08:00
wingdeans
a6a7a5f530 Fix annotation inconsistencies 2022-08-03 00:29:56 -04:00
Rot127
b65ef05e96
Fix: Allow to setup big endian bitvectors with less than 8bit values. (#2571) 2022-04-29 16:03:42 +02:00
Heersin
2fdb8ac07e
Fix copied bit count in rz_copy_nbits and add unit test (#2547) 2022-04-18 18:47:08 +02:00
Florian Märkl
0549b2b5fb
Fix trailing bits in rz_bv_set_to_bytes_le() (#2348)
Any traling bits in the last byte that is to be written should be left
alone.
2022-02-21 23:05:50 +01:00
Florian Märkl
c9d299155c
Fix UB in rz_bv_set_all(..., 64) (#2345)
Shifting a 64-bit value by 64 bits is UB. Restructuring it like this is
fine because the bv len must never be 0.
2022-02-21 13:31:18 +00:00
Florian Märkl
76cec13591
Fix rz_bv_len_bytes() (#2334) 2022-02-18 18:11:33 +01:00
Florian Märkl
bdd9b20afe Fix rz_bv_div() and rz_bv_mod()
Problems fixed:
* Division by repeated addition is horribly slow, now it's classic
  shift+sub
* a / a = 1, not a
* rz_bv_set_all() was setting too many bits if len % 8 != 0
* Removed print on div by 0 because we define it to all 1s (like BAP)

mod just uses div and we use regular ut64 division when possible.
2022-02-13 17:57:17 +01:00
Florian Märkl
05f47d2397 Add ctz and clz ops to RzBitVector
These are typical "count trailing/leading zeroes" to be used in
division for example.
2022-02-13 17:57:17 +01:00
Florian Märkl
e2751e5c49 Make big bvs store in typical little endian
Previously, bits were stored in the big endian in reversed order, such
that bit 0 of byte 0 would contain the msb. This requires reversing them
for any calculation and becomes even more confusing when the size is not
a multiple of 8, so let's just store them in classic little endian with
bit 0 of byte 0 containing the lsb.
This also fixes rz_bv_as_hex_string() for bvs that have a size not
divisiable by 0.
2022-02-13 17:57:17 +01:00
Florian Märkl
9401c9d67e Fix rz_bv_as_hex_string() for big 0
This was printing `0x` instead of `0x0` for big vectors when padding was
disabled.
2022-02-13 17:57:17 +01:00
Florian Märkl
76e776907e
[RzIL] Add remaining comparison aliases and tests (#2295) 2022-02-08 17:29:35 +01:00
Florian Märkl
efa41b86d7 [RzIL] Refine events and add tests
* Removed nullability where not needed
* Made events take RzILVal instead of force-converting to RzILBitVector
  for variables
* Added more fine-grained tests for events from specific ops
2022-01-22 18:40:10 +01:00
Florian Märkl
c8a6359d83
Change IL string syntax to s-expressions (#2193)
Before:
	store(key:var(v:ptr), value:add(x:load(key:var(v:ptr), mem:0),
	    y:int(value:1, length:8)), mem:0)
After:
	(store 0 (var ptr) (+ (load 0 (var ptr)) (bv 8 0x1)))

S-Expressions like this are more concise and will be trivial to parse,
in case we want a parser for this later. It is also very similar to what
BAP uses, so it will be easier to compare.
2022-01-11 10:08:41 +01:00
Florian Märkl
54318a2268
[RzIL] Rework Variable Handling (#2174)
There are now three kinds of vars: global, local and local pure. Global
and local pure are exactly like in BAP, local ones are defined by their
initial set op and have the scope of a single lifted instruction.

The set op now handles both global and local vars, let is now pure and
binds only inside its body. Vars have static types, global and local are
always mutable, local pure naturally immutable.

The var op specifies the kind of variable to take from, and the
individual variable sets are separate. This corresponds to BAP's
behavior where the kind of variable is part of the identifier.

Variable content storage has also been rewritten and RzILBag removed.
2022-01-05 11:16:47 +00:00
Florian Märkl
5ddb19c692 [RzIL] Clarify what append actually does
high/low is more descriptive than x/y. Test has also been added.
2022-01-03 17:18:33 +01:00
Florian Märkl
9938a85c4b
[RzIL] Add RzReg/Variable Binding (#2155)
Plugins do not create register-based variables themselves anymore, but
they are derived from the register profile. However not all registers
may be bound and not all variables may be actual registers. The concrete
relations between an RzILVM and registers is described by an
RzILRegBinding object. It is simply a list of register names and
variables are created of the same names. When stepping with aezs, the
registers are synchronized automatically. From now on, aezv is primarily
useful for debugging, but interacting with emulation from the user side
should be done with ar.
2022-01-02 17:56:19 +00:00
Florian Märkl
aa80d2870f
[RzIL] Make cast work as in BAP (#2152) 2021-12-28 21:22:30 +01:00
Florian Märkl
78025648ea
[RzIL] Make RzILOp statically typed (#2140) 2021-12-27 00:13:24 +08:00
Giovanni
5963da2bba
[RzIL] Add loadw/storew and bind memories against RzBuffer (#2105)
* [RzIL] Load/Store via RzIO and RzBuffer
* [RzIL] LoadW/StorWe via RzIO and RzBuffer
Co-authored-by: Florian Märkl <info@florianmaerkl.de>
2021-12-24 16:53:20 +01:00
wargio
8c2cb05af8 [RzIL] Add let, rewrite using RzPVector and various bugfixes 2021-12-14 12:34:55 +01:00
Dhruv Maroo
a255ff57a2
Fix some more Coverity issues (#2044)
* C352467: Free the heap allocated string after appending to `RzStrBuf`

* Remove unused variable `nofail`

* C352453: Free `fieldstr` after appending to `format`

* C350836: Free `name` before returning inside the loop

* C349252, C349231: Free `ta` when breaking halfway through the loop

* C349248: Free `pj` before breaking from switch statement

* C366622: Check result of `strtok` (`fcnname_aux`) for `NULL`

* Free `RzList` before returning from macro `SYNC_READ`

  * Fixes Coverity issues 366625, 366624, 366623, 366621 and probably
    a few others as well

* C366620: Check `arg` for `NULL` before dereferncing in `strchr`

* C366618: Cleanup `parse_lis` and use `rz_str_*` API instead of C `str` API

* Fix `SYNC_` macros in `cmd_regs.c` to no return prematurely

* C365952: Check `x` for `NULL` before dereferencing

* C365936: Use a wider integer type for the bitshift

* C365808: Fix double free of `real_type`

* C365805: Remove redundant `NULL` check for `path`
2021-12-13 23:09:46 +08:00
Florian Märkl
4cb13c3183
Add rz_bv_set_from_bytes_le() (#2068)
Also make rz_bv_as_hex_string() always pad with the same number of 0s if
len % 4 != 0.
2021-12-06 12:28:26 +08:00
Paul I
44f8e8903d
Add rz_bv_init/rz_bv_fini API (#2062) 2021-12-04 10:19:26 +08:00
Dhruv Maroo
648c267bc7
Rename and refactor to use RzBitVector instead of RzILBitVector (#2048) 2021-12-01 14:28:04 +05:30
Renamed from librz/il/definitions/bitvector.c (Browse further)