-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
x/sys: add unsafe mmap #56123
Comments
Can you write down the exact new API that you propose adding? Thanks. |
I need this in I'm currently I know my package is pretty much irrelevant, but exposing the autogenerated bits that allow Hence, answering @ianlancetaylor, I'd like to propose func MmapPtr(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
func MunmapPtr(addr uintptr, length uintptr) (err error) And maybe, for the platforms that support it: func MremapPtr(oldaddr uintptr, oldsize uintptr, newaddr uintptr, newsize uintptr, flags int) (ret uintptr, err error) These match the portability layer already in |
Related discussion: #58625 |
It seems like the pointers should at least be pointers. What about:
? |
I don't know that I'm qualified to answer that. I'll just say this is not Go managed memory, and as said above That was my reasoning, but I'm happy to be overridden by someone more knowledgeable. 🙂 |
In general, the unix syscall packages try to be as type-safe as possible, so using |
If this is a likely approve, I don't minding submitting a CL myself. |
This proposal has been added to the active column of the proposals project |
Have all remaining concerns about this proposal been addressed? The proposal is to add:
|
Based on the discussion above, this proposal seems like a likely accept. The proposal is to add:
|
While trying to implement this a few nits came up. For func MmapPtr(addr unsafe.Pointer, length uintptr, prot int, flag int, fd int, pos int64) (ret unsafe.Pointer, err error) But func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error) Should we be consistent with the syscall, or with Go? Both work for me. I also see no reason to use Final nit, should For func MremapPtr(oldaddr unsafe.Pointer, oldsize uintptr, newaddr unsafe.Pointer, newsize uintptr, flags int) (ret unsafe.Pointer, err error) Is this intentional? I'd also use camel case for the arguments, but that's less important. |
If it helps I've tested golang/sys#197 with my package and it works fine: ncruces/go-sqlite3@main...mmap |
Change https://go.dev/cl/592415 mentions this issue: |
Since The
I think that the NetBSD argument order makes more sense, so let's stick with that. Fortunately the compiler will catch any confusion. Thanks for raising these issues. |
Thanks @ianlancetaylor. I can add tests after the proposal is accepted. |
It does seem like if you are editing code to update from unix.Mmap to unix.MmapPtr you should not have to permute the existing arguments. That suggest we should change the MmapPtr list. The current Mmap is:
We need to add addr, which maybe makes sense next to length, and length should be a uintptr now (arguably), so func MmapPtr(fd int, offset int64, addr unsafe.Pointer, length is the minimal change that will make sense if you are editing. Clearly I just copied the system call, but unix.Mmap and unix.MmapPtr will be next to each other in docs and should be consistent. Similarly comparing: func Mremap(oldData []byte, newLength int, flags int) (data []byte, err error) This looks right from a parallelism point of view. |
Either decision is fine by me. |
Made the changes to golang/sys#197, and added some tests. |
No change in consensus, so accepted. 🎉 The proposal is to add:
|
I'm confused. I was under the impression the proposal had been updated in #56123 (comment) to add: func MmapPtr(fd int, offset int64, addr unsafe.Pointer, length uintptr, prot int, flags int) (ret unsafe.Pointer, err error)
func MunmapPtr(addr unsafe.Pointer, length uintptr) (err error)
func MremapPtr(oldAddr unsafe.Pointer, oldSize uintptr, newAddr unsafe.Pointer, newSize uintptr, flags int) (ret unsafe.Pointer, err error) Notice the different parameter order for |
I think @rsc didn't update the info source for the bot that posted the accept message. We should go with what you just wrote. Thanks. |
Yes, sorry. The right proposal is #56123 (comment). |
i use go version go1.18.1 linux/amd64. And i wat to use syscall.Mmap, but i got an error. It seems i can't use syscall.MAP_FIXED.
but s is nil. I cat't use fix adress. why? when i use |
who can help me? i use |
It's hard to understand what you're trying to do, and why you need That said, there's no useful way to use |
The current implementation of the
Mmap
wrappers for the Unix-derived OSes does not dispatch directly to the relevantsyscall
but relies on themmapper
layer. Themmapper
adds address range validation and returnsEINVAL
for the ranges not matching the content of the mapping registry. While it might be arguably correct for most of the use cases, it also prevents partialMunmaps
and similar valid parts of the pretty richmmap
semantics. I suggest that the existing implementation should be left as-is, but undisturbed access to the underlying OS primitives should also be provided. The scope of the extension would be minimal, as the low-level part already is there -- the package should just export more entry points. The naming details are irrelevant to me, so I leave them unspecified: I will be equally happy withMmapUnsafe
as withMmapRaw
.The only suggestion I'd make is the return type: the functions should operate with
uintptr
, as theirWinAPI
VirtualAlloc()
counterparts do. Wrapping theuintptr
in a slice just to unwrap it in the follow-up step to recover theuintptr
adds no value. I'll make the slice when I'm done.The text was updated successfully, but these errors were encountered: