-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add hooks to debug OpenSSL memory allocations #111539
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: Adeel Mujahid <[email protected]>
Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 2 out of 8 changed files in this pull request and generated 1 comment.
Files not reviewed (6)
- src/native/libs/System.Security.Cryptography.Native/apibridge_30.h: Language not supported
- src/native/libs/System.Security.Cryptography.Native/entrypoints.c: Language not supported
- src/native/libs/System.Security.Cryptography.Native/openssl.c: Language not supported
- src/native/libs/System.Security.Cryptography.Native/openssl.h: Language not supported
- src/native/libs/System.Security.Cryptography.Native/opensslshim.h: Language not supported
- src/native/libs/System.Security.Cryptography.Native/pal_ssl.c: Language not supported
src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Crypto.cs
Outdated
Show resolved
Hide resolved
src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Crypto.cs
Show resolved
Hide resolved
I moved the tracking to native code. And it seems stable during my tests. I think we are ready for another round of review |
src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Crypto.cs
Outdated
Show resolved
Hide resolved
src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Crypto.cs
Outdated
Show resolved
Hide resolved
Interop.Crypto.GetOpenSslAllocationCount(); | ||
Interop.Crypto.GetOpenSslAllocatedMemory(); | ||
Interop.Crypto.ForEachTrackedAllocation((a, b, c, d) => { }); | ||
Interop.Crypto.EnableTracking(); | ||
Interop.Crypto.DisableTracking(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What of this is important? It seems like it's mainly testing the P/Invokes, which suggests it should be deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The methods otherwise don't have a caller, meaning that the compiler will just eliminate them and they are not reachable via reflection.
There might be better way to prevent that from happening, I am open to suggestions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The right way to do this is to list these methods in ILLink.Descriptors.LibraryBuild.xml. For example:
src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Crypto.cs
Outdated
Show resolved
Hide resolved
src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Crypto.cs
Outdated
Show resolved
Hide resolved
src/native/libs/System.Security.Cryptography.Native/memory_debug.c
Outdated
Show resolved
Hide resolved
src/native/libs/System.Security.Cryptography.Native/memory_debug.c
Outdated
Show resolved
Hide resolved
src/native/libs/System.Security.Cryptography.Native/memory_debug.c
Outdated
Show resolved
Hide resolved
src/native/libs/System.Security.Cryptography.Native/opensslshim.h
Outdated
Show resolved
Hide resolved
@@ -313,6 +313,12 @@ extern bool g_libSslUses32BitTime; | |||
REQUIRED_FUNCTION(CRYPTO_malloc) \ | |||
LEGACY_FUNCTION(CRYPTO_num_locks) \ | |||
LEGACY_FUNCTION(CRYPTO_set_locking_callback) \ | |||
LIGHTUP_FUNCTION(CRYPTO_THREAD_lock_new) \ | |||
LIGHTUP_FUNCTION(CRYPTO_atomic_add) \ | |||
RENAMED_FUNCTION(CRYPTO_set_mem_functions11, CRYPTO_set_mem_functions) \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that what you've done to work around the OSSL 1.0 version of this function will work in a non-portable build from 1.1.
I believe it will try to call the non-existent function CRYPTO_set_mem_functions11 and fail to link.
But maybe I was more clever with RENAMED_FUNCTION that I'm giving myself credit for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do feel we do not need to worry about 1.0 anymore .... as long as it does not break the build.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do feel we do not need to worry about 1.0 anymore .... as long as it does not break the build.
Official builds are currently done on Ubuntu 16.04 with 1.0.2. Jeremy and I have had discussions on bumping to 1.1, but at the moment I think it needs to build against 1.0.2 headers.
This ressurects #101626. CC: @wfurt.
Changes since his PR:
Open questions:
#if DEBUG
?We had several cases when users complained about large memory use. For than native it is quite difficult to figure out where the memory goes. This PR aims to make that somewhat easier.
OpenSSL provides hooks for memory function so this PR adds switch to optimally hook into that.
The only one caveat that the
CRYPTO_set_mem_functions
works only if called before any allocations e.g. it needs to be done very early in the process. So I end up putting into initialization process.The simple use pattern is something like
Access through Reflection should be OK since this is only last resort debug hook e.g. it does not need stable API and convenient access.