sbinet / go-python

naive go bindings to the CPython2 C-API
Other
1.53k stars 139 forks source link

Double free if passing args to Py_Main #9

Closed tv42 closed 10 years ago

tv42 commented 10 years ago
$ go build -o go-python.buggy ./cmd/go-python/main.go 
[0 tv@brute ~/go/src/github.com/sbinet/go-python]$ ./go-python.buggy -c 'print 1+1'
2
*** Error in `./go-python.buggy': double free or corruption (out): 0x00007f15b5060ff0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x80996)[0x7f15b4860996]
./go-python.buggy[0x4307e1]
======= Memory map: ========
00400000-005cb000 r-xp 00000000 00:10 1714079                            /home/tv/go/src/github.com/sbinet/go-python/go-python.buggy
007ca000-007cb000 r--p 001ca000 00:10 1714079                            /home/tv/go/src/github.com/sbinet/go-python/go-python.buggy
007cb000-007d5000 rw-p 001cb000 00:10 1714079                            /home/tv/go/src/github.com/sbinet/go-python/go-python.buggy
007d5000-007f1000 rw-p 00000000 00:00 0 
02390000-0248c000 rw-p 00000000 00:00 0                                  [heap]
c000000000-c000002000 rw-p 00000000 00:00 0 
c207ff0000-c208100000 rw-p 00000000 00:00 0 
7f15ac000000-7f15ac021000 rw-p 00000000 00:00 0 
7f15ac021000-7f15b0000000 ---p 00000000 00:00 0 
7f15b1b8c000-7f15b1ba1000 r-xp 00000000 00:10 19145                      /lib/x86_64-linux-gnu/libgcc_s.so.1
7f15b1ba1000-7f15b1da0000 ---p 00015000 00:10 19145                      /lib/x86_64-linux-gnu/libgcc_s.so.1
7f15b1da0000-7f15b1da1000 r--p 00014000 00:10 19145                      /lib/x86_64-linux-gnu/libgcc_s.so.1
7f15b1da1000-7f15b1da2000 rw-p 00015000 00:10 19145                      /lib/x86_64-linux-gnu/libgcc_s.so.1
7f15b1da2000-7f15b2484000 r--p 00000000 00:10 31725                      /usr/lib/locale/locale-archive
7f15b2484000-7f15b25b9000 rw-p 00000000 00:00 0 
7f15b25b9000-7f15b25ba000 ---p 00000000 00:00 0 
7f15b25ba000-7f15b2dba000 rw-p 00000000 00:00 0                          [stack:24223]
7f15b2dba000-7f15b2dbb000 ---p 00000000 00:00 0 
7f15b2dbb000-7f15b36bb000 rw-p 00000000 00:00 0 
7f15b36bb000-7f15b36bc000 ---p 00000000 00:00 0 
7f15b36bc000-7f15b3ebc000 rw-p 00000000 00:00 0                          [stack:24221]
7f15b3ebc000-7f15b3fbf000 r-xp 00000000 00:10 19159                      /lib/x86_64-linux-gnu/libm-2.17.so
7f15b3fbf000-7f15b41be000 ---p 00103000 00:10 19159                      /lib/x86_64-linux-gnu/libm-2.17.so
7f15b41be000-7f15b41bf000 r--p 00102000 00:10 19159                      /lib/x86_64-linux-gnu/libm-2.17.so
7f15b41bf000-7f15b41c0000 rw-p 00103000 00:10 19159                      /lib/x86_64-linux-gnu/libm-2.17.so
7f15b41c0000-7f15b41c2000 r-xp 00000000 00:10 19211                      /lib/x86_64-linux-gnu/libutil-2.17.so
7f15b41c2000-7f15b43c1000 ---p 00002000 00:10 19211                      /lib/x86_64-linux-gnu/libutil-2.17.so
7f15b43c1000-7f15b43c2000 r--p 00001000 00:10 19211                      /lib/x86_64-linux-gnu/libutil-2.17.so
7f15b43c2000-7f15b43c3000 rw-p 00002000 00:10 19211                      /lib/x86_64-linux-gnu/libutil-2.17.so
7f15b43c3000-7f15b43c6000 r-xp 00000000 00:10 19140                      /lib/x86_64-linux-gnu/libdl-2.17.so
7f15b43c6000-7f15b45c5000 ---p 00003000 00:10 19140                      /lib/x86_64-linux-gnu/libdl-2.17.so
7f15b45c5000-7f15b45c6000 r--p 00002000 00:10 19140                      /lib/x86_64-linux-gnu/libdl-2.17.so
7f15b45c6000-7f15b45c7000 rw-p 00003000 00:10 19140                      /lib/x86_64-linux-gnu/libdl-2.17.so
7f15b45c7000-7f15b45df000 r-xp 00000000 00:10 19215                      /lib/x86_64-linux-gnu/libz.so.1.2.8
7f15b45df000-7f15b47de000 ---p 00018000 00:10 19215                      /lib/x86_64-linux-gnu/libz.so.1.2.8
7f15b47de000-7f15b47df000 r--p 00017000 00:10 19215                      /lib/x86_64-linux-gnu/libz.so.1.2.8
7f15b47df000-7f15b47e0000 rw-p 00018000 00:10 19215                      /lib/x86_64-linux-gnu/libz.so.1.2.8
7f15b47e0000-7f15b499d000 r-xp 00000000 00:10 19131                      /lib/x86_64-linux-gnu/libc-2.17.so
7f15b499d000-7f15b4b9d000 ---p 001bd000 00:10 19131                      /lib/x86_64-linux-gnu/libc-2.17.so
7f15b4b9d000-7f15b4ba1000 r--p 001bd000 00:10 19131                      /lib/x86_64-linux-gnu/libc-2.17.so
7f15b4ba1000-7f15b4ba3000 rw-p 001c1000 00:10 19131                      /lib/x86_64-linux-gnu/libc-2.17.so
7f15b4ba3000-7f15b4ba8000 rw-p 00000000 00:00 0 
7f15b4ba8000-7f15b4bbf000 r-xp 00000000 00:10 19192                      /lib/x86_64-linux-gnu/libpthread-2.17.so
7f15b4bbf000-7f15b4dbf000 ---p 00017000 00:10 19192                      /lib/x86_64-linux-gnu/libpthread-2.17.so
7f15b4dbf000-7f15b4dc0000 r--p 00017000 00:10 19192                      /lib/x86_64-linux-gnu/libpthread-2.17.so
7f15b4dc0000-7f15b4dc1000 rw-p 00018000 00:10 19192                      /lib/x86_64-linux-gnu/libpthread-2.17.so
7f15b4dc1000-7f15b4dc5000 rw-p 00000000 00:00 0 
7f15b4dc5000-7f15b50a1000 r-xp 00000000 00:10 44746                      /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0
7f15b50a1000-7f15b52a0000 ---p 002dc000 00:10 44746                      /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0
7f15b52a0000-7f15b52a2000 r--p 002db000 00:10 44746                      /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0
7f15b52a2000-7f15b5317000 rw-p 002dd000 00:10 44746                      /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0
7f15b5317000-7f15b5329000 rw-p 00000000 00:00 0 
7f15b5329000-7f15b534c000 r-xp 00000000 00:10 19118                      /lib/x86_64-linux-gnu/ld-2.17.so
7f15b536c000-7f15b5531000 rw-p 00000000 00:00 0                          [stack:24222]
7f15b5537000-7f15b554b000 rw-p 00000000 00:00 0 
7f15b554b000-7f15b554c000 r--p 00022000 00:10 19118                      /lib/x86_64-linux-gnu/ld-2.17.so
7f15b554c000-7f15b554e000 rw-p 00023000 00:10 19118                      /lib/x86_64-linux-gnu/ld-2.17.so
7fff7f5e9000-7fff7f60a000 rw-p 00000000 00:00 0                          [stack]
7fff7f6dd000-7fff7f6df000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
SIGABRT: abort
PC=0x7f15b4816f77
signal arrived during cgo execution

runtime.cgocall(0x40c140, 0x7f15b536de70)
    /home/tv/src/go/src/pkg/runtime/cgocall.c:148 +0x116 fp=0x7f15b536de58
github.com/sbinet/go-python._Cfunc_free(0x7f15b5060ff0)
    /home/tv/tmp/go-build799516118/github.com/sbinet/go-python/_obj/_cgo_defun.c:2872 +0x31 fp=0x7f15b536de70
github.com/sbinet/go-python.func·001()
    /home/tv/go/src/github.com/sbinet/go-python/veryhigh.go:25 +0x70 fp=0x7f15b536de98
github.com/sbinet/go-python.Py_Main(0xc20800c000, 0x3, 0x3, 0x0)
    /home/tv/go/src/github.com/sbinet/go-python/veryhigh.go:29 +0x166 fp=0x7f15b536df20
main.main()
    /home/tv/go/src/github.com/sbinet/go-python/cmd/go-python/main.go:17 +0x44 fp=0x7f15b536df48
runtime.main()
    /home/tv/src/go/src/pkg/runtime/proc.c:231 +0x11a fp=0x7f15b536dfa0
runtime.goexit()
    /home/tv/src/go/src/pkg/runtime/proc.c:1438 fp=0x7f15b536dfa8
created by _rt0_go
    /home/tv/src/go/src/pkg/runtime/asm_amd64.s:97 +0x120

goroutine 17 [syscall]:
runtime.goexit()
    /home/tv/src/go/src/pkg/runtime/proc.c:1438

rax     0x0
rbx     0x0
rcx     0xffffffffffffffff
rdx     0x6
rdi     0x5e9c
rsi     0x5e9c
rbp     0x7fff7f6083e0
rsp     0x7fff7f6079a8
r8      0x0
r9      0x11
r10     0x8
r11     0x206
r12     0x4
r13     0x7
r14     0x7
r15     0x7fff7f607b50
rip     0x7f15b4816f77
rflags  0x206
cs      0x33
fs      0x0
gs      0x0
$ git stash pop -q
$ git diff
diff --git i/veryhigh.go w/veryhigh.go
index 96bf183..aa96c53 100644
--- i/veryhigh.go
+++ w/veryhigh.go
@@ -20,11 +20,6 @@ func Py_Main(args []string) int {
        for idx, arg := range args {
                argv[idx] = C.CString(arg)
        }
-       defer func() {
-               for idx, _ := range argv {
-                       C.free(unsafe.Pointer(argv[idx]))
-               }
-       }()
        return int(C.Py_Main(argc, &argv[0]))
 }

$ go build -o go-python.good ./cmd/go-python/main.go 
$ ./go-python.good -c 'print 1+1'
2
$