author | mgronlun |
Wed, 30 Oct 2019 19:43:52 +0100 | |
changeset 58863 | c16ac7a2eba4 |
parent 58240 | 046533575954 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
58863 | 2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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 |
||
26 |
package jdk.jfr.internal; |
|
27 |
||
28 |
import java.io.File; |
|
29 |
import java.io.FileNotFoundException; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.InputStream; |
|
32 |
import java.io.RandomAccessFile; |
|
33 |
import java.io.Reader; |
|
52015
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
34 |
import java.lang.invoke.MethodHandles; |
50113 | 35 |
import java.lang.reflect.Constructor; |
36 |
import java.lang.reflect.Field; |
|
37 |
import java.lang.reflect.Method; |
|
38 |
import java.lang.reflect.ReflectPermission; |
|
39 |
import java.nio.channels.FileChannel; |
|
40 |
import java.nio.channels.ReadableByteChannel; |
|
58863 | 41 |
import java.nio.file.DirectoryStream; |
50113 | 42 |
import java.nio.file.FileVisitResult; |
43 |
import java.nio.file.Files; |
|
44 |
import java.nio.file.Path; |
|
45 |
import java.nio.file.Paths; |
|
46 |
import java.nio.file.SimpleFileVisitor; |
|
47 |
import java.nio.file.StandardOpenOption; |
|
48 |
import java.nio.file.attribute.BasicFileAttributes; |
|
49 |
import java.security.AccessControlContext; |
|
50 |
import java.security.AccessController; |
|
51 |
import java.security.Permission; |
|
52 |
import java.security.PrivilegedAction; |
|
53 |
import java.security.PrivilegedActionException; |
|
54 |
import java.security.PrivilegedExceptionAction; |
|
55 |
import java.util.ArrayList; |
|
56 |
import java.util.Iterator; |
|
57 |
import java.util.List; |
|
58 |
import java.util.Objects; |
|
59 |
import java.util.PropertyPermission; |
|
60 |
import java.util.concurrent.Callable; |
|
61 |
||
62 |
import jdk.internal.misc.Unsafe; |
|
63 |
import jdk.internal.module.Modules; |
|
64 |
import jdk.jfr.Event; |
|
65 |
import jdk.jfr.FlightRecorder; |
|
66 |
import jdk.jfr.FlightRecorderListener; |
|
67 |
import jdk.jfr.FlightRecorderPermission; |
|
68 |
import jdk.jfr.Recording; |
|
58863 | 69 |
import jdk.jfr.internal.consumer.FileAccess; |
50113 | 70 |
|
71 |
/** |
|
72 |
* Contains JFR code that does |
|
73 |
* {@link AccessController#doPrivileged(PrivilegedAction)} |
|
74 |
*/ |
|
75 |
public final class SecuritySupport { |
|
76 |
private final static Unsafe unsafe = Unsafe.getUnsafe(); |
|
52015
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
77 |
private final static MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); |
50113 | 78 |
private final static Module JFR_MODULE = Event.class.getModule(); |
79 |
public final static SafePath JFC_DIRECTORY = getPathInProperty("java.home", "lib/jfr"); |
|
58863 | 80 |
public final static FileAccess PRIVILIGED = new Privileged(); |
50113 | 81 |
static final SafePath USER_HOME = getPathInProperty("user.home", null); |
82 |
static final SafePath JAVA_IO_TMPDIR = getPathInProperty("java.io.tmpdir", null); |
|
83 |
||
52698
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
84 |
static { |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
85 |
// ensure module java.base can read module jdk.jfr as early as possible |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
86 |
addReadEdge(Object.class); |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
87 |
addHandlerExport(Object.class); |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
88 |
addEventsExport(Object.class); |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
89 |
addInstrumentExport(Object.class); |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
90 |
} |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
91 |
|
50113 | 92 |
final static class SecureRecorderListener implements FlightRecorderListener { |
93 |
||
94 |
private final AccessControlContext context; |
|
95 |
private final FlightRecorderListener changeListener; |
|
96 |
||
97 |
SecureRecorderListener(AccessControlContext context, FlightRecorderListener changeListener) { |
|
98 |
this.context = Objects.requireNonNull(context); |
|
99 |
this.changeListener = Objects.requireNonNull(changeListener); |
|
100 |
} |
|
101 |
||
102 |
@Override |
|
103 |
public void recordingStateChanged(Recording recording) { |
|
104 |
AccessController.doPrivileged((PrivilegedAction<Void>) () -> { |
|
105 |
try { |
|
106 |
changeListener.recordingStateChanged(recording); |
|
107 |
} catch (Throwable t) { |
|
108 |
// Prevent malicious user to propagate exception callback in the wrong context |
|
109 |
Logger.log(LogTag.JFR, LogLevel.WARN, "Unexpected exception in listener " + changeListener.getClass()+ " at recording state change"); |
|
110 |
} |
|
111 |
return null; |
|
112 |
}, context); |
|
113 |
} |
|
114 |
||
115 |
@Override |
|
116 |
public void recorderInitialized(FlightRecorder recorder) { |
|
117 |
AccessController.doPrivileged((PrivilegedAction<Void>) () -> { |
|
118 |
try { |
|
119 |
changeListener.recorderInitialized(recorder); |
|
120 |
} catch (Throwable t) { |
|
121 |
// Prevent malicious user to propagate exception callback in the wrong context |
|
122 |
Logger.log(LogTag.JFR, LogLevel.WARN, "Unexpected exception in listener " + changeListener.getClass()+ " when initializing FlightRecorder"); |
|
123 |
} |
|
124 |
return null; |
|
125 |
}, context); |
|
126 |
} |
|
127 |
||
128 |
public FlightRecorderListener getChangeListener() { |
|
129 |
return changeListener; |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
private static final class DirectoryCleaner extends SimpleFileVisitor<Path> { |
|
134 |
@Override |
|
135 |
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { |
|
136 |
Files.delete(path); |
|
137 |
return FileVisitResult.CONTINUE; |
|
138 |
} |
|
139 |
||
140 |
@Override |
|
141 |
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
|
142 |
if (exc != null) { |
|
143 |
throw exc; |
|
144 |
} |
|
145 |
Files.delete(dir); |
|
146 |
return FileVisitResult.CONTINUE; |
|
147 |
} |
|
148 |
} |
|
149 |
||
150 |
/** |
|
151 |
* Path created by the default file provider,and not |
|
152 |
* a malicious provider. |
|
153 |
* |
|
154 |
*/ |
|
58863 | 155 |
public static final class SafePath implements Comparable<SafePath> { |
50113 | 156 |
private final Path path; |
157 |
private final String text; |
|
158 |
||
159 |
public SafePath(Path p) { |
|
160 |
// sanitize |
|
161 |
text = p.toString(); |
|
162 |
path = Paths.get(text); |
|
163 |
} |
|
164 |
||
165 |
public SafePath(String path) { |
|
166 |
this(Paths.get(path)); |
|
167 |
} |
|
168 |
||
169 |
public Path toPath() { |
|
170 |
return path; |
|
171 |
} |
|
172 |
||
58863 | 173 |
public File toFile() { |
174 |
return path.toFile(); |
|
175 |
} |
|
176 |
||
50113 | 177 |
public String toString() { |
178 |
return text; |
|
179 |
} |
|
58240
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
52698
diff
changeset
|
180 |
|
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
52698
diff
changeset
|
181 |
@Override |
58863 | 182 |
public int compareTo(SafePath that) { |
183 |
return that.text.compareTo(this.text); |
|
184 |
} |
|
185 |
||
186 |
@Override |
|
58240
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
52698
diff
changeset
|
187 |
public boolean equals(Object other) { |
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
52698
diff
changeset
|
188 |
if(other != null && other instanceof SafePath){ |
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
52698
diff
changeset
|
189 |
return this.toPath().equals(((SafePath) other).toPath()); |
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
52698
diff
changeset
|
190 |
} |
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
52698
diff
changeset
|
191 |
return false; |
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
52698
diff
changeset
|
192 |
} |
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
52698
diff
changeset
|
193 |
|
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
52698
diff
changeset
|
194 |
@Override |
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
52698
diff
changeset
|
195 |
public int hashCode() { |
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
52698
diff
changeset
|
196 |
return this.toPath().hashCode(); |
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
52698
diff
changeset
|
197 |
} |
50113 | 198 |
} |
199 |
||
200 |
private interface RunnableWithCheckedException { |
|
201 |
public void run() throws Exception; |
|
202 |
} |
|
203 |
||
204 |
private interface CallableWithoutCheckException<T> { |
|
205 |
public T call(); |
|
206 |
} |
|
207 |
||
208 |
private static <U> U doPrivilegedIOWithReturn(Callable<U> function) throws IOException { |
|
209 |
try { |
|
210 |
return AccessController.doPrivileged(new PrivilegedExceptionAction<U>() { |
|
211 |
@Override |
|
212 |
public U run() throws Exception { |
|
213 |
return function.call(); |
|
214 |
} |
|
215 |
}, null); |
|
216 |
} catch (PrivilegedActionException e) { |
|
217 |
Throwable t = e.getCause(); |
|
218 |
if (t instanceof IOException) { |
|
219 |
throw (IOException) t; |
|
220 |
} |
|
221 |
throw new IOException("Unexpected error during I/O operation. " + t.getMessage(), t); |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
private static void doPriviligedIO(RunnableWithCheckedException function) throws IOException { |
|
226 |
doPrivilegedIOWithReturn(() -> { |
|
227 |
function.run(); |
|
228 |
return null; |
|
229 |
}); |
|
230 |
} |
|
231 |
||
232 |
private static void doPrivileged(Runnable function, Permission... perms) { |
|
233 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
234 |
@Override |
|
235 |
public Void run() { |
|
236 |
function.run(); |
|
237 |
return null; |
|
238 |
} |
|
239 |
}, null, perms); |
|
240 |
} |
|
241 |
||
242 |
private static void doPrivileged(Runnable function) { |
|
243 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
244 |
@Override |
|
245 |
public Void run() { |
|
246 |
function.run(); |
|
247 |
return null; |
|
248 |
} |
|
249 |
}); |
|
250 |
} |
|
251 |
||
252 |
private static <T> T doPrivilegedWithReturn(CallableWithoutCheckException<T> function, Permission... perms) { |
|
253 |
return AccessController.doPrivileged(new PrivilegedAction<T>() { |
|
254 |
@Override |
|
255 |
public T run() { |
|
256 |
return function.call(); |
|
257 |
} |
|
258 |
}, null, perms); |
|
259 |
} |
|
260 |
||
261 |
public static List<SafePath> getPredefinedJFCFiles() { |
|
262 |
List<SafePath> list = new ArrayList<>(); |
|
263 |
try { |
|
264 |
Iterator<Path> pathIterator = doPrivilegedIOWithReturn(() -> { |
|
265 |
return Files.newDirectoryStream(JFC_DIRECTORY.toPath(), "*").iterator(); |
|
266 |
}); |
|
267 |
while (pathIterator.hasNext()) { |
|
268 |
Path path = pathIterator.next(); |
|
269 |
if (path.toString().endsWith(".jfc")) { |
|
270 |
list.add(new SafePath(path)); |
|
271 |
} |
|
272 |
} |
|
273 |
} catch (IOException ioe) { |
|
274 |
Logger.log(LogTag.JFR, LogLevel.WARN, "Could not access .jfc-files in " + JFC_DIRECTORY + ", " + ioe.getMessage()); |
|
275 |
} |
|
276 |
return list; |
|
277 |
} |
|
278 |
||
279 |
static void makeVisibleToJFR(Class<?> clazz) { |
|
280 |
Module classModule = clazz.getModule(); |
|
281 |
Modules.addReads(JFR_MODULE, classModule); |
|
282 |
if (clazz.getPackage() != null) { |
|
283 |
String packageName = clazz.getPackage().getName(); |
|
284 |
Modules.addExports(classModule, packageName, JFR_MODULE); |
|
285 |
Modules.addOpens(classModule, packageName, JFR_MODULE); |
|
286 |
} |
|
287 |
} |
|
288 |
||
289 |
/** |
|
290 |
* Adds a qualified export of the internal.jdk.jfr.internal.handlers package |
|
291 |
* (for EventHandler) |
|
292 |
*/ |
|
293 |
static void addHandlerExport(Class<?> clazz) { |
|
294 |
Modules.addExports(JFR_MODULE, Utils.HANDLERS_PACKAGE_NAME, clazz.getModule()); |
|
295 |
} |
|
296 |
||
52698
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
297 |
static void addEventsExport(Class<?> clazz) { |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
298 |
Modules.addExports(JFR_MODULE, Utils.EVENTS_PACKAGE_NAME, clazz.getModule()); |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
299 |
} |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
300 |
|
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
301 |
static void addInstrumentExport(Class<?> clazz) { |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
302 |
Modules.addExports(JFR_MODULE, Utils.INSTRUMENT_PACKAGE_NAME, clazz.getModule()); |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
303 |
} |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
304 |
|
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
305 |
static void addReadEdge(Class<?> clazz) { |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
306 |
Modules.addReads(clazz.getModule(), JFR_MODULE); |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
307 |
} |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
308 |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
52015
diff
changeset
|
309 |
public static void registerEvent(Class<? extends jdk.internal.event.Event> eventClass) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
52015
diff
changeset
|
310 |
doPrivileged(() -> MetadataRepository.getInstance().register(eventClass), new FlightRecorderPermission(Utils.REGISTER_EVENT)); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
52015
diff
changeset
|
311 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
52015
diff
changeset
|
312 |
|
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
52015
diff
changeset
|
313 |
public static void registerMirror(Class<? extends Event> eventClass) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
52015
diff
changeset
|
314 |
doPrivileged(() -> MetadataRepository.getInstance().registerMirror(eventClass), new FlightRecorderPermission(Utils.REGISTER_EVENT)); |
50113 | 315 |
} |
316 |
||
58863 | 317 |
public static void setProperty(String propertyName, String value) { |
318 |
doPrivileged(() -> System.setProperty(propertyName, value), new PropertyPermission(propertyName, "write")); |
|
319 |
} |
|
320 |
||
50113 | 321 |
static boolean getBooleanProperty(String propertyName) { |
322 |
return doPrivilegedWithReturn(() -> Boolean.getBoolean(propertyName), new PropertyPermission(propertyName, "read")); |
|
323 |
} |
|
324 |
||
325 |
private static SafePath getPathInProperty(String prop, String subPath) { |
|
326 |
return doPrivilegedWithReturn(() -> { |
|
327 |
String path = System.getProperty(prop); |
|
328 |
if (path == null) { |
|
329 |
return null; |
|
330 |
} |
|
331 |
File file = subPath == null ? new File(path) : new File(path, subPath); |
|
332 |
return new SafePath(file.getAbsolutePath()); |
|
333 |
}, new PropertyPermission("*", "read")); |
|
334 |
} |
|
335 |
||
336 |
// Called by JVM during initialization of JFR |
|
337 |
static Thread createRecorderThread(ThreadGroup systemThreadGroup, ClassLoader contextClassLoader) { |
|
338 |
// The thread should have permission = new Permission[0], and not "modifyThreadGroup" and "modifyThread" on the stack, |
|
339 |
// but it's hard circumvent if we are going to pass in system thread group in the constructor |
|
340 |
Thread thread = doPrivilegedWithReturn(() -> new Thread(systemThreadGroup, "JFR Recorder Thread"), new RuntimePermission("modifyThreadGroup"), new RuntimePermission("modifyThread")); |
|
341 |
doPrivileged(() -> thread.setContextClassLoader(contextClassLoader), new RuntimePermission("setContextClassLoader"), new RuntimePermission("modifyThread")); |
|
342 |
return thread; |
|
343 |
} |
|
344 |
||
345 |
static void registerShutdownHook(Thread shutdownHook) { |
|
346 |
doPrivileged(() -> Runtime.getRuntime().addShutdownHook(shutdownHook), new RuntimePermission("shutdownHooks")); |
|
347 |
} |
|
348 |
||
349 |
static void setUncaughtExceptionHandler(Thread thread, Thread.UncaughtExceptionHandler eh) { |
|
350 |
doPrivileged(() -> thread.setUncaughtExceptionHandler(eh), new RuntimePermission("modifyThread")); |
|
351 |
} |
|
352 |
||
353 |
static void moveReplace(SafePath from, SafePath to) throws IOException { |
|
354 |
doPrivilegedIOWithReturn(() -> Files.move(from.toPath(), to.toPath())); |
|
355 |
} |
|
356 |
||
357 |
static void clearDirectory(SafePath safePath) throws IOException { |
|
358 |
doPriviligedIO(() -> Files.walkFileTree(safePath.toPath(), new DirectoryCleaner())); |
|
359 |
} |
|
360 |
||
58863 | 361 |
static SafePath toRealPath(SafePath safePath) throws IOException { |
50113 | 362 |
return new SafePath(doPrivilegedIOWithReturn(() -> safePath.toPath().toRealPath())); |
363 |
} |
|
364 |
||
365 |
static boolean existDirectory(SafePath directory) throws IOException { |
|
366 |
return doPrivilegedIOWithReturn(() -> Files.exists(directory.toPath())); |
|
367 |
} |
|
368 |
||
369 |
static RandomAccessFile createRandomAccessFile(SafePath path) throws Exception { |
|
370 |
return doPrivilegedIOWithReturn(() -> new RandomAccessFile(path.toPath().toFile(), "rw")); |
|
371 |
} |
|
372 |
||
373 |
public static InputStream newFileInputStream(SafePath safePath) throws IOException { |
|
374 |
return doPrivilegedIOWithReturn(() -> Files.newInputStream(safePath.toPath())); |
|
375 |
} |
|
376 |
||
377 |
public static long getFileSize(SafePath safePath) throws IOException { |
|
378 |
return doPrivilegedIOWithReturn(() -> Files.size(safePath.toPath())); |
|
379 |
} |
|
380 |
||
381 |
static SafePath createDirectories(SafePath safePath) throws IOException { |
|
382 |
Path p = doPrivilegedIOWithReturn(() -> Files.createDirectories(safePath.toPath())); |
|
383 |
return new SafePath(p); |
|
384 |
} |
|
385 |
||
386 |
public static boolean exists(SafePath safePath) throws IOException { |
|
58863 | 387 |
// Files.exist(path) is allocation intensive |
388 |
return doPrivilegedIOWithReturn(() -> safePath.toPath().toFile().exists()); |
|
50113 | 389 |
} |
390 |
||
391 |
public static boolean isDirectory(SafePath safePath) throws IOException { |
|
392 |
return doPrivilegedIOWithReturn(() -> Files.isDirectory(safePath.toPath())); |
|
393 |
} |
|
394 |
||
395 |
static void delete(SafePath localPath) throws IOException { |
|
396 |
doPriviligedIO(() -> Files.delete(localPath.toPath())); |
|
397 |
} |
|
398 |
||
399 |
static boolean isWritable(SafePath safePath) throws IOException { |
|
400 |
return doPrivilegedIOWithReturn(() -> Files.isWritable(safePath.toPath())); |
|
401 |
} |
|
402 |
||
403 |
static void deleteOnExit(SafePath safePath) { |
|
404 |
doPrivileged(() -> safePath.toPath().toFile().deleteOnExit()); |
|
405 |
} |
|
406 |
||
407 |
static ReadableByteChannel newFileChannelToRead(SafePath safePath) throws IOException { |
|
408 |
return doPrivilegedIOWithReturn(() -> FileChannel.open(safePath.toPath(), StandardOpenOption.READ)); |
|
409 |
} |
|
410 |
||
411 |
public static InputStream getResourceAsStream(String name) throws IOException { |
|
412 |
return doPrivilegedIOWithReturn(() -> SecuritySupport.class.getResourceAsStream(name)); |
|
413 |
} |
|
414 |
||
415 |
public static Reader newFileReader(SafePath safePath) throws FileNotFoundException, IOException { |
|
416 |
return doPrivilegedIOWithReturn(() -> Files.newBufferedReader(safePath.toPath())); |
|
417 |
} |
|
418 |
||
419 |
static void touch(SafePath path) throws IOException { |
|
420 |
doPriviligedIO(() -> new RandomAccessFile(path.toPath().toFile(), "rw").close()); |
|
421 |
} |
|
422 |
||
423 |
static void setAccessible(Method method) { |
|
424 |
doPrivileged(() -> method.setAccessible(true), new ReflectPermission("suppressAccessChecks")); |
|
425 |
} |
|
426 |
||
427 |
static void setAccessible(Field field) { |
|
428 |
doPrivileged(() -> field.setAccessible(true), new ReflectPermission("suppressAccessChecks")); |
|
429 |
} |
|
430 |
||
431 |
static void setAccessible(Constructor<?> constructor) { |
|
432 |
doPrivileged(() -> constructor.setAccessible(true), new ReflectPermission("suppressAccessChecks")); |
|
433 |
} |
|
434 |
||
435 |
static void ensureClassIsInitialized(Class<?> clazz) { |
|
436 |
unsafe.ensureClassInitialized(clazz); |
|
437 |
} |
|
438 |
||
52015
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
439 |
static Class<?> defineClass(Class<?> lookupClass, byte[] bytes) { |
58863 | 440 |
return AccessController.doPrivileged(new PrivilegedAction<Class<?>>() { |
52015
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
441 |
@Override |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
442 |
public Class<?> run() { |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
443 |
try { |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
444 |
return MethodHandles.privateLookupIn(lookupClass, LOOKUP).defineClass(bytes); |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
445 |
} catch (IllegalAccessException e) { |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
446 |
throw new InternalError(e); |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
447 |
} |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
448 |
} |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
449 |
}); |
50113 | 450 |
} |
451 |
||
58863 | 452 |
public static Thread createThreadWitNoPermissions(String threadName, Runnable runnable) { |
50113 | 453 |
return doPrivilegedWithReturn(() -> new Thread(runnable, threadName), new Permission[0]); |
454 |
} |
|
455 |
||
456 |
static void setDaemonThread(Thread t, boolean daeomn) { |
|
457 |
doPrivileged(()-> t.setDaemon(daeomn), new RuntimePermission("modifyThread")); |
|
458 |
} |
|
459 |
||
460 |
public static SafePath getAbsolutePath(SafePath path) throws IOException { |
|
461 |
return new SafePath(doPrivilegedIOWithReturn((()-> path.toPath().toAbsolutePath()))); |
|
462 |
} |
|
58863 | 463 |
|
464 |
private final static class Privileged extends FileAccess { |
|
465 |
@Override |
|
466 |
public RandomAccessFile openRAF(File f, String mode) throws IOException { |
|
467 |
return doPrivilegedIOWithReturn( () -> new RandomAccessFile(f, mode)); |
|
468 |
} |
|
469 |
||
470 |
@Override |
|
471 |
public DirectoryStream<Path> newDirectoryStream(Path directory) throws IOException { |
|
472 |
return doPrivilegedIOWithReturn( () -> Files.newDirectoryStream(directory)); |
|
473 |
} |
|
474 |
||
475 |
@Override |
|
476 |
public String getAbsolutePath(File f) throws IOException { |
|
477 |
return doPrivilegedIOWithReturn( () -> f.getAbsolutePath()); |
|
478 |
} |
|
479 |
@Override |
|
480 |
public long length(File f) throws IOException { |
|
481 |
return doPrivilegedIOWithReturn( () -> f.length()); |
|
482 |
} |
|
483 |
||
484 |
@Override |
|
485 |
public long fileSize(Path p) throws IOException { |
|
486 |
return doPrivilegedIOWithReturn( () -> Files.size(p)); |
|
487 |
} |
|
488 |
} |
|
489 |
||
490 |
||
50113 | 491 |
} |