Skip to content

Commit

Permalink
Remove references to SecurityManager and PrivilegedActions
Browse files Browse the repository at this point in the history
- Fixes smallrye#396
  • Loading branch information
gastaldi committed Feb 18, 2025
1 parent 87d84bd commit 23fb096
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 265 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.smallrye.common.classloader;

import java.lang.invoke.MethodHandles;
import java.security.AccessController;
import java.security.PrivilegedAction;

/**
* A utility to define classes within a target lookup.
Expand All @@ -22,21 +20,11 @@ private ClassDefiner() {
* @return the defined class (not {@code null})
*/
public static Class<?> defineClass(MethodHandles.Lookup lookup, Class<?> parent, String className, byte[] classBytes) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(DefineClassPermission.getInstance());
try {
MethodHandles.Lookup privateLookupIn = MethodHandles.privateLookupIn(parent, lookup);
return privateLookupIn.defineClass(classBytes);
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.getMessage());
}

return AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
@Override
public Class<?> run() {
try {
MethodHandles.Lookup privateLookupIn = MethodHandles.privateLookupIn(parent, lookup);
return privateLookupIn.defineClass(classBytes);
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.getMessage());
}
}
});
}
}

This file was deleted.

146 changes: 64 additions & 82 deletions cpu/src/main/java/io/smallrye/common/cpu/CacheInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Locale;

Expand Down Expand Up @@ -81,93 +79,77 @@ public static int getSmallestInstructionCacheLineSize() {
}

static {
cacheLevels = AccessController.doPrivileged(new PrivilegedAction<>() {
@Override
public CacheLevelInfo[] run() {
if (determineCacheLevel()) {
try {
String osArch = System.getProperty("os.name", "unknown").toLowerCase(Locale.US);
if (osArch.contains("linux")) {
// try to read /sys fs
final File cpu0 = new File("/sys/devices/system/cpu/cpu0/cache");
if (cpu0.exists()) {
// great!
final File[] files = cpu0.listFiles();
if (files != null) {
ArrayList<File> indexes = new ArrayList<File>();
for (File file : files) {
if (file.getName().startsWith("index")) {
indexes.add(file);
}
}
final CacheLevelInfo[] levelInfoArray = new CacheLevelInfo[indexes.size()];
for (int i = 0; i < indexes.size(); i++) {
File file = indexes.get(i);
int index = parseIntFile(new File(file, "level"));
final CacheType type;
switch (parseStringFile(new File(file, "type"))) {
case "Data":
type = CacheType.DATA;
break;
case "Instruction":
type = CacheType.INSTRUCTION;
break;
case "Unified":
type = CacheType.UNIFIED;
break;
default:
type = CacheType.UNKNOWN;
break;
}
int size = parseIntKBFile(new File(file, "size"));
int lineSize = parseIntFile(new File(file, "coherency_line_size"));
levelInfoArray[i] = new CacheLevelInfo(index, type, size, lineSize);
}
return levelInfoArray;
CacheLevelInfo[] cacheLevelInfos;
if (Boolean.getBoolean("smallrye.cpu.determine-cache-level")) {
try {
String osArch = System.getProperty("os.name", "unknown").toLowerCase(Locale.US);
if (osArch.contains("linux")) {
// try to read /sys fs
final File cpu0 = new File("/sys/devices/system/cpu/cpu0/cache");
if (cpu0.exists()) {
// great!
final File[] files = cpu0.listFiles();
if (files != null) {
ArrayList<File> indexes = new ArrayList<File>();
for (File file : files) {
if (file.getName().startsWith("index")) {
indexes.add(file);
}
}
} else if (osArch.contains("mac os x")) {
// cache line size
final int lineSize = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.cachelinesize"));
if (lineSize != 0) {
// cache sizes
final int l1d = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l1dcachesize"));
final int l1i = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l1icachesize"));
final int l2 = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l2cachesize"));
final int l3 = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l3cachesize"));
ArrayList<CacheLevelInfo> list = new ArrayList<CacheLevelInfo>();
if (l1d != 0) {
list.add(new CacheLevelInfo(1, CacheType.DATA, l1d / 1024, lineSize));
}
if (l1i != 0) {
list.add(new CacheLevelInfo(1, CacheType.INSTRUCTION, l1i / 1024, lineSize));
}
if (l2 != 0) {
list.add(new CacheLevelInfo(2, CacheType.UNIFIED, l2 / 1024, lineSize));
}
if (l3 != 0) {
list.add(new CacheLevelInfo(3, CacheType.UNIFIED, l3 / 1024, lineSize));
}
if (list.size() > 0) {
return list.toArray(new CacheLevelInfo[list.size()]);
}
final CacheLevelInfo[] levelInfoArray = new CacheLevelInfo[indexes.size()];
for (int i = 0; i < indexes.size(); i++) {
File file = indexes.get(i);
int index = parseIntFile(new File(file, "level"));
final CacheType type = switch (parseStringFile(new File(file, "type"))) {
case "Data" -> CacheType.DATA;
case "Instruction" -> CacheType.INSTRUCTION;
case "Unified" -> CacheType.UNIFIED;
default -> CacheType.UNKNOWN;
};
int size = parseIntKBFile(new File(file, "size"));
int lineSize = parseIntFile(new File(file, "coherency_line_size"));
levelInfoArray[i] = new CacheLevelInfo(index, type, size, lineSize);
}
} else if (osArch.contains("windows")) {
// TODO: use the wmic utility to get cache line info
cacheLevelInfos = levelInfoArray;
}
} catch (Throwable ignored) {
}
// all has failed
return new CacheLevelInfo[0];
} else {
return new CacheLevelInfo[] { new CacheLevelInfo(0, CacheType.UNKNOWN, 0, 64) };
} else if (osArch.contains("mac os x")) {
// cache line size
final int lineSize = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.cachelinesize"));
if (lineSize != 0) {
// cache sizes
final int l1d = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l1dcachesize"));
final int l1i = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l1icachesize"));
final int l2 = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l2cachesize"));
final int l3 = safeParseInt(parseProcessOutput("/usr/sbin/sysctl", "-n", "hw.l3cachesize"));
ArrayList<CacheLevelInfo> list = new ArrayList<CacheLevelInfo>();
if (l1d != 0) {
list.add(new CacheLevelInfo(1, CacheType.DATA, l1d / 1024, lineSize));
}
if (l1i != 0) {
list.add(new CacheLevelInfo(1, CacheType.INSTRUCTION, l1i / 1024, lineSize));
}
if (l2 != 0) {
list.add(new CacheLevelInfo(2, CacheType.UNIFIED, l2 / 1024, lineSize));
}
if (l3 != 0) {
list.add(new CacheLevelInfo(3, CacheType.UNIFIED, l3 / 1024, lineSize));
}
if (!list.isEmpty()) {
cacheLevelInfos = list.toArray(new CacheLevelInfo[0]);
}
}
} else if (osArch.contains("windows")) {
// TODO: use the wmic utility to get cache line info
}
} catch (Throwable ignored) {
}
});
}

private static boolean determineCacheLevel() {
return Boolean.parseBoolean(System.getProperty("smallrye.cpu.determine-cache-level", "false"));
// all has failed
cacheLevelInfos = new CacheLevelInfo[0];
} else {
cacheLevelInfos = new CacheLevelInfo[] { new CacheLevelInfo(0, CacheType.UNKNOWN, 0, 64) };
}
cacheLevels = cacheLevelInfos;
}

static int parseIntFile(final File file) {
Expand Down
60 changes: 0 additions & 60 deletions net/src/main/java/io/smallrye/common/net/GetHostInfoAction.java

This file was deleted.

55 changes: 53 additions & 2 deletions net/src/main/java/io/smallrye/common/net/HostName.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.AccessController;
import java.util.regex.Pattern;

import io.smallrye.common.constraint.Assert;

Expand All @@ -19,7 +19,7 @@ public final class HostName {
private static volatile String nodeName;

static {
String[] names = AccessController.doPrivileged(new GetHostInfoAction());
String[] names = resolveHosts();
hostName = names[0];
qualifiedHostName = names[1];
nodeName = names[2];
Expand Down Expand Up @@ -90,4 +90,55 @@ public static void setNodeName(final String nodeName) {
Assert.checkNotNullParam("nodeName", nodeName);
HostName.nodeName = nodeName;
}

private static String[] resolveHosts() {
// allow host name to be overridden
String qualifiedHostName = System.getProperty("jboss.qualified.host.name");
String providedHostName = System.getProperty("jboss.host.name");
String providedNodeName = System.getProperty("jboss.node.name");
if (qualifiedHostName == null) {
// if host name is specified, don't pick a qualified host name that isn't related to it
qualifiedHostName = providedHostName;
if (qualifiedHostName == null) {
// POSIX-like OSes including Mac should have this set
qualifiedHostName = System.getenv("HOSTNAME");
}
if (qualifiedHostName == null) {
// Certain versions of Windows
qualifiedHostName = System.getenv("COMPUTERNAME");
}
if (qualifiedHostName == null) {
try {
qualifiedHostName = HostName.getLocalHost().getHostName();
} catch (UnknownHostException e) {
qualifiedHostName = null;
}
}
if (qualifiedHostName != null
&& Pattern.compile("^\\d+\\.\\d+\\.\\d+\\.\\d+$|:").matcher(qualifiedHostName).find()) {
// IP address is not acceptable
qualifiedHostName = null;
}
if (qualifiedHostName == null) {
// Give up
qualifiedHostName = "unknown-host.unknown-domain";
} else {
qualifiedHostName = qualifiedHostName.trim().toLowerCase();
}
}
if (providedHostName == null) {
// Use the host part of the qualified host name
final int idx = qualifiedHostName.indexOf('.');
providedHostName = idx == -1 ? qualifiedHostName : qualifiedHostName.substring(0, idx);
}
if (providedNodeName == null) {
providedNodeName = providedHostName;
}
return new String[] {
providedHostName,
qualifiedHostName,
providedNodeName
};
}

}
Loading

0 comments on commit 23fb096

Please sign in to comment.