2
2
3
3
package org.utbot.instrumentation.process
4
4
5
+ import org.utbot.common.withAccessibility
5
6
import sun.security.provider.PolicyFile
7
+ import java.lang.reflect.AccessibleObject
6
8
import java.net.URI
7
9
import java.nio.file.Files
8
10
import java.nio.file.Paths
@@ -28,6 +30,15 @@ internal fun permissions(block: SimplePolicy.() -> Unit) {
28
30
}
29
31
}
30
32
33
+ /* *
34
+ * Make this [AccessibleObject] accessible and run a block inside sandbox.
35
+ */
36
+ fun <O : AccessibleObject , R > O.runSandbox (block : O .() -> R ): R {
37
+ return withAccessibility {
38
+ sandbox { block() }
39
+ }
40
+ }
41
+
31
42
/* *
32
43
* Run [block] in sandbox mode.
33
44
*
@@ -45,12 +56,12 @@ internal fun permissions(block: SimplePolicy.() -> Unit) {
45
56
* ```
46
57
* Read more [about policy file and syntax](https://docs.oracle.com/javase/7/docs/technotes/guides/security/PolicyFiles.html#Examples)
47
58
*/
48
- internal fun <T > sandbox (block : () -> T ): T {
59
+ fun <T > sandbox (block : () -> T ): T {
49
60
val policyPath = Paths .get(System .getProperty(" user.home" ), " .utbot" , " sandbox.policy" )
50
61
return sandbox(policyPath.toUri()) { block() }
51
62
}
52
63
53
- internal fun <T > sandbox (file : URI , block : () -> T ): T {
64
+ fun <T > sandbox (file : URI , block : () -> T ): T {
54
65
val path = Paths .get(file)
55
66
val perms = mutableListOf<Permission >(
56
67
RuntimePermission (" accessDeclaredMembers" )
@@ -64,12 +75,12 @@ internal fun <T> sandbox(file: URI, block: () -> T): T {
64
75
return sandbox(perms, allCodeSource) { block() }
65
76
}
66
77
67
- internal fun <T > sandbox (permission : List <Permission >, cs : CodeSource , block : () -> T ): T {
78
+ fun <T > sandbox (permission : List <Permission >, cs : CodeSource , block : () -> T ): T {
68
79
val perms = permission.fold(Permissions ()) { acc, p -> acc.add(p); acc }
69
80
return sandbox(perms, cs) { block() }
70
81
}
71
82
72
- internal fun <T > sandbox (perms : PermissionCollection , cs : CodeSource , block : () -> T ): T {
83
+ fun <T > sandbox (perms : PermissionCollection , cs : CodeSource , block : () -> T ): T {
73
84
val acc = AccessControlContext (arrayOf(ProtectionDomain (cs, perms)))
74
85
return try {
75
86
AccessController .doPrivileged(PrivilegedAction { block() }, acc)
0 commit comments