blob: eb594db6cb56bf1c88006e498e57796c2c479289 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
package main
var xorshift32State uint32 = 1
func xorshift32(x uint32) uint32 {
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"
x ^= x << 13
x ^= x >> 17
x ^= x << 5
return x
}
func randuint32() uint32 {
xorshift32State = xorshift32(xorshift32State)
return xorshift32State
}
func main() {
testNonPointerHeap()
}
var scalarSlices [4][]byte
var randSeeds [4]uint32
func testNonPointerHeap() {
maxSliceSize := uint32(1024)
if ^uintptr(0) <= 0xffff {
// 16-bit and lower devices, such as AVR.
// Heap size is a real issue there, while it is still useful to run
// these tests. Therefore, lower the max slice size.
maxSliceSize = 64
}
// Allocate roughly 0.5MB of memory.
for i := 0; i < 1000; i++ {
// Pick a random index that the optimizer can't predict.
index := randuint32() % 4
// Check whether the contents of the previous allocation was correct.
rand := randSeeds[index]
for _, b := range scalarSlices[index] {
rand = xorshift32(rand)
if b != byte(rand) {
panic("memory was overwritten!")
}
}
// Allocate a randomly-sized slice, randomly sliced to be smaller.
sliceLen := randuint32() % maxSliceSize
slice := make([]byte, sliceLen)
cutLen := randuint32() % maxSliceSize
if cutLen < sliceLen {
slice = slice[cutLen:]
}
scalarSlices[index] = slice
// Fill the slice with a pattern that looks random but is easily
// calculated and verified.
rand = randuint32() + 1
randSeeds[index] = rand
for i := 0; i < len(slice); i++ {
rand = xorshift32(rand)
slice[i] = byte(rand)
}
}
println("ok")
}
|