|
| 1 | +--- |
| 2 | +layout: docs-experimental |
| 3 | +toc_group: python |
| 4 | +link_title: Embedding Permissions |
| 5 | +permalink: /reference-manual/python/Embedding-Permissions/ |
| 6 | +redirect_from: /reference-manual/python/OSInterface/ |
| 7 | +--- |
| 8 | + |
| 9 | +# Permissions for Python Embeddings |
| 10 | + |
| 11 | +## Access Control and Security Limits for Python Scripts |
| 12 | + |
| 13 | +Embedding GraalPy into Java works with the [GraalVM Polyglot Sandbox](https://www.graalvm.org/latest/reference-manual/embed-languages/#controlling-access-to-host-functions). |
| 14 | + |
| 15 | +## Python's POSIX Interface |
| 16 | + |
| 17 | +The way the operating system interface is exposed to Python scripts is GraalPy-specific. |
| 18 | +By default all access is routed through Java interfaces, but some packages rely on details of POSIX APIs and require direct native access. |
| 19 | + |
| 20 | +Graal languages (those implemented on the [Truffle framework](https://www.graalvm.org/latest/graalvm-as-a-platform/language-implementation-framework/)) usually implement system-related functions using the [Truffle abstraction layer](https://github.com/oracle/graal/blob/master/truffle/docs/README.md), which is OS independent and provides extension points for the users when embedding GraalPy or other Graal languages into Java applications. |
| 21 | +See, for example, the [Truffle FileSystem service-provider](https://www.graalvm.org/truffle/javadoc/org/graalvm/polyglot/io/FileSystem.html). |
| 22 | + |
| 23 | +The standard Python library also provides an OS abstraction, but exposes lower level interfaces. For example, the `os` module directly exposes some POSIX functions. |
| 24 | +On non-POSIX platforms, this interface is emulated to a degree. |
| 25 | + |
| 26 | +GraalPy provides two alternative implementations (each referred to as a "backend") of system-related functionality offered by built-in Python modules such as `os`. |
| 27 | +The `PosixModuleBackend` option determines which backend is used: valid values are `native` and `java`. |
| 28 | + |
| 29 | +### Native Backend |
| 30 | + |
| 31 | +This backend directly calls the POSIX API in mostly the same way as CPython (the reference Python implementation). |
| 32 | + |
| 33 | +This approach is the most compatible with CPython and provides bare access to the underlying OS interface without an intermediate emulation layer. |
| 34 | + |
| 35 | +By default, this implementation bypasses the Truffle abstraction layer, and therefore it is not sandboxed and does not support custom implementations of [Truffle FileSystem service-provider](https://www.graalvm.org/truffle/javadoc/org/graalvm/polyglot/io/FileSystem.html), and other Polyglot API providers related to system interfaces. |
| 36 | + |
| 37 | +The native backend is chosen by default when GraalPy is started via the `graalpy` or any other Python related launcher. |
| 38 | +The exceptions are Python related launchers with `-managed` suffix available only in Oracle GraalVM (for example, `graalpy-managed`), which by default use the `java` POSIX backend. |
| 39 | + |
| 40 | +#### Limitations of the Native Backend |
| 41 | + |
| 42 | +Known limitations are: |
| 43 | + |
| 44 | +* `os.fork` is not supported |
| 45 | +* `_posixsubprocess.fork_exec` does not support the `preexec_fn` parameter |
| 46 | + |
| 47 | +### Java Backend |
| 48 | + |
| 49 | +This backend uses the [Truffle abstraction layer](https://github.com/oracle/graal/blob/master/truffle/docs/README.md) and therefore supports custom Polyglot API providers related to system interfaces and sandboxing. |
| 50 | +Because this abstraction is POSIX agnostic, it does not expose all the necessary functionality. Some functionality is emulated, and some functionality is unsupported. |
| 51 | + |
| 52 | +The Java backend is the default when GraalPy is run via the `Context` API, that is, [embedded in Java applications](https://github.com/oracle/graal/blob/master/docs/reference-manual/embedding/embed-languages.md), or when it is launched using Python-related launchers with the `-managed` suffix (available only in Oracle GraalVM). |
| 53 | + |
| 54 | +#### Limitations of the Java Backend |
| 55 | + |
| 56 | +GraalPy can log information about known incompatibility of functions executed at runtime, which includes the OS interface-related functions. |
| 57 | +To turn on this logging, use the command-line option `--log.python.compatibility.level=FINE` (or other desired logging level). |
| 58 | + |
| 59 | +Known limitations of the of the Java backend are: |
| 60 | + |
| 61 | +* Its state is disconnected from the actual OS state, which applies especially to: |
| 62 | + * *file descriptors*: Python-level file descriptors are not usable in native code. |
| 63 | + * *current working directory*: is initialized to the current working |
| 64 | + directory at the startup, but is then maintained separately. So, for example, `chdir` in Python |
| 65 | + does not change the actual current working directory of the process. |
| 66 | + * *umask*: has the same limitation as that of the current working directory, but it is always initialized |
| 67 | + to `0022` regardless of the actual system value at startup. |
| 68 | +* Resolution of file access/modification times depends on the JDK. |
| 69 | + The best the Java backend can guarantee is seconds resolution. |
| 70 | +* `os.access` and any other functionality based on `faccessat` POSIX function does not support: |
| 71 | + * effective IDs. |
| 72 | + * `follow_symlinks=False` unless the mode is only `F_OK`. |
| 73 | + |
| 74 | +## Python Native Extensions |
| 75 | + |
| 76 | +Python native extensions run by default as native binaries, with full access to the underlying system. |
0 commit comments