Skip to content

Commit

Permalink
prevent rewriting for array element type
Browse files Browse the repository at this point in the history
Fix issue iovisor#2352.
Corresponding llvm bug: https://bugs.llvm.org/show_bug.cgi?id=41918.

If the element type is array type, the rewriter may generate code like
  addr = ({ typeof(__u8 [16]) _val; __builtin_memset(&_val, 0, sizeof(_val));
            bpf_probe_read(&_val, sizeof(_val), (u64)&daddr->s6_addr); _val; })
for something like
  addr = daddr->s6_addr;
where s6_addr is an array.

The above code has an issue then as addr is pointing to some data
which is out of scope, which meaning compiler is free to use the space.

Let us disable such transformation until we find a good solution.

Signed-off-by: Yonghong Song <[email protected]>
  • Loading branch information
yonghong-song committed Feb 6, 2020
1 parent 685ec23 commit a4834a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/cc/frontends/clang/b_frontend_action.cc
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ bool ProbeVisitor::VisitMemberExpr(MemberExpr *E) {
if (!ProbeChecker(base, ptregs_, track_helpers_).needs_probe())
return true;

// If the base is an array, we will skip rewriting. See issue #2352.
if (E->getType()->isArrayType())
return true;

string rhs = rewriter_.getRewrittenText(expansionRange(SourceRange(rhs_start, GET_ENDLOC(E))));
string base_type = base->getType()->getPointeeType().getAsString();
string pre, post;
Expand All @@ -509,6 +513,10 @@ bool ProbeVisitor::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
if (is_addrof_)
return true;

// If the base is an array, we will skip rewriting. See issue #2352.
if (E->getType()->isArrayType())
return true;

if (!rewriter_.isRewritable(GET_BEGINLOC(E)))
return true;

Expand Down
16 changes: 8 additions & 8 deletions tools/tcptop.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ def range_check(string):
} else if (family == AF_INET6) {
struct ipv6_key_t ipv6_key = {.pid = pid};
__builtin_memcpy(&ipv6_key.saddr,
sk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32, sizeof(ipv6_key.saddr));
__builtin_memcpy(&ipv6_key.daddr,
sk->__sk_common.skc_v6_daddr.in6_u.u6_addr32, sizeof(ipv6_key.daddr));
bpf_probe_read(&ipv6_key.saddr, sizeof(ipv6_key.saddr),
&sk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32);
bpf_probe_read(&ipv6_key.daddr, sizeof(ipv6_key.daddr),
&sk->__sk_common.skc_v6_daddr.in6_u.u6_addr32);
ipv6_key.lport = sk->__sk_common.skc_num;
dport = sk->__sk_common.skc_dport;
ipv6_key.dport = ntohs(dport);
Expand Down Expand Up @@ -153,10 +153,10 @@ def range_check(string):
} else if (family == AF_INET6) {
struct ipv6_key_t ipv6_key = {.pid = pid};
__builtin_memcpy(&ipv6_key.saddr,
sk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32, sizeof(ipv6_key.saddr));
__builtin_memcpy(&ipv6_key.daddr,
sk->__sk_common.skc_v6_daddr.in6_u.u6_addr32, sizeof(ipv6_key.daddr));
bpf_probe_read(&ipv6_key.saddr, sizeof(ipv6_key.saddr),
&sk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32);
bpf_probe_read(&ipv6_key.daddr, sizeof(ipv6_key.daddr),
&sk->__sk_common.skc_v6_daddr.in6_u.u6_addr32);
ipv6_key.lport = sk->__sk_common.skc_num;
dport = sk->__sk_common.skc_dport;
ipv6_key.dport = ntohs(dport);
Expand Down

0 comments on commit a4834a6

Please sign in to comment.