File tree 4 files changed +74
-1
lines changed
4 files changed +74
-1
lines changed Original file line number Diff line number Diff line change @@ -201,3 +201,11 @@ func TestCgoPanicDeadlock(t *testing.T) {
201
201
t .Fatalf ("output does not start with %q:\n %s" , want , got )
202
202
}
203
203
}
204
+
205
+ func TestCgoCCodeSIGPROF (t * testing.T ) {
206
+ got := runTestProg (t , "testprogcgo" , "CgoCCodeSIGPROF" )
207
+ want := "OK\n "
208
+ if got != want {
209
+ t .Errorf ("expected %q got %v" , want , got )
210
+ }
211
+ }
Original file line number Diff line number Diff line change @@ -9,39 +9,56 @@ import (
9
9
"unsafe"
10
10
)
11
11
12
+ // Calling panic with one of the errors below will call errorString.Error
13
+ // which will call mallocgc to concatenate strings. That will fail if
14
+ // malloc is locked, causing a confusing error message. Throw a better
15
+ // error message instead.
16
+ func panicCheckMalloc (err error ) {
17
+ gp := getg ()
18
+ if gp != nil && gp .m != nil && gp .m .mallocing != 0 {
19
+ throw (string (err .(errorString )))
20
+ }
21
+ }
22
+
12
23
var indexError = error (errorString ("index out of range" ))
13
24
14
25
func panicindex () {
26
+ panicCheckMalloc (indexError )
15
27
panic (indexError )
16
28
}
17
29
18
30
var sliceError = error (errorString ("slice bounds out of range" ))
19
31
20
32
func panicslice () {
33
+ panicCheckMalloc (sliceError )
21
34
panic (sliceError )
22
35
}
23
36
24
37
var divideError = error (errorString ("integer divide by zero" ))
25
38
26
39
func panicdivide () {
40
+ panicCheckMalloc (divideError )
27
41
panic (divideError )
28
42
}
29
43
30
44
var overflowError = error (errorString ("integer overflow" ))
31
45
32
46
func panicoverflow () {
47
+ panicCheckMalloc (overflowError )
33
48
panic (overflowError )
34
49
}
35
50
36
51
var floatError = error (errorString ("floating point error" ))
37
52
38
53
func panicfloat () {
54
+ panicCheckMalloc (floatError )
39
55
panic (floatError )
40
56
}
41
57
42
58
var memoryError = error (errorString ("invalid memory address or nil pointer dereference" ))
43
59
44
60
func panicmem () {
61
+ panicCheckMalloc (memoryError )
45
62
panic (memoryError )
46
63
}
47
64
Original file line number Diff line number Diff line change @@ -277,7 +277,7 @@ func (f *Func) FileLine(pc uintptr) (file string, line int) {
277
277
278
278
func findmoduledatap (pc uintptr ) * moduledata {
279
279
for datap := & firstmoduledata ; datap != nil ; datap = datap .next {
280
- if datap .minpc <= pc && pc <= datap .maxpc {
280
+ if datap .minpc <= pc && pc < datap .maxpc {
281
281
return datap
282
282
}
283
283
}
Original file line number Diff line number Diff line change
1
+ // Copyright 2016 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
+ package main
6
+
7
+ // Test that SIGPROF received in C code does not crash the process
8
+ // looking for the C code's func pointer.
9
+
10
+ // The test fails when the function is the first C function.
11
+ // The exported functions are the first C functions, so we use that.
12
+
13
+ // extern void GoNop();
14
+ import "C"
15
+
16
+ import (
17
+ "bytes"
18
+ "fmt"
19
+ "runtime/pprof"
20
+ )
21
+
22
+ func init () {
23
+ register ("CgoCCodeSIGPROF" , CgoCCodeSIGPROF )
24
+ }
25
+
26
+ //export GoNop
27
+ func GoNop () {}
28
+
29
+ func CgoCCodeSIGPROF () {
30
+ c := make (chan bool )
31
+ go func () {
32
+ for {
33
+ <- c
34
+ for i := 0 ; i < 1e7 ; i ++ {
35
+ C .GoNop ()
36
+ }
37
+ c <- true
38
+ }
39
+ }()
40
+
41
+ var buf bytes.Buffer
42
+ pprof .StartCPUProfile (& buf )
43
+ c <- true
44
+ <- c
45
+ pprof .StopCPUProfile ()
46
+
47
+ fmt .Println ("OK" )
48
+ }
You can’t perform that action at this time.
0 commit comments