Skip to content

Commit f51198a

Browse files
committed
feat: add test for .plt.sec
1 parent feb8824 commit f51198a

7 files changed

Lines changed: 158 additions & 3 deletions

File tree

callgrind/tests/Makefile.am

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ include $(top_srcdir)/Makefile.tool-tests.am
44
SUBDIRS = .
55
DIST_SUBDIRS = .
66

7-
dist_noinst_SCRIPTS = filter_stderr filter_inline
7+
dist_noinst_SCRIPTS = filter_stderr filter_inline filter_plt_sec
88

99
EXTRA_DIST = \
1010
ann1.post.exp ann1.stderr.exp ann1.vgtest \
@@ -26,9 +26,10 @@ EXTRA_DIST = \
2626
find-source.vgtest find-source.stderr.exp find-source.post.exp \
2727
inline-samefile.vgtest inline-samefile.stderr.exp inline-samefile.stdout.exp inline-samefile.post.exp \
2828
inline-crossfile.vgtest inline-crossfile.stderr.exp inline-crossfile.stdout.exp inline-crossfile.post.exp \
29-
inline-crossfile-helper1.h inline-crossfile-helper2.h filter_inline
29+
inline-crossfile-helper1.h inline-crossfile-helper2.h filter_inline \
30+
plt-sec-skip.vgtest plt-sec-skip.stderr.exp plt-sec-skip.stdout.exp plt-sec-skip.post.exp
3031

31-
check_PROGRAMS = clreq simwork threads inline-samefile inline-crossfile
32+
check_PROGRAMS = clreq simwork threads inline-samefile inline-crossfile plt_sec_test
3233

3334
AM_CFLAGS += $(AM_FLAG_M3264_PRI)
3435
AM_CXXFLAGS += $(AM_FLAG_M3264_PRI)
@@ -37,4 +38,7 @@ AM_CXXFLAGS += $(AM_FLAG_M3264_PRI)
3738
inline_samefile_CFLAGS = $(AM_CFLAGS) -O2 -g
3839
inline_crossfile_CFLAGS = $(AM_CFLAGS) -O2 -g
3940

41+
# We need to generate .plt.sec sections by enabling control flow protection
42+
plt_sec_test_CFLAGS = $(AM_CFLAGS) -fcf-protection=full -O2
43+
4044
threads_LDADD = -lpthread

callgrind/tests/filter_plt_sec

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/sh
2+
3+
CGout=$1
4+
5+
# This is a wrong output of `puts` which includes a call to the .plt.sec section:
6+
# ```
7+
# fn=(408) puts
8+
# 0 9
9+
# cob=(3)
10+
# cfi=(3)
11+
# cfn=(410) 0x0000000004869530
12+
# calls=1 0
13+
# 0 16
14+
# 0 18
15+
# cfn=(414)
16+
# calls=1 0
17+
# 0 179
18+
# 0 46
19+
# cfn=(452)
20+
# calls=1 0
21+
# 0 183
22+
# 0 2
23+
#
24+
# ```
25+
26+
# We should expect this:
27+
# ```
28+
# fn=(408) puts
29+
# 0 9
30+
# cfn=(412) __strlen_avx2
31+
# calls=1 0
32+
# 0 14
33+
# 0 2
34+
# 0 18
35+
# cfn=(414) _IO_file_xsputn@@GLIBC_2.2.5
36+
# calls=1 0
37+
# 0 179
38+
# 0 46
39+
# cfn=(452) __overflow
40+
# calls=1 0
41+
# 0 183
42+
# 0 2
43+
#
44+
# ```
45+
#
46+
# Then just print the cfn lines
47+
48+
awk '
49+
/^fn=\([0-9]+\) puts$/ { in_puts = 1 }
50+
in_puts && /^fn=/ && !/^fn=\([0-9]+\) puts$/ { exit }
51+
in_puts {
52+
# Only print cfn lines (partial match, only check if we start with cfn=()
53+
if (/^cfn=\(/) {
54+
# Extract the function name (just split by space and take second part)
55+
split($0, parts, " ")
56+
print parts[2]
57+
} else {
58+
next
59+
}
60+
}
61+
' "$CGout"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__strlen_avx2
2+
_IO_file_xsputn@@GLIBC_2.2.5
3+
__overflow
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
Starting plt_sec_test
3+
String length: 11
4+
Allocated and initialized 10 ints
5+
Direct printf call
6+
Sum result: 4950
7+
plt_sec_test completed
8+
9+
Events : Ir
10+
Collected :
11+
12+
I refs:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Starting plt_sec_test
2+
String length: 11
3+
Allocated and initialized 10 ints
4+
Direct printf call
5+
Sum result: 4950
6+
plt_sec_test completed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
prog: plt_sec_test
2+
vgopts: --tool=callgrind --skip-plt=yes
3+
post: ./filter_plt_sec callgrind.out.* plt-sec-skip
4+
cleanup: rm callgrind.out.*

callgrind/tests/plt_sec_test.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "../callgrind.h"
5+
6+
// Forward declarations
7+
void lib_function1(void);
8+
void lib_function2(void);
9+
void lib_function3(void);
10+
11+
// Simple helper functions to do some work
12+
int compute_sum(int n) {
13+
int sum = 0;
14+
for (int i = 0; i < n; i++) {
15+
sum += i;
16+
}
17+
return sum;
18+
}
19+
20+
// Functions that call library functions
21+
void lib_function1(void) {
22+
// Call strlen (from libc)
23+
const char *str = "test string";
24+
int len = strlen(str);
25+
printf("String length: %d\n", len);
26+
}
27+
28+
void lib_function2(void) {
29+
// Call malloc/free (from libc)
30+
int *ptr = (int *)malloc(sizeof(int) * 10);
31+
if (ptr) {
32+
for (int i = 0; i < 10; i++) {
33+
ptr[i] = i;
34+
}
35+
printf("Allocated and initialized 10 ints\n");
36+
free(ptr);
37+
}
38+
}
39+
40+
void lib_function3(void) {
41+
// Call printf directly
42+
printf("Direct printf call\n");
43+
}
44+
45+
int main(void) {
46+
printf("Starting plt_sec_test\n");
47+
48+
// Request callgrind to start collecting data
49+
CALLGRIND_START_INSTRUMENTATION;
50+
51+
// Call functions that will use PLT/PLT.SEC
52+
lib_function1();
53+
lib_function2();
54+
lib_function3();
55+
56+
// Do some work
57+
int result = compute_sum(100);
58+
printf("Sum result: %d\n", result);
59+
60+
// Request callgrind to dump stats
61+
CALLGRIND_DUMP_STATS;
62+
63+
printf("plt_sec_test completed\n");
64+
return 0;
65+
}

0 commit comments

Comments
 (0)