From 8da159a30545221601e35bca3c0327de1e3109fe Mon Sep 17 00:00:00 2001 From: taichi-kotake Date: Fri, 5 Jun 2020 11:26:31 +0900 Subject: [PATCH] reduced memory consumption by reducing memory size to split --- cmd/cmd.go | 9 +++++---- pkg/memory/search.go | 13 +++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 0d983fb..a7591cf 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -54,11 +54,11 @@ func Plist() (string, error) { current_path, _ := os.Getwd() _, package_name := filepath.Split(current_path) for cmd, pid := range pids { - if cmd == package_name { + if cmd == package_name { fmt.Printf("Target PID has been set to %s.\n", pid) return pid, nil } - } + } return "", nil } @@ -104,10 +104,11 @@ func Attach(pid string) error { func Find(pid string, targetVal string, dataType string) ([]Found, error) { founds := []Found{} - // search value in /proc//mem + // parse /proc//map, and get writable area mapsPath := fmt.Sprintf("/proc/%s/maps", pid) - memPath := fmt.Sprintf("/proc/%s/mem", pid) addrRanges, err := memory.GetWritableAddrRanges(mapsPath) + // search value in /proc//mem + memPath := fmt.Sprintf("/proc/%s/mem", pid) if err != nil { return nil, err } diff --git a/pkg/memory/search.go b/pkg/memory/search.go index 7c6f129..35d2617 100644 --- a/pkg/memory/search.go +++ b/pkg/memory/search.go @@ -13,7 +13,7 @@ import ( "github.com/aktsk/apk-medit/pkg/converter" ) -var splitSize = 0x50000000 +var splitSize = 0x5000000 var bufferPool = sync.Pool{ New: func() interface{} { return make([]byte, splitSize) @@ -58,19 +58,19 @@ func GetWritableAddrRanges(mapsPath string) ([][2]int, error) { } type Err struct { - err error + err error } func (e *Err) Error() string { - return fmt.Sprint(e.err) + return fmt.Sprint(e.err) } type ParseErr struct { - *Err + *Err } type TooManyErr struct { - *Err + *Err } func FindDataInAddrRanges(memPath string, targetBytes []byte, addrRanges [][2]int) ([]int, error) { @@ -87,6 +87,7 @@ func FindDataInAddrRanges(memPath string, targetBytes []byte, addrRanges [][2]in fmt.Println(err) } for i := 0; i < (memSize/splitSize)+1; i++ { + // target memory is too big to read all of it, so split it and then search in memory splitIndex := (i + 1) * splitSize splittedBeginAddr := beginAddr + i*splitSize splittedEndAddr := endAddr @@ -97,7 +98,7 @@ func FindDataInAddrRanges(memPath string, targetBytes []byte, addrRanges [][2]in ReadMemory(f, b, splittedBeginAddr, splittedEndAddr) findDataInSplittedMemory(&b, targetBytes, searchLength, splittedBeginAddr, 0, &foundAddrs) bufferPool.Put(b) - if len(foundAddrs) > 50000 { + if len(foundAddrs) > 500000 { fmt.Println("Too many addresses with target data found...") return foundAddrs, TooManyErr{&Err{errors.New("Error: Too many addresses")}} }