Skip to content

Instantly share code, notes, and snippets.

@knqyf263
Last active March 30, 2020 16:20
Show Gist options
  • Save knqyf263/8e457ee2c1deff816804f2c1c195bdb1 to your computer and use it in GitHub Desktop.
Save knqyf263/8e457ee2c1deff816804f2c1c195bdb1 to your computer and use it in GitHub Desktop.
mmap panic
package main
import (
"fmt"
"io/ioutil"
"os"
"syscall"
"unsafe"
"golang.org/x/sys/unix"
)
type Foo struct {
a int
}
func main() {
f, err := ioutil.TempFile("", "test")
if err != nil {
panic(err)
}
defer os.Remove(f.Name())
//m := []byte{0, 0} // it works
m, err := unix.Mmap(int(f.Fd()), 0, 10, syscall.PROT_READ, syscall.MAP_SHARED) // error
if err != nil {
panic(err)
}
var data *[100]byte
data = (*[100]byte)(unsafe.Pointer(&m[0]))
foo := (*Foo)(unsafe.Pointer(&data[0]))
if foo == nil {
fmt.Println("foo is nil")
}
fmt.Println(foo.a)
}
@knqyf263
Copy link
Author

knqyf263 commented Mar 30, 2020

$ go run main.go
unexpected fault address 0x11fe000
fatal error: fault
[signal SIGBUS: bus error code=0x2 addr=0x11fe000 pc=0x109feb5]

goroutine 1 [running]:
runtime.throw(0x10d018f, 0x5)
        /usr/local/Cellar/go/1.14.1/libexec/src/runtime/panic.go:1114 +0x72 fp=0xc00009aeb8 sp=0xc00009ae88 pc=0x102e802
runtime.sigpanic()
        /usr/local/Cellar/go/1.14.1/libexec/src/runtime/signal_unix.go:692 +0x443 fp=0xc00009aee8 sp=0xc00009aeb8 pc=0x1042863
main.main()
        /Users/teppei/src/github.com/knqyf263/playground/mmap/main.go:38 +0x145 fp=0xc00009af88 sp=0xc00009aee8 pc=0x109feb5
runtime.main()
        /usr/local/Cellar/go/1.14.1/libexec/src/runtime/proc.go:203 +0x212 fp=0xc00009afe0 sp=0xc00009af88 pc=0x1030e82
runtime.goexit()
        /usr/local/Cellar/go/1.14.1/libexec/src/runtime/asm_amd64.s:1373 +0x1 fp=0xc00009afe8 sp=0xc00009afe0 pc=0x105b1e1

Process finished with exit code 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment