2828#include <sys/socket.h>
2929#include <sys/un.h>
3030#include <unistd.h>
31+ #include <fcntl.h>
32+ #include <sys/stat.h>
33+ #include <errno.h>
34+ #include <limits.h>
35+ #include <dlfcn.h>
3136
3237#include "logging.h"
3338
3944
4045#define ID_VEC (is64 , is_debug ) (((is64) << 1) | (is_debug))
4146
42- const char kSockName [] = "5291374ceda0aef7c5d86cd2a4f6a3ac\0" ;
47+ const char sock_name [] = "5291374ceda0aef7c5d86cd2a4f6a3ac\0" ;
48+ const char * linker_path = LP_SELECT ("/apex/com.android.runtime/bin/linker" , "/apex/com.android.runtime/bin/linker64" );
4349
4450static ssize_t xrecvmsg (int sockfd , struct msghdr * msg , int flags ) {
4551 int rec = recvmsg (sockfd , msg , flags );
@@ -77,6 +83,7 @@ static int recv_fd(int sockfd) {
7783
7884 int result ;
7985 memcpy (& result , data , sizeof (int ));
86+
8087 return result ;
8188}
8289
@@ -118,13 +125,7 @@ static void write_string(int fd, const char *str, size_t len) {
118125
119126int main (int argc , char * * argv ) {
120127 LOGD ("dex2oat wrapper ppid=%d, uid=%d" , getppid (), getuid ());
121-
122- if (getenv ("LD_LIBRARY_PATH" ) == NULL ) {
123- char const * libenv = LP_SELECT (
124- "LD_LIBRARY_PATH=/apex/com.android.art/lib:/apex/com.android.os.statsd/lib" ,
125- "LD_LIBRARY_PATH=/apex/com.android.art/lib64:/apex/com.android.os.statsd/lib64" );
126- putenv ((char * )libenv );
127- }
128+ unsetenv ("LD_LIBRARY_PATH" );
128129
129130 int sock_fd = socket (AF_UNIX , SOCK_STREAM , 0 );
130131 if (sock_fd < 0 ) {
@@ -137,7 +138,7 @@ int main(int argc, char **argv) {
137138 .sun_family = AF_UNIX ,
138139 .sun_path = { 0 },
139140 };
140- strlcpy (sock .sun_path + 1 , kSockName , sizeof (sock .sun_path ) - 1 );
141+ strlcpy (sock .sun_path + 1 , sock_name , sizeof (sock .sun_path ) - 1 );
141142
142143 size_t len = sizeof (sa_family_t ) + strlen (sock .sun_path + 1 ) + 1 ;
143144 sock_fd = socket (AF_UNIX , SOCK_STREAM , 0 );
@@ -158,8 +159,11 @@ int main(int argc, char **argv) {
158159
159160 return 1 ;
160161 }
162+
163+ char stock_fd_path [64 ];
164+ snprintf (stock_fd_path , sizeof (stock_fd_path ), "/proc/%d/fd/%d" , getpid (), stock_fd );
161165
162- LOGI ("stock dex2oat fd: %d" , stock_fd );
166+ LOGI ("stock dex2oat fd: %d (%s) " , stock_fd , stock_fd_path );
163167
164168 close (sock_fd );
165169
@@ -187,9 +191,14 @@ int main(int argc, char **argv) {
187191 if (is_in_denylist ) {
188192 LOGD ("App is in denylist, exiting" );
189193
190- fexecve (stock_fd , (char * * )argv , environ );
194+ char * new_argv [argc + 2 ];
195+ memset (new_argv , 0 , sizeof (new_argv ));
191196
192- LOGE ("fexecve failed" );
197+ new_argv [0 ] = stock_fd_path ;
198+ memcpy (& new_argv [1 ], & argv [1 ], sizeof (char * ) * argc );
199+
200+ execve (linker_path , new_argv , environ );
201+ LOGE ("execve failed" );
193202
194203 close (stock_fd );
195204
@@ -229,11 +238,16 @@ int main(int argc, char **argv) {
229238 snprintf (liboat_fd_path , sizeof (liboat_fd_path ), "/proc/%d/fd/%d" , getpid (), hooker_fd );
230239
231240 setenv ("LD_PRELOAD" , liboat_fd_path , 1 );
232- LOGD ("Set env LD_PRELOAD=%s" , liboat_fd_path );
233241
234- fexecve (stock_fd , (char * * )argv , environ );
242+ char * new_argv [argc + 3 ];
243+ memset (new_argv , 0 , sizeof (new_argv ));
244+
245+ new_argv [0 ] = stock_fd_path ;
246+ memcpy (& new_argv [1 ], & argv [1 ], sizeof (char * ) * argc );
247+
248+ execve (linker_path , new_argv , environ );
235249
236- PLOGE ("fexecve failed" );
250+ PLOGE ("execve failed" );
237251
238252 close (stock_fd );
239253
0 commit comments