Skip to content

Implement mmap.mmap.move() - #1370

Open
deronnax wants to merge 2 commits into
oracle:masterfrom
deronnax:mmap-move
Open

deronnax wants to merge 2 commits into
oracle:masterfrom
deronnax:mmap-move

Conversation

@deronnax

Copy link
Copy Markdown

Fixes #1368.

mmap.mmap was missing the move(dest, src, count) method.

Changes

  • New move builtin in MMapBuiltins, mirroring CPython's mmap_move_method:
    • ValueError("mmap closed or invalid") on a closed map,
    • TypeError("mmap can't modify a readonly memory map.") on an ACCESS_READ map,
    • ValueError("source, destination, or count out of range") for negative or out-of-bounds arguments.
  • memmove semantics on top of PosixSupport.mmapReadBytes/mmapWriteBytes: the copy goes through a 64 KiB buffer, chunk by chunk, backwards when dest > src, so overlapping ranges are handled.
  • New tests in tests/test_mmap.py: the reproducer from the issue, error cases, chunked overlapping copies in both directions, and a read-only map.
  • test.test_mmap.MmapTests.test_move from CPython is now tagged as passing on all platforms.

Validation

  • Reproducer from the issue prints b'aabcdf', like CPython.
  • macOS aarch64, local mx python-jvm build:
    • mx graalpytest graalpython/com.oracle.graal.python.test/src/tests/test_mmap.py: 12 passed, 2 skipped (the Windows-only ones).
    • mx graalpytest --tagged graalpython/lib-python/3/test/test_mmap.py: 28 passed, including test_move.
  • GitHub Actions on my fork: style, ECJ and SVM builds pass, and python-unittest-gate and the tagged tests pass on linux-amd64. Note: python-svm-build-gate-linux-amd64 currently fails upstream too, on mx python-check-call-host-inlining (host-inlining.txt not found, see the CI of [GR-81685] Improvements of GetAttribute operation: new quickenings & revisit cache limits. #1369). To get past it, I disabled that check on a separate CI-only branch.

🤖 Generated with Claude Code

Fixes oracle#1368.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Mathieu Dupuy <deronnax@gmail.com>
@oracle-contributor-agreement

Copy link
Copy Markdown

Thank you for your pull request and welcome to our community! To contribute, please sign the Oracle Contributor Agreement (OCA).
The following contributors of this PR have not signed the OCA:

To sign the OCA, please create an Oracle account and sign the OCA in Oracle's Contributor Agreement Application.

When signing the OCA, please provide your GitHub username. After signing the OCA and getting an OCA approval from Oracle, this PR will be automatically updated.

If you are an Oracle employee, please make sure that you are a member of the main Oracle GitHub organization, and your membership in this organization is public.

@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Required At least one contributor does not have an approved Oracle Contributor Agreement. label Nov 26, 2026
@deronnax

Copy link
Copy Markdown
Author

I signed the Oracle OCA, but so far it did not seem to work.

@msimacek

Copy link
Copy Markdown
Contributor

Hi, @deronnax, I think the problem is that your email is not publicly visible in your github profile, so the bot cannot pair your signature with your profile

@deronnax

Copy link
Copy Markdown
Author

Interesting. I made it publicly visible again, let's see.

@deronnax

Copy link
Copy Markdown
Author

let's try to close and reopen, to see if it wakes the bot up.

@deronnax deronnax closed this Nov 30, 2026
@deronnax deronnax reopened this Nov 30, 2026
@msimacek

Copy link
Copy Markdown
Contributor

Hi @deronnax, I checked the internal API that the bot is using and it doesn't show a valid OCA for the email that is in your github profile. What email did you use in the OCA form? Is there some confirmation email or screenshot from the OCA app that you could forward to me to debug this further?

@deronnax

Copy link
Copy Markdown
Author

Looks good to me (the email and the handle are valid). Maybe the company name? It's my personal company, it is not shown in my github profile.
Page DocuSign avec données sensibles masquées

@msimacek

Copy link
Copy Markdown
Contributor

Looks ok to me, I raised an internal ticket against the OCA service

@oracle-contributor-agreement

Copy link
Copy Markdown

Thank you for signing the OCA.

@oracle-contributor-agreement oracle-contributor-agreement Bot added OCA Verified All contributors have signed the Oracle Contributor Agreement. and removed OCA Required At least one contributor does not have an approved Oracle Contributor Agreement. labels Dec 1, 2026

private static void copyChunk(Node inliningTarget, PosixSupport posixSupport, Object handle, long from, long to, byte[] buffer, int n, PRaiseNode raiseNode)
throws PosixException {
int nread = posixSupport.mmapReadBytes(handle, from, buffer, n);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Support moves on write-only file mappings in the Java backend

With --python.PosixModuleBackend=java, a file mapping created with prot=mmap.PROT_WRITE passes the writability check, but copyChunk reads from a write-only channel and leaks NonReadableChannelException. The same move succeeds on CPython 3.13 on Linux x86-64. Give the Java backend a way to read the source bytes for move, and translate unsupported channel access into a Python exception.

}
}

@Builtin(name = "move", minNumOfPositionalArgs = 4, parameterNames = {"$self", "dest", "src", "count"})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Make all mmap.move parameters positional-only

m.move(dest=1, src=0, count=4) changes the mapping on GraalPy, whereas CPython 3.13 raises TypeError. Set numOfPositionalOnlyArgs = 4 so GraalPy rejects the keyword call before modifying the mapping.

Comment on lines +856 to +858
if (self.isClosed()) {
throw raiseNode.raise(inliningTarget, ValueError, MMAP_CLOSED_OR_INVALID);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Check for a closed mapping before converting move arguments

PythonQuaternaryClinicBuiltinNode converts the arguments before MoveNode.move checks self.isClosed(). On a closed map, m.move(None, 0, 1) therefore raises TypeError, while CPython 3.13 raises ValueError without inspecting the arguments. Check for closure before conversion and keep the check after conversion because __index__ can run Python code and close the mapping.

Comment on lines +898 to +902
int nread = posixSupport.mmapReadBytes(handle, from, buffer, n);
if (nread != n) {
throw raiseNode.raise(inliningTarget, PythonBuiltinClassType.SystemError, MMAP_CHANGED_LENGTH);
}
posixSupport.mmapWriteBytes(handle, to, buffer, n);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Handle short transfers when moving bytes on Java-backed maps

A SeekableByteChannel can transfer fewer bytes than requested without reaching EOF or encountering an I/O failure. copyChunk treats a short read as a changed mapping length, and EmulatedPosixSupport.mmapWriteBytes treats a short write as EIO after partially updating the destination. Fill each chunk before writing it and retry short writes from the remaining buffer offset, with explicit handling for EOF and zero progress.

- Make all move() parameters positional-only.
- Check that the mapping is open before converting the arguments, and
  again afterwards since __index__ can close it (like CPython 3.13).
- Java POSIX backend: open writable mappings for reading as well, so
  that move() works on PROT_WRITE-only mappings, and translate channel
  access errors into OSError.
- Java POSIX backend: handle short reads and writes on mmap channels,
  make AnonymousMap advance its position, and raise EIO properly when a
  write makes no progress.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Mathieu Dupuy <deronnax@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mmap.mmap.move() is missing

2 participants