Skip to content

Commit

Permalink
fix: Configure unix socket library for Graalvm (#1961)
Browse files Browse the repository at this point in the history
This is a fix for the graalvm configuration issues related to the jnr.unixsocket library.

The proxy classes are now being properly configured for reflection and dynamic proxy.
Native library files that are usually loaded as resources from jffi-1.3.13-native.jar are included in the image.

Fixes #1940
  • Loading branch information
hessjcg authored May 1, 2024
1 parent c627962 commit e054059
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 3 deletions.
6 changes: 6 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-enxio</artifactId>
<version>0.32.17</version>
</dependency>

<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-unixsocket</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
package com.google.cloud.sql.nativeimage;

import com.google.api.gax.nativeimage.NativeImageUtils;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization;
import org.graalvm.nativeimage.hosted.RuntimeReflection;
Expand Down Expand Up @@ -112,5 +121,76 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
if (bouncyCastleAlpnSslUtils != null) {
RuntimeClassInitialization.initializeAtRunTime(bouncyCastleAlpnSslUtils);
}
if (access.findClassByName("jnr.ffi.provider.FFIProvider") != null) {

// Disabling this as ASM (runtime code generation library) can sometimes cause issues during
// native image build.
String asmEnabledPropertyKey = "jnr.ffi.asm.enabled";
if (System.getProperty(asmEnabledPropertyKey) == null) {
System.setProperty(asmEnabledPropertyKey, String.valueOf(false));
}

NativeImageUtils.registerForReflectiveInstantiation(access, "jnr.ffi.provider.jffi.Provider");
RuntimeClassInitialization.initializeAtBuildTime("jnr.ffi.provider.jffi.NativeLibraryLoader");

// StubLoader loads the native stub library and is only intended to be called reflectively.
// Note that this configuration only covers linux x86_64 platform at the moment.
NativeImageUtils.registerClassForReflection(access, "com.kenai.jffi.internal.StubLoader");
NativeImageUtils.registerClassForReflection(access, "com.kenai.jffi.Version");

NativeImageUtils.registerClassForReflection(
access, "jnr.ffi.provider.jffi.platform.x86_64.linux.TypeAliases");

// Scan the jnr.constants.* packages and register all for reflection
registerPackageForReflection(access, "jnr.constants");
}
}

/** Registers all the classes under the specified package for reflection. */
public static void registerPackageForReflection(FeatureAccess access, String packageName) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

try {
String path = packageName.replace('.', '/');

Enumeration<URL> resources = classLoader.getResources(path);
while (resources.hasMoreElements()) {
URL url = resources.nextElement();

URLConnection connection = url.openConnection();
if (connection instanceof JarURLConnection) {
List<String> classes = findClassesInJar((JarURLConnection) connection, packageName);
for (String className : classes) {
NativeImageUtils.registerClassHierarchyForReflection(access, className);
}
}
}
} catch (IOException e) {
throw new RuntimeException("Failed to load classes under package name.", e);
}
}

private static List<String> findClassesInJar(JarURLConnection urlConnection, String packageName)
throws IOException {

List<String> result = new ArrayList<>();

final JarFile jarFile = urlConnection.getJarFile();
final Enumeration<JarEntry> entries = jarFile.entries();

while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String entryName = entry.getName();

if (entryName.endsWith(".class")) {
String javaClassName = entryName.replace(".class", "").replace('/', '.');

if (javaClassName.startsWith(packageName)) {
result.add(javaClassName);
}
}
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[
{
"name": "jnr.ffi.provider.LoadedLibrary"
},
{
"name": "jnr.unixsocket.Native$LibC",
"interfaces": [
"jnr.unixsocket.Native$LibC",
"jnr.ffi.provider.LoadedLibrary"
]
},
{
"name": "jnr.enxio.channels.Native$LibC",
"interfaces": [
"jnr.enxio.channels.Native$LibC",
"jnr.ffi.provider.LoadedLibrary"
]
}
]
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Args =\
-H:+UnlockExperimentalVMOptions \
-H:+AddAllCharsets \
-H:+BuildReport \
-H:ReflectionConfigurationResources=META-INF/native-image/com.google.cloud.sql/cloud-sql-jdbc-socket-factory-parent/jni-unix-socket-config.json \
-H:ResourceConfigurationResources=META-INF/native-image/com.google.cloud.sql/cloud-sql-jdbc-socket-factory-parent/resource-config.json \
-H:DynamicProxyConfigurationResources=META-INF/native-image/com.google.cloud.sql/cloud-sql-jdbc-socket-factory-parent/proxy-config.json \
--features=com.google.cloud.sql.nativeimage.CloudSqlFeature
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"condition": {
"typeReachable": "jnr.unixsocket.Native$LibC"
},
"name": "jnr.unixsocket.Native$LibC",
"interfaces": [
"jnr.unixsocket.Native$LibC",
"jnr.ffi.provider.LoadedLibrary"
]
},
{
"condition": {
"typeReachable":"jnr.enxio.channels.Native$LibC"
},
"name": "jnr.enxio.channels.Native$LibC",
"interfaces": [
"jnr.enxio.channels.Native$LibC",
"jnr.ffi.provider.LoadedLibrary"
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"resources":[
{"pattern":"\\Qcom.google.cloud.sql/project.properties\\E"},
{"pattern":"\\QMETA-INF/services/java.sql.Driver\\E"},
{"pattern":"\\Qcom/mysql/cj/util/TimeZoneMapping.properties\\E"}
{"pattern":"\\Qcom/mysql/cj/util/TimeZoneMapping.properties\\E"},
{"pattern":"jni/.*\\.so"},
{"pattern":"jni/.*\\.dll"}
],
"bundles": [
{"name":"com.mysql.cj.LocalizedErrorMessages"},
Expand Down
39 changes: 39 additions & 0 deletions core/src/test/java/com/google/cloud/sql/core/ConnectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
Expand Down Expand Up @@ -118,6 +120,43 @@ public void create_successfulPublicConnection() throws IOException, InterruptedE
assertThat(readLine(socket)).isEqualTo(SERVER_MESSAGE);
}

private boolean isWindows() {
String os = System.getProperty("os.name").toLowerCase();
return os.contains("win");
}

@Test
public void create_successfulUnixSocketConnection() throws IOException, InterruptedException {
if (isWindows()) {
System.out.println("Skipping unix socket test on Windows.");
return;
}

Path socketTestDir = Files.createTempDirectory("sockettest");
Path socketPath = socketTestDir.resolve("test.sock");
FakeUnixSocketServer unixSocketServer = new FakeUnixSocketServer(socketPath.toString());

try {

ConnectionConfig config =
new ConnectionConfig.Builder()
.withCloudSqlInstance("myProject:myRegion:myInstance")
.withIpTypes("PRIMARY")
.withUnixSocketPath(socketPath.toString())
.build();

unixSocketServer.start();

Connector connector = newConnector(config.getConnectorConfig(), 10000);

Socket socket = connector.connect(config, TEST_MAX_REFRESH_MS);

assertThat(readLine(socket)).isEqualTo(SERVER_MESSAGE);
} finally {
unixSocketServer.close();
}
}

@Test
public void create_successfulDomainScopedConnection() throws IOException, InterruptedException {
FakeSslServer sslServer = new FakeSslServer();
Expand Down
133 changes: 133 additions & 0 deletions core/src/test/java/com/google/cloud/sql/core/FakeUnixSocketServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2024 Google LLC
*
* 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.cloud.sql.core;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import jnr.enxio.channels.NativeSelectorProvider;
import jnr.unixsocket.UnixServerSocketChannel;
import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This is a simple echo unix socket server adapted from the JNR socket project.
* https://github.com/jnr/jnr-unixsocket/blob/master/src/test/java/jnr/unixsocket/example/UnixServer.java
*/
class FakeUnixSocketServer {
private static Logger log = LoggerFactory.getLogger(FakeUnixSocketServer.class);
private final String path;
private final UnixSocketAddress address;
private UnixServerSocketChannel channel;
private final AtomicBoolean closed = new AtomicBoolean();

FakeUnixSocketServer(String path) {
this.path = path;
this.address = new UnixSocketAddress(path);
}

public synchronized void close() throws IOException {
if (!this.closed.get()) {
this.closed.set(true);
}
if (this.channel != null) {
channel.close();
this.channel = null;
}
}

public synchronized void start() throws IOException {
java.io.File path = new java.io.File(this.path);
path.deleteOnExit();

this.channel = UnixServerSocketChannel.open();
channel.configureBlocking(false);
channel.socket().bind(address);

Thread t = new Thread(this::run);
t.start();
}

public void run() {
log.info("Starting fake unix socket server at path " + this.path);
try {
Selector sel = NativeSelectorProvider.getInstance().openSelector();
channel.register(sel, SelectionKey.OP_ACCEPT, new ServerActor(channel));

log.info("Waiting for connections path " + this.path);
while (!this.closed.get()) {
if (sel.select() > 0) {
Set<SelectionKey> keys = sel.selectedKeys();
Iterator<SelectionKey> iterator = keys.iterator();
boolean running = false;
boolean cancelled = false;
while (iterator.hasNext()) {
SelectionKey k = iterator.next();
Actor a = (Actor) k.attachment();
if (a.rxready()) {
running = true;
} else {
k.cancel();
cancelled = true;
}
iterator.remove();
}
if (!running && cancelled) {
log.info("No Actors Running any more");
break;
}
}
}
} catch (IOException ex) {
log.info("IOException: waiting for sockets", ex);
}
log.info("UnixServer EXIT");
}

static interface Actor {
public boolean rxready();
}

static final class ServerActor implements Actor {
private final UnixServerSocketChannel channel;

public ServerActor(UnixServerSocketChannel channel) {
this.channel = channel;
}

@Override
public final boolean rxready() {
log.info("Handling unix socket connect.");
try {
UnixSocketChannel client = channel.accept();
client.configureBlocking(false);
ByteBuffer response = ByteBuffer.wrap("HELLO\n".getBytes("UTF-8"));
client.write(response);
log.info("Handling unix socket done.");
return true;
} catch (IOException ex) {
return false;
}
}
}
}
6 changes: 4 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,8 @@
com.google.auth:google-auth-library-credentials
Necessary for Unix socket support:
org.ow2.asm:asm-util AND com.github.jnr:jnr-unixsocket
org.ow2.asm:asm-util AND com.github.jnr:jnr-unixsocket AND
com.github.jnr:jnr-enxio
Necessary for Postgres support:
org.postgresql:postgresql
Expand All @@ -583,7 +584,7 @@
junit:junit,com.google.truth:truth,org.bouncycastle:bcpkix-jdk15on
com.google.cloud.sql:jdbc-socket-factory-core:test-jar
-->
org.ow2.asm:asm-util,com.github.jnr:jnr-unixsocket,org.postgresql:postgresql,junit:junit,com.google.truth:truth,com.microsoft.sqlserver:mssql-jdbc,com.google.guava:guava,org.bouncycastle:bcpkix-jdk15on,com.google.cloud.sql:jdbc-socket-factory-core:test-jar,com.google.auth:google-auth-library-credentials
org.ow2.asm:asm-util,com.github.jnr:jnr-unixsocket,com.github.jnr:jnr-enxio,org.postgresql:postgresql,junit:junit,com.google.truth:truth,com.microsoft.sqlserver:mssql-jdbc,com.google.guava:guava,org.bouncycastle:bcpkix-jdk15on,com.google.cloud.sql:jdbc-socket-factory-core:test-jar,com.google.auth:google-auth-library-credentials
</ignoredDependencies>
</configuration>
<executions>
Expand Down Expand Up @@ -740,6 +741,7 @@
<!-- Include all matching tests during native image testing -->
<excludes combine.self="override"/>
<includes>
<include>**/ConnectorsTest</include>
<include>**/*IntegrationTests</include>
</includes>
</configuration>
Expand Down

0 comments on commit e054059

Please sign in to comment.