Signal SIGSEGV: segmentation violation code=0x1

here is my code
shmid, _, err := syscall.Syscall(syscall.SYS_SHMGET, uintptr(10001), 1<<32, IpcCreate|0600)
data_shmaddr, _, _ := syscall.Syscall(syscall.SYS_SHMAT, shmid, 0, 0)
C.memcpy(unsafe.Pointer(data_shmaddr+uintptr(per_offs)), unsafe.Pointer((C.CString)(string(*data))), C.ulong(len(*data)))

i want to copy ‘data’ to sharememory with memcpy, but it seems like i can’t map all the address from data_shmaddr to (data_shmaddr+1<<32).
here is what i got:
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x7fe3502cca80 pc=0x7fe34d9320b4]

runtime stack:
runtime.throw(0x552be5, 0x2a)
/data1/user1/go_env/go/src/runtime/panic.go:619 +0x81
/data1/user1/go_env/go/src/runtime/signal_unix.go:372 +0x28e

goroutine 1 [syscall]:
runtime.cgocall(0x4fe960, 0xc4200edd40, 0x29)
/data1/user1/go_env/go/src/runtime/cgocall.go:128 +0x64 fp=0xc4200edd00 sp=0xc4200edcc8 pc=0x403034
main._Cfunc_memcpy(0x7fe3502cca80, 0x7fe3440008c0, 0x1, 0x0)
_cgo_gotypes.go:78 +0x4e fp=0xc4200edd40 sp=0xc4200edd00 pc=0x4fdb7e
main.(*MemPoolMng).PutData.func1(0x7fe3502cca80, 0x7fe3440008c0, 0x1, 0x1)
/data1/user2/GoServer/mempool_go/mempool_mgr.go:107 +0x99 fp=0xc4200edd78 sp=0xc4200edd40 pc=0x4fe7a9
main.(*MemPoolMng).PutData(0xc4200182d0, 0xc420158000, 0xf4240)
/data1/user2/GoServer/mempool_go/mempool_mgr.go:107 +0x1da fp=0xc4200ede58 sp=0xc4200edd78 pc=0x4fe37a
main.handleProtoClient(0x565720, 0xc42000e060)
/data1/user2/GoServer/mempool_go/server.go:58 +0x16f fp=0xc4200eded0 sp=0xc4200ede58 pc=0x4fcf8f
/data1/user2/GoServer/mempool_go/server.go:39 +0x261 fp=0xc4200edf88 sp=0xc4200eded0 pc=0x4fcca1
/data1/user1/go_env/go/src/runtime/proc.go:198 +0x212 fp=0xc4200edfe0 sp=0xc4200edf88 pc=0x42b772
/data1/user1/go_env/go/src/runtime/asm_amd64.s:2361 +0x1 fp=0xc4200edfe8 sp=0xc4200edfe0 pc=0x455601
exit status 2

Can anyone help me please?

It may help if you show us a complete program, rather than just a snippet. What are you using for IpcCreate? How did you declare data?

Without knowing those, I worked with your example. I have this:

package main

//#include <stdio.h>
//#include <string.h>
//#include <sys/ipc.h>
//#include <sys/shm.h>
//void *memcpy(void *dest, const void *src, size_t n);
//int printf(const char *format, ...);
//#define BUFSIZE 32
//void dump(char *p)
//      int i;
//      for(i = 0; i < BUFSIZE; i++)
//              printf("%d: %d\n",i,p[i]);
//      printf("\n");
import "C"

import (

const bufsize int = 32
const offset = 10

var IpcCreate uintptr
var per_offs int = offset
var buf [bufsize]byte
var data *[bufsize]byte

// a tag so we can recognize if it's copying properly
const tagsize int = 10
const tag byte = 42

func main() {
        data = &buf
        IpcCreate = C.IPC_CREAT

        shmid, _, err := syscall.Syscall(syscall.SYS_SHMGET, uintptr(10001), 1<<32, IpcCreate|0600)
        if err != 0 {
                fmt.Fprintf(os.Stderr,"Cannot get shared memory. Error %d\n", err)
        data_shmaddr, _, err := syscall.Syscall(syscall.SYS_SHMAT, shmid, 0, 0)
        if err != 0 {
                fmt.Fprintf(os.Stderr,"Cannot attach to shared memory. Error %d\n", err)

        // write the tag into the buffer

        for i := 0; i < tagsize; i++ { buf[i] = tag }

        // copy the buffer into the shared memory, starting at byte 10

                 unsafe.Pointer(data), C.ulong(len(*data)))

        // dump data[]
        // this shows that 42 is in locations 0-9


        // dump bufsize bytes of shared memory area
        // this shows that 42 is in locations 10-19

        fmt.Printf("shared memory:\n")

I think this does what you want it to. If not, then please state your problem more clearly.

Note to everyone: This program uses Cgo and both the unsafe and syscall packages. So don’t expect it to be cross-platform. :stuck_out_tongue: If you have Linux, you can try it.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.