Skip to content

Commit 7ecaa40

Browse files
committed
Load posix struct sizes/offsets from the native library
1 parent eb1c4d2 commit 7ecaa40

File tree

15 files changed

+438
-292
lines changed

15 files changed

+438
-292
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/SocketTests.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import static org.junit.Assert.assertThat;
8686
import static org.junit.Assert.assertTrue;
8787
import static org.junit.Assert.fail;
88+
import static org.junit.Assume.assumeNoException;
8889
import static org.junit.Assume.assumeTrue;
8990

9091
import java.io.File;
@@ -110,7 +111,6 @@
110111

111112
import com.oracle.graal.python.builtins.PythonOS;
112113
import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum;
113-
import com.oracle.graal.python.runtime.PosixConstants;
114114
import com.oracle.graal.python.runtime.PosixConstants.MandatoryIntConstant;
115115
import com.oracle.graal.python.runtime.PosixSupportLibrary;
116116
import com.oracle.graal.python.runtime.PosixSupportLibrary.AcceptResult;
@@ -122,6 +122,7 @@
122122
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet4SockAddr;
123123
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet6SockAddr;
124124
import com.oracle.graal.python.runtime.PosixSupportLibrary.InvalidAddressException;
125+
import com.oracle.graal.python.runtime.PosixSupportLibrary.InvalidUnixSocketPathException;
125126
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
126127
import com.oracle.graal.python.runtime.PosixSupportLibrary.RecvfromResult;
127128
import com.oracle.graal.python.runtime.PosixSupportLibrary.SelectResult;
@@ -469,7 +470,13 @@ public void streamWriteReadUnix() throws PosixException, IOException {
469470
srv.bind(unixSockAddr);
470471
srv.listen(5);
471472
Socket cli = new Socket(AF_UNIX.value, SOCK_STREAM.value);
472-
cli.connect(createUsa(unixSockAddr));
473+
UniversalSockAddr addr = null;
474+
try {
475+
addr = lib.createUniversalSockAddrUnix(posixSupport, unixSockAddr);
476+
} catch (InvalidUnixSocketPathException e) {
477+
assumeNoException(e);
478+
}
479+
cli.connect(addr);
473480

474481
Socket c = new Socket(srv.acceptFd(UNIX_SOCK_ADDR_UNNAMED), AF_UNIX.value, SOCK_STREAM.value);
475482

@@ -1021,7 +1028,6 @@ private byte[] getNulTerminatedTempFileName() throws IOException {
10211028
f.delete();
10221029
f.deleteOnExit();
10231030
byte[] fileNameBytes = f.getAbsolutePath().getBytes();
1024-
assumeTrue(fileNameBytes.length + 1 <= PosixConstants.SIZEOF_STRUCT_SOCKADDR_UN_SUN_PATH.value);
10251031
return Arrays.copyOf(fileNameBytes, fileNameBytes.length + 1);
10261032
}
10271033

@@ -1079,7 +1085,19 @@ private int createSocket(int family, int type, int protocol) throws PosixExcepti
10791085
}
10801086

10811087
private UniversalSockAddr createUsa(FamilySpecificSockAddr src) {
1082-
return lib.createUniversalSockAddr(posixSupport, src);
1088+
if (src instanceof Inet4SockAddr inet4SockAddr) {
1089+
return lib.createUniversalSockAddrInet4(posixSupport, inet4SockAddr);
1090+
} else if (src instanceof Inet6SockAddr inet6SockAddr) {
1091+
return lib.createUniversalSockAddrInet6(posixSupport, inet6SockAddr);
1092+
} else if (src instanceof UnixSockAddr unixSockAddr) {
1093+
try {
1094+
return lib.createUniversalSockAddrUnix(posixSupport, unixSockAddr);
1095+
} catch (InvalidUnixSocketPathException e) {
1096+
throw new RuntimeException(e);
1097+
}
1098+
} else {
1099+
throw new AssertionError("Unexpected subclass of FamilySpecificSockAddr: " + src.getClass().getName());
1100+
}
10831101
}
10841102

10851103
private int getIntSockOpt(int socket, int level, int option) throws PosixException {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SocketModuleBuiltins.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@
108108
import com.oracle.graal.python.runtime.PosixSupportLibrary;
109109
import com.oracle.graal.python.runtime.PosixSupportLibrary.AddrInfoCursor;
110110
import com.oracle.graal.python.runtime.PosixSupportLibrary.AddrInfoCursorLibrary;
111-
import com.oracle.graal.python.runtime.PosixSupportLibrary.FamilySpecificSockAddr;
112111
import com.oracle.graal.python.runtime.PosixSupportLibrary.GetAddrInfoException;
113112
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet4SockAddr;
114113
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet6SockAddr;
@@ -464,7 +463,7 @@ Object getServByPort(int port, Object protocolNameObj,
464463
try {
465464
gil.release(true);
466465
try {
467-
UniversalSockAddr addr = posixLib.createUniversalSockAddr(getPosixSupport(), new Inet4SockAddr(port, INADDR_ANY.value));
466+
UniversalSockAddr addr = posixLib.createUniversalSockAddrInet4(getPosixSupport(), new Inet4SockAddr(port, INADDR_ANY.value));
468467
int flags = 0;
469468
if (protocolName != null && equalNode.execute(protocolName, T_UDP, TS_ENCODING)) {
470469
flags |= NI_DGRAM.value;
@@ -561,19 +560,19 @@ Object getNameInfo(VirtualFrame frame, PTuple sockaddr, int flags,
561560
gil.acquire();
562561
}
563562

564-
FamilySpecificSockAddr queryAddr;
563+
UniversalSockAddr queryAddr;
565564
if (family == AF_INET.value) {
566565
if (addrLen != 2) {
567566
throw raise(OSError, ErrorMessages.IPV4_MUST_BE_2_TUPLE);
568567
}
569-
queryAddr = new Inet4SockAddr(port, sockAddrLibrary.asInet4SockAddr(resolvedAddr).getAddress());
568+
queryAddr = posixLib.createUniversalSockAddrInet4(getPosixSupport(), new Inet4SockAddr(port, sockAddrLibrary.asInet4SockAddr(resolvedAddr).getAddress()));
570569
} else if (family == AF_INET6.value) {
571-
queryAddr = new Inet6SockAddr(port, sockAddrLibrary.asInet6SockAddr(resolvedAddr).getAddress(), flowinfo, scopeid);
570+
queryAddr = posixLib.createUniversalSockAddrInet6(getPosixSupport(), new Inet6SockAddr(port, sockAddrLibrary.asInet6SockAddr(resolvedAddr).getAddress(), flowinfo, scopeid));
572571
} else {
573572
throw raise(OSError, ErrorMessages.UNKNOWN_FAMILY);
574573
}
575574

576-
Object[] getnameinfo = posixLib.getnameinfo(getPosixSupport(), posixLib.createUniversalSockAddr(getPosixSupport(), queryAddr), flags);
575+
Object[] getnameinfo = posixLib.getnameinfo(getPosixSupport(), queryAddr, flags);
577576
TruffleString host = posixLib.getPathAsString(getPosixSupport(), getnameinfo[0]);
578577
TruffleString service = posixLib.getPathAsString(getPosixSupport(), getnameinfo[1]);
579578
return factory.createTuple(new Object[]{host, service});

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/socket/SocketNodes.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
import static com.oracle.graal.python.runtime.PosixConstants.AF_UNSPEC;
5656
import static com.oracle.graal.python.runtime.PosixConstants.AI_PASSIVE;
5757
import static com.oracle.graal.python.runtime.PosixConstants.INADDR_BROADCAST;
58-
import static com.oracle.graal.python.runtime.PosixConstants.SIZEOF_STRUCT_SOCKADDR_UN_SUN_PATH;
5958
import static com.oracle.graal.python.runtime.PosixConstants.SOCK_DGRAM;
6059
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
6160
import static com.oracle.graal.python.util.PythonUtils.arrayCopyOf;
@@ -90,10 +89,12 @@
9089
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet4SockAddr;
9190
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet6SockAddr;
9291
import com.oracle.graal.python.runtime.PosixSupportLibrary.InvalidAddressException;
92+
import com.oracle.graal.python.runtime.PosixSupportLibrary.InvalidUnixSocketPathException;
9393
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
9494
import com.oracle.graal.python.runtime.PosixSupportLibrary.UniversalSockAddr;
9595
import com.oracle.graal.python.runtime.PosixSupportLibrary.UniversalSockAddrLibrary;
9696
import com.oracle.graal.python.runtime.PosixSupportLibrary.UnixSockAddr;
97+
import com.oracle.graal.python.runtime.PosixSupportLibrary.UnsupportedPosixFeatureException;
9798
import com.oracle.graal.python.runtime.PythonContext;
9899
import com.oracle.graal.python.runtime.exception.PException;
99100
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
@@ -141,7 +142,7 @@ UniversalSockAddr doInet(VirtualFrame frame, @SuppressWarnings("unused") PSocket
141142
int port = parsePort(frame, caller, asIntNode, inliningTarget, errorProfile, hostAndPort[1]);
142143
UniversalSockAddr addr = setIpAddrNode.execute(frame, host, AF_INET.value);
143144
Object posixSupport = context.getPosixSupport();
144-
return posixLib.createUniversalSockAddr(posixSupport, new Inet4SockAddr(port, sockAddrLib.asInet4SockAddr(addr).getAddress()));
145+
return posixLib.createUniversalSockAddrInet4(posixSupport, new Inet4SockAddr(port, sockAddrLib.asInet4SockAddr(addr).getAddress()));
145146
}
146147

147148
@Specialization(guards = "isInet6(socket)")
@@ -177,7 +178,7 @@ UniversalSockAddr doInet6(VirtualFrame frame, @SuppressWarnings("unused") PSocke
177178
}
178179
UniversalSockAddr addr = setIpAddrNode.execute(frame, host, AF_INET6.value);
179180
Object posixSupport = context.getPosixSupport();
180-
return posixLib.createUniversalSockAddr(posixSupport, new Inet6SockAddr(port, sockAddrLib.asInet6SockAddr(addr).getAddress(), flowinfo, scopeid));
181+
return posixLib.createUniversalSockAddrInet6(posixSupport, new Inet6SockAddr(port, sockAddrLib.asInet6SockAddr(addr).getAddress(), flowinfo, scopeid));
181182
}
182183

183184
@Specialization(guards = "isUnix(socket)")
@@ -208,15 +209,14 @@ UniversalSockAddr doUnix(VirtualFrame frame, @SuppressWarnings("unused") PSocket
208209
// not a linux "abstract" address -> needs a terminating zero
209210
path = arrayCopyOf(path, path.length + 1);
210211
}
211-
if (path.length > SIZEOF_STRUCT_SOCKADDR_UN_SUN_PATH.value) {
212-
throw raise(OSError, ErrorMessages.AF_UNIX_PATH_TOO_LONG, caller);
213-
}
214212
PythonContext context = PythonContext.get(this);
215213
Object posixSupport = context.getPosixSupport();
216214
try {
217-
return posixLib.createUniversalSockAddr(posixSupport, new UnixSockAddr(path));
218-
} catch (PosixSupportLibrary.UnsupportedPosixFeatureException e) {
215+
return posixLib.createUniversalSockAddrUnix(posixSupport, new UnixSockAddr(path));
216+
} catch (UnsupportedPosixFeatureException e) {
219217
throw raise(OSError, ErrorMessages.AF_UNIX_NOT_SUPPORTED, caller);
218+
} catch (InvalidUnixSocketPathException e) {
219+
throw raise(OSError, ErrorMessages.AF_UNIX_PATH_TOO_LONG, caller);
220220
}
221221
}
222222

@@ -297,14 +297,14 @@ UniversalSockAddr setipaddr(VirtualFrame frame, byte[] name, int family,
297297
if (family != AF_INET.value && family != AF_UNSPEC.value) {
298298
throw raise(OSError, ErrorMessages.ADDRESS_FAMILY_MISMATCHED);
299299
}
300-
return posixLib.createUniversalSockAddr(posixSupport, new Inet4SockAddr(0, INADDR_BROADCAST.value));
300+
return posixLib.createUniversalSockAddrInet4(posixSupport, new Inet4SockAddr(0, INADDR_BROADCAST.value));
301301
}
302302
/* avoid a name resolution in case of numeric address */
303303
/* check for an IPv4 address */
304304
if (family == AF_INET.value || family == AF_UNSPEC.value) {
305305
byte[] bytes = inetPtoNCachedPNode.execute(inliningTarget, posixLib, posixSupport, AF_INET.value, name);
306306
if (bytes != null) {
307-
return posixLib.createUniversalSockAddr(posixSupport, new Inet4SockAddr(0, bytes));
307+
return posixLib.createUniversalSockAddrInet4(posixSupport, new Inet4SockAddr(0, bytes));
308308
}
309309
}
310310
/*
@@ -315,7 +315,7 @@ UniversalSockAddr setipaddr(VirtualFrame frame, byte[] name, int family,
315315
if ((family == AF_INET6.value || family == AF_UNSPEC.value) && !hasScopeId(name)) {
316316
byte[] bytes = inetPtoNCachedPNode.execute(inliningTarget, posixLib, posixSupport, AF_INET6.value, name);
317317
if (bytes != null) {
318-
return posixLib.createUniversalSockAddr(posixSupport, new Inet6SockAddr(0, bytes, 0, 0));
318+
return posixLib.createUniversalSockAddrInet6(posixSupport, new Inet6SockAddr(0, bytes, 0, 0));
319319
}
320320
}
321321
/* perform a name resolution */

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/EmulatedPosixSupport.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3408,21 +3408,18 @@ static final class Item {
34083408
}
34093409

34103410
@ExportMessage
3411-
static class CreateUniversalSockAddr {
3412-
@Specialization
3413-
static UniversalSockAddr inet4(EmulatedPosixSupport receiver, Inet4SockAddr src) {
3414-
return EmulatedUniversalSockAddrImpl.inet4(src.getAddressAsBytes(), src.getPort());
3415-
}
3411+
UniversalSockAddr createUniversalSockAddrInet4(Inet4SockAddr src) {
3412+
return EmulatedUniversalSockAddrImpl.inet4(src.getAddressAsBytes(), src.getPort());
3413+
}
34163414

3417-
@Specialization
3418-
static UniversalSockAddr inet6(EmulatedPosixSupport receiver, Inet6SockAddr src) {
3419-
return EmulatedUniversalSockAddrImpl.inet6(src.getAddress(), src.getScopeId(), src.getPort());
3420-
}
3415+
@ExportMessage
3416+
UniversalSockAddr createUniversalSockAddrInet6(Inet6SockAddr src) {
3417+
return EmulatedUniversalSockAddrImpl.inet6(src.getAddress(), src.getScopeId(), src.getPort());
3418+
}
34213419

3422-
@Specialization
3423-
static UniversalSockAddr unix(EmulatedPosixSupport receiver, UnixSockAddr src) {
3424-
throw new UnsupportedPosixFeatureException("AF_UNIX cannot be emulated");
3425-
}
3420+
@ExportMessage
3421+
UniversalSockAddr createUniversalSockAddrUnix(UnixSockAddr src) {
3422+
throw new UnsupportedPosixFeatureException("AF_UNIX cannot be emulated");
34263423
}
34273424

34283425
@ExportLibrary(UniversalSockAddrLibrary.class)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/ImageBuildtimePosixSupport.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@
4545
import java.util.HashSet;
4646
import java.util.IdentityHashMap;
4747

48+
import com.oracle.graal.python.runtime.PosixSupportLibrary.InvalidUnixSocketPathException;
4849
import org.graalvm.nativeimage.ImageInfo;
4950

5051
import com.oracle.graal.python.runtime.PosixSupportLibrary.AcceptResult;
5152
import com.oracle.graal.python.runtime.PosixSupportLibrary.AddrInfoCursor;
5253
import com.oracle.graal.python.runtime.PosixSupportLibrary.Buffer;
53-
import com.oracle.graal.python.runtime.PosixSupportLibrary.FamilySpecificSockAddr;
5454
import com.oracle.graal.python.runtime.PosixSupportLibrary.GetAddrInfoException;
55+
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet4SockAddr;
56+
import com.oracle.graal.python.runtime.PosixSupportLibrary.Inet6SockAddr;
5557
import com.oracle.graal.python.runtime.PosixSupportLibrary.InvalidAddressException;
5658
import com.oracle.graal.python.runtime.PosixSupportLibrary.OpenPtyResult;
5759
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
@@ -60,6 +62,7 @@
6062
import com.oracle.graal.python.runtime.PosixSupportLibrary.SelectResult;
6163
import com.oracle.graal.python.runtime.PosixSupportLibrary.Timeval;
6264
import com.oracle.graal.python.runtime.PosixSupportLibrary.UniversalSockAddr;
65+
import com.oracle.graal.python.runtime.PosixSupportLibrary.UnixSockAddr;
6366
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6467
import com.oracle.truffle.api.TruffleLanguage.Env;
6568
import com.oracle.truffle.api.library.CachedLibrary;
@@ -974,10 +977,24 @@ final TruffleString crypt(TruffleString word, TruffleString salt,
974977
}
975978

976979
@ExportMessage
977-
final UniversalSockAddr createUniversalSockAddr(FamilySpecificSockAddr src,
980+
final UniversalSockAddr createUniversalSockAddrInet4(Inet4SockAddr src,
978981
@CachedLibrary("this.nativePosixSupport") PosixSupportLibrary nativeLib) {
979982
checkNotInImageBuildtime();
980-
return nativeLib.createUniversalSockAddr(nativePosixSupport, src);
983+
return nativeLib.createUniversalSockAddrInet4(nativePosixSupport, src);
984+
}
985+
986+
@ExportMessage
987+
final UniversalSockAddr createUniversalSockAddrInet6(Inet6SockAddr src,
988+
@CachedLibrary("this.nativePosixSupport") PosixSupportLibrary nativeLib) {
989+
checkNotInImageBuildtime();
990+
return nativeLib.createUniversalSockAddrInet6(nativePosixSupport, src);
991+
}
992+
993+
@ExportMessage
994+
final UniversalSockAddr createUniversalSockAddrUnix(UnixSockAddr src,
995+
@CachedLibrary("this.nativePosixSupport") PosixSupportLibrary nativeLib) throws InvalidUnixSocketPathException {
996+
checkNotInImageBuildtime();
997+
return nativeLib.createUniversalSockAddrUnix(nativePosixSupport, src);
981998
}
982999

9831000
@ExportMessage

0 commit comments

Comments
 (0)