Skip to content

Commit

Permalink
reduced memory consumption by reducing memory size to split
Browse files Browse the repository at this point in the history
  • Loading branch information
tkmru committed Jun 5, 2020
1 parent 63e3c46 commit 8da159a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
9 changes: 5 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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/<pid>/mem
// parse /proc/<pid>/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/<pid>/mem
memPath := fmt.Sprintf("/proc/%s/mem", pid)
if err != nil {
return nil, err
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/memory/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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")}}
}
Expand Down

0 comments on commit 8da159a

Please sign in to comment.