Home | History | Annotate | Download | only in bytes
      1 // Copyright 2013 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 //
      5 // +build linux
      6 
      7 package bytes_test
      8 
      9 import (
     10 	. "bytes"
     11 	"syscall"
     12 	"testing"
     13 	"unsafe"
     14 )
     15 
     16 // This file tests the situation where memeq is checking
     17 // data very near to a page boundary. We want to make sure
     18 // equal does not read across the boundary and cause a page
     19 // fault where it shouldn't.
     20 
     21 // This test runs only on linux. The code being tested is
     22 // not OS-specific, so it does not need to be tested on all
     23 // operating systems.
     24 
     25 func TestEqualNearPageBoundary(t *testing.T) {
     26 	pagesize := syscall.Getpagesize()
     27 	b := make([]byte, 4*pagesize)
     28 	i := pagesize
     29 	for ; uintptr(unsafe.Pointer(&b[i]))%uintptr(pagesize) != 0; i++ {
     30 	}
     31 	syscall.Mprotect(b[i-pagesize:i], 0)
     32 	syscall.Mprotect(b[i+pagesize:i+2*pagesize], 0)
     33 	defer syscall.Mprotect(b[i-pagesize:i], syscall.PROT_READ|syscall.PROT_WRITE)
     34 	defer syscall.Mprotect(b[i+pagesize:i+2*pagesize], syscall.PROT_READ|syscall.PROT_WRITE)
     35 
     36 	// both of these should fault
     37 	//pagesize += int(b[i-1])
     38 	//pagesize += int(b[i+pagesize])
     39 
     40 	for j := 0; j < pagesize; j++ {
     41 		b[i+j] = 'A'
     42 	}
     43 	for j := 0; j <= pagesize; j++ {
     44 		Equal(b[i:i+j], b[i+pagesize-j:i+pagesize])
     45 		Equal(b[i+pagesize-j:i+pagesize], b[i:i+j])
     46 	}
     47 }
     48