From 41cd1f7a88c2d8970ea8b256d22bccbe247c292e Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Fri, 9 Jun 2023 22:07:31 +0200 Subject: [PATCH] Work around SecurityManager issues during initialization of vector api (JDK-8309727) (#12362) --- .../lucene/util/VectorUtilPanamaProvider.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lucene/core/src/java20/org/apache/lucene/util/VectorUtilPanamaProvider.java b/lucene/core/src/java20/org/apache/lucene/util/VectorUtilPanamaProvider.java index 2d8a2238008..fd599c232fb 100644 --- a/lucene/core/src/java20/org/apache/lucene/util/VectorUtilPanamaProvider.java +++ b/lucene/core/src/java20/org/apache/lucene/util/VectorUtilPanamaProvider.java @@ -16,6 +16,8 @@ */ package org.apache.lucene.util; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.logging.Logger; import jdk.incubator.vector.ByteVector; import jdk.incubator.vector.FloatVector; @@ -29,11 +31,7 @@ /** A VectorUtil provider implementation that leverages the Panama Vector API. */ final class VectorUtilPanamaProvider implements VectorUtilProvider { - /** - * The bit size of the preferred species (this field is package private to allow the lookup to - * load it). - */ - static final int INT_SPECIES_PREF_BIT_SIZE = IntVector.SPECIES_PREFERRED.vectorBitSize(); + private static final int INT_SPECIES_PREF_BIT_SIZE = IntVector.SPECIES_PREFERRED.vectorBitSize(); private static final VectorSpecies PREF_FLOAT_SPECIES = FloatVector.SPECIES_PREFERRED; private static final VectorSpecies PREF_BYTE_SPECIES; @@ -62,11 +60,29 @@ final class VectorUtilPanamaProvider implements VectorUtilProvider { } } + // Extracted to a method to be able to apply the SuppressForbidden annotation + @SuppressWarnings("removal") + @SuppressForbidden(reason = "security manager") + private static T doPrivileged(PrivilegedAction action) { + return AccessController.doPrivileged(action); + } + VectorUtilPanamaProvider() { if (INT_SPECIES_PREF_BIT_SIZE < 128) { throw new UnsupportedOperationException( "Vector bit size is less than 128: " + INT_SPECIES_PREF_BIT_SIZE); } + + // hack to work around for JDK-8309727: + try { + doPrivileged( + () -> + FloatVector.fromArray(PREF_FLOAT_SPECIES, new float[PREF_FLOAT_SPECIES.length()], 0)); + } catch (SecurityException se) { + throw new UnsupportedOperationException( + "We hit initialization failure described in JDK-8309727: " + se); + } + var log = Logger.getLogger(getClass().getName()); log.info( "Java vector incubator API enabled; uses preferredBitSize=" + INT_SPECIES_PREF_BIT_SIZE);