Skip to content

Commit 7c4362e

Browse files
committed
[GR-29135] Use platform specific values of native POSIX constants
PullRequest: graalpython/1674
2 parents e84b253 + 6772b86 commit 7c4362e

File tree

22 files changed

+1193
-363
lines changed

22 files changed

+1193
-363
lines changed

graalpython/com.oracle.graal.python.cext/posix/posix.c

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <dirent.h>
5252
#include <errno.h>
5353
#include <fcntl.h>
54+
#include <signal.h>
5455
#include <stdio.h>
5556
#include <stdint.h>
5657
#include <stdlib.h>
@@ -66,11 +67,6 @@
6667
#include <unistd.h>
6768

6869

69-
// TODO remove this once we properly synchronize constants between Java and C
70-
static int fixDirFd(int dirFd) {
71-
return dirFd == -100 ? AT_FDCWD : dirFd;
72-
}
73-
7470
int64_t call_getpid() {
7571
return getpid();
7672
}
@@ -105,33 +101,7 @@ int32_t set_inheritable(int32_t fd, int32_t inheritable) {
105101
}
106102

107103
int32_t call_openat(int32_t dirFd, const char *pathname, int32_t flags, int32_t mode) {
108-
int fixedFlags = flags;
109-
// TODO remove this once we properly synchronize constants between Java and C
110-
111-
#define WRONG_O_CLOEXEC 524288
112-
#define WRONG_O_APPEND 1024
113-
#define WRONG_O_TRUNC 512
114-
#define WRONG_O_EXCL 128
115-
#define WRONG_O_CREAT 64
116-
117-
fixedFlags &= ~(WRONG_O_CLOEXEC | WRONG_O_APPEND | WRONG_O_TRUNC | WRONG_O_EXCL | WRONG_O_CREAT);
118-
119-
if (flags & WRONG_O_CREAT) {
120-
fixedFlags |= O_CREAT;
121-
}
122-
if (flags & WRONG_O_EXCL) {
123-
fixedFlags |= O_EXCL;
124-
}
125-
if (flags & WRONG_O_TRUNC) {
126-
fixedFlags |= O_TRUNC;
127-
}
128-
if (flags & WRONG_O_APPEND) {
129-
fixedFlags |= O_APPEND;
130-
}
131-
if (flags & WRONG_O_CLOEXEC) {
132-
fixedFlags |= O_CLOEXEC;
133-
}
134-
return openat(fixDirFd(dirFd), pathname, fixedFlags, mode);
104+
return openat(dirFd, pathname, flags, mode);
135105
}
136106

137107
int32_t call_close(int32_t fd) {
@@ -300,7 +270,7 @@ static void stat_struct_to_longs(struct stat *st, int64_t *out) {
300270

301271
int32_t call_fstatat(int32_t dirFd, const char *path, int32_t followSymlinks, int64_t *out) {
302272
struct stat st;
303-
int result = fstatat(fixDirFd(dirFd), path, &st, followSymlinks ? 0 : AT_SYMLINK_NOFOLLOW);
273+
int result = fstatat(dirFd, path, &st, followSymlinks ? 0 : AT_SYMLINK_NOFOLLOW);
304274
if (result == 0) {
305275
stat_struct_to_longs(&st, out);
306276
}
@@ -330,15 +300,15 @@ int32_t call_uname(char *sysname, char *nodename, char *release, char *version,
330300
}
331301

332302
int32_t call_unlinkat(int32_t dirFd, const char *pathname, int32_t rmdir) {
333-
return unlinkat(fixDirFd(dirFd), pathname, rmdir ? AT_REMOVEDIR : 0);
303+
return unlinkat(dirFd, pathname, rmdir ? AT_REMOVEDIR : 0);
334304
}
335305

336306
int32_t call_symlinkat(const char *target, int32_t dirFd, const char *linkpath) {
337-
return symlinkat(target, fixDirFd(dirFd), linkpath);
307+
return symlinkat(target, dirFd, linkpath);
338308
}
339309

340310
int32_t call_mkdirat(int32_t dirFd, const char *pathname, int32_t mode) {
341-
return mkdirat(fixDirFd(dirFd), pathname, mode);
311+
return mkdirat(dirFd, pathname, mode);
342312
}
343313

344314
int32_t call_getcwd(char *buf, uint64_t size) {
@@ -394,14 +364,14 @@ int32_t call_readdir(intptr_t dirp, char *nameBuf, uint64_t nameBufSize, int64_t
394364

395365
int32_t call_utimensat(int32_t dirFd, const char *path, int64_t *timespec, int32_t followSymlinks) {
396366
if (!timespec) {
397-
return utimensat(fixDirFd(dirFd), path, NULL, followSymlinks ? 0 : AT_SYMLINK_NOFOLLOW);
367+
return utimensat(dirFd, path, NULL, followSymlinks ? 0 : AT_SYMLINK_NOFOLLOW);
398368
} else {
399369
struct timespec times[2];
400370
times[0].tv_sec = timespec[0];
401371
times[0].tv_nsec = timespec[1];
402372
times[1].tv_sec = timespec[2];
403373
times[1].tv_nsec = timespec[3];
404-
return utimensat(fixDirFd(dirFd), path, times, followSymlinks ? 0 : AT_SYMLINK_NOFOLLOW);
374+
return utimensat(dirFd, path, times, followSymlinks ? 0 : AT_SYMLINK_NOFOLLOW);
405375
}
406376
}
407377

@@ -419,7 +389,7 @@ int32_t call_futimens(int32_t fd, int64_t *timespec) {
419389
}
420390

421391
int32_t call_renameat(int32_t oldDirFd, const char *oldPath, int32_t newDirFd, const char *newPath) {
422-
return renameat(fixDirFd(oldDirFd), oldPath, fixDirFd(newDirFd), newPath);
392+
return renameat(oldDirFd, oldPath, newDirFd, newPath);
423393
}
424394

425395
int32_t call_faccessat(int32_t dirFd, const char *path, int32_t mode, int32_t effectiveIds, int32_t followSymlinks) {
@@ -430,19 +400,19 @@ int32_t call_faccessat(int32_t dirFd, const char *path, int32_t mode, int32_t ef
430400
if (effectiveIds) {
431401
flags |= AT_EACCESS;
432402
}
433-
return faccessat(fixDirFd(dirFd), path, mode, flags);
403+
return faccessat(dirFd, path, mode, flags);
434404
}
435405

436406
int32_t call_fchmodat(int32_t dirFd, const char *path, int32_t mode, int32_t followSymlinks) {
437-
return fchmodat(fixDirFd(dirFd), path, mode, followSymlinks ? 0 : AT_SYMLINK_NOFOLLOW);
407+
return fchmodat(dirFd, path, mode, followSymlinks ? 0 : AT_SYMLINK_NOFOLLOW);
438408
}
439409

440410
int32_t call_fchmod(int32_t fd, int32_t mode) {
441411
return fchmod(fd, mode);
442412
}
443413

444414
int64_t call_readlinkat(int32_t dirFd, const char *path, char *buf, uint64_t size) {
445-
return readlinkat(fixDirFd(dirFd), path, buf, size);
415+
return readlinkat(dirFd, path, buf, size);
446416
}
447417

448418
int64_t call_waitpid(int64_t pid, int32_t *status, int32_t options) {

graalpython/com.oracle.graal.python.cext/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ def build_libposix(capi_home):
443443
files = [os.path.abspath(os.path.join(src_dir, f)) for f in os.listdir(src_dir) if f.endswith(".c")]
444444
module = Extension(libposix_name,
445445
sources=files,
446-
extra_compile_args=cflags_warnings)
446+
extra_compile_args=cflags_warnings + ['-Wall', '-Werror'])
447447
args = [verbosity, 'build', 'install_lib', '-f', '--install-dir=%s' % capi_home, "clean"]
448448
setup(
449449
script_name='setup' + libposix_name,

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/builtins/modules/DirFdConversionNodeTests.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,25 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43+
import static com.oracle.graal.python.PythonLanguage.getContext;
44+
import static com.oracle.graal.python.runtime.PosixConstants.AT_FDCWD;
45+
46+
import java.math.BigInteger;
47+
48+
import org.graalvm.polyglot.Context;
49+
import org.hamcrest.CoreMatchers;
50+
import org.junit.After;
51+
import org.junit.Assert;
52+
import org.junit.Before;
53+
import org.junit.Rule;
54+
import org.junit.Test;
55+
import org.junit.rules.ExpectedException;
56+
4357
import com.oracle.graal.python.builtins.modules.PosixModuleBuiltins.DirFdConversionNode;
4458
import com.oracle.graal.python.builtins.objects.PNone;
4559
import com.oracle.graal.python.builtins.objects.frame.PFrame;
4660
import com.oracle.graal.python.builtins.objects.function.PArguments;
4761
import com.oracle.graal.python.runtime.ExecutionContext;
48-
import com.oracle.graal.python.runtime.PosixSupportLibrary;
4962
import com.oracle.graal.python.runtime.PythonContext;
5063
import com.oracle.graal.python.runtime.exception.PException;
5164
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
@@ -54,18 +67,6 @@
5467
import com.oracle.truffle.api.Truffle;
5568
import com.oracle.truffle.api.frame.VirtualFrame;
5669
import com.oracle.truffle.api.nodes.RootNode;
57-
import org.graalvm.polyglot.Context;
58-
import org.hamcrest.CoreMatchers;
59-
import org.junit.After;
60-
import org.junit.Assert;
61-
import org.junit.Before;
62-
import org.junit.Rule;
63-
import org.junit.Test;
64-
import org.junit.rules.ExpectedException;
65-
66-
import java.math.BigInteger;
67-
68-
import static com.oracle.graal.python.PythonLanguage.getContext;
6970

7071
public class DirFdConversionNodeTests {
7172
@Rule public ExpectedException expectedException = ExpectedException.none();
@@ -82,8 +83,8 @@ public void tearDown() {
8283

8384
@Test
8485
public void none() {
85-
Assert.assertEquals(PosixSupportLibrary.DEFAULT_DIR_FD, call(PNone.NONE));
86-
Assert.assertEquals(PosixSupportLibrary.DEFAULT_DIR_FD, call(PNone.NO_VALUE));
86+
Assert.assertEquals(AT_FDCWD.value, call(PNone.NONE));
87+
Assert.assertEquals(AT_FDCWD.value, call(PNone.NO_VALUE));
8788
}
8889

8990
@Test

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/builtins/modules/FileDescriptorConversionNodeTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
package com.oracle.graal.python.builtins.modules;
4242

4343
import static com.oracle.graal.python.PythonLanguage.getContext;
44+
import static com.oracle.graal.python.runtime.PosixConstants.AT_FDCWD;
4445

4546
import java.math.BigInteger;
4647

@@ -58,7 +59,6 @@
5859
import com.oracle.graal.python.builtins.objects.frame.PFrame;
5960
import com.oracle.graal.python.builtins.objects.function.PArguments;
6061
import com.oracle.graal.python.runtime.ExecutionContext;
61-
import com.oracle.graal.python.runtime.PosixSupportLibrary;
6262
import com.oracle.graal.python.runtime.PythonContext;
6363
import com.oracle.graal.python.runtime.exception.PException;
6464
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
@@ -86,7 +86,7 @@ public void none() {
8686
expectedException.expect(PException.class);
8787
expectedException.expectMessage("TypeError: argument must be an int, or have a fileno() method.");
8888
call(PNone.NONE);
89-
Assert.assertEquals(PosixSupportLibrary.DEFAULT_DIR_FD, call(PNone.NO_VALUE));
89+
Assert.assertEquals(AT_FDCWD.value, call(PNone.NO_VALUE));
9090
}
9191

9292
@Test

graalpython/com.oracle.graal.python.test/src/tests/test_posix.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ def open(name, flags):
8484

8585
class PosixTests(unittest.TestCase):
8686

87+
def test_platform_constants(self):
88+
if sys.platform == 'darwin':
89+
self.assertEqual(8, posix.O_APPEND)
90+
else:
91+
self.assertEqual(0x400, posix.O_APPEND)
92+
8793
def test_uname(self):
8894
# just like cpython, a simple smoke test
8995
uname = posix.uname()

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/FcntlModuleBuiltins.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5454
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryClinicBuiltinNode;
5555
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
56+
import com.oracle.graal.python.runtime.PosixConstants;
57+
import com.oracle.graal.python.runtime.PosixConstants.IntConstant;
5658
import com.oracle.graal.python.runtime.PosixSupportLibrary;
5759
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
5860
import com.oracle.graal.python.runtime.PythonCore;
@@ -73,12 +75,16 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
7375

7476
@Override
7577
public void initialize(PythonCore core) {
76-
builtinConstants.put("LOCK_SH", PosixSupportLibrary.LOCK_SH);
77-
builtinConstants.put("LOCK_EX", PosixSupportLibrary.LOCK_EX);
78-
builtinConstants.put("LOCK_NB", PosixSupportLibrary.LOCK_NB);
79-
builtinConstants.put("LOCK_UN", PosixSupportLibrary.LOCK_UN);
80-
81-
builtinConstants.put("F_WRLCK", 42); // TODO
78+
for (IntConstant c : PosixConstants.flockOperation) {
79+
if (c.defined) {
80+
builtinConstants.put(c.name, c.getValueIfDefined());
81+
}
82+
}
83+
for (IntConstant c : PosixConstants.flockType) {
84+
if (c.defined) {
85+
builtinConstants.put(c.name, c.getValueIfDefined());
86+
}
87+
}
8288
super.initialize(core);
8389
}
8490

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MMapModuleBuiltins.java

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242

4343
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.OverflowError;
4444
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError;
45-
import static com.oracle.graal.python.runtime.PosixSupportLibrary.MAP_ANONYMOUS;
46-
import static com.oracle.graal.python.runtime.PosixSupportLibrary.MAP_PRIVATE;
47-
import static com.oracle.graal.python.runtime.PosixSupportLibrary.MAP_SHARED;
48-
import static com.oracle.graal.python.runtime.PosixSupportLibrary.PROT_READ;
49-
import static com.oracle.graal.python.runtime.PosixSupportLibrary.PROT_WRITE;
45+
import static com.oracle.graal.python.runtime.PosixConstants.MAP_ANONYMOUS;
46+
import static com.oracle.graal.python.runtime.PosixConstants.MAP_PRIVATE;
47+
import static com.oracle.graal.python.runtime.PosixConstants.MAP_SHARED;
48+
import static com.oracle.graal.python.runtime.PosixConstants.PROT_READ;
49+
import static com.oracle.graal.python.runtime.PosixConstants.PROT_WRITE;
5050
import static com.oracle.graal.python.runtime.PosixSupportLibrary.ST_SIZE;
5151

5252
import java.util.List;
@@ -63,6 +63,7 @@
6363
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6464
import com.oracle.graal.python.nodes.function.builtins.PythonClinicBuiltinNode;
6565
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
66+
import com.oracle.graal.python.runtime.PosixConstants;
6667
import com.oracle.graal.python.runtime.PosixSupportLibrary;
6768
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
6869
import com.oracle.truffle.api.dsl.Cached;
@@ -85,6 +86,18 @@ public MMapModuleBuiltins() {
8586
builtinConstants.put("ACCESS_READ", PMMap.ACCESS_READ);
8687
builtinConstants.put("ACCESS_WRITE", PMMap.ACCESS_WRITE);
8788
builtinConstants.put("ACCESS_COPY", PMMap.ACCESS_COPY);
89+
90+
for (PosixConstants.IntConstant c : PosixConstants.mmapFlags) {
91+
if (c.defined) {
92+
builtinConstants.put(c.name, c.getValueIfDefined());
93+
}
94+
}
95+
96+
for (PosixConstants.IntConstant c : PosixConstants.mmapProtection) {
97+
if (c.defined) {
98+
builtinConstants.put(c.name, c.getValueIfDefined());
99+
}
100+
}
88101
}
89102

90103
@Builtin(name = "mmap", minNumOfPositionalArgs = 3, parameterNames = {"cls", "fd", "length", "flags", "prot", "access", "offset"}, constructsClass = PythonBuiltinClassType.PMMap)
@@ -98,8 +111,8 @@ public MMapModuleBuiltins() {
98111
@ArgumentClinic(name = "offset", conversion = ClinicConversion.Long, defaultValue = "0")
99112
public abstract static class MMapNode extends PythonClinicBuiltinNode {
100113
protected static final int ACCESS_ARG_DEFAULT = PMMap.ACCESS_DEFAULT;
101-
protected static final int FLAGS_DEFAULT = MAP_SHARED;
102-
protected static final int PROT_DEFAULT = PROT_WRITE | PROT_READ;
114+
protected static final int FLAGS_DEFAULT = MAP_SHARED.value;
115+
protected static final int PROT_DEFAULT = PROT_WRITE.value | PROT_READ.value;
103116

104117
private static final int ANONYMOUS_FD = -1;
105118

@@ -120,22 +133,22 @@ PMMap doFile(VirtualFrame frame, Object clazz, int fd, long lengthIn, int flagsI
120133
int access = accessIn;
121134
switch (access) {
122135
case PMMap.ACCESS_READ:
123-
flags = MAP_SHARED;
124-
prot = PROT_READ;
136+
flags = MAP_SHARED.value;
137+
prot = PROT_READ.value;
125138
break;
126139
case PMMap.ACCESS_WRITE:
127-
flags = MAP_SHARED;
128-
prot = PROT_READ | PROT_WRITE;
140+
flags = MAP_SHARED.value;
141+
prot = PROT_READ.value | PROT_WRITE.value;
129142
break;
130143
case PMMap.ACCESS_COPY:
131-
flags = MAP_PRIVATE;
132-
prot = PROT_READ | PROT_WRITE;
144+
flags = MAP_PRIVATE.value;
145+
prot = PROT_READ.value | PROT_WRITE.value;
133146
break;
134147
case PMMap.ACCESS_DEFAULT:
135148
// map prot to access type
136-
if (((prot & PROT_READ) != 0) && ((prot & PROT_WRITE) != 0)) {
149+
if (((prot & PROT_READ.value) != 0) && ((prot & PROT_WRITE.value) != 0)) {
137150
// ACCESS_DEFAULT
138-
} else if ((prot & PROT_WRITE) != 0) {
151+
} else if ((prot & PROT_WRITE.value) != 0) {
139152
access = PMMap.ACCESS_WRITE;
140153
} else {
141154
access = PMMap.ACCESS_READ;
@@ -174,7 +187,7 @@ PMMap doFile(VirtualFrame frame, Object clazz, int fd, long lengthIn, int flagsI
174187
int dupFd;
175188
if (fd == ANONYMOUS_FD) {
176189
dupFd = ANONYMOUS_FD;
177-
flags |= MAP_ANONYMOUS;
190+
flags |= MAP_ANONYMOUS.value;
178191
// TODO: CPython uses mapping to "/dev/zero" on systems that do not support
179192
// MAP_ANONYMOUS, maybe this can be detected and handled by the POSIX layer
180193
} else {

0 commit comments

Comments
 (0)