github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/prog/alloc_test.go (about) 1 // Copyright 2018 syzkaller project authors. All rights reserved. 2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. 3 4 package prog 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/google/syzkaller/pkg/testutil" 11 ) 12 13 func TestMemAlloc(t *testing.T) { 14 t.Parallel() 15 type op struct { 16 addr uint64 17 size int // if positive do noteAlloc, otherwise -- alloc 18 alignment uint64 19 } 20 tests := [][]op{ 21 { 22 // Just sequential allocation. 23 {0, -1, 1}, 24 {64, -64, 1}, 25 {128, -65, 1}, 26 {256, -16, 1}, 27 {320, -8, 1}, 28 }, 29 { 30 // First reserve some memory and then allocate. 31 {0, 1, 1}, 32 {64, 63, 1}, 33 {128, 64, 1}, 34 {192, 65, 1}, 35 {320, -1, 1}, 36 {448, 1, 1}, 37 {384, -1, 1}, 38 {576, 1, 1}, 39 {640, -128, 1}, 40 }, 41 { 42 // Aligned memory allocation. 43 {0, -1, 1}, 44 {512, -1, 512}, 45 {1024, -1, 512}, 46 {128, -1, 128}, 47 {64, -1, 1}, 48 // 128 used, jumps on. 49 {192, -1, 1}, 50 {256, -1, 1}, 51 {320, -1, 1}, 52 {384, -1, 1}, 53 {448, -1, 1}, 54 // 512 used, jumps on. 55 {576, -1, 1}, 56 // Next 512 available at 1536. 57 {1536, -1, 512}, 58 // Next smallest available. 59 {640, -1, 1}, 60 // Next 64 byte aligned block. 61 {1600, -512, 1}, 62 }, 63 } 64 for ti, test := range tests { 65 test := test 66 t.Run(fmt.Sprint(ti), func(t *testing.T) { 67 ma := newMemAlloc(16 << 20) 68 for i, op := range test { 69 if op.size > 0 { 70 t.Logf("#%v: noteAlloc(%v, %v)", i, op.addr, op.size) 71 ma.noteAlloc(op.addr, uint64(op.size)) 72 continue 73 } 74 t.Logf("#%v: alloc(%v) = %v", i, -op.size, op.addr) 75 addr := ma.alloc(nil, uint64(-op.size), op.alignment) 76 if addr != op.addr { 77 t.Fatalf("bad result %v, expecting %v", addr, op.addr) 78 } 79 } 80 }) 81 } 82 } 83 84 func TestVmaAlloc(t *testing.T) { 85 t.Parallel() 86 target, err := GetTarget("test", "64") 87 if err != nil { 88 t.Fatal(err) 89 } 90 r := newRand(target, testutil.RandSource(t)) 91 va := newVmaAlloc(1000) 92 for i := 0; i < 30; i++ { 93 size := r.rand(4) + 1 94 page := va.alloc(r, size) 95 t.Logf("alloc(%v) = %3v-%3v", size, page, page+size) 96 } 97 }