diff options
author | Emile <hanemile@protonmail.com> | 2020-02-23 16:40:04 +0100 |
---|---|---|
committer | Emile <hanemile@protonmail.com> | 2020-02-23 16:40:04 +0100 |
commit | 370dee5c07b37fd662d88a10c3c971f7e008cdd4 (patch) | |
tree | 85db5f74e7eb55f5de71ae5c80a58bf6d0e4d8fd /src/init.go | |
parent | 4fc5cb4c6d6296c96e4ecb0aad7816ae78000553 (diff) |
random offsets
Diffstat (limited to 'src/init.go')
-rw-r--r-- | src/init.go | 82 |
1 files changed, 68 insertions, 14 deletions
diff --git a/src/init.go b/src/init.go index 4b3f2d9..eadf505 100644 --- a/src/init.go +++ b/src/init.go @@ -87,10 +87,14 @@ func initArena(config *Config) *r2pipe.Pipe { return r2p } -// getRandomOffsets returns random offsets for all bots +// genRandomOffsets returns random offsets for all bots // This is used to get the offset the bots are initially placed in -func getRandomOffsets(config Config) []int { +func genRandomOffsets(config *Config) { + logrus.Info("Generating random bot offsets") + + // define the amount of bots, an array to store the offsets in and a counter + // to store the amount of tries it took to find a random positon for the bots var amountOfBots int = len(config.Bots) var offsets []int var roundCounter int = 0 @@ -99,35 +103,85 @@ func getRandomOffsets(config Config) []int { rand.Seed(time.Now().UTC().UnixNano()) for { - // define an integer array to store the random offsets in - //var offsets []int = []int{} + + // reset the offsets array + offsets = []int{} // define a random address + // | ------------------------------------- | ----- | + // | generate an address in this space address := rand.Intn(config.Memsize - config.MaxProgSize) + logrus.Tracef("%d", address) // for all bots, try to generate another random address after the intially // generated address and test if it fits in memory for i := 0; i < amountOfBots; i++ { + + // append the address to the offsets array offsets = append(offsets, address) - // generate a random value in range [maxProgSize, maxProgSize + 300) - address += rand.Intn(config.MaxProgSize+300) + config.MaxProgSize + // define a min and max bound + // + // | ------|-|----------------------------------|-| + // | | | in this space | | + // a b c d e + // + // a = 0x0 + // b = address + // c = address + config.MaxProcSize (min) + // d = config.Memsize - config.MaxProgSize (max) + // e = config.Memsize + min := address + config.MaxProgSize + max := config.Memsize - config.MaxProgSize + + // if the new minimum bound is bigger or equal to the maximum bound, + // discard this try and start with a fresh new initial address + if min >= max { + roundCounter++ + break + } + + // generate a new address in the [min, max) range defined above + address = rand.Intn(max-min) + min + logrus.Tracef("%d", address) - // if there is not enough memory remaining after the last generated - // address, start from be beginning - if address+config.MaxProgSize > config.Memsize { + // If there isn't enough space inbetween the address and the biggest + // possible address, as in, the biggest possible bot can't fit in that + // space, discard and start with a new fresh initial address + if (config.Memsize-config.MaxProgSize)-address < config.MaxProgSize { roundCounter++ - continue + break } } - // if enough addresses have been generated, break out of the for loop - break + // if the needed amount of offsets has been found, break out of the infinite loop + if len(offsets) == amountOfBots { + break + } + } + + logrus.Infof("Initial bot positions found after %d tries", roundCounter) + + // debug print all offsets + var fields0 logrus.Fields = make(logrus.Fields) + for i := 0; i < len(offsets); i++ { + fields0[fmt.Sprintf("%d", i)] = offsets[i] } + logrus.WithFields(fields0).Debug("Offsets") + + // shuffle the offsets + rand.Shuffle(len(offsets), func(i, j int) { + offsets[i], offsets[j] = offsets[j], offsets[i] + }) - log.Printf("[+] Initial bot positions found after %d trues", roundCounter) + // debug print the shuffled offsets + var fields1 logrus.Fields = make(logrus.Fields) + for i := 0; i < len(offsets); i++ { + fields1[fmt.Sprintf("%d", i)] = offsets[i] + } + logrus.WithFields(fields1).Debug("Shuffled offsets") - return offsets + config.RandomOffsets = offsets } // place the bot in the arena at the given address |