runtime_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright 2012 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. package runtime_test
  5. import (
  6. "io"
  7. // "io/ioutil"
  8. // "os"
  9. // "os/exec"
  10. . "runtime"
  11. "runtime/debug"
  12. // "strconv"
  13. // "strings"
  14. "testing"
  15. "unsafe"
  16. )
  17. var errf error
  18. func errfn() error {
  19. return errf
  20. }
  21. func errfn1() error {
  22. return io.EOF
  23. }
  24. func BenchmarkIfaceCmp100(b *testing.B) {
  25. for i := 0; i < b.N; i++ {
  26. for j := 0; j < 100; j++ {
  27. if errfn() == io.EOF {
  28. b.Fatal("bad comparison")
  29. }
  30. }
  31. }
  32. }
  33. func BenchmarkIfaceCmpNil100(b *testing.B) {
  34. for i := 0; i < b.N; i++ {
  35. for j := 0; j < 100; j++ {
  36. if errfn1() == nil {
  37. b.Fatal("bad comparison")
  38. }
  39. }
  40. }
  41. }
  42. func BenchmarkDefer(b *testing.B) {
  43. for i := 0; i < b.N; i++ {
  44. defer1()
  45. }
  46. }
  47. func defer1() {
  48. defer func(x, y, z int) {
  49. if recover() != nil || x != 1 || y != 2 || z != 3 {
  50. panic("bad recover")
  51. }
  52. }(1, 2, 3)
  53. return
  54. }
  55. func BenchmarkDefer10(b *testing.B) {
  56. for i := 0; i < b.N/10; i++ {
  57. defer2()
  58. }
  59. }
  60. func defer2() {
  61. for i := 0; i < 10; i++ {
  62. defer func(x, y, z int) {
  63. if recover() != nil || x != 1 || y != 2 || z != 3 {
  64. panic("bad recover")
  65. }
  66. }(1, 2, 3)
  67. }
  68. }
  69. func BenchmarkDeferMany(b *testing.B) {
  70. for i := 0; i < b.N; i++ {
  71. defer func(x, y, z int) {
  72. if recover() != nil || x != 1 || y != 2 || z != 3 {
  73. panic("bad recover")
  74. }
  75. }(1, 2, 3)
  76. }
  77. }
  78. /* The go tool is not present in gccgo.
  79. // The profiling signal handler needs to know whether it is executing runtime.gogo.
  80. // The constant RuntimeGogoBytes in arch_*.h gives the size of the function;
  81. // we don't have a way to obtain it from the linker (perhaps someday).
  82. // Test that the constant matches the size determined by 'go tool nm -S'.
  83. // The value reported will include the padding between runtime.gogo and the
  84. // next function in memory. That's fine.
  85. func TestRuntimeGogoBytes(t *testing.T) {
  86. switch GOOS {
  87. case "android", "nacl":
  88. t.Skipf("skipping on %s", GOOS)
  89. }
  90. dir, err := ioutil.TempDir("", "go-build")
  91. if err != nil {
  92. t.Fatalf("failed to create temp directory: %v", err)
  93. }
  94. defer os.RemoveAll(dir)
  95. out, err := exec.Command("go", "build", "-o", dir+"/hello", "../../test/helloworld.go").CombinedOutput()
  96. if err != nil {
  97. t.Fatalf("building hello world: %v\n%s", err, out)
  98. }
  99. out, err = exec.Command("go", "tool", "nm", "-size", dir+"/hello").CombinedOutput()
  100. if err != nil {
  101. t.Fatalf("go tool nm: %v\n%s", err, out)
  102. }
  103. for _, line := range strings.Split(string(out), "\n") {
  104. f := strings.Fields(line)
  105. if len(f) == 4 && f[3] == "runtime.gogo" {
  106. size, _ := strconv.Atoi(f[1])
  107. if GogoBytes() != int32(size) {
  108. t.Fatalf("RuntimeGogoBytes = %d, should be %d", GogoBytes(), size)
  109. }
  110. return
  111. }
  112. }
  113. t.Fatalf("go tool nm did not report size for runtime.gogo")
  114. }
  115. */
  116. // golang.org/issue/7063
  117. func TestStopCPUProfilingWithProfilerOff(t *testing.T) {
  118. SetCPUProfileRate(0)
  119. }
  120. // Addresses to test for faulting behavior.
  121. // This is less a test of SetPanicOnFault and more a check that
  122. // the operating system and the runtime can process these faults
  123. // correctly. That is, we're indirectly testing that without SetPanicOnFault
  124. // these would manage to turn into ordinary crashes.
  125. // Note that these are truncated on 32-bit systems, so the bottom 32 bits
  126. // of the larger addresses must themselves be invalid addresses.
  127. // We might get unlucky and the OS might have mapped one of these
  128. // addresses, but probably not: they're all in the first page, very high
  129. // adderesses that normally an OS would reserve for itself, or malformed
  130. // addresses. Even so, we might have to remove one or two on different
  131. // systems. We will see.
  132. var faultAddrs = []uint64{
  133. // low addresses
  134. 0,
  135. 1,
  136. 0xfff,
  137. // high (kernel) addresses
  138. // or else malformed.
  139. 0xffffffffffffffff,
  140. 0xfffffffffffff001,
  141. 0xffffffffffff0001,
  142. 0xfffffffffff00001,
  143. 0xffffffffff000001,
  144. 0xfffffffff0000001,
  145. 0xffffffff00000001,
  146. 0xfffffff000000001,
  147. 0xffffff0000000001,
  148. 0xfffff00000000001,
  149. 0xffff000000000001,
  150. 0xfff0000000000001,
  151. 0xff00000000000001,
  152. 0xf000000000000001,
  153. 0x8000000000000001,
  154. }
  155. func TestSetPanicOnFault(t *testing.T) {
  156. // This currently results in a fault in the signal trampoline on
  157. // dragonfly/386 - see issue 7421.
  158. if GOOS == "dragonfly" && GOARCH == "386" {
  159. t.Skip("skipping test on dragonfly/386")
  160. }
  161. old := debug.SetPanicOnFault(true)
  162. defer debug.SetPanicOnFault(old)
  163. nfault := 0
  164. for _, addr := range faultAddrs {
  165. testSetPanicOnFault(t, uintptr(addr), &nfault)
  166. }
  167. if nfault == 0 {
  168. t.Fatalf("none of the addresses faulted")
  169. }
  170. }
  171. func testSetPanicOnFault(t *testing.T, addr uintptr, nfault *int) {
  172. if GOOS == "nacl" {
  173. t.Skip("nacl doesn't seem to fault on high addresses")
  174. }
  175. defer func() {
  176. if err := recover(); err != nil {
  177. *nfault++
  178. }
  179. }()
  180. // The read should fault, except that sometimes we hit
  181. // addresses that have had C or kernel pages mapped there
  182. // readable by user code. So just log the content.
  183. // If no addresses fault, we'll fail the test.
  184. v := *(*byte)(unsafe.Pointer(addr))
  185. t.Logf("addr %#x: %#x\n", addr, v)
  186. }
  187. func eqstring_generic(s1, s2 string) bool {
  188. if len(s1) != len(s2) {
  189. return false
  190. }
  191. // optimization in assembly versions:
  192. // if s1.str == s2.str { return true }
  193. for i := 0; i < len(s1); i++ {
  194. if s1[i] != s2[i] {
  195. return false
  196. }
  197. }
  198. return true
  199. }
  200. func TestEqString(t *testing.T) {
  201. // This isn't really an exhaustive test of eqstring, it's
  202. // just a convenient way of documenting (via eqstring_generic)
  203. // what eqstring does.
  204. s := []string{
  205. "",
  206. "a",
  207. "c",
  208. "aaa",
  209. "ccc",
  210. "cccc"[:3], // same contents, different string
  211. "1234567890",
  212. }
  213. for _, s1 := range s {
  214. for _, s2 := range s {
  215. x := s1 == s2
  216. y := eqstring_generic(s1, s2)
  217. if x != y {
  218. t.Errorf(`eqstring("%s","%s") = %t, want %t`, s1, s2, x, y)
  219. }
  220. }
  221. }
  222. }