Initial commit
This commit is contained in:
commit
dfaa9e36c9
14 changed files with 1192 additions and 0 deletions
42
.gitignore
vendored
Normal file
42
.gitignore
vendored
Normal file
|
@ -0,0 +1,42 @@
|
|||
.gradle
|
||||
build/
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea/modules.xml
|
||||
.idea/jarRepositories.xml
|
||||
.idea/compiler.xml
|
||||
.idea/libraries/
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
out/
|
||||
!**/src/main/**/out/
|
||||
!**/src/test/**/out/
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
bin/
|
||||
!**/src/main/**/bin/
|
||||
!**/src/test/**/bin/
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
58
build.gradle
Normal file
58
build.gradle
Normal file
|
@ -0,0 +1,58 @@
|
|||
plugins {
|
||||
id 'java-library'
|
||||
id 'maven-publish'
|
||||
}
|
||||
|
||||
group = 'dev.pfaff'
|
||||
version = '1.0-SNAPSHOT'
|
||||
|
||||
apply from: "${project.gradle.gradleUserHomeDir}/local.gradle"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation platform('org.junit:junit-bom:5.9.1')
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
def targetJavaVersion = 19
|
||||
tasks.withType(JavaCompile).configureEach {
|
||||
it.options.release.set(targetJavaVersion)
|
||||
}
|
||||
|
||||
java {
|
||||
toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
|
||||
archivesBaseName = project.properties.name
|
||||
withSourcesJar()
|
||||
withJavadocJar()
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
unfettered(MavenPublication) {
|
||||
from components.java
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
maven {
|
||||
name = "Gitea"
|
||||
url = uri("https://git.pfaff.dev/api/packages/michael/maven")
|
||||
|
||||
credentials(HttpHeaderCredentials) {
|
||||
name = "Authorization"
|
||||
value = "token ${project.ext.giteaAccessToken}"
|
||||
}
|
||||
|
||||
authentication {
|
||||
header(HttpHeaderAuthentication)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
6
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
6
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
#Thu Oct 19 10:21:15 EDT 2023
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
234
gradlew
vendored
Executable file
234
gradlew
vendored
Executable file
|
@ -0,0 +1,234 @@
|
|||
#!/bin/sh
|
||||
|
||||
#
|
||||
# Copyright © 2015-2021 the original authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
#
|
||||
# Gradle start up script for POSIX generated by Gradle.
|
||||
#
|
||||
# Important for running:
|
||||
#
|
||||
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
|
||||
# noncompliant, but you have some other compliant shell such as ksh or
|
||||
# bash, then to run this script, type that shell name before the whole
|
||||
# command line, like:
|
||||
#
|
||||
# ksh Gradle
|
||||
#
|
||||
# Busybox and similar reduced shells will NOT work, because this script
|
||||
# requires all of these POSIX shell features:
|
||||
# * functions;
|
||||
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
|
||||
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
|
||||
# * compound commands having a testable exit status, especially «case»;
|
||||
# * various built-in commands including «command», «set», and «ulimit».
|
||||
#
|
||||
# Important for patching:
|
||||
#
|
||||
# (2) This script targets any POSIX shell, so it avoids extensions provided
|
||||
# by Bash, Ksh, etc; in particular arrays are avoided.
|
||||
#
|
||||
# The "traditional" practice of packing multiple parameters into a
|
||||
# space-separated string is a well documented source of bugs and security
|
||||
# problems, so this is (mostly) avoided, by progressively accumulating
|
||||
# options in "$@", and eventually passing that to Java.
|
||||
#
|
||||
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
|
||||
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
|
||||
# see the in-line comments for details.
|
||||
#
|
||||
# There are tweaks for specific operating systems such as AIX, CygWin,
|
||||
# Darwin, MinGW, and NonStop.
|
||||
#
|
||||
# (3) This script is generated from the Groovy template
|
||||
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||
# within the Gradle project.
|
||||
#
|
||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
|
||||
# Resolve links: $0 may be a link
|
||||
app_path=$0
|
||||
|
||||
# Need this for daisy-chained symlinks.
|
||||
while
|
||||
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
|
||||
[ -h "$app_path" ]
|
||||
do
|
||||
ls=$( ls -ld "$app_path" )
|
||||
link=${ls#*' -> '}
|
||||
case $link in #(
|
||||
/*) app_path=$link ;; #(
|
||||
*) app_path=$APP_HOME$link ;;
|
||||
esac
|
||||
done
|
||||
|
||||
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=${0##*/}
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD=maximum
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
} >&2
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
} >&2
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "$( uname )" in #(
|
||||
CYGWIN* ) cygwin=true ;; #(
|
||||
Darwin* ) darwin=true ;; #(
|
||||
MSYS* | MINGW* ) msys=true ;; #(
|
||||
NONSTOP* ) nonstop=true ;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD=$JAVA_HOME/jre/sh/java
|
||||
else
|
||||
JAVACMD=$JAVA_HOME/bin/java
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD=java
|
||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||
case $MAX_FD in #(
|
||||
max*)
|
||||
MAX_FD=$( ulimit -H -n ) ||
|
||||
warn "Could not query maximum file descriptor limit"
|
||||
esac
|
||||
case $MAX_FD in #(
|
||||
'' | soft) :;; #(
|
||||
*)
|
||||
ulimit -n "$MAX_FD" ||
|
||||
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||
esac
|
||||
fi
|
||||
|
||||
# Collect all arguments for the java command, stacking in reverse order:
|
||||
# * args from the command line
|
||||
# * the main class name
|
||||
# * -classpath
|
||||
# * -D...appname settings
|
||||
# * --module-path (only if needed)
|
||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
|
||||
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if "$cygwin" || "$msys" ; then
|
||||
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
|
||||
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
|
||||
|
||||
JAVACMD=$( cygpath --unix "$JAVACMD" )
|
||||
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
for arg do
|
||||
if
|
||||
case $arg in #(
|
||||
-*) false ;; # don't mess with options #(
|
||||
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
|
||||
[ -e "$t" ] ;; #(
|
||||
*) false ;;
|
||||
esac
|
||||
then
|
||||
arg=$( cygpath --path --ignore --mixed "$arg" )
|
||||
fi
|
||||
# Roll the args list around exactly as many times as the number of
|
||||
# args, so each arg winds up back in the position where it started, but
|
||||
# possibly modified.
|
||||
#
|
||||
# NB: a `for` loop captures its iteration list before it begins, so
|
||||
# changing the positional parameters here affects neither the number of
|
||||
# iterations, nor the values presented in `arg`.
|
||||
shift # remove old arg
|
||||
set -- "$@" "$arg" # push replacement arg
|
||||
done
|
||||
fi
|
||||
|
||||
# Collect all arguments for the java command;
|
||||
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
|
||||
# shell script including quotes and variable substitutions, so put them in
|
||||
# double quotes to make sure that they get re-expanded; and
|
||||
# * put everything else in single quotes, so that it's not re-expanded.
|
||||
|
||||
set -- \
|
||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||
-classpath "$CLASSPATH" \
|
||||
org.gradle.wrapper.GradleWrapperMain \
|
||||
"$@"
|
||||
|
||||
# Use "xargs" to parse quoted args.
|
||||
#
|
||||
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
||||
#
|
||||
# In Bash we could simply go:
|
||||
#
|
||||
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
|
||||
# set -- "${ARGS[@]}" "$@"
|
||||
#
|
||||
# but POSIX shell has neither arrays nor command substitution, so instead we
|
||||
# post-process each arg (as a line of input to sed) to backslash-escape any
|
||||
# character that might be a shell metacharacter, then use eval to reverse
|
||||
# that process (while maintaining the separation between arguments), and wrap
|
||||
# the whole thing up as a single "set" statement.
|
||||
#
|
||||
# This will of course break if any of these variables contains a newline or
|
||||
# an unmatched quote.
|
||||
#
|
||||
|
||||
eval "set -- $(
|
||||
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
|
||||
xargs -n1 |
|
||||
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
|
||||
tr '\n' ' '
|
||||
)" '"$@"'
|
||||
|
||||
exec "$JAVACMD" "$@"
|
89
gradlew.bat
vendored
Normal file
89
gradlew.bat
vendored
Normal file
|
@ -0,0 +1,89 @@
|
|||
@rem
|
||||
@rem Copyright 2015 the original author or authors.
|
||||
@rem
|
||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@rem you may not use this file except in compliance with the License.
|
||||
@rem You may obtain a copy of the License at
|
||||
@rem
|
||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||
@rem
|
||||
@rem Unless required by applicable law or agreed to in writing, software
|
||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
2
settings.gradle
Normal file
2
settings.gradle
Normal file
|
@ -0,0 +1,2 @@
|
|||
rootProject.name = 'unfettered'
|
||||
|
27
src/main/java/dev/pfaff/unfettered/CompressedOopsMode.java
Normal file
27
src/main/java/dev/pfaff/unfettered/CompressedOopsMode.java
Normal file
|
@ -0,0 +1,27 @@
|
|||
package dev.pfaff.unfettered;
|
||||
|
||||
public enum CompressedOopsMode {
|
||||
ProbablyDisabled,
|
||||
UnscaledNarrow,
|
||||
ZeroBasedNarrow,
|
||||
DisjointBaseNarrow,
|
||||
HeapBasedNarrow;
|
||||
|
||||
public static final CompressedOopsMode RUNNING;
|
||||
|
||||
static {
|
||||
String propValue = System.getProperty("java.vm.compressedOopsMode");
|
||||
if (propValue != null) {
|
||||
RUNNING = switch (propValue) {
|
||||
case "32-bit" -> UnscaledNarrow;
|
||||
case "Zero based" -> ZeroBasedNarrow;
|
||||
case "Non-zero disjoint base" -> DisjointBaseNarrow;
|
||||
case "Non-zero based" -> HeapBasedNarrow;
|
||||
default -> throw new AssertionError(
|
||||
"Unsupported JVM: value of property java.vm.compressedOopsMode is unrecognized.");
|
||||
};
|
||||
} else {
|
||||
RUNNING = ProbablyDisabled;
|
||||
}
|
||||
}
|
||||
}
|
6
src/main/java/dev/pfaff/unfettered/FallibleSupplier.java
Normal file
6
src/main/java/dev/pfaff/unfettered/FallibleSupplier.java
Normal file
|
@ -0,0 +1,6 @@
|
|||
package dev.pfaff.unfettered;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface FallibleSupplier<T, E extends Throwable> {
|
||||
T get() throws E;
|
||||
}
|
37
src/main/java/dev/pfaff/unfettered/Log2.java
Normal file
37
src/main/java/dev/pfaff/unfettered/Log2.java
Normal file
|
@ -0,0 +1,37 @@
|
|||
package dev.pfaff.unfettered;
|
||||
|
||||
public final class Log2 {
|
||||
/**
|
||||
* Exact integer log2. See <a href="https://stackoverflow.com/a/3305710">https://stackoverflow.com/a/3305710</a>.
|
||||
* <p>
|
||||
* <h4>Implementations notes:</h4>
|
||||
*
|
||||
* Returns {@code 0} for input {@code 0}.
|
||||
*/
|
||||
public static int log2nlz(int n) {
|
||||
if (n == 0) return 0;
|
||||
if (Integer.bitCount(n) != 1) throw new IllegalArgumentException("Cannot compute exact log2 of " + n);
|
||||
return log2nlzUnchecked(n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exact long log2. See {@link #log2nlz} for details.
|
||||
* <p>
|
||||
* <h4>Implementations notes:</h4>
|
||||
*
|
||||
* Returns {@code 0} for input {@code 0}.
|
||||
*/
|
||||
public static int log2nlz(long n) {
|
||||
if (n == 0L) return 0;
|
||||
if (Long.bitCount(n) != 1) throw new IllegalArgumentException("Cannot compute exact log2 of " + n);
|
||||
return log2nlzUnchecked(n);
|
||||
}
|
||||
|
||||
public static int log2nlzUnchecked(int n) {
|
||||
return 31 - Integer.numberOfLeadingZeros(n);
|
||||
}
|
||||
|
||||
public static int log2nlzUnchecked(long n) {
|
||||
return 63 - Long.numberOfLeadingZeros(n);
|
||||
}
|
||||
}
|
527
src/main/java/dev/pfaff/unfettered/Unfettered.java
Normal file
527
src/main/java/dev/pfaff/unfettered/Unfettered.java
Normal file
|
@ -0,0 +1,527 @@
|
|||
package dev.pfaff.unfettered;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import static dev.pfaff.unfettered.Log2.log2nlz;
|
||||
import static dev.pfaff.unfettered.Util.reflectUnchecked;
|
||||
import static java.lang.invoke.MethodType.methodType;
|
||||
|
||||
/**
|
||||
* <h2>Breaking free of the chains:</h2>
|
||||
*
|
||||
* <p>This class goes through a lengthy process to shatter the restraints of JPMS without requiring the end-user,
|
||||
* launcher, or mod loader to inject any JVM args.</p>
|
||||
*
|
||||
* <p>First, we get a reference to {@link sun.misc.Unsafe#theUnsafe} via reflection, which is not yet locked down by
|
||||
* the ongoing encapsulation crusade.</p>
|
||||
*
|
||||
* <p>Then, using this reference, we get the static field base and offset of {@link MethodHandles.Lookup#IMPL_LOOKUP}.
|
||||
* As far as I am aware, this is the only instance of {@link MethodHandles.Lookup} with an
|
||||
* {@link MethodHandles.Lookup#allowedModes allowedModes} value of {@link MethodHandles.Lookup#TRUSTED TRUSTED}. We then
|
||||
* check the OOP pointer width and, equipped with these, use our reference to {@link sun.misc.Unsafe#theUnsafe} to copy
|
||||
* the OOP pointer stored in the static field into a 1-element array on the LVT.</p>
|
||||
*
|
||||
* <p>Now might be a good time to interject and point out how pointless it is for the JDK to hide
|
||||
* {@link MethodHandles.Lookup#allowedModes} (see {@code MethodHandles.java} line 1433:
|
||||
* {@code Reflection.registerFieldsToFilter(Lookup.class, Set.of("lookupClass", "allowedModes"));}) from reflection
|
||||
* without also hiding {@link MethodHandles.Lookup#IMPL_LOOKUP}.</p>
|
||||
*
|
||||
* <p>Now equipped with an unrestricted {@link MethodHandles.Lookup}, we get a {@link MethodHandle} for
|
||||
* {@link Module#implAddExportsOrOpens(String, Module, boolean, boolean)}.</p>
|
||||
*
|
||||
* <p>Phew, almost there! Now we get a reference to this and the {@code java.base} {@link Module}s via
|
||||
* {@link Class#getModule()} ({@code UnsafeUtil.class.getModule()} and {@code String.class.getModule()}). Next, we use
|
||||
* that handle we just got to export the {@code jdk.internal.misc} package in the {@code java.base} module to this
|
||||
* module.</p>
|
||||
*
|
||||
* <p>Finally, references to {@code jdk.internal.misc.Unsafe#theUnsafe} and a usable {@link MethodHandles.Lookup} (the
|
||||
* one we borrowed earlier doesn't have access to any of <em>our</em> classes): For the former, we simply call
|
||||
* {@code jdk.internal.misc.Unsafe#getUnsafe()}. It's a bit more complicated for the latter, but nothing compared to
|
||||
* what we've accomplished thus far.</p>
|
||||
*
|
||||
* <p>First, we set a {@code static final} field to the value of {@link MethodHandles#lookup()}. Then, we get the
|
||||
* offset of the {@link MethodHandles.Lookup#allowedModes allowedModes} field using our shiny new reference to
|
||||
* {@code jdk.internal.misc.Unsafe#theUnsafe} and then, once again using {@code jdk.internal.misc.Unsafe#theUnsafe}, set
|
||||
* the value to {@link MethodHandles.Lookup#TRUSTED}.</p>
|
||||
*
|
||||
* <p>And that's it! We now have full access to the classpath and the memory space of the JVM.</p>
|
||||
*/
|
||||
public final class Unfettered {
|
||||
// needed for bootstrap
|
||||
private static final sun.misc.Unsafe theSunUnsafe = reflectUnchecked(() -> {
|
||||
var f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
f.setAccessible(true);
|
||||
return (sun.misc.Unsafe) f.get(null);
|
||||
});
|
||||
|
||||
public static final MethodHandles.Lookup theJdkTrustedLookup;
|
||||
|
||||
// see the class documentation for a detailed breakdown of this initialization.
|
||||
static {
|
||||
final Object IMPL_LOOKUP_BASE;
|
||||
final long IMPL_LOOKUP_OFFSET;
|
||||
{
|
||||
final Field f = reflectUnchecked(() -> MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP"));
|
||||
IMPL_LOOKUP_BASE = theSunUnsafe.staticFieldBase(f);
|
||||
IMPL_LOOKUP_OFFSET = theSunUnsafe.staticFieldOffset(f);
|
||||
}
|
||||
|
||||
final MethodHandles.Lookup[] borrowedLookup = new MethodHandles.Lookup[1];
|
||||
if (sun.misc.Unsafe.ARRAY_OBJECT_INDEX_SCALE == Integer.BYTES) {
|
||||
theSunUnsafe.putInt(borrowedLookup,
|
||||
sun.misc.Unsafe.ARRAY_OBJECT_BASE_OFFSET,
|
||||
theSunUnsafe.getInt(IMPL_LOOKUP_BASE, IMPL_LOOKUP_OFFSET));
|
||||
} else {
|
||||
assert sun.misc.Unsafe.ARRAY_OBJECT_INDEX_SCALE == Long.BYTES
|
||||
: "OOPs are neither 32-bit nor 64-bit: pointerWidth=" + sun.misc.Unsafe.ADDRESS_SIZE + ", " +
|
||||
"arrayObjectIndexScale=" + sun.misc.Unsafe.ARRAY_OBJECT_INDEX_SCALE;
|
||||
theSunUnsafe.putLong(borrowedLookup,
|
||||
sun.misc.Unsafe.ARRAY_OBJECT_BASE_OFFSET,
|
||||
theSunUnsafe.getLong(IMPL_LOOKUP_BASE, IMPL_LOOKUP_OFFSET));
|
||||
}
|
||||
|
||||
theJdkTrustedLookup = borrowedLookup[0];
|
||||
}
|
||||
|
||||
public static final Class<?> C_Unsafe = reflectUnchecked(() -> theJdkTrustedLookup.findClass("jdk.internal.misc.Unsafe"));
|
||||
public static final Object theUnsafe;
|
||||
|
||||
static {
|
||||
try {
|
||||
theUnsafe = theJdkTrustedLookup.findStatic(C_Unsafe, "getUnsafe", methodType(C_Unsafe))
|
||||
.asType(methodType(Object.class))
|
||||
.invokeExact();
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static final long LOOKUP_ALLOWED_MODES_OFFSET = UnsafeWrapper.objectFieldOffset(MethodHandles.Lookup.class, "allowedModes");
|
||||
|
||||
private static final MethodHandle MH_Lookup_constructor = reflectUnchecked(() -> {
|
||||
return theJdkTrustedLookup.findConstructor(MethodHandles.Lookup.class, methodType(void.class, Class.class));
|
||||
});
|
||||
|
||||
/**
|
||||
* Produces a new {@link MethodHandles.Lookup} with {@link MethodHandles.Lookup#TRUSTED} access.
|
||||
*/
|
||||
private static MethodHandles.Lookup makeTrustedLookup(Class<?> lookupClass) {
|
||||
MethodHandles.Lookup l;
|
||||
try {
|
||||
l = (MethodHandles.Lookup) MH_Lookup_constructor.invokeExact(lookupClass);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
UnsafeWrapper.putInt(l, LOOKUP_ALLOWED_MODES_OFFSET, -1);
|
||||
return l;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method handle lookup with the {@code allowedModes} field set to {@link MethodHandles.Lookup#TRUSTED}, giving it
|
||||
* unrestricted access to every class in the JVM.
|
||||
* <p>
|
||||
* <strong>Note that this will not be trusted until part of the way through static init</strong>
|
||||
*/
|
||||
public static final MethodHandles.Lookup theTrustedLookup = makeTrustedLookup(Unfettered.class);
|
||||
|
||||
public static MethodHandles.Lookup getTrustedLookup() {
|
||||
return theTrustedLookup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Forces this class to be static-initialized. Calling this method more than once will have no effect. Calling this
|
||||
* method is not even necessary before using any of the exposed unsafe APIs.
|
||||
*/
|
||||
public static void forceInit() {
|
||||
}
|
||||
|
||||
private static final MethodHandle MH_implAddExportsOrOpens = reflectUnchecked(() -> theJdkTrustedLookup.findSpecial(
|
||||
Module.class,
|
||||
"implAddExportsOrOpens",
|
||||
methodType(
|
||||
Void.TYPE,
|
||||
String.class,
|
||||
Module.class,
|
||||
Boolean.TYPE,
|
||||
Boolean.TYPE
|
||||
),
|
||||
Module.class
|
||||
));
|
||||
|
||||
/**
|
||||
* Exports the {@code pkgName} from the {@code module} to {@code target}. If {@code opens} is {@code true}, then the
|
||||
* {@code target} will have access to package-private members in addition to public ones.
|
||||
*/
|
||||
public static void export(Module module, String pkgName, boolean opens, Module target) {
|
||||
try {
|
||||
MH_implAddExportsOrOpens.invokeExact(module, pkgName, target, opens, true);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A handle to the private, no security checks method for getting the fields. DO NOT modify the returned array.
|
||||
*/
|
||||
private static final MethodHandle MH_getDeclaredFieldsQuick = reflectUnchecked(() -> {
|
||||
return getTrustedLookup().findVirtual(Class.class,
|
||||
"privateGetDeclaredFields",
|
||||
methodType(Field[].class, boolean.class));
|
||||
});
|
||||
|
||||
private static Field[] getPublicFieldsQuick(Class<?> clazz) {
|
||||
try {
|
||||
return (Field[]) MH_getDeclaredFieldsQuick.invokeExact(clazz, true);
|
||||
} catch (Throwable e) {
|
||||
// should never happen
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Field[] getDeclaredFieldsQuick(Class<?> clazz) {
|
||||
try {
|
||||
return (Field[]) MH_getDeclaredFieldsQuick.invokeExact(clazz, false);
|
||||
} catch (Throwable e) {
|
||||
// should never happen
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static final int POINTER_WIDTH = UnsafeWrapper.ADDRESS_SIZE;
|
||||
private static final int OOP_WIDTH = UnsafeWrapper.ARRAY_OBJECT_INDEX_SCALE;
|
||||
private static final boolean ARE_KPS_COMPRESSED = determineIfKpsAreCompressed();
|
||||
private static final int KLASS_PTR_WIDTH;
|
||||
|
||||
static {
|
||||
KLASS_PTR_WIDTH = areKpsCompressed() ? Integer.BYTES : pointerWidth();
|
||||
}
|
||||
|
||||
private static final boolean ARE_OOPS_COMPRESSED = determineIfOopsAreCompressed();
|
||||
public static final int COMPRESSED_OOP_SHIFT = log2nlz(UnsafeWrapper.ARRAY_OBJECT_INDEX_SCALE) + 1;
|
||||
|
||||
/**
|
||||
* The header size of an object instance, excluding the extra 4 bytes representing an array's length.
|
||||
*/
|
||||
// pointerWidth mark word, then klass ptr
|
||||
public static final int OBJECT_HEADER_SIZE = pointerWidth() + klassPtrWidth();
|
||||
|
||||
/**
|
||||
* The width of a native pointer.
|
||||
*/
|
||||
public static int pointerWidth() {
|
||||
return POINTER_WIDTH;
|
||||
}
|
||||
|
||||
/**
|
||||
* Width of an Ordinary Object Pointer as stored in the JVM. These may be compressed (see
|
||||
* {@link #areOopsCompressed()} on 64-bit JVMs.
|
||||
*/
|
||||
public static int oopWidth() {
|
||||
return OOP_WIDTH;
|
||||
}
|
||||
|
||||
/**
|
||||
* Width of a klass pointer.
|
||||
*/
|
||||
public static int klassPtrWidth() {
|
||||
return KLASS_PTR_WIDTH;
|
||||
}
|
||||
|
||||
private static boolean determineIfOopsAreCompressed() {
|
||||
if (pointerWidth() == oopWidth()) return false;
|
||||
// this constant is specified in the Hotspot source code...
|
||||
if (Runtime
|
||||
.getRuntime()
|
||||
.maxMemory() > (2L * 1_024L * 1_024L * 1_024L)) return true;
|
||||
String osName = System.getProperty("os.name");
|
||||
// but macOS seems to follow different rules.
|
||||
if (osName != null && osName.contains("Mac OS")) return true;
|
||||
switch (CompressedOopsMode.RUNNING) {
|
||||
case ProbablyDisabled -> {
|
||||
return false;
|
||||
}
|
||||
case UnscaledNarrow -> {
|
||||
return false;
|
||||
}
|
||||
case ZeroBasedNarrow -> {
|
||||
return true;
|
||||
}
|
||||
case DisjointBaseNarrow, HeapBasedNarrow -> {
|
||||
throw new AssertionError("Unsupported OOP compression mode: " + CompressedOopsMode.RUNNING);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static final class ObjectLayoutModel {
|
||||
@SuppressWarnings({"FieldMayBeStatic", "unused"})
|
||||
private final byte b = (byte) 0;
|
||||
}
|
||||
|
||||
private static boolean determineIfKpsAreCompressed() {
|
||||
// TODO: document why I didn't just use `ObjectLayoutModel.class.getDeclaredField("b")` instead.
|
||||
Field[] fs = getDeclaredFieldsQuick(ObjectLayoutModel.class);
|
||||
assert fs.length == 1 : "Unable to get fields on ObjectLayoutModel.";
|
||||
Field f = fs[0];
|
||||
assert "b".equals(f.getName()) : "Unable to find expected field on ObjectLayoutModel";
|
||||
assert f.getType() == byte.class : "Unable to find expected field on ObjectLayoutModel";
|
||||
// minus pointerWidth for mark word
|
||||
long o = UnsafeWrapper.objectFieldOffset(f) - pointerWidth();
|
||||
if (pointerWidth() == Integer.BYTES) {
|
||||
assert o == pointerWidth();
|
||||
return false;
|
||||
}
|
||||
if (o == pointerWidth()) {
|
||||
return false;
|
||||
} else if (o == Integer.BYTES) {
|
||||
return true;
|
||||
} else {
|
||||
throw new AssertionError("Unexpected field offset: " + o);
|
||||
}
|
||||
}
|
||||
|
||||
public static AssertionError unexpectedOopWidth() {
|
||||
throw new AssertionError("Unexpected OOP width: " + Unfettered.oopWidth());
|
||||
}
|
||||
|
||||
/**
|
||||
* When true, the JVM is using compressed pointers (this is the default). This will only be true on 64-bit JVMs.
|
||||
*/
|
||||
public static boolean areOopsCompressed() {
|
||||
return ARE_OOPS_COMPRESSED;
|
||||
}
|
||||
|
||||
public static boolean areKpsCompressed() {
|
||||
return ARE_KPS_COMPRESSED;
|
||||
}
|
||||
|
||||
static {
|
||||
if (areOopsCompressed()) {
|
||||
assert oopWidth() == Integer.BYTES
|
||||
: "oopWidth is expected to be 4 bytes when CompressedOops are enabled. oopWidth=" + oopWidth();
|
||||
assert pointerWidth() == Long.BYTES
|
||||
: "CompressedOops on 32-bit? pointerWidth=" + pointerWidth() + ", oopWidth=" + oopWidth();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the OOP of the object. If CompressedOops is enabled (see {@link #areOopsCompressed()}) then the returned
|
||||
* value will not be valid native pointer. To make it so, use {@link #uncompressOop}.
|
||||
*/
|
||||
public static long getOopOfObject(Object obj) {
|
||||
if (oopWidth() == Long.BYTES) {
|
||||
return getOopOfObjectAssume64bit(obj);
|
||||
} else if (oopWidth() == Integer.BYTES) {
|
||||
return Integer.toUnsignedLong(getOopOfObjectAssume32bit(obj));
|
||||
} else {
|
||||
throw new UnsupportedOperationException("Unsupported OOP width: " + oopWidth());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the OOP of the object and decompresses it if it is compressed.
|
||||
*/
|
||||
public static long getUncompressedOopOfObject(Object obj) {
|
||||
return uncompressOop(getOopOfObject(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Gets the OOP of the object. If CompressedOops is enabled (see {@link #areOopsCompressed()}) then the returned
|
||||
* value will not be valid native pointer. To make it so, use {@link #uncompressOop}.</p>
|
||||
*
|
||||
* <p>You probably want {@link #getOopOfObjectAssume32bit(Object)} instead.</p>
|
||||
*/
|
||||
public static int getOopOfObjectAssumeCompressed(Object obj) {
|
||||
assert areOopsCompressed() : "OOPs are not compressed";
|
||||
return getOopOfObjectAssume32bit(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the OOP of the object, assuming that it is 32-bits wide.
|
||||
*/
|
||||
public static int getOopOfObjectAssume32bit(Object obj) {
|
||||
assert oopWidth() == Integer.BYTES : "OOPs are not 32-bits wide. oopWidth=" + oopWidth();
|
||||
return getOopOfObject32bit(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the OOP of the object, assuming that it is 64-bits wide.
|
||||
*/
|
||||
public static long getOopOfObjectAssume64bit(Object obj) {
|
||||
assert oopWidth() == Long.BYTES : "OOPs are not 64-bits wide. oopWidth=" + oopWidth();
|
||||
return getOopOfObject64bit(obj);
|
||||
}
|
||||
|
||||
private static int getOopOfObject32bit(Object obj) {
|
||||
return UnsafeWrapper.getInt(new Object[]{obj}, UnsafeWrapper.ARRAY_OBJECT_BASE_OFFSET);
|
||||
}
|
||||
|
||||
private static long getOopOfObject64bit(Object obj) {
|
||||
return UnsafeWrapper.getLong(new Object[]{obj}, UnsafeWrapper.ARRAY_OBJECT_BASE_OFFSET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dereferences an OOP.
|
||||
*/
|
||||
public static Object dereferenceOop(long oop) {
|
||||
if (oopWidth() == Long.BYTES) {
|
||||
return dereferenceOopAssume64bit(oop);
|
||||
} else if (oopWidth() == Integer.BYTES) {
|
||||
// TODO: document this constant because I forget where it comes from/why not using `(int) ... == ... `
|
||||
// instead
|
||||
assert (oop & 0xffff_ffffL) == oop : "OOP is not 32-bit: 0x" + Long.toHexString(oop);
|
||||
//assert ((int) oop) == oop : "OOP is not 32-bit: 0x" + Long.toHexString(oop);
|
||||
return dereferenceOopAssume32bit((int) oop);
|
||||
} else {
|
||||
throw new UnsupportedOperationException("Unsupported OOP width: " + oopWidth());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dereferences an OOP, assuming that it is compressed.
|
||||
*/
|
||||
public static Object dereferenceOopAssumeCompressed(int oop) {
|
||||
assert areOopsCompressed() : "OOPs are not compressed";
|
||||
return dereferenceOopAssume32bit(oop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dereferences an OOP, assuming that it is 32-bits wide.
|
||||
*/
|
||||
public static Object dereferenceOopAssume32bit(int oop) {
|
||||
assert oopWidth() == Integer.BYTES : "OOPs are not 32-bits wide. oopWidth=" + oopWidth();
|
||||
int[] slot = (int[]) UnsafeWrapper.allocateUninitializedArray(int.class, 1);
|
||||
UnsafeWrapper.putInt(slot, UnsafeWrapper.ARRAY_OBJECT_BASE_OFFSET, oop);
|
||||
return UnsafeWrapper.getReference(slot, UnsafeWrapper.ARRAY_OBJECT_BASE_OFFSET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dereferences an OOP, assuming that it is 64-bits wide.
|
||||
*/
|
||||
public static Object dereferenceOopAssume64bit(long oop) {
|
||||
assert oopWidth() == Long.BYTES : "OOPs are not 64-bits wide. oopWidth=" + oopWidth();
|
||||
long[] slot = (long[]) UnsafeWrapper.allocateUninitializedArray(long.class, 1);
|
||||
UnsafeWrapper.putLong(slot, UnsafeWrapper.ARRAY_OBJECT_BASE_OFFSET, oop);
|
||||
return UnsafeWrapper.getReference(slot, UnsafeWrapper.ARRAY_OBJECT_BASE_OFFSET);
|
||||
}
|
||||
|
||||
/**
|
||||
* If CompressesOops are enabled (see {@link #areOopsCompressed()}), returns the {@code oop} with compression
|
||||
* removed. Otherwise, returns the {@code oop} itself.
|
||||
*/
|
||||
public static long uncompressOop(long oop) {
|
||||
if (areOopsCompressed()) {
|
||||
return oop << COMPRESSED_OOP_SHIFT;
|
||||
} else {
|
||||
return oop;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If CompressesOops are enabled (see {@link #areOopsCompressed()}), returns the {@code oop} with compression
|
||||
* applied. Otherwise, returns the {@code oop} itself.
|
||||
*/
|
||||
public static long compressOop(long oop) {
|
||||
if (areOopsCompressed()) {
|
||||
return oop >>> COMPRESSED_OOP_SHIFT;
|
||||
} else {
|
||||
return oop;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If {@code enableCompression}, returns the {@code ptr} with compression removed. Otherwise, returns the
|
||||
* {@code ptr} itself.
|
||||
*/
|
||||
public static long maybeUncompressPtr(long oop, boolean enableCompression) {
|
||||
if (enableCompression) {
|
||||
return oop << COMPRESSED_OOP_SHIFT;
|
||||
} else {
|
||||
return oop;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If {@code enableCompression}, returns the {@code ptr} with compression applied. Otherwise, returns the
|
||||
* {@code ptr} itself.
|
||||
*/
|
||||
public static long maybeCompressPtr(long oop, boolean enableCompression) {
|
||||
if (enableCompression) {
|
||||
return oop >>> COMPRESSED_OOP_SHIFT;
|
||||
} else {
|
||||
return oop;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum offset of the class's fields. This is not an efficient operation. Callers are strongly
|
||||
* encouraged to calculate the result once and store it for reuse.
|
||||
*/
|
||||
public static long classMaxFieldOffset(Class<?> clazz) {
|
||||
if (clazz.isPrimitive()) throw new IllegalArgumentException("Cannot calculate max field offset of a " +
|
||||
"primitive");
|
||||
if (clazz.isArray()) throw new IllegalArgumentException("Cannot calculate max field offset of an array");
|
||||
long maxOffset = -1L;
|
||||
do {
|
||||
for (var field : getDeclaredFieldsQuick(clazz)) {
|
||||
maxOffset = Math.max(UnsafeWrapper.objectFieldOffset(field), maxOffset);
|
||||
}
|
||||
clazz = clazz.getSuperclass();
|
||||
} while (clazz != null);
|
||||
return maxOffset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the total length of all the class's fields. This is not an efficient operation. Callers are strongly
|
||||
* encouraged to calculate the result once and store it for reuse.
|
||||
*/
|
||||
public static long classFieldsLength(Class<?> clazz) {
|
||||
if (clazz.isPrimitive()) throw new IllegalArgumentException("Cannot calculate max field offset of a " +
|
||||
"primitive");
|
||||
if (clazz.isArray()) throw new IllegalArgumentException("Cannot calculate max field offset of an array");
|
||||
|
||||
long maxOffset = -1;
|
||||
Field maxField = null;
|
||||
|
||||
do {
|
||||
for (var field : getDeclaredFieldsQuick(clazz)) {
|
||||
if (Modifier.isStatic(field.getModifiers())) continue;
|
||||
long offset = UnsafeWrapper.objectFieldOffset(field);
|
||||
assert offset != maxOffset : "Illegal duplicate field offset";
|
||||
if (offset > maxOffset) {
|
||||
maxOffset = offset;
|
||||
maxField = field;
|
||||
}
|
||||
}
|
||||
clazz = clazz.getSuperclass();
|
||||
} while (clazz != null);
|
||||
|
||||
if (maxField == null) return 0;
|
||||
|
||||
int fieldLen;
|
||||
var fieldTy = maxField.getType();
|
||||
if (fieldTy.isPrimitive()) {
|
||||
if (fieldTy == byte.class || fieldTy == boolean.class) fieldLen = 1;
|
||||
else if (fieldTy == short.class || fieldTy == char.class) fieldLen = 2;
|
||||
else if (fieldTy == int.class || fieldTy == float.class) fieldLen = 4;
|
||||
else if (fieldTy == long.class || fieldTy == double.class) fieldLen = 8;
|
||||
else throw new AssertionError("Illegal field type: " + fieldTy);
|
||||
} else {
|
||||
fieldLen = oopWidth();
|
||||
}
|
||||
return maxOffset + fieldLen;
|
||||
}
|
||||
|
||||
public static long getKlassPointer(long oop) {
|
||||
// add pointerWidth for mark word
|
||||
if (klassPtrWidth() == Integer.BYTES) {
|
||||
return UnsafeWrapper.getInt(oop + pointerWidth());
|
||||
} else {
|
||||
assert klassPtrWidth() == Long.BYTES : "Unexpected klass ptr width: " + klassPtrWidth();
|
||||
return UnsafeWrapper.getLong(oop + pointerWidth());
|
||||
}
|
||||
}
|
||||
}
|
143
src/main/java/dev/pfaff/unfettered/UnsafeWrapper.java
Normal file
143
src/main/java/dev/pfaff/unfettered/UnsafeWrapper.java
Normal file
|
@ -0,0 +1,143 @@
|
|||
package dev.pfaff.unfettered;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static dev.pfaff.unfettered.Unfettered.theJdkTrustedLookup;
|
||||
import static java.lang.invoke.MethodType.methodType;
|
||||
|
||||
public final class UnsafeWrapper {
|
||||
public static final MethodHandle MH_objectFieldOffsetF, MH_staticFieldOffsetF;
|
||||
public static final MethodHandle MH_objectFieldOffset;
|
||||
public static final MethodHandle MH_getIntP, MH_getLongP;
|
||||
public static final MethodHandle MH_getIntBO, MH_getLongBO, MH_getReferenceBO;
|
||||
public static final MethodHandle MH_putInt, MH_putLong;
|
||||
public static final MethodHandle MH_allocateUninitializedArray;
|
||||
|
||||
public static final int ADDRESS_SIZE = sun.misc.Unsafe.ADDRESS_SIZE;
|
||||
public static final int ARRAY_OBJECT_INDEX_SCALE = sun.misc.Unsafe.ARRAY_OBJECT_INDEX_SCALE;
|
||||
public static final int ARRAY_OBJECT_BASE_OFFSET = sun.misc.Unsafe.ARRAY_OBJECT_BASE_OFFSET;
|
||||
|
||||
static {
|
||||
try {
|
||||
var l = theJdkTrustedLookup;
|
||||
MH_objectFieldOffsetF = l.bind(Unfettered.theUnsafe,
|
||||
"objectFieldOffset",
|
||||
methodType(long.class, Field.class));
|
||||
MH_objectFieldOffset = l.bind(Unfettered.theUnsafe,
|
||||
"objectFieldOffset",
|
||||
methodType(long.class, Class.class, String.class));
|
||||
MH_staticFieldOffsetF = l.bind(Unfettered.theUnsafe,
|
||||
"staticFieldOffset",
|
||||
methodType(long.class, Field.class));
|
||||
MH_getIntP = l.bind(Unfettered.theUnsafe, "getInt", methodType(int.class, long.class));
|
||||
MH_getLongP = l.bind(Unfettered.theUnsafe, "getLong", methodType(long.class, long.class));
|
||||
MH_getIntBO = l.bind(Unfettered.theUnsafe, "getInt", methodType(int.class, Object.class, long.class));
|
||||
MH_getLongBO = l.bind(Unfettered.theUnsafe, "getLong", methodType(long.class, Object.class, long.class));
|
||||
MH_getReferenceBO = l.bind(Unfettered.theUnsafe,
|
||||
"getReference",
|
||||
methodType(Object.class, Object.class, long.class));
|
||||
MH_putInt = l.bind(Unfettered.theUnsafe,
|
||||
"putInt",
|
||||
methodType(void.class, Object.class, long.class, int.class));
|
||||
MH_putLong = l.bind(Unfettered.theUnsafe, "putLong", methodType(void.class, Object.class, long.class,
|
||||
long.class));
|
||||
MH_allocateUninitializedArray = l.bind(Unfettered.theUnsafe,
|
||||
"allocateUninitializedArray",
|
||||
methodType(Object.class, Class.class, int.class));
|
||||
|
||||
// ADDRESS_SIZE = (int) l.findStaticGetter(C_Unsafe, "ADDRESS_SIZE", int.class).invokeExact();
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static long objectFieldOffset(Field field) {
|
||||
try {
|
||||
return (long) MH_objectFieldOffsetF.invokeExact(field);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static long objectFieldOffset(Class clazz, String field) {
|
||||
try {
|
||||
return (long) MH_objectFieldOffset.invokeExact(clazz, field);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static long staticFieldOffset(Field field) {
|
||||
try {
|
||||
return (long) MH_staticFieldOffsetF.invokeExact(field);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static int getInt(long ptr) {
|
||||
try {
|
||||
return (int) MH_getIntP.invokeExact(ptr);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static long getLong(long ptr) {
|
||||
try {
|
||||
return (long) MH_getLongP.invokeExact(ptr);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static int getInt(Object base, long offset) {
|
||||
try {
|
||||
return (int) MH_getIntBO.invokeExact(base, offset);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static long getLong(Object base, long offset) {
|
||||
try {
|
||||
return (long) MH_getLongBO.invokeExact(base, offset);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Object getReference(Object base, int offset) {
|
||||
try {
|
||||
return MH_getReferenceBO.invokeExact(base, offset);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void putInt(Object base, long offset, int i) {
|
||||
try {
|
||||
MH_putInt.invokeExact(base, offset, i);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void putLong(Object base, long offset, long l) {
|
||||
try {
|
||||
MH_putLong.invokeExact(base, offset, l);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Object allocateUninitializedArray(Class<?> component, int length) {
|
||||
try {
|
||||
return MH_allocateUninitializedArray.invokeExact(component, length);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
11
src/main/java/dev/pfaff/unfettered/Util.java
Normal file
11
src/main/java/dev/pfaff/unfettered/Util.java
Normal file
|
@ -0,0 +1,11 @@
|
|||
package dev.pfaff.unfettered;
|
||||
|
||||
public final class Util {
|
||||
public static <R> R reflectUnchecked(FallibleSupplier<R, ? extends ReflectiveOperationException> supplier) {
|
||||
try {
|
||||
return supplier.get();
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
10
src/test/java/dev/pfaff/unfettered/UnfetteredTests.java
Normal file
10
src/test/java/dev/pfaff/unfettered/UnfetteredTests.java
Normal file
|
@ -0,0 +1,10 @@
|
|||
package dev.pfaff.unfettered;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public final class UnfetteredTests {
|
||||
@Test
|
||||
public void testUnfetteredInit() {
|
||||
Unfettered.forceInit();
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue