Skip to content

Commit

Permalink
Merge pull request #46 from AnxiangLemon/main
Browse files Browse the repository at this point in the history
update rpc call
  • Loading branch information
NSEcho authored Sep 21, 2024
2 parents 5439ad0 + 07ba897 commit 28875e8
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions frida/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,20 @@ func (s *Script) DisableDebugger() error {
// ExportsCall will try to call fn from the rpc.exports with args provided
func (s *Script) ExportsCall(fn string, args ...any) any {
ch := s.makeExportsCall(fn, args...)
defer releaseChannel(ch)
ret := <-ch
return ret
}

// ExportsCallWithContext will try to call fn from the rpc.exports with args provided using context provided.
func (s *Script) ExportsCallWithContext(ctx context.Context, fn string, args ...any) any {
ch := s.makeExportsCall(fn, args...)

for {
select {
case <-ctx.Done():
// because the context is done, we still need to read from the channel
go func() {
<-ch
}()
return ErrContextCancelled
case ret := <-ch:
return ret
}
defer releaseChannel(ch)
select {
case <-ctx.Done():
return ErrContextCancelled
case ret := <-ch:
return ret
}
}

Expand Down Expand Up @@ -169,8 +164,7 @@ func (s *Script) hijackFn(message string, data []byte) {
}
ch := callerCh.(chan any)
ch <- ret
close(ch)

rpcCalls.Delete(rpcID)
} else {
var args []reflect.Value
switch s.fn.Type().NumIn() {
Expand Down Expand Up @@ -214,11 +208,35 @@ func (s *Script) makeExportsCall(fn string, args ...any) chan any {
rpc[ct] = aIface
}

ch := make(chan any)
ch := getChannel()
rpcCalls.Store(rpcData[1], ch)

bt, _ := json.Marshal(rpc)
s.Post(string(bt), nil)

return ch
}

var channelPool = sync.Pool{
New: func() interface{} {
return make(chan any, 1)
},
}

func getChannel() chan any {
ch := channelPool.Get().(chan any)
select {
case <-ch:
default:
}

return ch
}

func releaseChannel(ch chan any) {
select {
case <-ch:
default:
}
channelPool.Put(ch)
}

0 comments on commit 28875e8

Please sign in to comment.