Skip to content

Refactoring session management - #172

Merged
nmittler merged 5 commits into
google:masterfrom
nmittler:my-session-refactor
Jun 19, 2017
Merged

nmittler merged 5 commits into
google:masterfrom
nmittler:my-session-refactor

Conversation

@nmittler

@nmittler nmittler commented Apr 16, 2017 •

Copy link
Copy Markdown
Contributor

This change breaks session management into two distinct types:

  • SslSessionWrapper: These are created as BoringSSL calls back the new session handler, allowing the application to cache sessions. Clients will also offer these to BoringSSL for reuse if a compatible session was found. BoringSSL is free to use it or not, but the Conscrypt code no longer makes assumptions here. Instead, it always uses the ActiveSession.

  • ActiveSession: This is a session that wraps the SSL instance (rather than the SSL_SESSION wherever possible). That way no concern has to be paid to what BoringSSL is doing with sessions under the covers.

Fixes #98

@nmittler nmittler added this to the 1.0.0 milestone Apr 16, 2017
@nmittler
nmittler requested review from davidben and kruton April 16, 2017 00:40
@nmittler

Copy link
Copy Markdown
Contributor Author

@kruton I'm handing off to you. This likely needs more testing.

@davidben can you take a glance to see if this generally addresses your concerns?

@nmittler

Copy link
Copy Markdown
Contributor Author

@kruton FYI I have done some work on porting over the one of the integration tests as well. We'll probably want to get that working here as well. nmittler@8d40a89


const SSL_CIPHER* cipher = ssl_session->cipher;
const char* name = SSL_CIPHER_get_name(cipher);
JNI_TRACE("ssl_session=%p NativeCrypto_SSL_cipher => %s", ssl_session, name);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole point was to avoid SSL_get_session and direct accesses of SSL_SESSION fields. I meant in the C++ code and the Java code. If you need to add a call to SSL_get_session, there is probably something wrong. If you need to reach into a field directly, there is also probably something wrong.

Use BoringSSL's public APIs. Don't reach into library internals. This should be SSL_get_current_cipher.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed ... I wasn't sure what the right method was here. Done.

}

JNI_TRACE("ssl_session=%p NativeCrypto_get_SSL_tlsext_hostname => \"%s\"",
ssl_session, ssl_session->tlsext_hostname);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. SSL_get_servername.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_cipher, "(J)Ljava/lang/String;"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, get_SSL_tlsext_hostname, "(J)Ljava/lang/String;"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_get_version, "(J)Ljava/lang/String;"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_cipher, "(J)Ljava/lang/String;"),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The end result of this change should be that many JNI accessors go away because they were incorrect.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. ATM this change only affects the socket. Before submitting, the engine class should also be changed so that the old methods are no longer used (and can therefore be removed).

* (i.e. {@link #newInstance(byte[], String, int, AbstractSessionContext)}) and offered to
* another connection, it will have all of the information needed to resume the session.
*/
final class CacheableSession implements SSLSession {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be an SSLSession at all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't disagree.

@kruton WDYT?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that seems like it would simplify this section of the code. It never escapes, so it should be a simple change correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

* Serializes the native state of the session (ID, cipher, and keys but
* not certificates). Returns a byte[] containing the DER-encoded state.
* See apache mod_ssl.
*/

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not appear to be used (and is a rather disturbing sounding function). You're not serializing an SSL

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@davidben

Copy link
Copy Markdown
Collaborator

I didn't do a complete review, just checked on a few things to see if the refactor did what #98 was about.

@nmittler nmittler left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidben PTAL

}

JNI_TRACE("ssl_session=%p NativeCrypto_get_SSL_tlsext_hostname => \"%s\"",
ssl_session, ssl_session->tlsext_hostname);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


const SSL_CIPHER* cipher = ssl_session->cipher;
const char* name = SSL_CIPHER_get_name(cipher);
JNI_TRACE("ssl_session=%p NativeCrypto_SSL_cipher => %s", ssl_session, name);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed ... I wasn't sure what the right method was here. Done.

* Serializes the native state of the session (ID, cipher, and keys but
* not certificates). Returns a byte[] containing the DER-encoded state.
* See apache mod_ssl.
*/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_cipher, "(J)Ljava/lang/String;"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, get_SSL_tlsext_hostname, "(J)Ljava/lang/String;"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_get_version, "(J)Ljava/lang/String;"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_cipher, "(J)Ljava/lang/String;"),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. ATM this change only affects the socket. Before submitting, the engine class should also be changed so that the old methods are no longer used (and can therefore be removed).

}

@Override
@SuppressWarnings("unused") // used by NativeCrypto.SSLHandshakeCallbacks / info_callback

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment probably needs to be changed from info_callback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done .... renamed to new_session_callback.

@SuppressWarnings("MissingOverride") // For compilation with Java 6.
public SSLSession getHandshakeSession() {
return handshakeSession;
return sslSession;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kind of nervous about this relaxing a restriction we'll have to live with forever. The Javadoc for this method says:

Returns: null if this instance is not currently handshaking, ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Done.

OpenSSLSocketImplWrapper(Socket socket, String hostname, int port,
boolean autoClose, SSLParametersImpl sslParameters) throws IOException {
super(socket, hostname, port, autoClose, sslParameters);
if (!socket.isConnected()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this class changed? This seems unrelated to the topic.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right ... I was originally hacking this to get the new integration tests to pass. Let me revert for now and will address the integration tests with a follow-on PR.

CONSCRYPT_NATIVE_METHOD(NativeCrypto, getDirectBufferAddress, "(Ljava/nio/Buffer;)J"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_BIO_new, "(J)J"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_get0_session, "(J)J"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_get1_session, "(J)J"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this will be removed when OpenSSLEngineImpl is also fixed?

@nmittler

nmittler commented May 9, 2017

Copy link
Copy Markdown
Contributor Author

@kruton I've just pushed some major changes. PTAL.

@nmittler
nmittler force-pushed the my-session-refactor branch 2 times, most recently from 3aac1ad to aa808b0 Compare May 9, 2017 16:39
Iterator<SSLSession> i = sessions.values().iterator();
do {
SSLSession session = i.next();
Iterator<SslSessionWrapper> i = sessionsIterator();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: whitespace. git clang-format should have caught the whitespace, though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

*/
private final HashMap<HostAndPort, SSLSession> sessionsByHostAndPort = new HashMap<>();
@SuppressWarnings("serial")
private final HashMap<HostAndPort, SslSessionWrapper> sessionsByHostAndPort =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either Map or LinkedHashMap would be a better choice for the field type here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


/**
* Object used for locking access to the sessions.
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unorthodox, but it works.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's admittedly ugly. Alternatively, we could synchronize on the class. WDYT?

Comment thread Android.mk Outdated

bundled_test_java_files := $(filter-out \
%/org/conscrypt/AbstractSessionContextTest.java \
%/org/conscrypt/ClientSessionContextTest.java \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: not sure if github is displaying this wrong or if there is missing whitespace here.

Method m_getResponses;
try {
m_getResponses = session.getClass().getDeclaredMethod("getStatusResponses");
m_getResponses = session.getClass().getDeclaredMethod("getOcspStapledResponse");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably shouldn't be renamed since it's the name of record currently.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good point. done.

@nmittler
nmittler force-pushed the my-session-refactor branch 6 times, most recently from 29301c5 to af80971 Compare May 19, 2017 23:30
@nmittler
nmittler force-pushed the my-session-refactor branch from af80971 to 10c86d4 Compare May 23, 2017 23:29
@nmittler

Copy link
Copy Markdown
Contributor Author

@kruton @davidben PTAL I think everything brought up so far has been addressed.

@nmittler
nmittler force-pushed the my-session-refactor branch 2 times, most recently from 0bb075a to 98f3316 Compare June 7, 2017 17:17
@nmittler

nmittler commented Jun 7, 2017

Copy link
Copy Markdown
Contributor Author

@kruton I've re-based onto the latest master (with the engine/engine-socket changes).

PTAL

@nmittler nmittler self-assigned this Jun 9, 2017
@nmittler
nmittler requested a review from flooey June 9, 2017 14:30
static SSLSession wrapSSLSession(AbstractOpenSSLSession sslSession) {
return new OpenSSLExtendedSessionImpl(sslSession);
static SSLSession wrapSSLSession(SSLSession sslSession) {
return new DelegatingExtendedSSLSession((ActiveSession) sslSession);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change these to take ActiveSession as the input for type safety.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed... done.

static SSLSession wrapSSLSession(AbstractOpenSSLSession sslSession) {
return new OpenSSLExtendedSessionImpl(sslSession);
static SSLSession wrapSSLSession(SSLSession sslSession) {
return new DelegatingExtendedSSLSession((ActiveSession) sslSession);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change these to take ActiveSession as the input for type safety.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

return new OpenSSLExtendedSessionImpl(sslSession);
return new DelegatingExtendedSSLSession((ActiveSession) sslSession);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change these to take ActiveSession as the input for type safety.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed .... done.

// timeout as part of their validity condition.
if (!session.isValid()) {
// Let the subclass know.
removingSession(session);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call this onBeforeRemoveSession or something indicating it an event callback (prefix of on...).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

} else {
return session;
// Let the subclass know.
addingSession(session);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onBeforeAddSession

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

abstract SslSessionWrapper getSessionFromPersistentCache(byte[] sessionId);

/**
* Makes sure cache size is < maximumSize.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this sort by oldest to newest? That would seem to make the most sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here should be identical to the old code. Since they are stored in a LinkedHashMap, iteration should be based on insertion order. Or am I missing something?

@@ -0,0 +1,334 @@
package org.conscrypt;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a license header.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

This change breaks session management into two distinct types:

- SslSessionWrapper: These are created as BoringSSL calls back the new session handler, allowing the application to cache sessions. Clients will also offer these to BoringSSL for reuse if a compatible session was found. BoringSSL is free to use it or not, but the Conscrypt code no longer makes assumptions here. Instead, it always uses the ActiveSession.

- ActiveSession: This is a session that wraps the SSL instance (rather than the SSL_SESSION wherever possible). That way no concern has to be paid to what BoringSSL is doing with sessions under the covers.

Fixes google#98
@nmittler
nmittler force-pushed the my-session-refactor branch from 241f530 to 3c9f16e Compare June 16, 2017 20:54
@nmittler

Copy link
Copy Markdown
Contributor Author

@kruton any other comments?

@kruton

kruton commented Jun 16, 2017

Copy link
Copy Markdown
Contributor

No, LGTM.

@nmittler

Copy link
Copy Markdown
Contributor Author

@davidben would you mind giving this a scan through to make sure it's addressing your original concerns?

@flooey do you want to take a look as well?

@nmittler

nmittler commented Jun 19, 2017 •

Copy link
Copy Markdown
Contributor Author

@kruton @flooey are you guys ok with me submitting? It's a big change ... I'd like to get it in.

@davidben davidben left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only skimmed this since I don't really know the Java half of Conscrypt. I'll defer to others for that. I mostly just looked at the C side and immediate callers. Some comments, but otherwise looks good!

Thanks for doing this, and sorry about my slowness. :-(

CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_session_id, "(J)[B"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_get_version, "(J)Ljava/lang/String;"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_cipher, "(J)Ljava/lang/String;"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, get_SSL_SESSION_tlsext_hostname, "(J)Ljava/lang/String;"),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be unused now (YAY!!!!).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah cool, I missed that! Removed!

try {
// Cache the newly established session.
AbstractSessionContext ctx = sessionContext();
ctx.cacheSession(SslSessionWrapper.newInstance(sslSessionNativePtr, sslSession));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SslSessionWrapper.newInstance takes ownership of sslSessionNativePtr, right? So suppose ctx.cacheSession throws an exception. We've already taken ownership of sslSessionNativePtr, but this function will return false. That will cause the C code to think we haven't taken ownership and double-free.

Two possible fixes:

  • The C-level callback returns true if it's called the callback at all and this method returns void. The promise would be that the Java code, no matter what happens, takes ownership of sslSessionNativePtr. That would mean you want to wrap sslSessionNativePtr as the first line I guess.

  • Alternatively, SslSessionWrapper.newInstance does not take ownership. Rather it calls SSL_SESSION_up_ref before stashing the pointer on itself. The Java onNewSessionEstablished then also does not take ownership of the provided reference, so the C-level callback always returns false. As in the first option, this function also now returns void.

(It's perfectly fine for the return value to not match whether the session was. It's some bizarre OpenSSL legacy thing and doesn't terminate the connection or anything. Think of the return value as that goofy out_copy parameter to the other callback. It's another one of those weird things where OpenSSL should have just picked one and left it at that. Pick whichever return value works better and don't think too hard about making it match whether operations succeeded or not.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidben Good catch! Yes I think moving newInstance to the first line is the way to go. I think I'd like to keep the signature to this method the same as the BoringSSL API, however for consistency.

}

static SSL_SESSION* server_session_requested_callback(SSL* ssl, uint8_t* id, int id_len,
int* out_copy) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't do anything right now, right? I probably would have left this and the Java stub out for now. Your call.

(This callback only gets called if the internal session cache can't find anything, so it's fine to leave it out.)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably also suggest bothering to caching anything via the callback on the server if you're not implementing this hook. (You might already be doing this.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah exactly ... the callback isn't currently being used on the Java side, but I do have a TBD to implement it. So I think I'd prefer keeping this here so that the only required change is to Java.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bothering or not bothering? The wording makes it sound like a typo here.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er, sorry I meant "not bothering". I'm really bad at typing.

@nmittler nmittler left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidben PTAL

}

static SSL_SESSION* server_session_requested_callback(SSL* ssl, uint8_t* id, int id_len,
int* out_copy) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah exactly ... the callback isn't currently being used on the Java side, but I do have a TBD to implement it. So I think I'd prefer keeping this here so that the only required change is to Java.

CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_session_id, "(J)[B"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_get_version, "(J)Ljava/lang/String;"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_cipher, "(J)Ljava/lang/String;"),
CONSCRYPT_NATIVE_METHOD(NativeCrypto, get_SSL_SESSION_tlsext_hostname, "(J)Ljava/lang/String;"),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah cool, I missed that! Removed!

try {
// Cache the newly established session.
AbstractSessionContext ctx = sessionContext();
ctx.cacheSession(SslSessionWrapper.newInstance(sslSessionNativePtr, sslSession));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidben Good catch! Yes I think moving newInstance to the first line is the way to go. I think I'd like to keep the signature to this method the same as the BoringSSL API, however for consistency.

return true;
} catch (Exception ignored) {
sessionWrapper = SslSessionWrapper.newInstance(sslSessionNativePtr, sslSession);
} catch (Throwable e) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm. This is still kinda screwy. If newInstance throws in activeSession.getSessionContext() (sorry, I missed that there was something before the constructor call), then this works. But if it throws in Impl(), at SSL_SESSION_get_version, the finalizer will still run, right? Which means that Impl has taken ownership of the pointer and we double-free.

I'm not sure I see the point of making the return value match the C one. You don't match the out_copy thing and it's the same deal. Again, the return value doesn't mean success or failure. It's purely a weird lifetimes thing. In a language like Java, it doesn't make sense to handle lifetimes by tossing booleans around, so it doesn't make sense to keep the return value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reworked things ... take a look and see if it's more clear now.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much clearer. Thanks! One small comment: you should be able to avoid checking for NativeRef.SSL_SESSION throwing. I suppose it does technically throw NPE, but then all this freeing is moot anyway. (Also, you'd have crashed before that point in up_ref and the callback will never give you a NULL pointer.)

jclass cls = env->GetObjectClass(sslHandshakeCallbacks);
jmethodID methodID = env->GetMethodID(cls, "serverSessionRequested", "([B)J");
JNI_TRACE("ssl=%p server_session_requested_callback calling serverSessionRequested", ssl);
jlong ssl_session_address = env->CallLongMethod(sslHandshakeCallbacks, methodID, id_array);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lifetimes here are very non-obvious without C and Java halves together. See the mess with the new session callback. The thing about these callbacks across JNI is that you cannot review one without the other. I believe this is correct, but you need to remember to SSL_SESSION_up_ref in Java while the SslSessionWrapper is alive.

It would be much better to take it out so that, when the Java half is reviewed, the C half is in the diff at the same time for simultaneous review.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Done.

JNI_TRACE("ssl=%p new_session_callback calling onNewSessionEstablished", ssl);
jboolean ret = env->CallBooleanMethod(sslHandshakeCallbacks, methodID,
env->CallBooleanMethod(sslHandshakeCallbacks, methodID,
reinterpret_cast<jlong>(session));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super nit-picky nitpick: formatting? (I just have a key-binding for clang-format and try not to think too hard about such things.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

jmethodID methodID = env->GetMethodID(cls, "onNewSessionEstablished", "(J)V");
JNI_TRACE("ssl=%p new_session_callback calling onNewSessionEstablished", ssl);
jboolean ret = env->CallBooleanMethod(sslHandshakeCallbacks, methodID,
env->CallBooleanMethod(sslHandshakeCallbacks, methodID,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CallVoidMethod?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oof ... done.

} catch (Exception ignored) {
// Failed constructing the wrapper. Its finalizer will not release the reference, so
// we need to here.
NativeCrypto.SSL_SESSION_free(sslSessionNativePtr);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this works, since you moved the saving to the end, but this relies on, e.g., SslSessionWrapper.newInstance not doing anything after creating the SslSessionWrapper. Another option would be to say SslSessionWrapper.newInstance and SslSessionWrapper.Impl() do not take ownership of the reference. Instead, the Impl constructor just looks like:

  NativeCrypto.SSL_SESSION_up_ref(sslSessionNativePointer);
  this.sslSessionNativePointer = sslSessionNativePointer;

Though I guess that might be different from the rest of Conscrypt? Whatever works.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, now using NativeRef.

@nmittler

Copy link
Copy Markdown
Contributor Author

@davidben PTAL

return true;
} catch (Exception ignored) {
sessionWrapper = SslSessionWrapper.newInstance(sslSessionNativePtr, sslSession);
} catch (Throwable e) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much clearer. Thanks! One small comment: you should be able to avoid checking for NativeRef.SSL_SESSION throwing. I suppose it does technically throw NPE, but then all this freeing is moot anyway. (Also, you'd have crashed before that point in up_ref and the callback will never give you a NULL pointer.)

@davidben

Copy link
Copy Markdown
Collaborator

Hrm, GitHub is hiding my comment:

Much clearer. Thanks! One small comment: you should be able to avoid checking for NativeRef.SSL_SESSION throwing. I suppose it does technically throw NPE, but then all this freeing is moot anyway. (Also, you'd have crashed before that point in up_ref and the callback will never give you a NULL pointer.)

@kruton kruton left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me after latest round with NativeRef where it takes ownership immediately.

@nmittler

Copy link
Copy Markdown
Contributor Author

@kruton @davidben Thanks for the review guys! Will submit after the CI checks pass!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants