Re: [go-nuts] signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x7fcb5ae54390

2017-08-18 Thread Ian Lance Taylor
On Fri, Aug 18, 2017 at 5:36 PM, Justin Israel  wrote:
>
> I have cgo code that looks like this in a few of my own libs and I just
> started seeing crashes like this as of Go 1.8. Could it be related to the
> need for using runtime.KeepAlive? I have been adding that to my functions
> that look like this

As a general guideline, the only code that should require
runtime.KeepAlive is code that uses runtime.SetFinalizer.  If you
never use SetFinalizer, you should not need to use KeepAlive.

Either way, memory allocated by C.CString or C.malloc will never be
touched by the Go garbage collector, and using runtime.KeepAlive on it
will not have any effect.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x7fcb5ae54390

2017-08-18 Thread Justin Israel
On Sat, Aug 19, 2017, 8:38 AM Ian Lance Taylor  wrote:

> On Fri, Aug 18, 2017 at 8:59 AM, Bhaskar Singhal
>  wrote:
> >
> > C function just copies the parameters passed. All of the params have
> escaped
> > to heap as seen in output generated by using -gcflags "-m -m" during
> build.
> >
> > I am trying to figure out if there is a possibility that gc frees any of
> > these parameters before the C function call returns.
>
> Go's garbage collector never frees memory allocated by C.CString, just
> as it never frees memory allocated by C.malloc.
>
> Ian
>
>
> > On Fri, Aug 18, 2017 at 7:57 PM, Ian Lance Taylor 
> wrote:
> >>
> >> On Fri, Aug 18, 2017 at 12:53 AM, Bhaskar Singhal
> >>  wrote:
> >> > I am running into a seg fault. The code keeps crashing either due to
> >> > unexpected signal or double free.
> >> >
> >> > Any pointers on what I am doing wrong here:
> >> >
> >> > Code Snippet:
> >> > // Put puts the given key / value to the kvstore
> >> > func (kvs *kvstore) Put(key []byte, value []byte) error {
> >> > /* Encode key to avoid null characters in byte[] */
> >> > strk := base64.StdEncoding.EncodeToString([]byte(key))
> >> > csk := C.CString(strk)
> >> > defer C.free(unsafe.Pointer(csk))
> >> >
> >> > /* Encode value to avoid null characters in byte[] */
> >> > strv := base64.StdEncoding.EncodeToString([]byte(value))
> >> > csv := C.CString(strv)
> >> > defer C.free(unsafe.Pointer(csv))
> >> >
> >> > if kvs.kvtree == nil {
> >> > return fmt.Errorf("put failed because kvtree is nil")
> >> > }
> >> > size := C.int32_t(len(strv))
> >> >
> >> > ret := C.put(kvs.kvtree, csk, csv, )
> >> > if ret != C.int8_t(1) {
> >> > return fmt.Errorf("kvtree put failed %g", ret)
> >> > }
> >> > return nil
> >> > }
> >> >
> >> > C Function Put:
> >> > int8_t put(KVTree* kv,
> >> >   const char* key,
> >> >   const char* value,
> >> >   const int32_t* valuebytes);
> >>
> >>
> >> It really depends on how the C function behaves.  My guess is that it
> >> does not expect the values to be freed.
> >>
> >> Ian
> >
> >
>

I have cgo code that looks like this in a few of my own libs and I just
started seeing crashes like this as of Go 1.8. Could it be related to the
need for using runtime.KeepAlive? I have been adding that to my functions
that look like this


> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x7fcb5ae54390

2017-08-18 Thread Ian Lance Taylor
On Fri, Aug 18, 2017 at 8:59 AM, Bhaskar Singhal
 wrote:
>
> C function just copies the parameters passed. All of the params have escaped
> to heap as seen in output generated by using -gcflags "-m -m" during build.
>
> I am trying to figure out if there is a possibility that gc frees any of
> these parameters before the C function call returns.

Go's garbage collector never frees memory allocated by C.CString, just
as it never frees memory allocated by C.malloc.

Ian


> On Fri, Aug 18, 2017 at 7:57 PM, Ian Lance Taylor  wrote:
>>
>> On Fri, Aug 18, 2017 at 12:53 AM, Bhaskar Singhal
>>  wrote:
>> > I am running into a seg fault. The code keeps crashing either due to
>> > unexpected signal or double free.
>> >
>> > Any pointers on what I am doing wrong here:
>> >
>> > Code Snippet:
>> > // Put puts the given key / value to the kvstore
>> > func (kvs *kvstore) Put(key []byte, value []byte) error {
>> > /* Encode key to avoid null characters in byte[] */
>> > strk := base64.StdEncoding.EncodeToString([]byte(key))
>> > csk := C.CString(strk)
>> > defer C.free(unsafe.Pointer(csk))
>> >
>> > /* Encode value to avoid null characters in byte[] */
>> > strv := base64.StdEncoding.EncodeToString([]byte(value))
>> > csv := C.CString(strv)
>> > defer C.free(unsafe.Pointer(csv))
>> >
>> > if kvs.kvtree == nil {
>> > return fmt.Errorf("put failed because kvtree is nil")
>> > }
>> > size := C.int32_t(len(strv))
>> >
>> > ret := C.put(kvs.kvtree, csk, csv, )
>> > if ret != C.int8_t(1) {
>> > return fmt.Errorf("kvtree put failed %g", ret)
>> > }
>> > return nil
>> > }
>> >
>> > C Function Put:
>> > int8_t put(KVTree* kv,
>> >   const char* key,
>> >   const char* value,
>> >   const int32_t* valuebytes);
>>
>>
>> It really depends on how the C function behaves.  My guess is that it
>> does not expect the values to be freed.
>>
>> Ian
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x7fcb5ae54390

2017-08-18 Thread Bhaskar Singhal
C function just copies the parameters passed. All of the params have
escaped to heap as seen in output generated by using -gcflags "-m -m"
during build.

I am trying to figure out if there is a possibility that gc frees any of
these parameters before the C function call returns.

On Fri, Aug 18, 2017 at 7:57 PM, Ian Lance Taylor  wrote:

> On Fri, Aug 18, 2017 at 12:53 AM, Bhaskar Singhal
>  wrote:
> > I am running into a seg fault. The code keeps crashing either due to
> > unexpected signal or double free.
> >
> > Any pointers on what I am doing wrong here:
> >
> > Code Snippet:
> > // Put puts the given key / value to the kvstore
> > func (kvs *kvstore) Put(key []byte, value []byte) error {
> > /* Encode key to avoid null characters in byte[] */
> > strk := base64.StdEncoding.EncodeToString([]byte(key))
> > csk := C.CString(strk)
> > defer C.free(unsafe.Pointer(csk))
> >
> > /* Encode value to avoid null characters in byte[] */
> > strv := base64.StdEncoding.EncodeToString([]byte(value))
> > csv := C.CString(strv)
> > defer C.free(unsafe.Pointer(csv))
> >
> > if kvs.kvtree == nil {
> > return fmt.Errorf("put failed because kvtree is nil")
> > }
> > size := C.int32_t(len(strv))
> >
> > ret := C.put(kvs.kvtree, csk, csv, )
> > if ret != C.int8_t(1) {
> > return fmt.Errorf("kvtree put failed %g", ret)
> > }
> > return nil
> > }
> >
> > C Function Put:
> > int8_t put(KVTree* kv,
> >   const char* key,
> >   const char* value,
> >   const int32_t* valuebytes);
>
>
> It really depends on how the C function behaves.  My guess is that it
> does not expect the values to be freed.
>
> Ian
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x7fcb5ae54390

2017-08-18 Thread Ian Lance Taylor
On Fri, Aug 18, 2017 at 12:53 AM, Bhaskar Singhal
 wrote:
> I am running into a seg fault. The code keeps crashing either due to
> unexpected signal or double free.
>
> Any pointers on what I am doing wrong here:
>
> Code Snippet:
> // Put puts the given key / value to the kvstore
> func (kvs *kvstore) Put(key []byte, value []byte) error {
> /* Encode key to avoid null characters in byte[] */
> strk := base64.StdEncoding.EncodeToString([]byte(key))
> csk := C.CString(strk)
> defer C.free(unsafe.Pointer(csk))
>
> /* Encode value to avoid null characters in byte[] */
> strv := base64.StdEncoding.EncodeToString([]byte(value))
> csv := C.CString(strv)
> defer C.free(unsafe.Pointer(csv))
>
> if kvs.kvtree == nil {
> return fmt.Errorf("put failed because kvtree is nil")
> }
> size := C.int32_t(len(strv))
>
> ret := C.put(kvs.kvtree, csk, csv, )
> if ret != C.int8_t(1) {
> return fmt.Errorf("kvtree put failed %g", ret)
> }
> return nil
> }
>
> C Function Put:
> int8_t put(KVTree* kv,
>   const char* key,
>   const char* value,
>   const int32_t* valuebytes);


It really depends on how the C function behaves.  My guess is that it
does not expect the values to be freed.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x7fcb5ae54390

2017-08-18 Thread Bhaskar Singhal
I am running into a seg fault. The code keeps crashing either due to 
unexpected signal or double free.

Any pointers on what I am doing wrong here:

Code Snippet:
// Put puts the given key / value to the kvstore
func (kvs *kvstore) Put(key []byte, value []byte) error {
/* Encode key to avoid null characters in byte[] */
strk := base64.StdEncoding.EncodeToString([]byte(key))
csk := C.CString(strk)
defer C.free(unsafe.Pointer(csk))

/* Encode value to avoid null characters in byte[] */
strv := base64.StdEncoding.EncodeToString([]byte(value))
csv := C.CString(strv)
defer C.free(unsafe.Pointer(csv))

if kvs.kvtree == nil {
return fmt.Errorf("put failed because kvtree is nil")
}
size := C.int32_t(len(strv))

ret := C.put(kvs.kvtree, csk, csv, )
if ret != C.int8_t(1) {
return fmt.Errorf("kvtree put failed %g", ret)
}
return nil
}

C Function Put: 
int8_t put(KVTree* kv,  
  const char* key,
  const char* value, 
  const int32_t* valuebytes);   


Stack Trace:
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x80 addr=0x0 
pc=0x7fcb5ae54390]

runtime stack:
runtime.throw(0xeffa94, 0x2a)
/usr/lib/go-1.8/src/runtime/panic.go:596 +0x95
runtime.sigpanic()
/usr/lib/go-1.8/src/runtime/signal_unix.go:274 +0x2db

goroutine 1117 [syscall, locked to thread]:
runtime.cgocall(0xc88080, 0xc4204d7a70, 0xc4204d7a98)
/usr/lib/go-1.8/src/runtime/cgocall.go:131 +0xe2 fp=0xc4204d7a40 
sp=0xc4204d7a00
github.com/app/go-app/tempdb._Cfunc_put(0x34ebfd0, 0x7fcb4c027550, 
0x7fcb4c008b10, 0x7fcb4c00cf90, 0x0)
github.com/app/go-app/tempdb/_obj/_cgo_gotypes.go:131 +0x4a 
fp=0xc4204d7a70 sp=0xc4204d7a40
github.com/app/go-app/tempdb.(*kvstore).Put.func4(0x34ebfd0, 
0x7fcb4c027550, 0x7fcb4c008b10, 0x7fcb4c00cf90, 0xc42192a1ec)

/root/go-app/build/_workspace/src/github.com/app/go-app/tempdb/database.go:164 
+0x82 fp=0xc4204d7aa8 sp=0xc4204d7a70
github.com/app/go-app/tempdb.(*kvstore).Put(0xc420246c60, 0xc4218bc8a0, 
0x29, 0x30, 0xc42192a1e4, 0x1, 0x1, 0x0, 0x0)

/root/go-app/build/_workspace/src/github.com/app/go-app/tempdb/database.go:164 
+0x1d3 fp=0xc4204d7b40 sp=0xc4204d7aa8
github.com/app/go-app/core.WriteBlockReceipts(0x16dd480, 0xc420246c60, 
0xffcd5c15251c6f60, 0x36efda5ab3f463e3, 0x2f747de9501058d8, 
0x1000f51c73e6dc25, 0xe8, 0x0, 0x0, 0x0, ...)

/root/go-app/build/_workspace/src/github.com/app/go-app/core/database_util.go:428
 
+0x2f3 fp=0xc4204d7c88 sp=0xc4204d7b40
github.com/app/go-app/core.(*BC).InsertRC.func1(0x1)

/root/go-app/build/_workspace/src/github.com/app/go-app/core/bc.go:750 
+0x4d3 fp=0xc4204d7fa0 sp=0xc4204d7c88
github.com/app/go-app/core.(*BC).InsertRC.func2(0xc421825670, 0xc420d0fce0, 
0x1)

/root/go-app/build/_workspace/src/github.com/app/go-app/core/bc.go:777 
+0x56 fp=0xc4204d7fc8 sp=0xc4204d7fa0
runtime.goexit()
/usr/lib/go-1.8/src/runtime/asm_amd64.s:2197 +0x1 fp=0xc4204d7fd0 
sp=0xc4204d7fc8
created by github.com/app/go-app/core.(*BC).InsertRC

/root/go-app/build/_workspace/src/github.com/app/go-app/core/bc.go:778 
+0x5da



Thanks,
Bhaskar

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.