aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/UnsafeProvider.java
diff options
context:
space:
mode:
authorGravatar shahan <shahan@google.com>2018-01-02 18:29:58 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-01-02 18:32:07 -0800
commit6ad05df111478e412ea760b60039224d0497228b (patch)
treef9f126675811f987743538056cf0519221d805a1 /src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/UnsafeProvider.java
parent5a0857e54f96ab6cf763902e27ba79bc203d38d3 (diff)
@AutoCodec: Uses sun.misc.Unsafe to retrieve fields for serialization.
This is fast (profiles indicate a 9% penalty vs getters) and does not require getters. * Also adds a Marshaller for java.util.regex.Pattern. * Minor fixes for codecs that were broken by this change. PiperOrigin-RevId: 180619680
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/UnsafeProvider.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/UnsafeProvider.java67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/UnsafeProvider.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/UnsafeProvider.java
new file mode 100644
index 0000000000..4a1fe6c389
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec/UnsafeProvider.java
@@ -0,0 +1,67 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// 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
+//
+// http://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.
+
+package com.google.devtools.build.lib.skyframe.serialization.autocodec;
+
+import java.lang.reflect.Field;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+import sun.misc.Unsafe;
+
+/**
+ * An accessor for Unsafe.
+ *
+ * <p>Not for general consumption. Public only so that generated codecs in different packages can
+ * access this.
+ */
+public class UnsafeProvider {
+ private static final Unsafe UNSAFE = getUnsafe();
+
+ public static Unsafe getInstance() {
+ return UNSAFE;
+ }
+
+ /**
+ * Gets a reference to {@link sun.misc.Unsafe} throwing an {@link AssertionError} on failure.
+ *
+ * <p>Failure is highly unlikely, but possible if the underlying VM stores unsafe in an unexpected
+ * location.
+ */
+ private static Unsafe getUnsafe() {
+ try {
+ // sun.misc.Unsafe is intentionally difficult to get a hold of - it gives us the power to
+ // do things like access raw memory and segfault the JVM.
+ return AccessController.doPrivileged(
+ new PrivilegedExceptionAction<Unsafe>() {
+ @Override
+ public Unsafe run() throws Exception {
+ Class<Unsafe> unsafeClass = Unsafe.class;
+ // Unsafe usually exists in the field 'theUnsafe', however check all fields
+ // in case it's somewhere else in this VM's version of Unsafe.
+ for (Field f : unsafeClass.getDeclaredFields()) {
+ f.setAccessible(true);
+ Object fieldValue = f.get(null);
+ if (unsafeClass.isInstance(fieldValue)) {
+ return unsafeClass.cast(fieldValue);
+ }
+ }
+ throw new AssertionError("Failed to find sun.misc.Unsafe instance");
+ }
+ });
+ } catch (PrivilegedActionException pae) {
+ throw new AssertionError("Unable to get sun.misc.Unsafe", pae);
+ }
+ }
+}