author | egahlin |
Tue, 13 Aug 2019 03:58:29 +0200 | |
branch | JEP-349-branch |
changeset 57717 | 4ce66d271065 |
parent 57604 | 838f9a7635b6 |
child 57985 | be121cbf3284 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
57360 | 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; |
|
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
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; |
|
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
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"); |
|
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
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 |
*/ |
|
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
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 |
||
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
173 |
public File toFile() { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
174 |
return path.toFile(); |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
175 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
176 |
|
50113 | 177 |
public String toString() { |
178 |
return text; |
|
179 |
} |
|
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
180 |
|
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
181 |
@Override |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
182 |
public int compareTo(SafePath that) { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
183 |
return that.text.compareTo(this.text); |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
184 |
} |
50113 | 185 |
} |
186 |
||
187 |
private interface RunnableWithCheckedException { |
|
188 |
public void run() throws Exception; |
|
189 |
} |
|
190 |
||
191 |
private interface CallableWithoutCheckException<T> { |
|
192 |
public T call(); |
|
193 |
} |
|
194 |
||
195 |
private static <U> U doPrivilegedIOWithReturn(Callable<U> function) throws IOException { |
|
196 |
try { |
|
197 |
return AccessController.doPrivileged(new PrivilegedExceptionAction<U>() { |
|
198 |
@Override |
|
199 |
public U run() throws Exception { |
|
200 |
return function.call(); |
|
201 |
} |
|
202 |
}, null); |
|
203 |
} catch (PrivilegedActionException e) { |
|
204 |
Throwable t = e.getCause(); |
|
205 |
if (t instanceof IOException) { |
|
206 |
throw (IOException) t; |
|
207 |
} |
|
208 |
throw new IOException("Unexpected error during I/O operation. " + t.getMessage(), t); |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
private static void doPriviligedIO(RunnableWithCheckedException function) throws IOException { |
|
213 |
doPrivilegedIOWithReturn(() -> { |
|
214 |
function.run(); |
|
215 |
return null; |
|
216 |
}); |
|
217 |
} |
|
218 |
||
219 |
private static void doPrivileged(Runnable function, Permission... perms) { |
|
220 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
221 |
@Override |
|
222 |
public Void run() { |
|
223 |
function.run(); |
|
224 |
return null; |
|
225 |
} |
|
226 |
}, null, perms); |
|
227 |
} |
|
228 |
||
229 |
private static void doPrivileged(Runnable function) { |
|
230 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
231 |
@Override |
|
232 |
public Void run() { |
|
233 |
function.run(); |
|
234 |
return null; |
|
235 |
} |
|
236 |
}); |
|
237 |
} |
|
238 |
||
239 |
private static <T> T doPrivilegedWithReturn(CallableWithoutCheckException<T> function, Permission... perms) { |
|
240 |
return AccessController.doPrivileged(new PrivilegedAction<T>() { |
|
241 |
@Override |
|
242 |
public T run() { |
|
243 |
return function.call(); |
|
244 |
} |
|
245 |
}, null, perms); |
|
246 |
} |
|
247 |
||
248 |
public static List<SafePath> getPredefinedJFCFiles() { |
|
249 |
List<SafePath> list = new ArrayList<>(); |
|
250 |
try { |
|
251 |
Iterator<Path> pathIterator = doPrivilegedIOWithReturn(() -> { |
|
252 |
return Files.newDirectoryStream(JFC_DIRECTORY.toPath(), "*").iterator(); |
|
253 |
}); |
|
254 |
while (pathIterator.hasNext()) { |
|
255 |
Path path = pathIterator.next(); |
|
256 |
if (path.toString().endsWith(".jfc")) { |
|
257 |
list.add(new SafePath(path)); |
|
258 |
} |
|
259 |
} |
|
260 |
} catch (IOException ioe) { |
|
261 |
Logger.log(LogTag.JFR, LogLevel.WARN, "Could not access .jfc-files in " + JFC_DIRECTORY + ", " + ioe.getMessage()); |
|
262 |
} |
|
263 |
return list; |
|
264 |
} |
|
265 |
||
266 |
static void makeVisibleToJFR(Class<?> clazz) { |
|
267 |
Module classModule = clazz.getModule(); |
|
268 |
Modules.addReads(JFR_MODULE, classModule); |
|
269 |
if (clazz.getPackage() != null) { |
|
270 |
String packageName = clazz.getPackage().getName(); |
|
271 |
Modules.addExports(classModule, packageName, JFR_MODULE); |
|
272 |
Modules.addOpens(classModule, packageName, JFR_MODULE); |
|
273 |
} |
|
274 |
} |
|
275 |
||
276 |
/** |
|
277 |
* Adds a qualified export of the internal.jdk.jfr.internal.handlers package |
|
278 |
* (for EventHandler) |
|
279 |
*/ |
|
280 |
static void addHandlerExport(Class<?> clazz) { |
|
281 |
Modules.addExports(JFR_MODULE, Utils.HANDLERS_PACKAGE_NAME, clazz.getModule()); |
|
282 |
} |
|
283 |
||
52698
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
284 |
static void addEventsExport(Class<?> clazz) { |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
285 |
Modules.addExports(JFR_MODULE, Utils.EVENTS_PACKAGE_NAME, clazz.getModule()); |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
286 |
} |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
287 |
|
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
288 |
static void addInstrumentExport(Class<?> clazz) { |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
289 |
Modules.addExports(JFR_MODULE, Utils.INSTRUMENT_PACKAGE_NAME, clazz.getModule()); |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
290 |
} |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
291 |
|
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
292 |
static void addReadEdge(Class<?> clazz) { |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
293 |
Modules.addReads(clazz.getModule(), JFR_MODULE); |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
294 |
} |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
295 |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
52015
diff
changeset
|
296 |
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
|
297 |
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
|
298 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
52015
diff
changeset
|
299 |
|
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
52015
diff
changeset
|
300 |
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
|
301 |
doPrivileged(() -> MetadataRepository.getInstance().registerMirror(eventClass), new FlightRecorderPermission(Utils.REGISTER_EVENT)); |
50113 | 302 |
} |
303 |
||
57360 | 304 |
public static void setProperty(String propertyName, String value) { |
305 |
doPrivileged(() -> System.setProperty(propertyName, value), new PropertyPermission(propertyName, "write")); |
|
306 |
} |
|
307 |
||
50113 | 308 |
static boolean getBooleanProperty(String propertyName) { |
309 |
return doPrivilegedWithReturn(() -> Boolean.getBoolean(propertyName), new PropertyPermission(propertyName, "read")); |
|
310 |
} |
|
311 |
||
312 |
private static SafePath getPathInProperty(String prop, String subPath) { |
|
313 |
return doPrivilegedWithReturn(() -> { |
|
314 |
String path = System.getProperty(prop); |
|
315 |
if (path == null) { |
|
316 |
return null; |
|
317 |
} |
|
318 |
File file = subPath == null ? new File(path) : new File(path, subPath); |
|
319 |
return new SafePath(file.getAbsolutePath()); |
|
320 |
}, new PropertyPermission("*", "read")); |
|
321 |
} |
|
322 |
||
323 |
// Called by JVM during initialization of JFR |
|
324 |
static Thread createRecorderThread(ThreadGroup systemThreadGroup, ClassLoader contextClassLoader) { |
|
325 |
// The thread should have permission = new Permission[0], and not "modifyThreadGroup" and "modifyThread" on the stack, |
|
326 |
// but it's hard circumvent if we are going to pass in system thread group in the constructor |
|
327 |
Thread thread = doPrivilegedWithReturn(() -> new Thread(systemThreadGroup, "JFR Recorder Thread"), new RuntimePermission("modifyThreadGroup"), new RuntimePermission("modifyThread")); |
|
328 |
doPrivileged(() -> thread.setContextClassLoader(contextClassLoader), new RuntimePermission("setContextClassLoader"), new RuntimePermission("modifyThread")); |
|
329 |
return thread; |
|
330 |
} |
|
331 |
||
332 |
static void registerShutdownHook(Thread shutdownHook) { |
|
333 |
doPrivileged(() -> Runtime.getRuntime().addShutdownHook(shutdownHook), new RuntimePermission("shutdownHooks")); |
|
334 |
} |
|
335 |
||
336 |
static void setUncaughtExceptionHandler(Thread thread, Thread.UncaughtExceptionHandler eh) { |
|
337 |
doPrivileged(() -> thread.setUncaughtExceptionHandler(eh), new RuntimePermission("modifyThread")); |
|
338 |
} |
|
339 |
||
340 |
static void moveReplace(SafePath from, SafePath to) throws IOException { |
|
341 |
doPrivilegedIOWithReturn(() -> Files.move(from.toPath(), to.toPath())); |
|
342 |
} |
|
343 |
||
344 |
static void clearDirectory(SafePath safePath) throws IOException { |
|
345 |
doPriviligedIO(() -> Files.walkFileTree(safePath.toPath(), new DirectoryCleaner())); |
|
346 |
} |
|
347 |
||
57360 | 348 |
static SafePath toRealPath(SafePath safePath) throws IOException { |
50113 | 349 |
return new SafePath(doPrivilegedIOWithReturn(() -> safePath.toPath().toRealPath())); |
350 |
} |
|
351 |
||
352 |
static boolean existDirectory(SafePath directory) throws IOException { |
|
353 |
return doPrivilegedIOWithReturn(() -> Files.exists(directory.toPath())); |
|
354 |
} |
|
355 |
||
356 |
static RandomAccessFile createRandomAccessFile(SafePath path) throws Exception { |
|
357 |
return doPrivilegedIOWithReturn(() -> new RandomAccessFile(path.toPath().toFile(), "rw")); |
|
358 |
} |
|
359 |
||
360 |
public static InputStream newFileInputStream(SafePath safePath) throws IOException { |
|
361 |
return doPrivilegedIOWithReturn(() -> Files.newInputStream(safePath.toPath())); |
|
362 |
} |
|
363 |
||
364 |
public static long getFileSize(SafePath safePath) throws IOException { |
|
365 |
return doPrivilegedIOWithReturn(() -> Files.size(safePath.toPath())); |
|
366 |
} |
|
367 |
||
368 |
static SafePath createDirectories(SafePath safePath) throws IOException { |
|
369 |
Path p = doPrivilegedIOWithReturn(() -> Files.createDirectories(safePath.toPath())); |
|
370 |
return new SafePath(p); |
|
371 |
} |
|
372 |
||
373 |
public static boolean exists(SafePath safePath) throws IOException { |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57360
diff
changeset
|
374 |
// Files.exist(path) is allocation intensive |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57360
diff
changeset
|
375 |
return doPrivilegedIOWithReturn(() -> safePath.toPath().toFile().exists()); |
50113 | 376 |
} |
377 |
||
378 |
public static boolean isDirectory(SafePath safePath) throws IOException { |
|
379 |
return doPrivilegedIOWithReturn(() -> Files.isDirectory(safePath.toPath())); |
|
380 |
} |
|
381 |
||
382 |
static void delete(SafePath localPath) throws IOException { |
|
383 |
doPriviligedIO(() -> Files.delete(localPath.toPath())); |
|
384 |
} |
|
385 |
||
386 |
static boolean isWritable(SafePath safePath) throws IOException { |
|
387 |
return doPrivilegedIOWithReturn(() -> Files.isWritable(safePath.toPath())); |
|
388 |
} |
|
389 |
||
390 |
static void deleteOnExit(SafePath safePath) { |
|
391 |
doPrivileged(() -> safePath.toPath().toFile().deleteOnExit()); |
|
392 |
} |
|
393 |
||
394 |
static ReadableByteChannel newFileChannelToRead(SafePath safePath) throws IOException { |
|
395 |
return doPrivilegedIOWithReturn(() -> FileChannel.open(safePath.toPath(), StandardOpenOption.READ)); |
|
396 |
} |
|
397 |
||
398 |
public static InputStream getResourceAsStream(String name) throws IOException { |
|
399 |
return doPrivilegedIOWithReturn(() -> SecuritySupport.class.getResourceAsStream(name)); |
|
400 |
} |
|
401 |
||
402 |
public static Reader newFileReader(SafePath safePath) throws FileNotFoundException, IOException { |
|
403 |
return doPrivilegedIOWithReturn(() -> Files.newBufferedReader(safePath.toPath())); |
|
404 |
} |
|
405 |
||
406 |
static void touch(SafePath path) throws IOException { |
|
407 |
doPriviligedIO(() -> new RandomAccessFile(path.toPath().toFile(), "rw").close()); |
|
408 |
} |
|
409 |
||
410 |
static void setAccessible(Method method) { |
|
411 |
doPrivileged(() -> method.setAccessible(true), new ReflectPermission("suppressAccessChecks")); |
|
412 |
} |
|
413 |
||
414 |
static void setAccessible(Field field) { |
|
415 |
doPrivileged(() -> field.setAccessible(true), new ReflectPermission("suppressAccessChecks")); |
|
416 |
} |
|
417 |
||
418 |
static void setAccessible(Constructor<?> constructor) { |
|
419 |
doPrivileged(() -> constructor.setAccessible(true), new ReflectPermission("suppressAccessChecks")); |
|
420 |
} |
|
421 |
||
422 |
static void ensureClassIsInitialized(Class<?> clazz) { |
|
423 |
unsafe.ensureClassInitialized(clazz); |
|
424 |
} |
|
425 |
||
52015
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
426 |
static Class<?> defineClass(Class<?> lookupClass, byte[] bytes) { |
57360 | 427 |
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
|
428 |
@Override |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
429 |
public Class<?> run() { |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
430 |
try { |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
431 |
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
|
432 |
} catch (IllegalAccessException e) { |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
433 |
throw new InternalError(e); |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
434 |
} |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
435 |
} |
821bfc24d750
8181443: Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
mchung
parents:
50113
diff
changeset
|
436 |
}); |
50113 | 437 |
} |
438 |
||
439 |
static Thread createThreadWitNoPermissions(String threadName, Runnable runnable) { |
|
440 |
return doPrivilegedWithReturn(() -> new Thread(runnable, threadName), new Permission[0]); |
|
441 |
} |
|
442 |
||
443 |
static void setDaemonThread(Thread t, boolean daeomn) { |
|
444 |
doPrivileged(()-> t.setDaemon(daeomn), new RuntimePermission("modifyThread")); |
|
445 |
} |
|
446 |
||
447 |
public static SafePath getAbsolutePath(SafePath path) throws IOException { |
|
448 |
return new SafePath(doPrivilegedIOWithReturn((()-> path.toPath().toAbsolutePath()))); |
|
449 |
} |
|
57717
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
450 |
|
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
451 |
private final static class Privileged extends FileAccess { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
452 |
@Override |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
453 |
public RandomAccessFile openRAF(File f, String mode) throws IOException { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
454 |
return doPrivilegedIOWithReturn( () -> new RandomAccessFile(f, mode)); |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
455 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
456 |
|
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
457 |
@Override |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
458 |
public DirectoryStream<Path> newDirectoryStream(Path directory) throws IOException { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
459 |
return doPrivilegedIOWithReturn( () -> Files.newDirectoryStream(directory)); |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
460 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
461 |
|
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
462 |
@Override |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
463 |
public String getAbsolutePath(File f) throws IOException { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
464 |
return doPrivilegedIOWithReturn( () ->f.getAbsolutePath()); |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
465 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
466 |
@Override |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
467 |
public long length(File f) throws IOException { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
468 |
return doPrivilegedIOWithReturn( () ->f.length()); |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
469 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
470 |
|
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
471 |
@Override |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
472 |
public long fileSize(Path p) throws IOException { |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
473 |
return doPrivilegedIOWithReturn( () ->Files.size(p)); |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
474 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
475 |
} |
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
476 |
|
4ce66d271065
Security handling, but two tests fails with driver
egahlin
parents:
57604
diff
changeset
|
477 |
|
50113 | 478 |
} |