Skip to content

Commit

Permalink
Support basename() for addresses that are not DTrace pointers
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Loh <eugene.loh@oracle.com>
Reviewed-by: Kris Van Hees <kris.van.hees@oracle.com>
  • Loading branch information
euloh authored and kvanhees committed Feb 20, 2023
1 parent 8e0b0fe commit 850eb0d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
13 changes: 9 additions & 4 deletions bpf/basename.S
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ dt_basename :
mov SRC, %r1
mov DST, %r2

/*
* Copy src to dst for two reasons:
* - to get the string length
* - to have a copy that we can access directly
*/
/* r0 = bpf_probe_read_str(dst, STRSZ + 1, src) */
mov %r1, DST
lddw %r2, STRSZ
Expand All @@ -44,8 +49,8 @@ dt_basename :
sub LEN, 1
/* if (len s< 0) goto Lslash */
jslt LEN, 0, .Lslash
/* if (src[len] == '/') goto Lend */
mov %r1, SRC
/* if (dst[len] == '/') goto Lend */
mov %r1, DST
add %r1, LEN
ldxb %r1, [%r1+0]
and %r1, 0xff
Expand All @@ -61,8 +66,8 @@ dt_basename :
sub BGN, 1
/* if (bgn s< 0) goto Lcopy */
jslt BGN, 0, .Lcopy
/* if (src[bgn] != '/') goto Lbgn */
mov %r1, SRC
/* if (dst[bgn] != '/') goto Lbgn */
mov %r1, DST
add %r1, BGN
ldxb %r1, [%r1+0]
and %r1, 0xff
Expand Down
24 changes: 24 additions & 0 deletions test/unittest/funcs/tst.basename_nonDPTR.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Oracle Linux DTrace.
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/

#pragma D option quiet
#pragma D option destructive

BEGIN
{
/* "/foo/bar/baz/.//" */
system("printf '\x2f\x66\x6f\x6f\x2f\x62\x61\x72\x2f\x62\x61\x7a\x2f\x2e\x2f\x2f' > /dev/null 2>&1");
}

syscall::write:entry
/ppid == $pid/
{
printf("|%s|\n", basename((void *)arg1));
exit(0);
}

ERROR { exit(1); }
2 changes: 2 additions & 0 deletions test/unittest/funcs/tst.basename_nonDPTR.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
|.|

0 comments on commit 850eb0d

Please sign in to comment.