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 4package prog 5 6import ( 7 "fmt" 8 "testing" 9) 10 11func TestMemAlloc(t *testing.T) { 12 t.Parallel() 13 type op struct { 14 addr uint64 15 size int // if positive do noteAlloc, otherwise -- alloc 16 } 17 tests := [][]op{ 18 { 19 // Just sequential allocation. 20 {0, -1}, 21 {64, -64}, 22 {128, -65}, 23 {256, -16}, 24 {320, -8}, 25 }, 26 { 27 // First reserve some memory and then allocate. 28 {0, 1}, 29 {64, 63}, 30 {128, 64}, 31 {192, 65}, 32 {320, -1}, 33 {448, 1}, 34 {384, -1}, 35 {576, 1}, 36 {640, -128}, 37 }, 38 } 39 for ti, test := range tests { 40 test := test 41 t.Run(fmt.Sprint(ti), func(t *testing.T) { 42 ma := newMemAlloc(16 << 20) 43 for i, op := range test { 44 if op.size > 0 { 45 t.Logf("#%v: noteAlloc(%v, %v)", i, op.addr, op.size) 46 ma.noteAlloc(op.addr, uint64(op.size)) 47 continue 48 } 49 t.Logf("#%v: alloc(%v) = %v", i, -op.size, op.addr) 50 addr := ma.alloc(nil, uint64(-op.size)) 51 if addr != op.addr { 52 t.Fatalf("bad result %v", addr) 53 } 54 } 55 }) 56 } 57} 58 59func TestVmaAlloc(t *testing.T) { 60 t.Parallel() 61 target, err := GetTarget("test", "64") 62 if err != nil { 63 t.Fatal(err) 64 } 65 r := newRand(target, randSource(t)) 66 va := newVmaAlloc(1000) 67 for i := 0; i < 30; i++ { 68 size := r.rand(4) + 1 69 page := va.alloc(r, size) 70 t.Logf("alloc(%v) = %3v-%3v\n", size, page, page+size) 71 } 72} 73