Skip to content

Commit e054059

Browse files
authoredMay 1, 2024··
fix: Configure unix socket library for Graalvm (#1961)
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
1 parent c627962 commit e054059

File tree

9 files changed

+311
-3
lines changed

9 files changed

+311
-3
lines changed
 

‎core/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
<scope>test</scope>
5959
</dependency>
6060

61+
<dependency>
62+
<groupId>com.github.jnr</groupId>
63+
<artifactId>jnr-enxio</artifactId>
64+
<version>0.32.17</version>
65+
</dependency>
66+
6167
<dependency>
6268
<groupId>com.github.jnr</groupId>
6369
<artifactId>jnr-unixsocket</artifactId>

‎core/src/main/java/com/google/cloud/sql/nativeimage/CloudSqlFeature.java

+80
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
package com.google.cloud.sql.nativeimage;
1818

1919
import com.google.api.gax.nativeimage.NativeImageUtils;
20+
import java.io.IOException;
21+
import java.net.JarURLConnection;
22+
import java.net.URL;
23+
import java.net.URLConnection;
24+
import java.util.ArrayList;
25+
import java.util.Enumeration;
26+
import java.util.List;
27+
import java.util.jar.JarEntry;
28+
import java.util.jar.JarFile;
2029
import org.graalvm.nativeimage.hosted.Feature;
2130
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization;
2231
import org.graalvm.nativeimage.hosted.RuntimeReflection;
@@ -112,5 +121,76 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
112121
if (bouncyCastleAlpnSslUtils != null) {
113122
RuntimeClassInitialization.initializeAtRunTime(bouncyCastleAlpnSslUtils);
114123
}
124+
if (access.findClassByName("jnr.ffi.provider.FFIProvider") != null) {
125+
126+
// Disabling this as ASM (runtime code generation library) can sometimes cause issues during
127+
// native image build.
128+
String asmEnabledPropertyKey = "jnr.ffi.asm.enabled";
129+
if (System.getProperty(asmEnabledPropertyKey) == null) {
130+
System.setProperty(asmEnabledPropertyKey, String.valueOf(false));
131+
}
132+
133+
NativeImageUtils.registerForReflectiveInstantiation(access, "jnr.ffi.provider.jffi.Provider");
134+
RuntimeClassInitialization.initializeAtBuildTime("jnr.ffi.provider.jffi.NativeLibraryLoader");
135+
136+
// StubLoader loads the native stub library and is only intended to be called reflectively.
137+
// Note that this configuration only covers linux x86_64 platform at the moment.
138+
NativeImageUtils.registerClassForReflection(access, "com.kenai.jffi.internal.StubLoader");
139+
NativeImageUtils.registerClassForReflection(access, "com.kenai.jffi.Version");
140+
141+
NativeImageUtils.registerClassForReflection(
142+
access, "jnr.ffi.provider.jffi.platform.x86_64.linux.TypeAliases");
143+
144+
// Scan the jnr.constants.* packages and register all for reflection
145+
registerPackageForReflection(access, "jnr.constants");
146+
}
147+
}
148+
149+
/** Registers all the classes under the specified package for reflection. */
150+
public static void registerPackageForReflection(FeatureAccess access, String packageName) {
151+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
152+
153+
try {
154+
String path = packageName.replace('.', '/');
155+
156+
Enumeration<URL> resources = classLoader.getResources(path);
157+
while (resources.hasMoreElements()) {
158+
URL url = resources.nextElement();
159+
160+
URLConnection connection = url.openConnection();
161+
if (connection instanceof JarURLConnection) {
162+
List<String> classes = findClassesInJar((JarURLConnection) connection, packageName);
163+
for (String className : classes) {
164+
NativeImageUtils.registerClassHierarchyForReflection(access, className);
165+
}
166+
}
167+
}
168+
} catch (IOException e) {
169+
throw new RuntimeException("Failed to load classes under package name.", e);
170+
}
171+
}
172+
173+
private static List<String> findClassesInJar(JarURLConnection urlConnection, String packageName)
174+
throws IOException {
175+
176+
List<String> result = new ArrayList<>();
177+
178+
final JarFile jarFile = urlConnection.getJarFile();
179+
final Enumeration<JarEntry> entries = jarFile.entries();
180+
181+
while (entries.hasMoreElements()) {
182+
JarEntry entry = entries.nextElement();
183+
String entryName = entry.getName();
184+
185+
if (entryName.endsWith(".class")) {
186+
String javaClassName = entryName.replace(".class", "").replace('/', '.');
187+
188+
if (javaClassName.startsWith(packageName)) {
189+
result.add(javaClassName);
190+
}
191+
}
192+
}
193+
194+
return result;
115195
}
116196
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"name": "jnr.ffi.provider.LoadedLibrary"
4+
},
5+
{
6+
"name": "jnr.unixsocket.Native$LibC",
7+
"interfaces": [
8+
"jnr.unixsocket.Native$LibC",
9+
"jnr.ffi.provider.LoadedLibrary"
10+
]
11+
},
12+
{
13+
"name": "jnr.enxio.channels.Native$LibC",
14+
"interfaces": [
15+
"jnr.enxio.channels.Native$LibC",
16+
"jnr.ffi.provider.LoadedLibrary"
17+
]
18+
}
19+
]
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
Args =\
2+
-H:+UnlockExperimentalVMOptions \
23
-H:+AddAllCharsets \
4+
-H:+BuildReport \
5+
-H:ReflectionConfigurationResources=META-INF/native-image/com.google.cloud.sql/cloud-sql-jdbc-socket-factory-parent/jni-unix-socket-config.json \
6+
-H:ResourceConfigurationResources=META-INF/native-image/com.google.cloud.sql/cloud-sql-jdbc-socket-factory-parent/resource-config.json \
7+
-H:DynamicProxyConfigurationResources=META-INF/native-image/com.google.cloud.sql/cloud-sql-jdbc-socket-factory-parent/proxy-config.json \
38
--features=com.google.cloud.sql.nativeimage.CloudSqlFeature
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"condition": {
4+
"typeReachable": "jnr.unixsocket.Native$LibC"
5+
},
6+
"name": "jnr.unixsocket.Native$LibC",
7+
"interfaces": [
8+
"jnr.unixsocket.Native$LibC",
9+
"jnr.ffi.provider.LoadedLibrary"
10+
]
11+
},
12+
{
13+
"condition": {
14+
"typeReachable":"jnr.enxio.channels.Native$LibC"
15+
},
16+
"name": "jnr.enxio.channels.Native$LibC",
17+
"interfaces": [
18+
"jnr.enxio.channels.Native$LibC",
19+
"jnr.ffi.provider.LoadedLibrary"
20+
]
21+
}
22+
]

‎core/src/main/resources/META-INF/native-image/com.google.cloud.sql/cloud-sql-jdbc-socket-factory-parent/resource-config.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"resources":[
33
{"pattern":"\\Qcom.google.cloud.sql/project.properties\\E"},
44
{"pattern":"\\QMETA-INF/services/java.sql.Driver\\E"},
5-
{"pattern":"\\Qcom/mysql/cj/util/TimeZoneMapping.properties\\E"}
5+
{"pattern":"\\Qcom/mysql/cj/util/TimeZoneMapping.properties\\E"},
6+
{"pattern":"jni/.*\\.so"},
7+
{"pattern":"jni/.*\\.dll"}
68
],
79
"bundles": [
810
{"name":"com.mysql.cj.LocalizedErrorMessages"},

‎core/src/test/java/com/google/cloud/sql/core/ConnectorTest.java

+39
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.io.IOException;
3131
import java.io.InputStreamReader;
3232
import java.net.Socket;
33+
import java.nio.file.Files;
34+
import java.nio.file.Path;
3335
import java.time.Duration;
3436
import java.time.Instant;
3537
import java.util.Collections;
@@ -118,6 +120,43 @@ public void create_successfulPublicConnection() throws IOException, InterruptedE
118120
assertThat(readLine(socket)).isEqualTo(SERVER_MESSAGE);
119121
}
120122

123+
private boolean isWindows() {
124+
String os = System.getProperty("os.name").toLowerCase();
125+
return os.contains("win");
126+
}
127+
128+
@Test
129+
public void create_successfulUnixSocketConnection() throws IOException, InterruptedException {
130+
if (isWindows()) {
131+
System.out.println("Skipping unix socket test on Windows.");
132+
return;
133+
}
134+
135+
Path socketTestDir = Files.createTempDirectory("sockettest");
136+
Path socketPath = socketTestDir.resolve("test.sock");
137+
FakeUnixSocketServer unixSocketServer = new FakeUnixSocketServer(socketPath.toString());
138+
139+
try {
140+
141+
ConnectionConfig config =
142+
new ConnectionConfig.Builder()
143+
.withCloudSqlInstance("myProject:myRegion:myInstance")
144+
.withIpTypes("PRIMARY")
145+
.withUnixSocketPath(socketPath.toString())
146+
.build();
147+
148+
unixSocketServer.start();
149+
150+
Connector connector = newConnector(config.getConnectorConfig(), 10000);
151+
152+
Socket socket = connector.connect(config, TEST_MAX_REFRESH_MS);
153+
154+
assertThat(readLine(socket)).isEqualTo(SERVER_MESSAGE);
155+
} finally {
156+
unixSocketServer.close();
157+
}
158+
}
159+
121160
@Test
122161
public void create_successfulDomainScopedConnection() throws IOException, InterruptedException {
123162
FakeSslServer sslServer = new FakeSslServer();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.sql.core;
18+
19+
import java.io.IOException;
20+
import java.nio.ByteBuffer;
21+
import java.nio.channels.SelectionKey;
22+
import java.nio.channels.Selector;
23+
import java.util.Iterator;
24+
import java.util.Set;
25+
import java.util.concurrent.atomic.AtomicBoolean;
26+
import jnr.enxio.channels.NativeSelectorProvider;
27+
import jnr.unixsocket.UnixServerSocketChannel;
28+
import jnr.unixsocket.UnixSocketAddress;
29+
import jnr.unixsocket.UnixSocketChannel;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
33+
/**
34+
* This is a simple echo unix socket server adapted from the JNR socket project.
35+
* https://github.com/jnr/jnr-unixsocket/blob/master/src/test/java/jnr/unixsocket/example/UnixServer.java
36+
*/
37+
class FakeUnixSocketServer {
38+
private static Logger log = LoggerFactory.getLogger(FakeUnixSocketServer.class);
39+
private final String path;
40+
private final UnixSocketAddress address;
41+
private UnixServerSocketChannel channel;
42+
private final AtomicBoolean closed = new AtomicBoolean();
43+
44+
FakeUnixSocketServer(String path) {
45+
this.path = path;
46+
this.address = new UnixSocketAddress(path);
47+
}
48+
49+
public synchronized void close() throws IOException {
50+
if (!this.closed.get()) {
51+
this.closed.set(true);
52+
}
53+
if (this.channel != null) {
54+
channel.close();
55+
this.channel = null;
56+
}
57+
}
58+
59+
public synchronized void start() throws IOException {
60+
java.io.File path = new java.io.File(this.path);
61+
path.deleteOnExit();
62+
63+
this.channel = UnixServerSocketChannel.open();
64+
channel.configureBlocking(false);
65+
channel.socket().bind(address);
66+
67+
Thread t = new Thread(this::run);
68+
t.start();
69+
}
70+
71+
public void run() {
72+
log.info("Starting fake unix socket server at path " + this.path);
73+
try {
74+
Selector sel = NativeSelectorProvider.getInstance().openSelector();
75+
channel.register(sel, SelectionKey.OP_ACCEPT, new ServerActor(channel));
76+
77+
log.info("Waiting for connections path " + this.path);
78+
while (!this.closed.get()) {
79+
if (sel.select() > 0) {
80+
Set<SelectionKey> keys = sel.selectedKeys();
81+
Iterator<SelectionKey> iterator = keys.iterator();
82+
boolean running = false;
83+
boolean cancelled = false;
84+
while (iterator.hasNext()) {
85+
SelectionKey k = iterator.next();
86+
Actor a = (Actor) k.attachment();
87+
if (a.rxready()) {
88+
running = true;
89+
} else {
90+
k.cancel();
91+
cancelled = true;
92+
}
93+
iterator.remove();
94+
}
95+
if (!running && cancelled) {
96+
log.info("No Actors Running any more");
97+
break;
98+
}
99+
}
100+
}
101+
} catch (IOException ex) {
102+
log.info("IOException: waiting for sockets", ex);
103+
}
104+
log.info("UnixServer EXIT");
105+
}
106+
107+
static interface Actor {
108+
public boolean rxready();
109+
}
110+
111+
static final class ServerActor implements Actor {
112+
private final UnixServerSocketChannel channel;
113+
114+
public ServerActor(UnixServerSocketChannel channel) {
115+
this.channel = channel;
116+
}
117+
118+
@Override
119+
public final boolean rxready() {
120+
log.info("Handling unix socket connect.");
121+
try {
122+
UnixSocketChannel client = channel.accept();
123+
client.configureBlocking(false);
124+
ByteBuffer response = ByteBuffer.wrap("HELLO\n".getBytes("UTF-8"));
125+
client.write(response);
126+
log.info("Handling unix socket done.");
127+
return true;
128+
} catch (IOException ex) {
129+
return false;
130+
}
131+
}
132+
}
133+
}

‎pom.xml

+4-2
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,8 @@
569569
com.google.auth:google-auth-library-credentials
570570
571571
Necessary for Unix socket support:
572-
org.ow2.asm:asm-util AND com.github.jnr:jnr-unixsocket
572+
org.ow2.asm:asm-util AND com.github.jnr:jnr-unixsocket AND
573+
com.github.jnr:jnr-enxio
573574
574575
Necessary for Postgres support:
575576
org.postgresql:postgresql
@@ -583,7 +584,7 @@
583584
junit:junit,com.google.truth:truth,org.bouncycastle:bcpkix-jdk15on
584585
com.google.cloud.sql:jdbc-socket-factory-core:test-jar
585586
-->
586-
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
587+
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
587588
</ignoredDependencies>
588589
</configuration>
589590
<executions>
@@ -740,6 +741,7 @@
740741
<!-- Include all matching tests during native image testing -->
741742
<excludes combine.self="override"/>
742743
<includes>
744+
<include>**/ConnectorsTest</include>
743745
<include>**/*IntegrationTests</include>
744746
</includes>
745747
</configuration>

0 commit comments

Comments
 (0)
Please sign in to comment.