Skip to content

Commit

Permalink
type/format: fix union format generation
Browse files Browse the repository at this point in the history
  • Loading branch information
XVilka committed Mar 3, 2024
1 parent b8ddd4d commit 660146d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
9 changes: 6 additions & 3 deletions librz/type/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -2817,8 +2817,9 @@ static void base_type_to_format_no_unfold(const RzTypeDB *typedb, RZ_NONNULL RzB
}
case RZ_BASE_TYPE_KIND_UNION: {
// In `pf` unions defined like structs but all have 0 offset,
// which is why it uses `0` character as a marker
rz_strbuf_append(format, "0");
// which is why it uses `0` character as a marker when being unfolded,
// but when folded, they use the same `?` character as structures.
rz_strbuf_append(format, "?");
rz_strbuf_appendf(fields, "(%s)%s ", type->name, identifier);
break;
}
Expand Down Expand Up @@ -2905,6 +2906,7 @@ static void base_type_to_format_unfold(const RzTypeDB *typedb, RZ_NONNULL RzBase
case RZ_BASE_TYPE_KIND_UNION: {
// In `pf` unions defined like structs but all have 0 offset,
// which is why it uses `0` character as a marker
rz_strbuf_append(format, "0");
RzTypeUnionMember *memb;
rz_vector_foreach(&type->union_data.members, memb) {
const char *membtype = type_to_identifier(typedb, memb->type);
Expand Down Expand Up @@ -2999,7 +3001,8 @@ static void type_to_format(const RzTypeDB *typedb, RzStrBuf *buf, RzType *type)
if (format) {
rz_strbuf_append(buf, format);
} else {
if (type->identifier.kind == RZ_TYPE_IDENTIFIER_KIND_STRUCT) {
if (type->identifier.kind == RZ_TYPE_IDENTIFIER_KIND_STRUCT ||
type->identifier.kind == RZ_TYPE_IDENTIFIER_KIND_UNION) {
rz_strbuf_append(buf, "?");
} else if (type->identifier.kind == RZ_TYPE_IDENTIFIER_KIND_ENUM) {
rz_strbuf_append(buf, "E");
Expand Down
69 changes: 61 additions & 8 deletions test/db/cmd/types
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ tu xoo
tuc xoo
EOF
EXPECT=<<EOF
pf "ddd x y z"
pf "0ddd x y z"
union xoo {
int x;
int y;
Expand Down Expand Up @@ -313,7 +313,7 @@ te bar
tec bar
EOF
EXPECT=<<EOF
pf "i b"
pf "0i b"
union bla {
uint b;
};
Expand Down Expand Up @@ -744,7 +744,7 @@ tu x
tuc x
EOF
EXPECT=<<EOF
pf "fd a b"
pf "0fd a b"
union x {
float a;
int b;
Expand Down Expand Up @@ -842,7 +842,7 @@ EXPECT=<<EOF
EOF
RUN

NAME=tp
NAME=tp struct
FILE==
CMDS=<<EOF
td "struct person { int age; char name[10]; }"
Expand All @@ -861,6 +861,59 @@ EXPECT=<<EOF
EOF
RUN

NAME=tp union
FILE==
CMDS=<<EOF
e asm.arch=arm
e asm.bits=64
td "union bla { int a; int b; float c; }"
td "struct foo { union poo { int a; int b; long long c; } q; char *p; }"
td "struct alb { int a; union moo { int t; char b; long long c; } f; void *goo; }"
td "struct simple { union y { int a; int b; } g; void *val_4; void *val_5; void *val_6; }"
wx 13ffaa00
s 8
w Carlossssssssssssssssssssssssssssss
s 10
t bla
tp bla @ 0
t foo
tp foo @ 0
t alb
tp alb @ 0
t simple
tp simple @ 0
EOF
EXPECT=<<EOF
pf "0ddf a b c"
a : 0x00000000 = 11206419
b : 0x00000000 = 11206419
c : 0x00000000 = 1.57035377e-38
pf "?z (poo)q p"
q :
union<poo>
a : 0x00000000 = 11206419
b : 0x00000000 = 11206419
c : 0x00000000 = (qword)0x0000000000aaff13
p : 0x00000008 = "Carlossssssssssssssssssssssssssssss"
pf "d?p a (moo)f goo"
a : 0x00000000 = 11206419
f :
union<moo>
t : 0x00000004 = 0
b : 0x00000004 = '.'
c : 0x00000004 = (qword)0x6c72614300000000
goo : 0x0000000c = (qword)0x737373737373736f
pf "?ppp (y)g val_4 val_5 val_6"
g :
union<y>
a : 0x00000000 = 11206419
b : 0x00000000 = 11206419
val_4 : 0x00000004 = (qword)0x6c72614300000000
val_5 : 0x0000000c = (qword)0x737373737373736f
val_6 : 0x00000014 = (qword)0x7373737373737373
EOF
RUN

NAME=tp blocksize
FILE==
BROKEN=1
Expand Down Expand Up @@ -1257,7 +1310,7 @@ s 0x000006d2
pd 1
EOF
EXPECT=<<EOF
pf "[50]c[50]c[100]c title author subject"
pf "0[50]c[50]c[100]c title author subject"
var union Books books @ stack - 0x78
| 0x000006d2 488d4590 lea rax, [books.title]
EOF
Expand Down Expand Up @@ -2210,9 +2263,9 @@ tu foo
t foo
EOF
EXPECT=<<EOF
pfn "foo" "dc a b"
pf "dc a b"
pf "dc a b"
pfn "foo" "0dc a b"
pf "0dc a b"
pf "0dc a b"
EOF
RUN

Expand Down

0 comments on commit 660146d

Please sign in to comment.