Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RzTypes to use structs instead of SDB + New Tree-Sitter based C Parser #1047

Merged
merged 113 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
e6a35a0
Remove oldshell version of `t` commands
XVilka Apr 22, 2021
da1d931
Add RzCallable
XVilka Apr 19, 2021
8926fa1
Migrate from char* to RzType*
XVilka Apr 19, 2021
ce320b6
Conversion of type analysis to RzType
XVilka Apr 20, 2021
d76b66c
Migrate from SDB to RzType API in RzCore
XVilka Apr 22, 2021
9e8d083
Move type links to RzAnalysis
XVilka Apr 22, 2021
8de6983
Use hashtable for RzBaseTypes
XVilka Apr 25, 2021
59ca157
Some TinyCC cleanup
XVilka Apr 25, 2021
36ee4e9
Remove tcc-based types parser
XVilka Apr 25, 2021
27e6d4d
Introduce Tree-Sitter-based C types parser
XVilka Apr 25, 2021
7e908b5
Use hashtable for `pf` formats
XVilka Apr 26, 2021
2bc7cff
Remove MPC-based parser remnants
XVilka Apr 26, 2021
57b7515
Switch function types to RzCallable
XVilka Apr 27, 2021
8500979
Refactor type databases
XVilka May 20, 2021
fae8bd8
Complete the migration to the new RzType API
XVilka May 21, 2021
9e4a13b
Allow type aliases for undefined types
XVilka Jun 7, 2021
4761d76
Minor refactor of the type propagation algorithm
XVilka Jun 3, 2021
f47af4e
Do not sanitize type names during serialization
XVilka May 25, 2021
e8f0691
Type tests improvements
XVilka May 25, 2021
3455af3
Print verbose type parsing errors only in RZ_LOG_DEBUG mode
XVilka Jun 4, 2021
a3d6030
Migrate typelinks from SDB to hashtable
XVilka Jun 8, 2021
2e04d77
Add `tf-` and `tf-*` commands to remove function types
XVilka Jun 8, 2021
1d16148
Make RzCallable.ret optional
thestr4ng3r Jun 8, 2021
91db186
Make node_malformed_error() accept null nodes
thestr4ng3r Jun 8, 2021
9aabe05
Various Fixes
thestr4ng3r Jun 8, 2021
7bbb2a9
Fix the C output of structures and unions
XVilka Jun 9, 2021
ad62be9
Fix some tests
XVilka Jun 9, 2021
347611d
Add some missing types
XVilka Jun 9, 2021
ec98cae
`asm.leahints` -> `asm.hint.lea`
XVilka Jun 9, 2021
2402be2
Calculate struct/union size on the fly
XVilka Jun 9, 2021
a21e3c1
Avoid infinite loop on self-referential structs
XVilka Jun 9, 2021
cbb7b39
Resolve overlaps only between variables of the same kind
XVilka Jun 9, 2021
010bc61
Fix some UAFs
thestr4ng3r Jun 9, 2021
bb01482
Save parser state between `to` commands
XVilka Jun 10, 2021
deec169
Fix creating a pointer of a type
XVilka Jun 10, 2021
d8e9c9f
Fix parsing forward typedefs
XVilka Jun 10, 2021
358e2c9
Fix wrong `tl` command syntax in tests
XVilka Jun 10, 2021
06dc0f1
Always initialize error_msg
thestr4ng3r Jun 10, 2021
a2743a7
Rework Windows type and function types database
XVilka Jun 11, 2021
6b77644
Fix complex typedefs parsing
XVilka Jun 11, 2021
47d6f1e
Strip generated pfs
thestr4ng3r Jun 11, 2021
4b8acb6
Do not print atomic type in `pf` format
XVilka Jun 15, 2021
d288a64
Fix arrays and nested types C output
XVilka Jun 15, 2021
721de3d
Add `tfc` command to show/set function calling convention
XVilka Jun 15, 2021
4deb007
Fix types `pf` format generation
XVilka Jun 15, 2021
31607de
Fix C output for nested structures
XVilka Jun 16, 2021
7b63df5
Better default struct/union output
XVilka Jun 16, 2021
62e07dd
Fix type offsets calculation
XVilka Jun 16, 2021
7f692d9
More test fixes
XVilka Jun 16, 2021
200d17b
Fix `tu` command - remove Rizin and SDB output
XVilka Jun 16, 2021
6bb5fd0
New `afvd` output is better
XVilka Jun 16, 2021
45846da
Fix struct variable fields offset calculation
XVilka Jun 17, 2021
82e10e3
Various test fixes
XVilka Jun 17, 2021
902228e
Fix type format generation for nested structs
XVilka Jun 18, 2021
0ae6ed4
Fix multiple anonymous structs in the `td`
XVilka Jun 18, 2021
d4c60ba
Fix `pf` format generation for nested typedefs
XVilka Jun 18, 2021
112e0a2
Remove unused TCC preprocessor
XVilka Jun 18, 2021
2ccf01c
Remove unnecessary type linking for `aht`
XVilka Jun 18, 2021
d090658
Fix `afs` command for type parsing
XVilka Jun 21, 2021
27f1493
Use rizinorg tree-sitter-c fork
XVilka Jun 21, 2021
6d40f11
Fix types generation in signatures
XVilka Jun 21, 2021
fac74eb
Fix arguments numbering in `afs`
XVilka Jun 21, 2021
c8b8aa1
Fix Sparse Vector Creation in rz_analysis_function_args()
thestr4ng3r Jun 21, 2021
5368bfa
Do not resolve overlaps for arguments
XVilka Jun 22, 2021
764da09
Refactor return type propagation
XVilka Jun 22, 2021
5576837
Refactor type links resolution
XVilka Jun 23, 2021
d553696
Minor test fixes
XVilka Jun 23, 2021
8b534ef
Do not resolve overlaps for atomic type variables
XVilka Jun 24, 2021
288059a
Load types on signature match
XVilka Jun 25, 2021
a35d31b
Fix type paths walker
XVilka Jun 25, 2021
6a1dc58
Fix MacOS types and functions loading
XVilka Jun 28, 2021
2109537
Fix caller/callee type propagation
XVilka Jun 28, 2021
c7f6b89
New `t` output for atomic types
XVilka Jun 29, 2021
10215f8
Old types linking test was likely wrong
XVilka Jun 30, 2021
8b34751
New `pf` does not overwrite format without deletion
XVilka Jun 30, 2021
41612e9
Add typeclasses
XVilka Jun 30, 2021
229f851
Test type propagation fixes
XVilka Jul 1, 2021
a91f249
Allow type propagation from Integral typeclass to more complex types
XVilka Jul 2, 2021
a0238c0
Handle broken types
XVilka Jul 2, 2021
b55845d
Better pointer type analysis
XVilka Jul 2, 2021
6457099
Add types equality and fix type xrefs
XVilka Jul 2, 2021
7330a0d
Update tests
XVilka Jul 5, 2021
72238a3
Improve parsing of forward-looking definitions
XVilka Jul 5, 2021
3e540fa
Better handling of funciton pointers
XVilka Jul 6, 2021
42dec3b
Better handling of funciton pointers [2]
XVilka Jul 6, 2021
5e12076
Typelink fixes for JNI test
XVilka Jul 8, 2021
55bebec
Test fixes
XVilka Jul 8, 2021
3285809
Fix stack args are invisible in zignature (#1281)
Basstorm Jul 9, 2021
bd40b1f
Fix segfault on empty format
XVilka Jul 9, 2021
2384e2f
Fix value-based type propagation
XVilka Jul 9, 2021
ddab5bd
Fix args comments are missing in pd command (#1288)
Basstorm Jul 10, 2021
1389264
Fix running into deadloop when integrating dwarf info for freepascal …
Basstorm Jul 12, 2021
0d76a7d
Fix bashbot test (compared with Ghidra, BN)
XVilka Jul 12, 2021
38509a0
Workaround for bashbot test until #1295 is fixed
XVilka Jul 12, 2021
85f105a
Workaround until #1256 is fixed
XVilka Jul 12, 2021
e5a6019
Fix afs command not working on user defined retval and args (#1297)
Basstorm Jul 13, 2021
6c96042
Revert incorrect migration test change, previous case now covered by …
thestr4ng3r Jul 14, 2021
d839c6e
Implement projects migration for new RzTypes
XVilka Jul 13, 2021
cd11e51
Add V2->V3 project migration test
XVilka Jul 14, 2021
319f104
Add Individual v1->v2 Migration Tests
thestr4ng3r Jul 14, 2021
ca90904
Add docs about project migration tests
thestr4ng3r Jul 14, 2021
df2c869
Update tef test (#1310)
Basstorm Jul 15, 2021
8a0c0f0
Fixes for V2->V3 project migration tests
XVilka Jul 15, 2021
f7ed123
Mark non-C types and signature tests as broken
XVilka Jul 15, 2021
0f11c43
Increase test timeout for AppVeyor
XVilka Jul 16, 2021
231132b
Add Test for loading Windows builtin Types
thestr4ng3r Jul 17, 2021
ec0c702
Add temporary cache while callables sdb is inefficient
thestr4ng3r Jul 17, 2021
f18ebd2
Fix an assert in dwarf_process
thestr4ng3r Jul 17, 2021
a828ca1
Set asm.os=none by default
thestr4ng3r Jul 17, 2021
44e1428
Update tests
Basstorm Jul 19, 2021
2d2defc
Fix type parse assert
XVilka Jul 19, 2021
68c3287
Fallback to void when deserializing missing return type
thestr4ng3r Jul 19, 2021
039d694
Mark db/formats/pe/maxsecXP as broken because of OpenBSD timeout
thestr4ng3r Jul 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ for:
- cd test
- git clone -q --depth 1 https://github.com/rizinorg/rizin-testbins bins
- cd ..
- "%PYTHON%\\Scripts\\ninja -C build test"
- "%PYTHON%\\Scripts\\meson test -C build -t 10"
- cd test
- rz-test -o results.json -L db
- cd ..
Expand Down
45 changes: 25 additions & 20 deletions librz/analysis/analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ RZ_API RzAnalysis *rz_analysis_new(void) {
rz_analysis_hint_storage_init(analysis);
rz_interval_tree_init(&analysis->meta, rz_meta_item_free);
analysis->typedb = rz_type_db_new();
analysis->type_links = ht_up_new0();
analysis->sdb_fmts = sdb_ns(analysis->sdb, "spec", 1);
analysis->sdb_cc = sdb_ns(analysis->sdb, "cc", 1);
analysis->sdb_zigns = sdb_ns(analysis->sdb, "zigns", 1);
Expand Down Expand Up @@ -252,7 +253,9 @@ static bool analysis_set_os(RzAnalysis *analysis, const char *os) {
}
free(analysis->os);
analysis->os = strdup(os);
const char *dir_prefix = rz_sys_prefix(NULL);
rz_type_db_set_os(analysis->typedb, os);
rz_type_db_reload(analysis->typedb, dir_prefix);
return true;
}

Expand All @@ -270,13 +273,19 @@ RZ_API bool rz_analysis_set_triplet(RzAnalysis *analysis, const char *os, const
}

RZ_API bool rz_analysis_set_os(RzAnalysis *analysis, const char *os) {
const char *dir_prefix = rz_sys_prefix(NULL);
const char *dbpath = sdb_fmt(RZ_JOIN_3_PATHS("%s", RZ_SDB_FCNSIGN, "types-%s.sdb"),
dir_prefix, os);
rz_type_db_load_sdb(analysis->typedb, dbpath);
return rz_analysis_set_triplet(analysis, os, NULL, -1);
}

static bool is_arm_thumb_hack(RzAnalysis *analysis, int bits) {
if (!analysis || !analysis->cpu) {
return false;
}
if ((analysis->bits != bits) && !strcmp(analysis->cpu, "arm")) {
return (analysis->bits == 16 && bits == 32) || (analysis->bits == 32 && bits == 16);
}
return false;
}

RZ_API bool rz_analysis_set_bits(RzAnalysis *analysis, int bits) {
switch (bits) {
case 8:
Expand All @@ -285,8 +294,13 @@ RZ_API bool rz_analysis_set_bits(RzAnalysis *analysis, int bits) {
case 32:
case 64:
if (analysis->bits != bits) {
bool is_hack = is_arm_thumb_hack(analysis, bits);
const char *dir_prefix = rz_sys_prefix(NULL);
analysis->bits = bits;
rz_type_db_set_bits(analysis->typedb, bits);
if (!is_hack) {
rz_type_db_reload(analysis->typedb, dir_prefix);
}
rz_analysis_set_reg_profile(analysis);
}
return true;
Expand All @@ -302,6 +316,8 @@ RZ_API void rz_analysis_set_cpu(RzAnalysis *analysis, const char *cpu) {
analysis->pcalign = v;
}
rz_type_db_set_cpu(analysis->typedb, cpu);
const char *dir_prefix = rz_sys_prefix(NULL);
rz_type_db_reload(analysis->typedb, dir_prefix);
}

RZ_API int rz_analysis_set_big_endian(RzAnalysis *analysis, int bigend) {
Expand Down Expand Up @@ -414,6 +430,8 @@ RZ_API void rz_analysis_purge(RzAnalysis *analysis) {
rz_interval_tree_fini(&analysis->meta);
rz_interval_tree_init(&analysis->meta, rz_meta_item_free);
rz_type_db_purge(analysis->typedb);
ht_up_free(analysis->type_links);
analysis->type_links = ht_up_new0();
sdb_reset(analysis->sdb_zigns);
sdb_reset(analysis->sdb_classes);
sdb_reset(analysis->sdb_classes_attrs);
Expand Down Expand Up @@ -472,7 +490,7 @@ RZ_API bool rz_analysis_noreturn_add(RzAnalysis *analysis, const char *name, ut6
}
if (rz_type_func_exist(analysis->typedb, tmp_name)) {
fnl_name = strdup(tmp_name);
} else if (!(fnl_name = rz_type_func_guess(analysis->typedb, (char *)tmp_name))) {
} else if (!(fnl_name = rz_analysis_function_name_guess(analysis->typedb, (char *)tmp_name))) {
if (addr == UT64_MAX) {
if (name) {
sdb_bool_set(NDB, K_NORET_FUNC(name), true, 0);
Expand Down Expand Up @@ -508,19 +526,6 @@ RZ_API bool rz_analysis_noreturn_drop(RzAnalysis *analysis, const char *expr) {
fcnname = expr;
}
sdb_unset(NDB, K_NORET_FUNC(fcnname), 0);
#if 0
char *tmp;
// unnsecessary checks, imho the noreturn db should be pretty simple to allow forward and custom declarations without having to define the function prototype before
if (rz_type_func_exist (NDB, fcnname)) {
sdb_unset (NDB, K_NORET_FUNC (fcnname), 0);
return true;
} else if ((tmp = rz_type_func_guess (NDB, (char *)fcnname))) {
sdb_unset (NDB, K_NORET_FUNC (fcnname), 0);
free (tmp);
return true;
}
eprintf ("Can't find prototype for %s in types database", fcnname);
#endif
return false;
}

Expand All @@ -533,7 +538,7 @@ static bool rz_analysis_noreturn_at_name(RzAnalysis *analysis, const char *name)
if (rz_analysis_is_noreturn(analysis, name)) {
return true;
}
char *tmp = rz_type_func_guess(analysis->typedb, (char *)name);
char *tmp = rz_analysis_function_name_guess(analysis->typedb, (char *)name);
if (tmp) {
if (rz_analysis_is_noreturn(analysis, tmp)) {
free(tmp);
Expand Down Expand Up @@ -616,7 +621,7 @@ RZ_API bool rz_analysis_noreturn_at(RzAnalysis *analysis, ut64 addr) {
RZ_API RzList *rz_analysis_noreturn_functions(RzAnalysis *analysis) {
rz_return_val_if_fail(analysis, NULL);
// At first we read all noreturn functions from the Types DB
RzList *noretl = rz_type_noreturn_functions(analysis->typedb);
RzList *noretl = rz_type_noreturn_function_names(analysis->typedb);
// Then we propagate all noreturn functions that were inferred by
// the analysis process
SdbKv *kv;
Expand Down
32 changes: 32 additions & 0 deletions librz/analysis/d/functions-android.sdb.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
__android_log_print=func
func.__android_log_print.args=3
func.__android_log_print.arg.0=androidLogPriority,prio
func.__android_log_print.arg.1=const char *,tag
func.__android_log_print.arg.2=const char *,fmt
func.__android_log_print.ret=int

__android_log_write=func
func.__android_log_write.args=3
func.__android_log_write.arg.0=androidLogPriority,prio
func.__android_log_write.arg.1=const char *,tag
func.__android_log_write.arg.2=const char *,fmt
func.__android_log_write.ret=int

__android_log_vprint=func
func.__android_log_vprint.args=3
func.__android_log_vprint.arg.0=androidLogPriority,prio
func.__android_log_vprint.arg.1=const char *,tag
func.__android_log_vprint.arg.2=const char *,fmt
func.__android_log_vprint.ret=int

__android_log_assert=func
func.__android_log_assert.args=3
func.__android_log_assert.arg.0=const char *,cond
func.__android_log_assert.arg.1=const char *,tag
func.__android_log_assert.arg.2=const char *,fmt
func.__android_log_assert.ret=void

__stack_chk_fail=func
func.__stack_chk_fail.args=0
func.__stack_chk_fail.noreturn=true
func.__stack_chk_fail.ret=void
Loading