Last active
March 30, 2020 16:20
-
-
Save knqyf263/8e457ee2c1deff816804f2c1c195bdb1 to your computer and use it in GitHub Desktop.
mmap panic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Author
knqyf263
commented
Mar 30, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment