Skip to content

Commit cb878ca

Browse files
author
Elia Trachsel
committed
Networking Support for EspressoLibs
We added an option to disable networking on the user side. Then if we reach networking classes we always check if its enabled from the user and the Truffle side. Generally we use the host system to implement all networking functionalities by associating a fd with the appropriate channel (eg ServerSocketChannel) in TruffleIO. Then in the native methods we can use the provided fd to retrieve the corresponding host channel to implement the semantics.
1 parent 684c823 commit cb878ca

17 files changed

+1859
-24
lines changed

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/EspressoLanguage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public final class EspressoLanguage extends TruffleLanguage<EspressoContext> imp
148148
@CompilationFinal private boolean eagerFrameAnalysis;
149149
@CompilationFinal private boolean internalJvmciEnabled;
150150
@CompilationFinal private boolean useEspressoLibs;
151+
@CompilationFinal private boolean enableNetworking;
151152
@CompilationFinal private boolean continuum;
152153
@CompilationFinal private String nativeBackendId;
153154
@CompilationFinal private boolean useTRegex;

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/descriptors/EspressoSymbols.java

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public static void ensureInitialized() {
131131
public static final Symbol<Type> java_nio_file_AtomicMoveNotSupportedException = SYMBOLS.putType("Ljava/nio/file/AtomicMoveNotSupportedException;");
132132
public static final Symbol<Type> java_nio_file_AccessDeniedException = SYMBOLS.putType("Ljava/nio/file/AccessDeniedException;");
133133
public static final Symbol<Type> java_nio_file_NoSuchFileException = SYMBOLS.putType("Ljava/nio/file/NoSuchFileException;");
134+
public static final Symbol<Type> java_net_SocketException = SYMBOLS.putType("Ljava/net/SocketException;");
134135
public static final Symbol<Type> java_nio_file_InvalidPathException = SYMBOLS.putType("Ljava/nio/file/InvalidPathException;");
135136
public static final Symbol<Type> java_nio_file_NotDirectoryException = SYMBOLS.putType("Ljava/nio/file/NotDirectoryException;");
136137

@@ -163,10 +164,27 @@ public static void ensureInitialized() {
163164
public static final Symbol<Type> java_lang_module_ModuleDescriptor = SYMBOLS.putType("Ljava/lang/module/ModuleDescriptor;");
164165

165166
// Espresso Libs
167+
// libzip
166168
public static final Symbol<Type> java_util_zip_CRC32 = SYMBOLS.putType("Ljava/util/zip/CRC32;");
167169
public static final Symbol<Type> java_util_zip_Inflater = SYMBOLS.putType("Ljava/util/zip/Inflater;");
168170
public static final Symbol<Type> java_util_zip_DataFormatException = SYMBOLS.putType("Ljava/util/zip/DataFormatException;");
169-
171+
// libnet
172+
public static final Symbol<Type> java_net_NetworkInterface = SYMBOLS.putType("Ljava/net/NetworkInterface;");
173+
public static final Symbol<Type> java_net_NetworkInterface_array = SYMBOLS.putType("[Ljava/net/NetworkInterface;");
174+
public static final Symbol<Type> java_net_InetSocketAddress = SYMBOLS.putType("Ljava/net/InetSocketAddress;");
175+
public static final Symbol<Type> java_net_InetAddress = SYMBOLS.putType("Ljava/net/InetAddress;");
176+
public static final Symbol<Type> java_net_InetAddress$InetAddressHolder = SYMBOLS.putType("Ljava/net/InetAddress$InetAddressHolder;");
177+
public static final Symbol<Type> java_net_InterfaceAddress = SYMBOLS.putType("Ljava/net/InterfaceAddress;");
178+
public static final Symbol<Type> java_net_InterfaceAddress_array = SYMBOLS.putType("[Ljava/net/InterfaceAddress;");
179+
public static final Symbol<Type> java_net_Inet4Address = SYMBOLS.putType("Ljava/net/Inet4Address;");
180+
public static final Symbol<Type> java_net_Inet6Address = SYMBOLS.putType("Ljava/net/Inet6Address;");
181+
public static final Symbol<Type> java_net_Inet6Address$Inet6AddressHolder = SYMBOLS.putType("Ljava/net/Inet6Address$Inet6AddressHolder;");
182+
public static final Symbol<Type> java_net_InetAddress_array = SYMBOLS.putType("[Ljava/net/InetAddress;");
183+
public static final Symbol<Type> sun_net_ConnectionResetException = SYMBOLS.putType("Lsun/net/ConnectionResetException;");
184+
public static final Symbol<Type> java_net_UnknownHostException = SYMBOLS.putType("Ljava/net/UnknownHostException;");
185+
public static final Symbol<Type> sun_nio_ch_IOStatus = SYMBOLS.putType("Lsun/nio/ch/IOStatus;");
186+
public static final Symbol<Type> java_net_spi_InetAddressResolver$LookupPolicy = SYMBOLS.putType("Ljava/net/spi/InetAddressResolver$LookupPolicy;");
187+
public static final Symbol<Type> sun_nio_ch_Net = SYMBOLS.putType("Lsun/nio/ch/Net;");
170188
// URL class loader
171189
public static final Symbol<Type> java_net_URLClassLoader = SYMBOLS.putType("Ljava/net/URLClassLoader;");
172190
public static final Symbol<Type> java_net_URL = SYMBOLS.putType("Ljava/net/URL;");
@@ -777,6 +795,34 @@ public static class Names {
777795
public static final Symbol<Name> HIDDEN_CRC32 = SYMBOLS.putName("0HIDDEN_CRC32");
778796
public static final Symbol<Name> inputConsumed = SYMBOLS.putName("inputConsumed");
779797
public static final Symbol<Name> outputConsumed = SYMBOLS.putName("outputConsumed");
798+
// java.net
799+
public static final Symbol<Name> displayName = SYMBOLS.putName("displayName");
800+
public static final Symbol<Name> virtual = SYMBOLS.putName("virtual");
801+
public static final Symbol<Name> bindings = SYMBOLS.putName("bindings");
802+
public static final Symbol<Name> childs = SYMBOLS.putName("childs");
803+
public static final Symbol<Name> scope_ifname = SYMBOLS.putName("scope_ifname");
804+
public static final Symbol<Name> hostName = SYMBOLS.putName("hostName");
805+
public static final Symbol<Name> holder6 = SYMBOLS.putName("holder6");
806+
public static final Symbol<Name> ipaddress = SYMBOLS.putName("ipaddress");
807+
public static final Symbol<Name> scope_id = SYMBOLS.putName("scope_id");
808+
public static final Symbol<Name> maskLength = SYMBOLS.putName("maskLength");
809+
public static final Symbol<Name> broadcast = SYMBOLS.putName("broadcast");
810+
// sun.nio.ch.IOStatus
811+
public static final Symbol<Name> EOF = SYMBOLS.putName("EOF");
812+
public static final Symbol<Name> UNAVAILABLE = SYMBOLS.putName("UNAVAILABLE");
813+
public static final Symbol<Name> INTERRUPTED = SYMBOLS.putName("INTERRUPTED");
814+
public static final Symbol<Name> UNSUPPORTED = SYMBOLS.putName("UNSUPPORTED");
815+
public static final Symbol<Name> THROWN = SYMBOLS.putName("THROWN");
816+
public static final Symbol<Name> UNSUPPORTED_CASE = SYMBOLS.putName("UNSUPPORTED_CASE");
817+
// java.net.spi.InetAddressResolver.LookupPolicy
818+
public static final Symbol<Name> IPV4 = SYMBOLS.putName("IPV4");
819+
public static final Symbol<Name> IPV6 = SYMBOLS.putName("IPV6");
820+
public static final Symbol<Name> IPV4_FIRST = SYMBOLS.putName("IPV4_FIRST");
821+
public static final Symbol<Name> IPV6_FIRST = SYMBOLS.putName("IPV6_FIRST");
822+
// sun.nio.ch.Net
823+
public static final Symbol<Name> SHUT_RD = SYMBOLS.putName("SHUT_RD");
824+
public static final Symbol<Name> SHUT_WR = SYMBOLS.putName("SHUT_WR");
825+
public static final Symbol<Name> SHUT_RDWR = SYMBOLS.putName("SHUT_RDWR");
780826
// java.lang.invoke.*
781827
// CallSite
782828
public static final Symbol<Name> target = SYMBOLS.putName("target");
@@ -1457,6 +1503,24 @@ public static class Signatures {
14571503
Types._int,
14581504
Types.java_lang_Object);
14591505

1506+
public static final Symbol<Signature> java_net_NetworkInterface_init_signature = SYMBOLS.putSignature(Types._void,
1507+
/* name */ Types.java_lang_String,
1508+
/* index */ Types._int,
1509+
/* addrs */ Types.java_net_InetAddress_array);
1510+
1511+
public static final Symbol<Signature> java_net_Inet4Address_init_signature = SYMBOLS.putSignature(Types._void,
1512+
/* hostName */ Types.java_lang_String,
1513+
/* address */ Types._byte_array);
1514+
1515+
public static final Symbol<Signature> java_net_Inet6Address_init_signature = SYMBOLS.putSignature(Types._void,
1516+
/* hostName */ Types.java_lang_String,
1517+
/* address */ Types._byte_array,
1518+
/* scopeId */ Types._int);
1519+
1520+
public static final Symbol<Signature> java_net_InetSocketAddress_init_signature = SYMBOLS.putSignature(Types._void,
1521+
/* addr */ Types.java_net_InetAddress,
1522+
/* port */ Types._int);
1523+
14601524
public static void ensureInitialized() {
14611525
assert _void == ParserSymbols.ParserSignatures._void;
14621526
}

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/ffi/EspressoLibsNativeAccess.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@ public void unloadLibrary(@Pointer TruffleObject library) {
114114
getLogger().fine(() -> "Failed to locate symbol '" + symbolName + "' in espresso lib " + lib.name());
115115
} else {
116116
// Delegate library
117-
getLogger().fine(() -> "Espresso libs delegating for: " + symbolName);
118-
return delegate.lookupSymbol(library, symbolName);
117+
TruffleObject ret = delegate.lookupSymbol(library, symbolName);
118+
if (ret != null) {
119+
getLogger().fine(() -> "Found: " + symbolName + " through delegate library");
120+
}
121+
return ret;
119122
}
120123
return null;
121124
}

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/io/Throw.java

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
package com.oracle.truffle.espresso.io;
2424

2525
import java.io.IOException;
26+
import java.net.SocketException;
27+
import java.net.UnknownHostException;
2628
import java.nio.channels.AsynchronousCloseException;
2729
import java.nio.channels.ClosedByInterruptException;
2830
import java.nio.channels.ClosedChannelException;
@@ -60,19 +62,27 @@ public static EspressoException throwIOException(String message, EspressoContext
6062
throw context.getMeta().throwExceptionWithMessage(context.getTruffleIO().java_io_IOException, message);
6163
}
6264

65+
public static EspressoException throwSocketException(String message, EspressoContext context) {
66+
throw context.getMeta().throwExceptionWithMessage(context.getTruffleIO().java_net_SocketException, message);
67+
}
68+
69+
public static EspressoException throwSocketException(SocketException e, EspressoContext context) {
70+
throw throwSocketException(getMessageBoundary(e), context);
71+
}
72+
6373
public static EspressoException throwIOException(IOException e, EspressoContext context) {
6474
Class<?> exceptionClass = e.getClass();
6575
String message = getMessageBoundary(e);
6676
if (exceptionClass == ClosedByInterruptException.class) {
67-
throw throwClosedByInterruptException(message, context);
77+
throw throwClosedByInterruptException(context);
6878
}
6979

7080
if (exceptionClass == AsynchronousCloseException.class) {
71-
throw throwAsynchronousCloseException(message, context);
81+
throw throwAsynchronousCloseException(context);
7282
}
7383

7484
if (exceptionClass == ClosedChannelException.class) {
75-
throw throwClosedChannelException(message, context);
85+
throw throwClosedChannelException(context);
7686
}
7787

7888
if (exceptionClass == FileAlreadyExistsException.class) {
@@ -103,22 +113,38 @@ public static EspressoException throwIOException(IOException e, EspressoContext
103113
throw throwNotDirectoryException(message, context);
104114
}
105115

116+
if (exceptionClass == UnknownHostException.class) {
117+
throw throwUnknownHostException(message, context);
118+
}
119+
120+
if (exceptionClass == SocketException.class) {
121+
throw throwSocketException(message, context);
122+
}
123+
124+
if (isConnectionResetException(e)) {
125+
throw throwConnectionResetException(message, context);
126+
}
127+
106128
if (exceptionClass != IOException.class) {
107129
context.getLogger().warning(() -> "Not exact translation of IOException: " + exceptionClass);
108130
}
109131
throw throwIOException(message, context);
110132
}
111133

112-
public static EspressoException throwClosedByInterruptException(String message, EspressoContext context) {
113-
throw context.getMeta().throwExceptionWithMessage(context.getTruffleIO().java_nio_channels_ClosedByInterruptException, message);
134+
public static EspressoException throwClosedByInterruptException(EspressoContext context) {
135+
throw context.getMeta().throwException(context.getTruffleIO().java_nio_channels_ClosedByInterruptException);
136+
}
137+
138+
public static EspressoException throwAsynchronousCloseException(EspressoContext context) {
139+
throw context.getMeta().throwException(context.getTruffleIO().java_nio_channels_AsynchronousCloseException);
114140
}
115141

116-
public static EspressoException throwAsynchronousCloseException(String message, EspressoContext context) {
117-
throw context.getMeta().throwExceptionWithMessage(context.getTruffleIO().java_nio_channels_AsynchronousCloseException, message);
142+
public static EspressoException throwClosedChannelException(EspressoContext context) {
143+
throw context.getMeta().throwException(context.getTruffleIO().java_nio_channels_ClosedChannelException);
118144
}
119145

120-
public static EspressoException throwClosedChannelException(String message, EspressoContext context) {
121-
throw context.getMeta().throwExceptionWithMessage(context.getTruffleIO().java_nio_channels_ClosedChannelException, message);
146+
private static boolean isConnectionResetException(IOException e) {
147+
return e.getClass() == SocketException.class && (getMessageBoundary(e).equals("Connection reset"));
122148
}
123149

124150
public static EspressoException throwNonReadable(EspressoContext context) {
@@ -186,4 +212,14 @@ public static EspressoException throwNotLinkException(String message, EspressoCo
186212
Meta meta = context.getMeta();
187213
throw meta.throwExceptionWithMessage(meta.java_nio_file_NotLinkException, message);
188214
}
215+
216+
public static EspressoException throwConnectionResetException(String message, EspressoContext context) {
217+
Meta meta = context.getMeta();
218+
return meta.throwExceptionWithMessage(context.getTruffleIO().sun_net_ConnectionResetException, message);
219+
}
220+
221+
public static EspressoException throwUnknownHostException(String message, EspressoContext context) {
222+
Meta meta = context.getMeta();
223+
return meta.throwExceptionWithMessage(context.getTruffleIO().java_net_UnknownHostException, message);
224+
}
189225
}

0 commit comments

Comments
 (0)