Skip to content

Commit 4358898

Browse files
[GR-42846] [GR-42847] [GR-44577] [GR-41613] Integrate various fixes.
PullRequest: graal/14437
2 parents b01b14f + 6a74d77 commit 4358898

File tree

18 files changed

+729
-200
lines changed

18 files changed

+729
-200
lines changed

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/classfile/constantpool/ClassConstant.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public Resolved resolve(RuntimeConstantPool pool, int thisIndex, Klass accessing
124124
EspressoContext context = pool.getContext();
125125
Symbol<Symbol.Type> type = context.getTypes().fromName(klassName);
126126
Klass klass = context.getMeta().resolveSymbolOrFail(type, accessingKlass.getDefiningClassLoader(), accessingKlass.protectionDomain());
127-
if (!Klass.checkAccess(klass.getElementalType(), accessingKlass)) {
127+
if (!Klass.checkAccess(klass.getElementalType(), accessingKlass, false)) {
128128
Meta meta = context.getMeta();
129129
context.getLogger().log(Level.FINE,
130130
"Access check of: " + klass.getType() + " from " + accessingKlass.getType() + " throws IllegalAccessError");
@@ -201,7 +201,7 @@ public Resolved resolve(RuntimeConstantPool pool, int thisIndex, Klass accessing
201201
EspressoContext context = pool.getContext();
202202
Meta meta = context.getMeta();
203203
Klass klass = meta.resolveSymbolOrFail(context.getTypes().fromName(klassName), accessingKlass.getDefiningClassLoader(), accessingKlass.protectionDomain());
204-
if (!Klass.checkAccess(klass.getElementalType(), accessingKlass)) {
204+
if (!Klass.checkAccess(klass.getElementalType(), accessingKlass, false)) {
205205
context.getLogger().log(Level.FINE,
206206
"Access check of: " + klass.getType() + " from " + accessingKlass.getType() + " throws IllegalAccessError");
207207
throw meta.throwExceptionWithMessage(meta.java_lang_IllegalAccessError, meta.toGuestString(klassName));

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/impl/ClassRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ private ObjectKlass createKlass(EspressoContext context, ParserKlass parserKlass
448448
}
449449

450450
if (superKlass != null) {
451-
if (!Klass.checkAccess(superKlass, klass)) {
451+
if (!Klass.checkAccess(superKlass, klass, true)) {
452452
throw EspressoClassLoadingException.illegalAccessError("class " + type + " cannot access its superclass " + superKlassType);
453453
}
454454
if (!superKlass.permittedSubclassCheck(klass)) {
@@ -458,7 +458,7 @@ private ObjectKlass createKlass(EspressoContext context, ParserKlass parserKlass
458458

459459
for (ObjectKlass interf : superInterfaces) {
460460
if (interf != null) {
461-
if (!Klass.checkAccess(interf, klass)) {
461+
if (!Klass.checkAccess(interf, klass, true)) {
462462
throw EspressoClassLoadingException.illegalAccessError("class " + type + " cannot access its superinterface " + interf.getType());
463463
}
464464
if (!interf.permittedSubclassCheck(klass)) {

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/impl/Klass.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ protected static boolean hasFinalInstanceField(Class<?> clazz) {
636636
* <li>C is not public, and C and D are members of the same run-time package.
637637
* </ul>
638638
*/
639-
public static boolean checkAccess(Klass klass, Klass accessingKlass) {
639+
public static boolean checkAccess(Klass klass, Klass accessingKlass, boolean ignoreMagicAccessor) {
640640
if (accessingKlass == null) {
641641
return true;
642642
}
@@ -661,8 +661,23 @@ public static boolean checkAccess(Klass klass, Klass accessingKlass) {
661661
return true;
662662
}
663663
}
664-
return (context.getMeta().sun_reflect_MagicAccessorImpl.isAssignableFrom(accessingKlass));
665664

665+
if (ignoreMagicAccessor) {
666+
/*
667+
* Prevents any class inheriting from MagicAccessorImpl to have access to
668+
* MagicAccessorImpl just because it implements MagicAccessorImpl.
669+
*
670+
* Only generated accessors in the {sun|jdk.internal}.reflect package, defined by
671+
* {sun|jdk.internal}.reflect.DelegatingClassLoader(s) have access to MagicAccessorImpl.
672+
*/
673+
ObjectKlass magicAccessorImpl = context.getMeta().sun_reflect_MagicAccessorImpl;
674+
return !StaticObject.isNull(accessingKlass.getDefiningClassLoader()) &&
675+
context.getMeta().sun_reflect_DelegatingClassLoader.equals(accessingKlass.getDefiningClassLoader().getKlass()) &&
676+
magicAccessorImpl.getRuntimePackage().equals(accessingKlass.getRuntimePackage()) &&
677+
magicAccessorImpl.isAssignableFrom(accessingKlass);
678+
}
679+
680+
return (context.getMeta().sun_reflect_MagicAccessorImpl.isAssignableFrom(accessingKlass));
666681
}
667682

668683
public static boolean doModuleAccessChecks(Klass klass, Klass accessingKlass, EspressoContext context) {

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/meta/Meta.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2009,7 +2009,7 @@ public Klass resolveSymbolOrFail(Symbol<Type> type, @JavaType(ClassLoader.class)
20092009
public Klass resolveSymbolAndAccessCheck(Symbol<Type> type, Klass accessingKlass) {
20102010
assert accessingKlass != null;
20112011
Klass klass = resolveSymbolOrFail(type, accessingKlass.getDefiningClassLoader(), java_lang_NoClassDefFoundError, accessingKlass.protectionDomain());
2012-
if (!Klass.checkAccess(klass.getElementalType(), accessingKlass)) {
2012+
if (!Klass.checkAccess(klass.getElementalType(), accessingKlass, false)) {
20132013
throw throwException(java_lang_IllegalAccessError);
20142014
}
20152015
return klass;

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixStat.java

Lines changed: 97 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,22 @@
2424
*/
2525
package com.oracle.svm.core.posix;
2626

27+
import static org.graalvm.nativeimage.c.function.CFunction.Transition.NO_TRANSITION;
28+
29+
import org.graalvm.compiler.api.replacements.Fold;
2730
import org.graalvm.nativeimage.Platform;
2831
import org.graalvm.nativeimage.Platforms;
2932
import org.graalvm.nativeimage.c.CContext;
3033
import org.graalvm.nativeimage.c.constant.CConstant;
34+
import org.graalvm.nativeimage.c.function.CFunction;
35+
import org.graalvm.nativeimage.c.struct.SizeOf;
36+
import org.graalvm.nativeimage.c.type.CCharPointer;
37+
import org.graalvm.nativeimage.c.type.CConst;
38+
import org.graalvm.word.UnsignedWord;
39+
import org.graalvm.word.WordBase;
3140

3241
import com.oracle.svm.core.Uninterruptible;
3342
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
34-
import com.oracle.svm.core.headers.LibC;
35-
import com.oracle.svm.core.posix.headers.Errno;
3643
import com.oracle.svm.core.posix.headers.PosixDirectives;
3744
import com.oracle.svm.core.posix.headers.darwin.DarwinStat;
3845
import com.oracle.svm.core.posix.headers.linux.LinuxStat;
@@ -74,21 +81,6 @@ public final class PosixStat {
7481
@CConstant
7582
public static native int S_IXOTH();
7683

77-
public static boolean isOpen(int fd) {
78-
int result;
79-
if (Platform.includedIn(Platform.LINUX.class)) {
80-
LinuxStat.stat64 stat = UnsafeStackValue.get(LinuxStat.stat64.class);
81-
result = LinuxStat.fstat64(fd, stat);
82-
} else if (Platform.includedIn(Platform.DARWIN.class)) {
83-
DarwinStat.stat stat = UnsafeStackValue.get(DarwinStat.stat.class);
84-
result = DarwinStat.fstat(fd, stat);
85-
} else {
86-
throw VMError.shouldNotReachHere("Unsupported platform");
87-
}
88-
89-
return result == 0 || LibC.errno() != Errno.EBADF();
90-
}
91-
9284
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
9385
public static long getSize(int fd) {
9486
long size = -1;
@@ -102,11 +94,99 @@ public static long getSize(int fd) {
10294
if (DarwinStat.NoTransitions.fstat(fd, stat) == 0) {
10395
size = stat.st_size();
10496
}
97+
} else {
98+
throw VMError.shouldNotReachHere("Unsupported platform");
10599
}
106100
return size;
107101
}
108102

103+
@Fold
104+
public static int sizeOfStatStruct() {
105+
if (Platform.includedIn(Platform.LINUX.class)) {
106+
return SizeOf.get(LinuxStat.stat64.class);
107+
} else if (Platform.includedIn(Platform.DARWIN.class)) {
108+
return SizeOf.get(DarwinStat.stat.class);
109+
} else {
110+
throw VMError.shouldNotReachHere("Unsupported platform");
111+
}
112+
}
113+
114+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
115+
public static int st_uid(stat buf) {
116+
if (Platform.includedIn(Platform.LINUX.class)) {
117+
return ((LinuxStat.stat64) buf).st_uid();
118+
} else if (Platform.includedIn(Platform.DARWIN.class)) {
119+
return ((DarwinStat.stat) buf).st_uid();
120+
} else {
121+
throw VMError.shouldNotReachHere("Unsupported platform");
122+
}
123+
}
124+
125+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
126+
public static boolean S_ISLNK(stat buf) {
127+
return st_mode(buf).and(S_IFLNK()).equal(S_IFLNK());
128+
}
129+
130+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
131+
public static boolean S_ISDIR(stat buf) {
132+
return st_mode(buf).and(S_IFDIR()).equal(S_IFDIR());
133+
}
134+
135+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
136+
public static UnsignedWord st_mode(stat buf) {
137+
if (Platform.includedIn(Platform.LINUX.class)) {
138+
return ((LinuxStat.stat64) buf).st_mode();
139+
} else if (Platform.includedIn(Platform.DARWIN.class)) {
140+
return ((DarwinStat.stat) buf).st_mode();
141+
} else {
142+
throw VMError.shouldNotReachHere("Unsupported platform");
143+
}
144+
}
145+
146+
public static UnsignedWord st_nlink(stat buf) {
147+
if (Platform.includedIn(Platform.LINUX.class)) {
148+
return ((LinuxStat.stat64) buf).st_nlink();
149+
} else if (Platform.includedIn(Platform.DARWIN.class)) {
150+
return ((DarwinStat.stat) buf).st_nlink();
151+
} else {
152+
throw VMError.shouldNotReachHere("Unsupported platform");
153+
}
154+
}
155+
156+
/**
157+
* Pointer to the OS-specific stat struct.
158+
*/
159+
public interface stat extends WordBase {
160+
}
161+
109162
@Platforms(Platform.HOSTED_ONLY.class)
110163
private PosixStat() {
111164
}
165+
166+
public static class NoTransitions {
167+
@CFunction(transition = NO_TRANSITION)
168+
public static native int mkdir(@CConst CCharPointer pathname, int mode);
169+
170+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
171+
public static int fstat(int fd, stat buf) {
172+
if (Platform.includedIn(Platform.LINUX.class)) {
173+
return LinuxStat.NoTransitions.fstat64(fd, (LinuxStat.stat64) buf);
174+
} else if (Platform.includedIn(Platform.DARWIN.class)) {
175+
return DarwinStat.NoTransitions.fstat(fd, (DarwinStat.stat) buf);
176+
} else {
177+
throw VMError.shouldNotReachHere("Unsupported platform");
178+
}
179+
}
180+
181+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
182+
public static int lstat(CCharPointer path, stat buf) {
183+
if (Platform.includedIn(Platform.LINUX.class)) {
184+
return LinuxStat.NoTransitions.lstat64(path, (LinuxStat.stat64) buf);
185+
} else if (Platform.includedIn(Platform.DARWIN.class)) {
186+
return DarwinStat.NoTransitions.lstat(path, (DarwinStat.stat) buf);
187+
} else {
188+
throw VMError.shouldNotReachHere("Unsupported platform");
189+
}
190+
}
191+
}
112192
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.posix.headers;
26+
27+
import org.graalvm.nativeimage.c.CContext;
28+
import org.graalvm.nativeimage.c.function.CFunction;
29+
import org.graalvm.nativeimage.c.struct.CFieldAddress;
30+
import org.graalvm.nativeimage.c.struct.CStruct;
31+
import org.graalvm.nativeimage.c.type.CCharPointer;
32+
import org.graalvm.word.PointerBase;
33+
34+
// Checkstyle: stop
35+
36+
/**
37+
* Definitions manually translated from the C header file dirent.h.
38+
*/
39+
@CContext(PosixDirectives.class)
40+
public class Dirent {
41+
@CFunction
42+
public static native DIR fdopendir(int fd);
43+
44+
@CFunction
45+
public static native dirent readdir(DIR dir);
46+
47+
@CFunction
48+
public static native int closedir(DIR dir);
49+
50+
public interface DIR extends PointerBase {
51+
}
52+
53+
@CStruct(addStructKeyword = true)
54+
public interface dirent extends PointerBase {
55+
@CFieldAddress
56+
CCharPointer d_name();
57+
}
58+
}

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/Fcntl.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
*/
2525
package com.oracle.svm.core.posix.headers;
2626

27+
import static org.graalvm.nativeimage.c.function.CFunction.Transition.NO_TRANSITION;
28+
2729
import org.graalvm.nativeimage.c.CContext;
2830
import org.graalvm.nativeimage.c.constant.CConstant;
2931
import org.graalvm.nativeimage.c.function.CFunction;
30-
import org.graalvm.nativeimage.c.function.CFunction.Transition;
3132
import org.graalvm.nativeimage.c.type.CCharPointer;
33+
import org.graalvm.nativeimage.c.type.CConst;
3234

3335
// Checkstyle: stop
3436

@@ -41,6 +43,9 @@ public class Fcntl {
4143
@CConstant
4244
public static native int O_RDONLY();
4345

46+
@CConstant
47+
public static native int O_NOFOLLOW();
48+
4449
@CConstant
4550
public static native int O_RDWR();
4651

@@ -57,7 +62,17 @@ public class Fcntl {
5762
public static native int O_EXCL();
5863

5964
public static class NoTransitions {
60-
@CFunction(value = "openSII", transition = Transition.NO_TRANSITION)
65+
@CFunction(value = "openSII", transition = NO_TRANSITION)
6166
public static native int open(CCharPointer pathname, int flags, int mode);
67+
68+
@CFunction(transition = NO_TRANSITION)
69+
public static native int openat(int dirfd, @CConst CCharPointer pathname, int flags, int mode);
70+
71+
@CFunction(transition = NO_TRANSITION)
72+
public static native int unlink(@CConst CCharPointer pathname);
73+
74+
@CFunction(transition = NO_TRANSITION)
75+
public static native int unlinkat(int dirfd, CCharPointer pathname, int flags);
76+
6277
}
6378
}

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/PosixDirectives.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,21 @@
3737
public class PosixDirectives implements CContext.Directives {
3838
private static final String[] commonLibs = new String[]{
3939
"<dlfcn.h>",
40+
"<dirent.h>",
4041
"<fcntl.h>",
4142
"<limits.h>",
4243
"<locale.h>",
4344
"<pthread.h>",
44-
"<semaphore.h>",
4545
"<pwd.h>",
46+
"<semaphore.h>",
4647
"<signal.h>",
4748
"<errno.h>",
4849
"<sys/mman.h>",
4950
"<sys/resource.h>",
5051
"<sys/stat.h>",
5152
"<sys/time.h>",
5253
"<sys/times.h>",
54+
"<sys/types.h>",
5355
"<sys/utsname.h>",
5456
"<time.h>",
5557
"<unistd.h>",

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/Pwd.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
import org.graalvm.nativeimage.c.CContext;
2828
import org.graalvm.nativeimage.c.function.CFunction;
2929
import org.graalvm.nativeimage.c.struct.CField;
30+
import org.graalvm.nativeimage.c.struct.CPointerTo;
3031
import org.graalvm.nativeimage.c.struct.CStruct;
3132
import org.graalvm.nativeimage.c.type.CCharPointer;
33+
import org.graalvm.word.Pointer;
3234
import org.graalvm.word.PointerBase;
35+
import org.graalvm.word.UnsignedWord;
3336

3437
// Checkstyle: stop
3538

@@ -48,6 +51,14 @@ public interface passwd extends PointerBase {
4851
CCharPointer pw_dir();
4952
}
5053

54+
@CPointerTo(passwd.class)
55+
public interface passwdPointer extends Pointer {
56+
passwd read();
57+
}
58+
5159
@CFunction
5260
public static native passwd getpwuid(int __uid);
61+
62+
@CFunction
63+
public static native int getpwuid_r(int __uid, passwd pwd, CCharPointer buf, UnsignedWord buflen, passwdPointer result);
5364
}

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/Signal.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
package com.oracle.svm.core.posix.headers;
2626

27+
import static org.graalvm.nativeimage.c.function.CFunction.Transition.NO_TRANSITION;
28+
2729
import org.graalvm.nativeimage.Platform;
2830
import org.graalvm.nativeimage.Platforms;
2931
import org.graalvm.nativeimage.c.CContext;
@@ -446,4 +448,8 @@ public interface AArch64DarwinMContext64 extends PointerBase {
446448
long pc();
447449
}
448450

451+
public static class NoTransitions {
452+
@CFunction(transition = NO_TRANSITION)
453+
public static native int kill(int pid, int sig);
454+
}
449455
}

0 commit comments

Comments
 (0)