Skip to content

Commit 1e61667

Browse files
committed
SSLModuleBuiltins, SSLSocketBuiltins: remove unnecessary usage of cached PConstructAndRaiseNode behind TruffleBoundaries
1 parent 58353bc commit 1e61667

File tree

7 files changed

+51
-47
lines changed

7 files changed

+51
-47
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@
4646
import java.util.regex.Matcher;
4747
import java.util.regex.Pattern;
4848

49-
import com.oracle.graal.python.builtins.objects.exception.OsErrorBuiltins;
50-
import com.oracle.graal.python.builtins.objects.exception.UnicodeDecodeErrorBuiltins;
51-
import com.oracle.graal.python.builtins.objects.exception.UnicodeEncodeErrorBuiltins;
52-
import com.oracle.graal.python.builtins.objects.exception.UnicodeErrorBuiltins;
53-
import com.oracle.graal.python.builtins.objects.exception.UnicodeTranslateErrorBuiltins;
5449
import org.graalvm.nativeimage.ImageInfo;
5550

5651
import com.oracle.graal.python.PythonLanguage;
@@ -185,10 +180,15 @@
185180
import com.oracle.graal.python.builtins.objects.enumerate.EnumerateBuiltins;
186181
import com.oracle.graal.python.builtins.objects.exception.BaseExceptionBuiltins;
187182
import com.oracle.graal.python.builtins.objects.exception.ImportErrorBuiltins;
183+
import com.oracle.graal.python.builtins.objects.exception.OsErrorBuiltins;
188184
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
189185
import com.oracle.graal.python.builtins.objects.exception.StopIterationBuiltins;
190186
import com.oracle.graal.python.builtins.objects.exception.SyntaxErrorBuiltins;
191187
import com.oracle.graal.python.builtins.objects.exception.SystemExitBuiltins;
188+
import com.oracle.graal.python.builtins.objects.exception.UnicodeDecodeErrorBuiltins;
189+
import com.oracle.graal.python.builtins.objects.exception.UnicodeEncodeErrorBuiltins;
190+
import com.oracle.graal.python.builtins.objects.exception.UnicodeErrorBuiltins;
191+
import com.oracle.graal.python.builtins.objects.exception.UnicodeTranslateErrorBuiltins;
192192
import com.oracle.graal.python.builtins.objects.floats.FloatBuiltins;
193193
import com.oracle.graal.python.builtins.objects.floats.PFloat;
194194
import com.oracle.graal.python.builtins.objects.foreign.ForeignObjectBuiltins;
@@ -384,16 +384,14 @@ private static String[] initializeCoreFiles() {
384384
c = null;
385385
}
386386

387-
private static List<PythonBuiltins> filterBuiltins(List<PythonBuiltins> builtins) {
387+
private static void filterBuiltins(List<PythonBuiltins> builtins) {
388388
PythonOS currentOs = PythonOS.getPythonOS();
389-
List<PythonBuiltins> filtered = new ArrayList<>();
390389
for (PythonBuiltins builtin : builtins) {
391390
CoreFunctions annotation = builtin.getClass().getAnnotation(CoreFunctions.class);
392-
if (annotation.os() == PythonOS.PLATFORM_ANY || annotation.os() == currentOs) {
393-
filtered.add(builtin);
391+
if (annotation.os() != PythonOS.PLATFORM_ANY && annotation.os() != currentOs) {
392+
builtins.remove(builtin);
394393
}
395394
}
396-
return filtered;
397395
}
398396

399397
private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed) {
@@ -655,7 +653,7 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
655653
builtins.add(builtin);
656654
}
657655
}
658-
builtins = filterBuiltins(builtins);
656+
filterBuiltins(builtins);
659657
return builtins.toArray(new PythonBuiltins[builtins.size()]);
660658
}
661659

@@ -907,10 +905,6 @@ private PythonBuiltinClass initializeBuiltinClass(PythonBuiltinClassType type) {
907905
return builtinTypes[index];
908906
}
909907

910-
private static boolean canPublishBuiltin(PythonOS currentOs, Builtin builtin) {
911-
return builtin.os() == PythonOS.PLATFORM_ANY || builtin.os() == currentOs;
912-
}
913-
914908
private void initializeTypes() {
915909
// create class objects for builtin types
916910
for (PythonBuiltinClassType builtinClass : PythonBuiltinClassType.VALUES) {

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
9797
import com.oracle.truffle.api.dsl.NodeFactory;
9898
import com.oracle.truffle.api.dsl.Specialization;
99-
import com.oracle.truffle.api.frame.Frame;
10099
import com.oracle.truffle.api.frame.VirtualFrame;
101100

102101
@CoreFunctions(defineModule = "_ssl")
@@ -138,7 +137,7 @@ public static SSLProtocol getMaximumVersion() {
138137
static {
139138
SSLCipher[] computed;
140139
try {
141-
computed = SSLCipherSelector.selectCiphers(null, PConstructAndRaiseNode.getUncached(), null, DEFAULT_CIPHER_STRING);
140+
computed = SSLCipherSelector.selectCiphers(null, DEFAULT_CIPHER_STRING);
142141
} catch (PException e) {
143142
computed = new SSLCipher[0];
144143
}
@@ -365,31 +364,30 @@ Object getDefaultPaths() {
365364
abstract static class DecodeCertNode extends PythonUnaryBuiltinNode {
366365
@Specialization
367366
Object decode(VirtualFrame frame, Object path,
368-
@Cached PyUnicodeFSDecoderNode asPath,
369-
@Cached PConstructAndRaiseNode constructAndRaiseNode) {
370-
return decode(frame, toTruffleFile(frame, asPath, path), constructAndRaiseNode);
367+
@Cached PyUnicodeFSDecoderNode asPath) {
368+
return decode(toTruffleFile(frame, asPath, path));
371369
}
372370

373371
@TruffleBoundary
374-
private Object decode(Frame frame, TruffleFile file, PConstructAndRaiseNode constructAndRaiseNode) throws PException {
372+
private Object decode(TruffleFile file) throws PException {
375373
List<Object> l = new ArrayList<>();
376374
try (BufferedReader r = file.newBufferedReader()) {
377375
CertUtils.LoadCertError result = CertUtils.getCertificates(r, l);
378376
if (result != CertUtils.LoadCertError.NO_ERROR) {
379-
throw constructAndRaiseNode.raiseSSLError(frame, SSL_ERR_DECODING_PEM_FILE_S, result);
377+
throw PConstructAndRaiseNode.raiseUncachedSSLError(SSL_ERR_DECODING_PEM_FILE_S, result);
380378
}
381379
if (l.isEmpty()) {
382-
throw constructAndRaiseNode.raiseSSLError(frame, SSL_ERR_DECODING_PEM_FILE);
380+
throw PConstructAndRaiseNode.raiseUncachedSSLError(SSL_ERR_DECODING_PEM_FILE);
383381
}
384382
Object cert = l.get(0);
385383
if (!(cert instanceof X509Certificate)) {
386-
throw constructAndRaiseNode.raiseSSLError(frame, SSL_ERR_DECODING_PEM_FILE_UNEXPECTED_S, cert.getClass().getName());
384+
throw PConstructAndRaiseNode.raiseUncachedSSLError(SSL_ERR_DECODING_PEM_FILE_UNEXPECTED_S, cert.getClass().getName());
387385
}
388-
return CertUtils.decodeCertificate(frame, constructAndRaiseNode, getContext().factory(), (X509Certificate) l.get(0));
386+
return CertUtils.decodeCertificate(getContext().factory(), (X509Certificate) l.get(0));
389387
} catch (IOException ex) {
390-
throw constructAndRaiseNode.raiseSSLError(frame, SSL_CANT_OPEN_FILE_S, ex.toString());
388+
throw PConstructAndRaiseNode.raiseUncachedSSLError(SSL_CANT_OPEN_FILE_S, ex.toString());
391389
} catch (CertificateException | CRLException ex) {
392-
throw constructAndRaiseNode.raiseSSLError(frame, SSL_ERR_DECODING_PEM_FILE_S, ex.toString());
390+
throw PConstructAndRaiseNode.raiseUncachedSSLError(SSL_ERR_DECODING_PEM_FILE_S, ex.toString());
393391
}
394392
}
395393

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ssl/CertUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static boolean isCrl(boolean[] keyUsage) {
162162
* _ssl.c#_decode_certificate
163163
*/
164164
@TruffleBoundary
165-
public static PDict decodeCertificate(Frame frame, PConstructAndRaiseNode constructAndRaiseNode, PythonObjectSlowPathFactory factory, X509Certificate cert) throws CertificateParsingException {
165+
public static PDict decodeCertificate(PythonObjectSlowPathFactory factory, X509Certificate cert) throws CertificateParsingException {
166166
PDict dict = factory.createDict();
167167
HashingStorage storage = dict.getDictStorage();
168168
HashingStorageLibrary hlib = HashingStorageLibrary.getUncached();
@@ -178,7 +178,7 @@ public static PDict decodeCertificate(Frame frame, PConstructAndRaiseNode constr
178178
storage = setItem(hlib, storage, JAVA_X509_SUBJECT_ALT_NAME, parseSubjectAltName(cert, factory));
179179
storage = setItem(hlib, storage, JAVA_X509_VERSION, getVersion(cert));
180180
} catch (RuntimeException re) {
181-
throw constructAndRaiseNode.raiseSSLError(frame, SSLErrorCode.ERROR_SSL, re);
181+
throw PConstructAndRaiseNode.raiseUncachedSSLError(SSLErrorCode.ERROR_SSL, re);
182182
}
183183
dict.setDictStorage(storage);
184184
return dict;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ssl/SSLCipherSelector.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import com.oracle.graal.python.nodes.PConstructAndRaiseNode;
5555
import com.oracle.graal.python.nodes.PRaiseNode;
5656
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
57-
import com.oracle.truffle.api.frame.Frame;
5857
import com.oracle.truffle.api.nodes.Node;
5958

6059
public class SSLCipherSelector {
@@ -63,16 +62,16 @@ public class SSLCipherSelector {
6362
SSLCipher.TLS_AES_128_GCM_SHA256, SSLCipher.TLS_AES_128_CCM_SHA256};
6463

6564
@TruffleBoundary
66-
public static SSLCipher[] selectCiphers(Frame frame, PConstructAndRaiseNode constructAndRaiseNode, Node node, String cipherList) {
65+
public static SSLCipher[] selectCiphers(Node node, String cipherList) {
6766
List<SSLCipher> selected = new LinkedList<>();
6867
Set<SSLCipher> deleted = new HashSet<>();
6968
// Handle ciphersuites for TLS version <= 1.2. TLSv1.3 ciphersuites are handled
7069
// separately.
71-
selectCiphersFromList(frame, constructAndRaiseNode, node, cipherList, selected, deleted);
70+
selectCiphersFromList(node, cipherList, selected, deleted);
7271
// The call fails when no <= TLSv1.2 ciphersuites get selected, regardless of TLSv1.3
7372
// ciphersuites
7473
if (selected.size() == 0) {
75-
throw constructAndRaiseNode.raiseSSLError(frame, ErrorMessages.NO_CIPHER_CAN_BE_SELECTED);
74+
throw PConstructAndRaiseNode.raiseUncachedSSLError(ErrorMessages.NO_CIPHER_CAN_BE_SELECTED);
7675
}
7776
// The API that CPython uses is meant only for setting <= TLSv1.2 ciphersuites, but it
7877
// also unconditionally adds a hardcoded list of TLSv1.3 ciphersuites to the beginning
@@ -87,13 +86,13 @@ public static SSLCipher[] selectCiphers(Frame frame, PConstructAndRaiseNode cons
8786
return result;
8887
}
8988

90-
private static void selectCiphersFromList(Frame frame, PConstructAndRaiseNode constructAndRaiseNode, Node node, String cipherList, List<SSLCipher> selected, Set<SSLCipher> deleted) {
89+
private static void selectCiphersFromList(Node node, String cipherList, List<SSLCipher> selected, Set<SSLCipher> deleted) {
9190
for (String cipherString : cipherList.split("[:, ]")) {
92-
selectSingle(frame, constructAndRaiseNode, node, cipherString, selected, deleted);
91+
selectSingle(node, cipherString, selected, deleted);
9392
}
9493
}
9594

96-
private static void selectSingle(Frame frame, PConstructAndRaiseNode constructAndRaiseNode, Node node, String cipherString, List<SSLCipher> selected, Set<SSLCipher> deleted) {
95+
private static void selectSingle(Node node, String cipherString, List<SSLCipher> selected, Set<SSLCipher> deleted) {
9796
if (cipherString.startsWith("!")) {
9897
// Remove the ciphers from the list and prevent them from reappearing
9998
List<SSLCipher> ciphers = getCiphersForCipherString(node, cipherString.substring(1));
@@ -113,10 +112,10 @@ private static void selectSingle(Frame frame, PConstructAndRaiseNode constructAn
113112
} else if (cipherString.startsWith("@SECLEVEL=")) {
114113
throw PRaiseNode.raiseUncached(node, NotImplementedError, "@SECLEVEL not implemented");
115114
} else {
116-
throw constructAndRaiseNode.raiseSSLError(frame, ErrorMessages.NO_CIPHER_CAN_BE_SELECTED);
115+
throw PConstructAndRaiseNode.raiseUncachedSSLError(ErrorMessages.NO_CIPHER_CAN_BE_SELECTED);
117116
}
118117
} else if (cipherString.equals("DEFAULT")) {
119-
selectCiphersFromList(frame, constructAndRaiseNode, node, "ALL:!COMPLEMENTOFDEFAULT:!eNULL", selected, deleted);
118+
selectCiphersFromList(node, "ALL:!COMPLEMENTOFDEFAULT:!eNULL", selected, deleted);
120119
} else {
121120
List<SSLCipher> ciphers = getCiphersForCipherString(node, cipherString);
122121
for (SSLCipher cipher : ciphers) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ssl/SSLContextBuiltins.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -485,11 +485,9 @@ PList getCiphers(PSSLContext self) {
485485
@ArgumentClinic(name = "cipherlist", conversion = ArgumentClinic.ClinicConversion.String)
486486
@GenerateNodeFactory
487487
abstract static class SetCiphersNode extends PythonClinicBuiltinNode {
488-
489488
@Specialization
490-
Object setCiphers(VirtualFrame frame, PSSLContext self, String cipherlist,
491-
@Cached PConstructAndRaiseNode constructAndRaiseNode) {
492-
self.setCiphers(SSLCipherSelector.selectCiphers(frame, constructAndRaiseNode, this, cipherlist));
489+
Object setCiphers(PSSLContext self, String cipherlist) {
490+
self.setCiphers(SSLCipherSelector.selectCiphers(this, cipherlist));
493491
return PNone.NONE;
494492
}
495493

@@ -727,7 +725,7 @@ private TruffleFile toTruffleFile(VirtualFrame frame, PyUnicodeFSDecoderNode asP
727725
}
728726
}
729727

730-
private void fromString(Frame frame, PConstructAndRaiseNode constructAndRaiseNode, String dataString, PSSLContext context)
728+
private void fromString(VirtualFrame frame, PConstructAndRaiseNode constructAndRaiseNode, String dataString, PSSLContext context)
731729
throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException, CRLException {
732730
if (dataString.isEmpty()) {
733731
throw raise(ValueError, ErrorMessages.EMPTY_CERTIFICATE_DATA);
@@ -967,7 +965,7 @@ Object getCerts(VirtualFrame frame, PSSLContext self, @SuppressWarnings("unused"
967965
List<PDict> result = PythonUtils.newList();
968966
for (X509Certificate cert : self.getCACerts()) {
969967
if (CertUtils.isCA(cert, CertUtils.getKeyUsage(cert))) {
970-
PythonUtils.add(result, CertUtils.decodeCertificate(frame, constructAndRaiseNode, getContext().factory(), cert));
968+
PythonUtils.add(result, CertUtils.decodeCertificate(getContext().factory(), cert));
971969
}
972970
}
973971
return factory().createList(PythonUtils.toArray(result));

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ssl/SSLSocketBuiltins.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,14 @@ Object getPeerCertDER(PSSLSocket self, @SuppressWarnings("unused") boolean der)
310310
}
311311

312312
@Specialization(guards = "!der")
313-
PDict getPeerCertDict(VirtualFrame frame, PSSLSocket self, @SuppressWarnings("unused") boolean der,
314-
@Cached PConstructAndRaiseNode constructAndRaiseNode) {
313+
PDict getPeerCertDict(PSSLSocket self, @SuppressWarnings("unused") boolean der) {
315314
if (!self.isHandshakeComplete()) {
316315
throw raise(ValueError, ErrorMessages.HANDSHAKE_NOT_DONE_YET);
317316
}
318317
Certificate certificate = getCertificate(self.getEngine());
319318
if (certificate instanceof X509Certificate) {
320319
try {
321-
return CertUtils.decodeCertificate(frame, constructAndRaiseNode, getContext().factory(), (X509Certificate) certificate);
320+
return CertUtils.decodeCertificate(getContext().factory(), (X509Certificate) certificate);
322321
} catch (CertificateParsingException e) {
323322
return factory().createDict();
324323
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PConstructAndRaiseNode.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,22 @@ public PException raiseSSLError(Frame frame, SSLErrorCode errorCode, String form
254254
}
255255
}
256256

257+
public static PException raiseUncachedSSLError(String message) {
258+
return getUncached().raiseSSLError(null, message);
259+
}
260+
261+
public static PException raiseUncachedSSLError(String message, Object... formatArgs) {
262+
return getUncached().raiseSSLError(null, message, formatArgs);
263+
}
264+
265+
public static PException raiseUncachedSSLError(SSLErrorCode errorCode, Exception ex) {
266+
return getUncached().raiseSSLError(null, errorCode, ex);
267+
}
268+
269+
public static PException raiseUncachedSSLError(SSLErrorCode errorCode, String format, Object... formatArgs) {
270+
return getUncached().raiseSSLError(null, errorCode, format, formatArgs);
271+
}
272+
257273
public final PException raiseOSErrorSubType(Frame frame, PythonBuiltinClassType osErrorSubtype, String format, Object... fmtArgs) {
258274
String message = getFormattedMessage(format, fmtArgs);
259275
final OSErrorEnum osErrorEnum = errorType2errno(osErrorSubtype);

0 commit comments

Comments
 (0)