Skip to content

Commit

Permalink
update channelPool
Browse files Browse the repository at this point in the history
  • Loading branch information
AnxiangLemon committed Sep 21, 2024
1 parent 5439ad0 commit 9f02bef
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions frida/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,22 @@ 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
}

}

// Clean will clean the resources held by the script.
Expand Down Expand Up @@ -169,7 +165,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
Expand Down Expand Up @@ -214,11 +210,36 @@ 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 9f02bef

Please sign in to comment.