From c9e56d48445c78cdfdfdd9296ad195275ee2178d Mon Sep 17 00:00:00 2001 From: xgdgsc Date: Sat, 26 Feb 2022 12:07:29 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=F0=9F=90=9B=20fix=20bus=20error=20on=20sma?= =?UTF-8?q?ller=20readonly=20file=20in=20unix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stdlib/Mmap/src/Mmap.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/Mmap/src/Mmap.jl b/stdlib/Mmap/src/Mmap.jl index 629f53e8371ed..80eb24422837f 100644 --- a/stdlib/Mmap/src/Mmap.jl +++ b/stdlib/Mmap/src/Mmap.jl @@ -212,6 +212,7 @@ function mmap(io::IO, @static if Sys.isunix() prot, flags, iswrite = settings(file_desc, shared) iswrite && grow && grow!(io, offset, len) + !iswrite && mmaplen > filesize(io) && throw(ArgumentError("unable to mmap file size smaller than $mmaplen due to read-only permissions. Please check your dims")) # mmap the file ptr = ccall(:jl_mmap, Ptr{Cvoid}, (Ptr{Cvoid}, Csize_t, Cint, Cint, RawFD, Int64), C_NULL, mmaplen, prot, flags, file_desc, offset_page) From 10588f3a773ff8dc94e7a8428a01b6b8468d2a16 Mon Sep 17 00:00:00 2001 From: xgdgsc Date: Sat, 26 Feb 2022 14:05:09 +0800 Subject: [PATCH 2/9] Update stdlib/Mmap/src/Mmap.jl Co-authored-by: Jameson Nash --- stdlib/Mmap/src/Mmap.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/Mmap/src/Mmap.jl b/stdlib/Mmap/src/Mmap.jl index 80eb24422837f..cf282d2385f93 100644 --- a/stdlib/Mmap/src/Mmap.jl +++ b/stdlib/Mmap/src/Mmap.jl @@ -212,7 +212,8 @@ function mmap(io::IO, @static if Sys.isunix() prot, flags, iswrite = settings(file_desc, shared) iswrite && grow && grow!(io, offset, len) - !iswrite && mmaplen > filesize(io) && throw(ArgumentError("unable to mmap file size smaller than $mmaplen due to read-only permissions. Please check your dims")) + szfile = convert(Csize_t, len + offset) + !iswrite && szfile > filesize(io) && throw(ArgumentError("unable to increase file size to $szfile due to read-only permissions")) # mmap the file ptr = ccall(:jl_mmap, Ptr{Cvoid}, (Ptr{Cvoid}, Csize_t, Cint, Cint, RawFD, Int64), C_NULL, mmaplen, prot, flags, file_desc, offset_page) From de9c3753960966fe702712148cff8c9cc965c643 Mon Sep 17 00:00:00 2001 From: xgdgsc Date: Tue, 1 Mar 2022 22:38:36 +0800 Subject: [PATCH 3/9] Update stdlib/Mmap/src/Mmap.jl Co-authored-by: Jameson Nash --- stdlib/Mmap/src/Mmap.jl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/stdlib/Mmap/src/Mmap.jl b/stdlib/Mmap/src/Mmap.jl index cf282d2385f93..1f742659da7f3 100644 --- a/stdlib/Mmap/src/Mmap.jl +++ b/stdlib/Mmap/src/Mmap.jl @@ -211,9 +211,12 @@ function mmap(io::IO, # platform-specific mmapping @static if Sys.isunix() prot, flags, iswrite = settings(file_desc, shared) - iswrite && grow && grow!(io, offset, len) szfile = convert(Csize_t, len + offset) - !iswrite && szfile > filesize(io) && throw(ArgumentError("unable to increase file size to $szfile due to read-only permissions")) + if iswrite && grow + grow!(io, offset, len) + elseif szfile > filesize(io) + throw(ArgumentError("unable to increase file size to $szfile due to read-only permissions")) + end # mmap the file ptr = ccall(:jl_mmap, Ptr{Cvoid}, (Ptr{Cvoid}, Csize_t, Cint, Cint, RawFD, Int64), C_NULL, mmaplen, prot, flags, file_desc, offset_page) From b8bb51bfeaa6ff9bf4a4d9c52cfa2d4e4dc826d0 Mon Sep 17 00:00:00 2001 From: xgdgsc Date: Wed, 2 Mar 2022 00:01:40 +0800 Subject: [PATCH 4/9] logic cleanup --- stdlib/Mmap/src/Mmap.jl | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/stdlib/Mmap/src/Mmap.jl b/stdlib/Mmap/src/Mmap.jl index 1f742659da7f3..7ac0b897092be 100644 --- a/stdlib/Mmap/src/Mmap.jl +++ b/stdlib/Mmap/src/Mmap.jl @@ -208,14 +208,22 @@ function mmap(io::IO, mmaplen = (offset - offset_page) + len file_desc = gethandle(io) + szfile = convert(Csize_t, len + offset) + requestedSizeLarger = szfile > filesize(io) # platform-specific mmapping @static if Sys.isunix() prot, flags, iswrite = settings(file_desc, shared) - szfile = convert(Csize_t, len + offset) - if iswrite && grow - grow!(io, offset, len) - elseif szfile > filesize(io) - throw(ArgumentError("unable to increase file size to $szfile due to read-only permissions")) + if requestedSizeLarger + if iswrite + if grow + grow!(io, offset, len) + else + throw(ArgumentError("requested size $szfile larger than file size $(filesize(io)), but requested not to grow")) + end + end + if !iswrite + throw(ArgumentError("unable to increase file size to $szfile due to read-only permissions")) + end end # mmap the file ptr = ccall(:jl_mmap, Ptr{Cvoid}, (Ptr{Cvoid}, Csize_t, Cint, Cint, RawFD, Int64), @@ -223,8 +231,14 @@ function mmap(io::IO, systemerror("memory mapping failed", reinterpret(Int, ptr) == -1) else name, readonly, create = settings(io) - szfile = convert(Csize_t, len + offset) - readonly && szfile > filesize(io) && throw(ArgumentError("unable to increase file size to $szfile due to read-only permissions")) + if requestedSizeLarger + if readonly + throw(ArgumentError("unable to increase file size to $szfile due to read-only permissions")) + end + if !readonly && !grow + throw(ArgumentError("requested size $szfile larger than file size $(filesize(io)), but requested not to grow")) + end + end handle = create ? ccall(:CreateFileMappingW, stdcall, Ptr{Cvoid}, (OS_HANDLE, Ptr{Cvoid}, DWORD, DWORD, DWORD, Cwstring), file_desc, C_NULL, readonly ? PAGE_READONLY : PAGE_READWRITE, szfile >> 32, szfile & typemax(UInt32), name) : ccall(:OpenFileMappingW, stdcall, Ptr{Cvoid}, (DWORD, Cint, Cwstring), From b7829c9fe0d5b24e1891d93eb9141243983aaaf7 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Tue, 8 Nov 2022 11:43:46 -0500 Subject: [PATCH 5/9] Apply suggestions from code review --- stdlib/Mmap/src/Mmap.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/stdlib/Mmap/src/Mmap.jl b/stdlib/Mmap/src/Mmap.jl index 7ac0b897092be..68e658ca7754b 100644 --- a/stdlib/Mmap/src/Mmap.jl +++ b/stdlib/Mmap/src/Mmap.jl @@ -220,8 +220,7 @@ function mmap(io::IO, else throw(ArgumentError("requested size $szfile larger than file size $(filesize(io)), but requested not to grow")) end - end - if !iswrite + else throw(ArgumentError("unable to increase file size to $szfile due to read-only permissions")) end end @@ -234,8 +233,7 @@ function mmap(io::IO, if requestedSizeLarger if readonly throw(ArgumentError("unable to increase file size to $szfile due to read-only permissions")) - end - if !readonly && !grow + elseif !grow throw(ArgumentError("requested size $szfile larger than file size $(filesize(io)), but requested not to grow")) end end From 015812fd72de7f13f51e784b704bcd4405b20af1 Mon Sep 17 00:00:00 2001 From: xgdgsc Date: Thu, 12 Jan 2023 17:09:27 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=F0=9F=90=9B=20try=20fix=20ci=20fail?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stdlib/Mmap/src/Mmap.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/stdlib/Mmap/src/Mmap.jl b/stdlib/Mmap/src/Mmap.jl index 68e658ca7754b..123ea24681d96 100644 --- a/stdlib/Mmap/src/Mmap.jl +++ b/stdlib/Mmap/src/Mmap.jl @@ -209,7 +209,11 @@ function mmap(io::IO, file_desc = gethandle(io) szfile = convert(Csize_t, len + offset) - requestedSizeLarger = szfile > filesize(io) + requestedSizeLarger = false + if io isa Mmap.Anonymous + else + requestedSizeLarger = szfile > filesize(io) + end # platform-specific mmapping @static if Sys.isunix() prot, flags, iswrite = settings(file_desc, shared) From bc0cdaa547db5fb582327c20f5fa1b232d8f06c3 Mon Sep 17 00:00:00 2001 From: xgdgsc Date: Fri, 13 Jan 2023 09:50:18 +0800 Subject: [PATCH 7/9] Update stdlib/Mmap/src/Mmap.jl Co-authored-by: Stefan Karpinski --- stdlib/Mmap/src/Mmap.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/Mmap/src/Mmap.jl b/stdlib/Mmap/src/Mmap.jl index 123ea24681d96..1ac498171ce64 100644 --- a/stdlib/Mmap/src/Mmap.jl +++ b/stdlib/Mmap/src/Mmap.jl @@ -210,8 +210,7 @@ function mmap(io::IO, file_desc = gethandle(io) szfile = convert(Csize_t, len + offset) requestedSizeLarger = false - if io isa Mmap.Anonymous - else + if !(io isa Mmap.Anonymous) requestedSizeLarger = szfile > filesize(io) end # platform-specific mmapping From 8b02ca4f7ea728e6d7378c9e87398752925f830c Mon Sep 17 00:00:00 2001 From: xgdgsc Date: Sat, 15 Jul 2023 09:38:08 +0800 Subject: [PATCH 8/9] =?UTF-8?q?try=20skip=20for=20=F0=9F=8D=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stdlib/Mmap/src/Mmap.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/Mmap/src/Mmap.jl b/stdlib/Mmap/src/Mmap.jl index 1ac498171ce64..1016870ba50a2 100644 --- a/stdlib/Mmap/src/Mmap.jl +++ b/stdlib/Mmap/src/Mmap.jl @@ -210,7 +210,7 @@ function mmap(io::IO, file_desc = gethandle(io) szfile = convert(Csize_t, len + offset) requestedSizeLarger = false - if !(io isa Mmap.Anonymous) + if !(io isa Mmap.Anonymous || Sys.isapple()) requestedSizeLarger = szfile > filesize(io) end # platform-specific mmapping From 67bab23ec15fde7d395404c8189891c3b8a1fe88 Mon Sep 17 00:00:00 2001 From: xgdgsc Date: Sat, 15 Jul 2023 11:31:35 +0800 Subject: [PATCH 9/9] =?UTF-8?q?try=20keep=20old=20behaviror=20for=20?= =?UTF-8?q?=F0=9F=8D=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stdlib/Mmap/src/Mmap.jl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/stdlib/Mmap/src/Mmap.jl b/stdlib/Mmap/src/Mmap.jl index 1016870ba50a2..9dd02d5aa9d04 100644 --- a/stdlib/Mmap/src/Mmap.jl +++ b/stdlib/Mmap/src/Mmap.jl @@ -210,8 +210,10 @@ function mmap(io::IO, file_desc = gethandle(io) szfile = convert(Csize_t, len + offset) requestedSizeLarger = false - if !(io isa Mmap.Anonymous || Sys.isapple()) - requestedSizeLarger = szfile > filesize(io) + if !(io isa Mmap.Anonymous) + @static if !Sys.isapple() + requestedSizeLarger = szfile > filesize(io) + end end # platform-specific mmapping @static if Sys.isunix() @@ -227,6 +229,9 @@ function mmap(io::IO, throw(ArgumentError("unable to increase file size to $szfile due to read-only permissions")) end end + @static if Sys.isapple() + iswrite && grow && grow!(io, offset, len) + end # mmap the file ptr = ccall(:jl_mmap, Ptr{Cvoid}, (Ptr{Cvoid}, Csize_t, Cint, Cint, RawFD, Int64), C_NULL, mmaplen, prot, flags, file_desc, offset_page)