View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  /*
22   * Licensed under the Apache License, Version 2.0 (the "License");
23   * you may not use this file except in compliance with the License.
24   * You may obtain a copy of the License at
25   *
26   *     http://www.apache.org/licenses/LICENSE-2.0
27   *
28   * Unless required by applicable law or agreed to in writing, software
29   * distributed under the License is distributed on an "AS IS" BASIS,
30   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31   * See the License for the specific language governing permissions and
32   * limitations under the License.
33   */
34  package org.waarp.compress.zstdunsafe;
35  
36  import org.waarp.compress.IncompatibleJvmException;
37  import sun.misc.Unsafe;
38  
39  import java.lang.reflect.Field;
40  import java.nio.Buffer;
41  import java.nio.ByteOrder;
42  
43  import static java.lang.String.*;
44  
45  public final class UnsafeUtil {
46    private static boolean initialized;
47    public static final Unsafe UNSAFE;
48    private static final long ADDRESS_OFFSET;
49  
50    private UnsafeUtil() {
51    }
52  
53    static {
54      Unsafe tempUnsafe = null;
55      long tempField = 0;
56      try {
57        initLittleEndian();
58        tempUnsafe = initUnsafe();
59        tempField = initAccessor();
60        initialized = true;
61      } catch (final Exception e) {
62        initialized = false;
63      }
64      UNSAFE = tempUnsafe;
65      ADDRESS_OFFSET = tempField;
66    }
67  
68    public static boolean isValid() {
69      return initialized;
70    }
71  
72    /**
73     * @throws IncompatibleJvmException if this implementation is not available
74     */
75    private static final void initLittleEndian() {
76      if (initialized) {
77        return;
78      }
79      final ByteOrder order = ByteOrder.nativeOrder();
80      if (!order.equals(ByteOrder.LITTLE_ENDIAN)) {
81        throw new IncompatibleJvmException(
82            format("Zstandard requires a little endian platform (found %s)",
83                   order));
84      }
85    }
86  
87    /**
88     * @throws IncompatibleJvmException if this implementation is not available
89     */
90    private static final Unsafe initUnsafe() {
91      if (initialized) {
92        return UNSAFE;
93      }
94      try {
95        final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
96        theUnsafe.setAccessible(true);
97        return (Unsafe) theUnsafe.get(null);
98      } catch (final Exception e) {
99        throw new IncompatibleJvmException(
100           "Zstandard requires access to sun.misc.Unsafe");
101     }
102   }
103 
104   /**
105    * @throws IncompatibleJvmException if this implementation is not available
106    */
107   private static final long initAccessor() {
108     if (initialized) {
109       return ADDRESS_OFFSET;
110     }
111     try {
112       // Remove warning
113       final Class cls =
114           Class.forName("jdk.internal.module.IllegalAccessLogger");
115       final Field logger = cls.getDeclaredField("logger");
116       UNSAFE.putObjectVolatile(cls, UNSAFE.staticFieldOffset(logger), null);
117 
118       // fetch the address field for direct buffers
119       return UNSAFE.objectFieldOffset(Buffer.class.getDeclaredField("address"));
120     } catch (final Exception e) {
121       throw new IncompatibleJvmException(
122           "Zstandard requires access to java.nio.Buffer raw address field");
123     }
124   }
125 
126   public static final long getAddress(final Buffer buffer) {
127     if (!buffer.isDirect()) {
128       throw new IllegalArgumentException("buffer is not direct");
129     }
130     return UNSAFE.getLong(buffer, ADDRESS_OFFSET);
131   }
132 }