equal_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. package bytes_test
  7. import (
  8. . "bytes"
  9. "syscall"
  10. "testing"
  11. "unsafe"
  12. )
  13. // This file tests the situation where memeq is checking
  14. // data very near to a page boundary. We want to make sure
  15. // equal does not read across the boundary and cause a page
  16. // fault where it shouldn't.
  17. // This test runs only on linux. The code being tested is
  18. // not OS-specific, so it does not need to be tested on all
  19. // operating systems.
  20. func TestEqualNearPageBoundary(t *testing.T) {
  21. pagesize := syscall.Getpagesize()
  22. b := make([]byte, 4*pagesize)
  23. i := pagesize
  24. for ; uintptr(unsafe.Pointer(&b[i]))%uintptr(pagesize) != 0; i++ {
  25. }
  26. syscall.Mprotect(b[i-pagesize:i], 0)
  27. syscall.Mprotect(b[i+pagesize:i+2*pagesize], 0)
  28. defer syscall.Mprotect(b[i-pagesize:i], syscall.PROT_READ|syscall.PROT_WRITE)
  29. defer syscall.Mprotect(b[i+pagesize:i+2*pagesize], syscall.PROT_READ|syscall.PROT_WRITE)
  30. // both of these should fault
  31. //pagesize += int(b[i-1])
  32. //pagesize += int(b[i+pagesize])
  33. for j := 0; j < pagesize; j++ {
  34. b[i+j] = 'A'
  35. }
  36. for j := 0; j <= pagesize; j++ {
  37. Equal(b[i:i+j], b[i+pagesize-j:i+pagesize])
  38. Equal(b[i+pagesize-j:i+pagesize], b[i:i+j])
  39. }
  40. }