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