Skip to content

Enable symbolication and source context for net9.0-android #4033

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

Merged
merged 51 commits into from
Apr 2, 2025

Conversation

jamescrosswell
Copy link
Collaborator

@jamescrosswell jamescrosswell commented Mar 13, 2025

Closes #3761:

Fixes #3983:

Fixes #3985:

Summary

.NET 9 does introduced a new AssemblyStore format for APK files. Reading the new format is demonstrated in assembly-store-reader-mk2. This PR adds support for reading the new format to the AndroidAssemblyReaderFactory.

Also, the reason we didn't pick this up when we moved to net9.0 is that the device tests are/were only running against net8.0 targets. In this PR we'll try to run the device tests on net8.0-android/ios and net9.0-android/ios.

Notes for Reviewers

There's a lot of code here. However most of it is simply copied/vendored in from two external repositories.

ELFSharp

For the purposes of review, you can ignore all of the files in the "src/Sentry.Android.AssemblyReader/ELFSharp" folder.

The V2 format for APKs stores all assemblies in in an RID specific folder but Android requires all files in that folder to be ELF files... so during the build process any exe, dll, pdb or config files stored in there get "packed" into ELF files... we need to use ElfSharp to unpack these.

To avoid adding any dependencies, the code for ELFSharp has been copied in to the Sentry.Android.AssemblyReader project (with appropriate attribution).

dotnet/android

For the purposes of review, you can ignore most of the files in the "src/Sentry.Android.AssemblyReader/V2" folder.

src/Sentry.Android.AssemblyReader/V2/AndroidAssemblyStoreReaderV2.cs and AndroidAssemblyDirectoryReaderV2 are classes I wrote (although AndroidAssemblyDirectoryReaderV2.ArchiveAssemblyHelper was vendored in). Otherwise, all of the files in src/Sentry.Android.AssemblyReader/V2 have been copied from the dotnet/android repo, with appropriate attribution at the top of each file.

No functional changes were made to any of that code. The only alterations were:

  • Visibility changed from public to internal
  • Unused code from files that we vendored in has been removed
  • I removed the dependency on Xamarin.LibZipSharp (using the built in ZipFile class instead)
  • A little bit of Ctrl + . in resharper to get the code to conform to our coding conventions

References

Relates to:

@vaind
Copy link
Collaborator

vaind commented Mar 21, 2025

With all the vendored-in code this is pretty hard to review. Can you link a diff showing your changes to the vendored code?

@jamescrosswell
Copy link
Collaborator Author

jamescrosswell commented Mar 26, 2025

Failing test

Note that we originally had a test fail trying to decompress System.Runtime.dll. When loading this from an APK file that doesn't use the AssemblyStore in net9.0, the decompression fails here:

var decoded = LZ4Codec.Decode(inputBuffer, offset, inputLength, outputBuffer, 0, decompressedLength);
if (decoded != decompressedLength)
{
throw new Exception($"Failed to decompress LZ4 data of assembly {assemblyName} - decoded {decoded} instead of expected {decompressedLength} bytes");
}

When trying to decompress System.Runtime.dll in that build configuration, the LZ4 decompression library returns -1 as a result of a buffer overload from here
image

Basically I think the source bytes aren't a valid LZ4 archive 😕

I checked with the dotnet/android team and it turns out assemblies can be stored in the APK with a magic header that indicates they're compressed, even though they're not compressed (ref discord conversation). Additionally, Marek suggested we don't use System.Runtime.dll in testing as it's just a facade that would be stripped by the linker.

So although I was never able to get the tests to pass trying to read System.Runtime.dll, they do pass with the assemblies that we'd actually expect to find referenced by stack frames.

If we ever need to read facade assemblies in the future, this would need more digging.

@jamescrosswell jamescrosswell marked this pull request as ready for review March 29, 2025 23:40
Copy link
Member

@bruno-garcia bruno-garcia left a comment

Choose a reason for hiding this comment

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

I just skimmed this change. Ideally someone can do a proper review

$tfm = 'net8.0-'
if (!$Tfm)
{
$Tfm = 'net8.0'
Copy link
Member

Choose a reason for hiding this comment

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

once we clear all callsites to pass a tfm, we should delete this and throw an error instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is just a default argument, if you don't specify a target framework. We could try to change the default to latest or something like that - would need a little bit of extra scripting to work out what latest was.

{
#if ANDROID
Skip.If(true, "It's unknown whether the current Android app APK is an assembly store or not.");
#endif
using var sut = GetSut(isAssemblyStore, isCompressed: true);
if (isAssemblyStore)
using var sut = GetSut(false, true, isCompressed: true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
using var sut = GetSut(false, true, isCompressed: true);
using var sut = GetSut(isAot: false, isAssemblyStore: true, isCompressed: true);

#if ANDROID
Skip.If(true, "It's unknown whether the current Android app APK is an assembly store or not.");
#endif
using var sut = GetSut(false, false, isCompressed: true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
using var sut = GetSut(false, false, isCompressed: true);
using var sut = GetSut(isAot: false, isAssemblyStore: false, isCompressed: true);

@@ -54,27 +88,32 @@ public void CreatesCorrectReader(bool isAssemblyStore)
[InlineData(true)]
public void ReturnsNullIfAssemblyDoesntExist(bool isAssemblyStore)
{
using var sut = GetSut(isAssemblyStore, isCompressed: true);
using var sut = GetSut(false, isAssemblyStore, isCompressed: true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
using var sut = GetSut(false, isAssemblyStore, isCompressed: true);
using var sut = GetSut(isAot: false, isAssemblyStore, isCompressed: true);

@jamescrosswell jamescrosswell merged commit 13d4a9f into main Apr 2, 2025
26 checks passed
@jamescrosswell jamescrosswell deleted the store-v2 branch April 2, 2025 06:31
ShortDevelopment added a commit to nearby-sharing/android that referenced this pull request Apr 6, 2025
Should fix `net-9.0` symbolication on android
getsentry/sentry-dotnet#4033
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants