@@ -29,18 +29,23 @@ import (
29
29
"gvisor.dev/gvisor/pkg/usermem"
30
30
)
31
31
32
- func testMemoryManager (ctx context.Context ) * MemoryManager {
32
+ func testMemoryManagerWithMmapDirection (ctx context.Context , mmapDirection arch. MmapDirection ) * MemoryManager {
33
33
p := platform .FromContext (ctx )
34
34
mm := NewMemoryManager (p , pgalloc .MemoryFileFromContext (ctx ), false )
35
35
mm .layout = arch.MmapLayout {
36
- MinAddr : p .MinUserAddress (),
37
- MaxAddr : p .MaxUserAddress (),
38
- BottomUpBase : p .MinUserAddress (),
39
- TopDownBase : p .MaxUserAddress (),
36
+ MinAddr : p .MinUserAddress (),
37
+ MaxAddr : p .MaxUserAddress (),
38
+ BottomUpBase : p .MinUserAddress (),
39
+ TopDownBase : p .MaxUserAddress (),
40
+ DefaultDirection : mmapDirection ,
40
41
}
41
42
return mm
42
43
}
43
44
45
+ func testMemoryManager (ctx context.Context ) * MemoryManager {
46
+ return testMemoryManagerWithMmapDirection (ctx , arch .MmapBottomUp )
47
+ }
48
+
44
49
func (mm * MemoryManager ) realUsageAS () uint64 {
45
50
return uint64 (mm .vmas .Span ())
46
51
}
@@ -272,3 +277,66 @@ func TestAIOLookupAfterDestroy(t *testing.T) {
272
277
t .Errorf ("AIOContext found even after AIOContext manager is destroyed" )
273
278
}
274
279
}
280
+
281
+ func TestGetAllocationDirection (t * testing.T ) {
282
+ testCases := []struct {
283
+ name string
284
+ mmapDirection arch.MmapDirection
285
+ ar hostarch.AddrRange
286
+ vma * vma
287
+ expected pgalloc.Direction
288
+ }{
289
+ {
290
+ "No last fault in vma with mmap direction BottomUp" ,
291
+ arch .MmapBottomUp ,
292
+ hostarch.AddrRange {123 , 456 },
293
+ & vma {lastFault : 0 },
294
+ pgalloc .BottomUp ,
295
+ },
296
+ {
297
+ "No last fault in vma with mmap direction TopDown" ,
298
+ arch .MmapTopDown ,
299
+ hostarch.AddrRange {123 , 456 },
300
+ & vma {lastFault : 0 },
301
+ pgalloc .TopDown ,
302
+ },
303
+ {
304
+ "Last fault in vma equals to addr range, with mmap direction BottomUp" ,
305
+ arch .MmapBottomUp ,
306
+ hostarch.AddrRange {123 , 456 },
307
+ & vma {lastFault : 123 },
308
+ pgalloc .BottomUp ,
309
+ },
310
+ {
311
+ "Last fault in vma equals to addr range, with mmap direction TopDown" ,
312
+ arch .MmapTopDown ,
313
+ hostarch.AddrRange {123 , 456 },
314
+ & vma {lastFault : 123 },
315
+ pgalloc .TopDown ,
316
+ },
317
+ {
318
+ "Last fault in vma greater than addr range" ,
319
+ arch .MmapTopDown ,
320
+ hostarch.AddrRange {123 , 456 },
321
+ & vma {lastFault : 456 },
322
+ pgalloc .TopDown ,
323
+ },
324
+ {
325
+ "Last fault in vma smaller than addr range" ,
326
+ arch .MmapTopDown ,
327
+ hostarch.AddrRange {123 , 456 },
328
+ & vma {lastFault : 100 },
329
+ pgalloc .BottomUp ,
330
+ },
331
+ }
332
+ for _ , test := range testCases {
333
+ t .Run (test .name , func (t * testing.T ) {
334
+ ctx := contexttest .Context (t )
335
+ mm := testMemoryManagerWithMmapDirection (ctx , test .mmapDirection )
336
+ actual := mm .getAllocationDirection (test .ar , test .vma )
337
+ if actual != test .expected {
338
+ t .Errorf ("Unexpected allocation direction. Expected: %s, Actual: %s" , test .expected , actual )
339
+ }
340
+ })
341
+ }
342
+ }
0 commit comments