boundary_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2017 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. //go:build linux
  6. package bytes_test
  7. import (
  8. . "bytes"
  9. "syscall"
  10. "testing"
  11. )
  12. // This file tests the situation where byte operations are checking
  13. // data very near to a page boundary. We want to make sure those
  14. // operations do not read across the boundary and cause a page
  15. // fault where they shouldn't.
  16. // These tests run only on linux. The code being tested is
  17. // not OS-specific, so it does not need to be tested on all
  18. // operating systems.
  19. // dangerousSlice returns a slice which is immediately
  20. // preceded and followed by a faulting page.
  21. func dangerousSlice(t *testing.T) []byte {
  22. pagesize := syscall.Getpagesize()
  23. b, err := syscall.Mmap(0, 0, 3*pagesize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANONYMOUS|syscall.MAP_PRIVATE)
  24. if err != nil {
  25. t.Fatalf("mmap failed %s", err)
  26. }
  27. err = syscall.Mprotect(b[:pagesize], syscall.PROT_NONE)
  28. if err != nil {
  29. t.Fatalf("mprotect low failed %s\n", err)
  30. }
  31. err = syscall.Mprotect(b[2*pagesize:], syscall.PROT_NONE)
  32. if err != nil {
  33. t.Fatalf("mprotect high failed %s\n", err)
  34. }
  35. return b[pagesize : 2*pagesize]
  36. }
  37. func TestEqualNearPageBoundary(t *testing.T) {
  38. t.Parallel()
  39. b := dangerousSlice(t)
  40. for i := range b {
  41. b[i] = 'A'
  42. }
  43. for i := 0; i <= len(b); i++ {
  44. Equal(b[:i], b[len(b)-i:])
  45. Equal(b[len(b)-i:], b[:i])
  46. }
  47. }
  48. func TestIndexByteNearPageBoundary(t *testing.T) {
  49. t.Parallel()
  50. b := dangerousSlice(t)
  51. for i := range b {
  52. idx := IndexByte(b[i:], 1)
  53. if idx != -1 {
  54. t.Fatalf("IndexByte(b[%d:])=%d, want -1\n", i, idx)
  55. }
  56. }
  57. }
  58. func TestIndexNearPageBoundary(t *testing.T) {
  59. t.Parallel()
  60. q := dangerousSlice(t)
  61. if len(q) > 64 {
  62. // Only worry about when we're near the end of a page.
  63. q = q[len(q)-64:]
  64. }
  65. b := dangerousSlice(t)
  66. if len(b) > 256 {
  67. // Only worry about when we're near the end of a page.
  68. b = b[len(b)-256:]
  69. }
  70. for j := 1; j < len(q); j++ {
  71. q[j-1] = 1 // difference is only found on the last byte
  72. for i := range b {
  73. idx := Index(b[i:], q[:j])
  74. if idx != -1 {
  75. t.Fatalf("Index(b[%d:], q[:%d])=%d, want -1\n", i, j, idx)
  76. }
  77. }
  78. q[j-1] = 0
  79. }
  80. // Test differing alignments and sizes of q which always end on a page boundary.
  81. q[len(q)-1] = 1 // difference is only found on the last byte
  82. for j := 0; j < len(q); j++ {
  83. for i := range b {
  84. idx := Index(b[i:], q[j:])
  85. if idx != -1 {
  86. t.Fatalf("Index(b[%d:], q[%d:])=%d, want -1\n", i, j, idx)
  87. }
  88. }
  89. }
  90. q[len(q)-1] = 0
  91. }