Skip to content

Fix mmap.resize with offset #1897

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

Merged
merged 2 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/core/IronPython.Modules/mmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,8 @@ public void resize(long newsize) {
throw PythonOps.ValueError("new size out of range");
}

long capacity = checked(_offset + newsize);

if (_handle is not null
&& (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))) {
// resize on Posix platforms
Expand All @@ -941,14 +943,14 @@ public void resize(long newsize) {

// Resize the underlying file as needed.
int fd = unchecked((int)_handle.DangerousGetHandle());
PythonNT.ftruncateUnix(fd, newsize);
PythonNT.ftruncateUnix(fd, capacity);

#if NET8_0_OR_GREATER
_file = MemoryMappedFile.CreateFromFile(_handle, _mapName, newsize, _fileAccess, HandleInheritability.None, leaveOpen: true);
_file = MemoryMappedFile.CreateFromFile(_handle, _mapName, capacity, _fileAccess, HandleInheritability.None, leaveOpen: true);
#else
_sourceStream?.Dispose();
_sourceStream = new FileStream(new SafeFileHandle((IntPtr)fd, ownsHandle: false), FileAccess.ReadWrite);
_file = CreateFromFile(_sourceStream, _mapName, newsize, _fileAccess, HandleInheritability.None, leaveOpen: true);
_file = CreateFromFile(_sourceStream, _mapName, capacity, _fileAccess, HandleInheritability.None, leaveOpen: true);
#endif
_view = _file.CreateViewAccessor(_offset, newsize, _fileAccess);
return;
Expand All @@ -968,8 +970,6 @@ public void resize(long newsize) {
return;
}

long capacity = checked(_offset + newsize);

try {
if (newsize == 0) {
// resizing to an empty mapped region is not allowed
Expand Down Expand Up @@ -1401,17 +1401,18 @@ private struct SYSTEM_INFO {
internal short wProcessorRevision;
}

[SupportedOSPlatform("windows")]
[DllImport("kernel32", SetLastError = true)]
private static extern void GetSystemInfo(ref SYSTEM_INFO lpSystemInfo);

private static int GetAllocationGranularity() {
try {
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
return GetAllocationGranularityWorker();
} catch {
return System.Environment.SystemPageSize;
}
return System.Environment.SystemPageSize;
}

[SupportedOSPlatform("windows")]
[MethodImpl(MethodImplOptions.NoInlining)]
private static int GetAllocationGranularityWorker() {
SYSTEM_INFO info = new SYSTEM_INFO();
Expand Down
6 changes: 3 additions & 3 deletions src/core/IronPython.StdLib/lib/test/test_mmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_basic(self):
# resize() not supported
# No messages are printed, since the output of this test suite
# would then be different across platforms.
pass
raise # ironpython: all our runners currently support resize
else:
# resize() is supported
self.assertEqual(len(m), 512)
Expand Down Expand Up @@ -168,7 +168,7 @@ def test_access_parameter(self):
try:
m.resize(2*mapsize)
except SystemError: # resize is not universally supported
pass
raise # ironpython: all our runners currently support resize
except TypeError:
pass
else:
Expand Down Expand Up @@ -539,7 +539,7 @@ def test_offset (self):
try:
m.resize(512)
except SystemError:
pass
raise # ironpython: all our runners currently support resize
else:
# resize() is supported
self.assertEqual(len(m), 512)
Expand Down