forked from linzj/force_load_shared_library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgot_finder_test.cpp
139 lines (127 loc) · 2.58 KB
/
got_finder_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "got_finder.h"
#include "ptracer.h"
#include "log.h"
#include "found_info.h"
#include <stdio.h>
#include <assert.h>
#include <dlfcn.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/prctl.h>
class test_got_finder_client : public got_finder_client
{
public:
test_got_finder_client ();
inline bool
is_okay () const
{
return okay_;
};
private:
virtual bool found (const found_info &info);
bool okay_;
};
test_got_finder_client::test_got_finder_client () : okay_ (false) {}
bool
test_got_finder_client::found (const found_info &info)
{
LOGI ("test_got_finder_client::found, found dlopen at %08lx\n", info.target);
okay_ = true;
return false;
}
void
hold_dlopen ()
{
dlopen ("libshit.so", RTLD_NOW);
}
static void
do_test ()
{
ptracer ptracer (getppid ());
got_finder finder;
assert (ptracer.attach ());
test_got_finder_client client;
finder.find (&ptracer, "dlopen", getppid (), &client);
ptracer.detach ();
assert (client.is_okay ());
}
int
main ()
{
pid_t forkret;
int sv[2];
if (-1 == socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
{
LOGE ("pipe fails %s\n", strerror (errno));
return 0;
}
forkret = fork ();
if (forkret > 0)
{
int status;
int num = 0;
int readret;
#ifdef PR_SET_PTRACER
int ptrctlret = prctl (PR_SET_PTRACER, forkret, 0, 0, 0);
int writeret;
do
{
writeret = write (sv[0], &ptrctlret, sizeof (ptrctlret));
}
while (writeret == -1 && errno == EINTR);
if (ptrctlret == -1)
{
LOGE ("ptrctl fails %s\n", strerror (errno));
close (sv[1]);
goto fails;
}
#endif
close (sv[1]);
do
{
readret = read (sv[0], &num, sizeof (num));
}
while (readret == -1 && errno == EINTR);
#ifdef PR_SET_PTRACER
fails:
#endif
close (sv[0]);
waitpid (forkret, &status, 0);
return num;
}
else if (forkret == 0)
{
int writeret;
#ifdef PR_SET_PTRACER
int val;
int readret;
do
{
readret = read (sv[1], &val, sizeof (val));
}
while (readret == -1 && errno == EINTR);
if (val == -1)
{
_exit (0);
}
#endif
close (sv[0]);
do_test ();
int num = 0;
do
{
writeret = write (sv[1], &num, sizeof (num));
}
while (writeret == -1 && errno == EINTR);
close (sv[1]);
_exit (0);
}
else
{
LOGE ("fork fails %s\n", strerror (errno));
return 0;
}
return 0;
}