uint8_t* codeitem_end(
const
uint8_t **pData)
{
uint32_t num_of_list = DecodeUnsignedLeb128(pData);
for
(;num_of_list>0;num_of_list--) {
int32_t num_of_handlers=DecodeSignedLeb128(pData);
int
num=num_of_handlers;
if
(num_of_handlers<=0) {
num=-num_of_handlers;
}
for
(; num > 0; num--) {
DecodeUnsignedLeb128(pData);
DecodeUnsignedLeb128(pData);
}
if
(num_of_handlers<=0) {
DecodeUnsignedLeb128(pData);
}
}
return
(uint8_t*)(*pData);
}
extern
"C"
char
*base64_encode(
char
*str,
long
str_len,
long
* outlen){
long
len;
char
*res;
int
i,j;
const
char
*base64_table=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
;
if
(str_len % 3 == 0)
len=str_len/3*4;
else
len=(str_len/3+1)*4;
res=(
char
*)
malloc
(
sizeof
(
char
)*(len+1));
res[len]=
'\0'
;
*outlen=len;
for
(i=0,j=0;i<len-2;j+=3,i+=4)
{
res[i]=base64_table[str[j]>>2];
res[i+1]=base64_table[(str[j]&0x3)<<4 | (str[j+1]>>4)];
res[i+2]=base64_table[(str[j+1]&0xf)<<2 | (str[j+2]>>6)];
res[i+3]=base64_table[str[j+2]&0x3f];
}
switch
(str_len % 3)
{
case
1:
res[i-2]=
'='
;
res[i-1]=
'='
;
break
;
case
2:
res[i-1]=
'='
;
break
;
}
return
res;
}
extern
"C"
void
saveDexFileByExeMethod(ArtMethod* artmethod) REQUIRES_SHARED(Locks::mutator_lock_) {
char
*dexfilepath=(
char
*)
malloc
(
sizeof
(
char
)*1000);
if
(dexfilepath==nullptr)
{
LOG(ERROR)<<
"wayne::saveDexFileByExeMethod,methodName:"
<<artmethod->PrettyMethod().c_str()<<
"malloc 1000 byte failed"
;
return
;
}
int
result=0;
int
fcmdline =-1;
char
szCmdline[64]= {0};
char
szProcName[256] = {0};
int
procid = getpid();
sprintf
(szCmdline,
"/proc/%d/cmdline"
, procid);
fcmdline = open(szCmdline, O_RDONLY|O_CREAT,0644);
if
(fcmdline >0)
{
result=read(fcmdline, szProcName,256);
if
(result<0)
{
LOG(ERROR) <<
"wayne::saveDexFileByExeMethod,open cmdline file error"
;
}
close(fcmdline);
}
if
(szProcName[0])
{
const
DexFile* dex_file = artmethod->GetDexFile();
const
uint8_t* begin_=dex_file->Begin();
size_t
size_=dex_file->Size();
memset
(dexfilepath,0,1000);
int
size_int_=(
int
)size_;
memset
(dexfilepath,0,1000);
sprintf
(dexfilepath,
"/data/data/%s/wayne"
,szProcName);
mkdir(dexfilepath,0777);
memset
(dexfilepath,0,1000);
sprintf
(dexfilepath,
"/data/data/%s/wayne/%d_dexfile_execute.dex"
,szProcName,size_int_);
if
(access(dexfilepath,F_OK)!=-1){
LOG(ERROR) << dexfilepath <<
"File have exist"
;
}
else
{
int
fp=open(dexfilepath,O_CREAT|O_APPEND|O_RDWR,0666);
if
(fp>0)
{
result=write(fp,(
void
*)begin_,size_);
if
(result<0)
{
LOG(ERROR) <<
"wayne::saveDexFileByExeMethod,open dexfilepath error"
;
}
fsync(fp);
close(fp);
memset
(dexfilepath,0,1000);
sprintf
(dexfilepath,
"/data/data/%s/wayne/%d_classlist_execute.txt"
,szProcName,size_int_);
int
classlistfile=open(dexfilepath,O_CREAT|O_APPEND|O_RDWR,0666);
if
(classlistfile>0)
{
for
(
size_t
ii= 0; ii< dex_file->NumClassDefs(); ++ii)
{
const
dex::ClassDef& class_def = dex_file->GetClassDef(ii);
const
char
* descriptor = dex_file->GetClassDescriptor(class_def);
result=write(classlistfile,(
void
*)descriptor,
strlen
(descriptor));
if
(result<0)
{
LOG(ERROR) <<
"wayne::saveDexFileByExeMethod,write classlistfile file error"
;
}
const
char
* temp=
"\n"
;
result=write(classlistfile,(
void
*)temp,1);
if
(result<0)
{
LOG(ERROR) <<
"wayne::saveDexFileByExeMethod,write classlistfile file error"
;
}
}
fsync(classlistfile);
close(classlistfile);
}
}
}
}
if
(dexfilepath!=nullptr)
{
free
(dexfilepath);
dexfilepath=nullptr;
}
}
extern
"C"
void
saveArtMethod(ArtMethod* artMethod) REQUIRES_SHARED(Locks::mutator_lock_) {
char
*dexfilepath=(
char
*)
malloc
(
sizeof
(
char
)*1000);
if
(dexfilepath==nullptr)
{
LOG(ERROR) <<
"wayne::saveArtMethod,methodname:"
<<artMethod->PrettyMethod().c_str()<<
"malloc 1000 byte failed"
;
return
;
}
int
result=0;
int
fcmdline =-1;
char
szCmdline[64]= {0};
char
szProcName[256] = {0};
int
procid = getpid();
sprintf
(szCmdline,
"/proc/%d/cmdline"
, procid);
fcmdline = open(szCmdline, O_RDONLY|O_CREAT,0644);
if
(fcmdline >0)
{
result=read(fcmdline, szProcName,256);
if
(result<0)
{
LOG(ERROR) <<
"wayne::saveArtMethod,open cmdline file file error"
;
}
close(fcmdline);
}
if
(szProcName[0])
{
const
DexFile* dex_file = artMethod->GetDexFile();
const
uint8_t* begin_=dex_file->Begin();
size_t
size_=dex_file->Size();
memset
(dexfilepath,0,1000);
int
size_int_=(
int
)size_;
memset
(dexfilepath,0,1000);
sprintf
(dexfilepath,
"/data/data/%s/wayne/"
,szProcName);
mkdir(dexfilepath,0777);
memset
(dexfilepath,0,1000);
sprintf
(dexfilepath,
"/data/data/%s/wayne/%d_dexfile.dex"
,szProcName,size_int_);
if
(access(dexfilepath,F_OK) != -1){
LOG(ERROR) <<
"invoke DexFile have exist "
;
}
else
{
int
fp=open(dexfilepath,O_CREAT|O_APPEND|O_RDWR,0666);
if
(fp>0)
{
result=write(fp,(
void
*)begin_,size_);
if
(result<0)
{
LOG(ERROR) <<
"wayne::saveArtMethod,open dexfilepath file error"
;
}
fsync(fp);
close(fp);
memset
(dexfilepath,0,1000);
sprintf
(dexfilepath,
"/data/data/%s/wayne/%d_classlist.txt"
,szProcName,size_int_);
int
classlistfile=open(dexfilepath,O_CREAT|O_APPEND|O_RDWR,0666);
if
(classlistfile>0)
{
for
(
size_t
ii= 0; ii< dex_file->NumClassDefs(); ++ii)
{
const
dex::ClassDef& class_def = dex_file->GetClassDef(ii);
const
char
* descriptor = dex_file->GetClassDescriptor(class_def);
result=write(classlistfile,(
void
*)descriptor,
strlen
(descriptor));
if
(result<0)
{
LOG(ERROR) <<
"wayne::saveArtMethod,write classlistfile file error"
;
}
const
char
* temp=
"\n"
;
result=write(classlistfile,(
void
*)temp,1);
if
(result<0)
{
LOG(ERROR) <<
"wayne::saveArtMethod,write classlistfile file error"
;
}
}
fsync(classlistfile);
close(classlistfile);
}
}
}
const
dex::CodeItem* code_item = artMethod->GetCodeItem();
if
(LIKELY(code_item != nullptr))
{
uint8_t *item=(uint8_t *) code_item;
uint32_t code_item_len = dex_file->GetCodeItemSize(*code_item);
memset
(dexfilepath,0,1000);
int
size_int = (
int
)dex_file->Size();
uint32_t method_idx = artMethod->GetDexMethodIndex();
sprintf
(dexfilepath,
"/data/data/%s/wayne/%d_ins_%d.bin"
,szProcName,size_int,(
int
)gettidv1());
int
fp2=open(dexfilepath,O_CREAT|O_APPEND|O_RDWR,0666);
if
(fp2>0){
lseek(fp2,0,SEEK_END);
memset
(dexfilepath,0,1000);
int
offset=(
int
)(item - begin_);
sprintf
(dexfilepath,
"{name:'%s',method_idx:'%d',offset:'%d',code_item_len:'%d',ins:'"
,artMethod->PrettyMethod().c_str(),method_idx,offset,code_item_len);
int
contentlength=0;
while
(dexfilepath[contentlength]!=0) contentlength++;
result=write(fp2,(
void
*)dexfilepath,contentlength);
if
(result<0)
{
LOG(ERROR) <<
"wayne::saveArtMethod,write ins file error"
;
}
long
outlen=0;
char
* base64result=base64_encode((
char
*)item,(
long
)code_item_len,&outlen);
result=write(fp2,base64result,outlen);
if
(result<0)
{
LOG(ERROR) <<
"wayne::saveArtMethod,write ins file error"
;
}
result=write(fp2,
"'};"
,3);
if
(result<0)
{
LOG(ERROR) <<
"wayne::saveArtMethod,write ins file error"
;
}
fsync(fp2);
close(fp2);
if
(base64result!=nullptr){
free
(base64result);
base64result=nullptr;
}
}
}
}
if
(dexfilepath!=nullptr)
{
free
(dexfilepath);
dexfilepath=nullptr;
}
}
extern
"C"
void
myWayneCall(ArtMethod* artmethod) REQUIRES_SHARED(Locks::mutator_lock_) {
JValue *result=nullptr;
Thread *self=nullptr;
uint32_t temp=6;
uint32_t* args=&temp;
uint32_t args_size=6;
artmethod->Invoke(self, args, args_size, result,
"aaa"
);
}