bytedance / sonic

A blazingly fast JSON serializing & deserializing library
Apache License 2.0
6.71k stars 333 forks source link

mmap_unix.go missing BSD in Go build tag #564

Closed vendion closed 8 months ago

vendion commented 8 months ago

https://github.com/bytedance/sonic/blob/8c71eb047516a6b70f216887923f353f42d0501b/loader/mmap_unix.go#L1C1-L2C23

Would it be possible to make the _loader/mmapunix.go file target more Unix based OSes than just Darwin and Linux? I'm trying to build a project that indirectly depends on this package and I get the following build error.

# github.com/bytedance/sonic/loader
../../go/pkg/mod/github.com/bytedance/sonic@v1.10.2/loader/funcdata_latest.go:228:13: undefined: mmap
../../go/pkg/mod/github.com/bytedance/sonic@v1.10.2/loader/funcdata_latest.go:233:5: undefined: mprotect
AsterDY commented 8 months ago

didn't try before. Are you interested in giving a PR?

vendion commented 8 months ago

Well it is a very simple change as the syscalls for the BSDs should match those for Darwin (based on BSD anyways) & Linux so something like this should be more than enough:

diff --git a/loader/mmap_unix.go b/loader/mmap_unix.go
index 3ea944e..1d15e54 100644
--- a/loader/mmap_unix.go
+++ b/loader/mmap_unix.go
@@ -1,5 +1,5 @@
-//go:build darwin || linux
-// +build darwin linux
+//go:build !windows
+// +build !windows

 /**
  * Copyright 2023 ByteDance Inc.

Unless you have reasons for being explict in this case.

vendion commented 8 months ago

I didn't submit a PR, as it's a one line change and that the formatting of the file doesn't follow the standard gofmt standard so the whole flie would end up being needlessly modified.

diff --git a/loader/mmap_unix.go b/loader/mmap_unix.go
index 3ea944e..1d15e54 100644
--- a/loader/mmap_unix.go
+++ b/loader/mmap_unix.go
@@ -1,5 +1,5 @@
-//go:build darwin || linux
-// +build darwin linux
+//go:build !windows
+// +build !windows

 /**
  * Copyright 2023 ByteDance Inc.
@@ -20,26 +20,25 @@
 package loader

 import (
-    `syscall`
+   "syscall"
 )

 const (
-    _AP = syscall.MAP_ANON  | syscall.MAP_PRIVATE
-    _RX = syscall.PROT_READ | syscall.PROT_EXEC
-    _RW = syscall.PROT_READ | syscall.PROT_WRITE
+   _AP = syscall.MAP_ANON | syscall.MAP_PRIVATE
+   _RX = syscall.PROT_READ | syscall.PROT_EXEC
+   _RW = syscall.PROT_READ | syscall.PROT_WRITE
 )

-
 func mmap(nb int) uintptr {
-    if m, _, e := syscall.RawSyscall6(syscall.SYS_MMAP, 0, uintptr(nb), _RW, _AP, 0, 0); e != 0 {
-        panic(e)
-    } else {
-        return m
-    }
+   if m, _, e := syscall.RawSyscall6(syscall.SYS_MMAP, 0, uintptr(nb), _RW, _AP, 0, 0); e != 0 {
+       panic(e)
+   } else {
+       return m
+   }
 }

 func mprotect(p uintptr, nb int) {
-    if _, _, err := syscall.RawSyscall(syscall.SYS_MPROTECT, p, uintptr(nb), _RX); err != 0 {
-        panic(err)
-    }
+   if _, _, err := syscall.RawSyscall(syscall.SYS_MPROTECT, p, uintptr(nb), _RX); err != 0 {
+       panic(err)
+   }
 }
AsterDY commented 8 months ago

ok,I will submit it