Skip to content

Commit

Permalink
[monodroid] Prevent overlapped decompression of embedded assemblies (d…
Browse files Browse the repository at this point in the history
…otnet#7732)

Fixes: dotnet#7335

Context: d236af5

Commit d236af5 introduced embedded assembly compression, using LZ4,
which speeds up startup and reduces final package size.

Assemblies are compressed at build time and, at the same time, pre-
allocated buffers for the **decompressed** data are allocated in
`libxamarin-app.so`.  The buffers are then passed to the LZ4 APIs,
all threads using the same output buffer.  The assumption was that we
can do fine without locking as even if overlapped decompression
happens, the output data will be the same and so even if two threads
do the same thing at the same time, the data will be valid at all
times, so long as at least one thread completes the decompression.

This assumption proved to be **largely** true, but it appears that in
high concurrency cases it is possible that the data in the
decompression buffer differs.  This can result in app crashes:

	A/libc: Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 3092 (.NET ThreadPool), pid 2727 (myapp.name)
	A/DEBUG: pid: 2727, tid: 3092, name: .NET ThreadPool  >>> myapp.name <<<
	A/DEBUG:       #1 pc 0000000000029b1c  /data/app/myapp.name-B9t_3dF9i8mDxJEKodZw5w==/split_config.arm64_v8a.apk!libmono-android.release.so (offset 0x103d000) (xamarin::android::internal::MonodroidRuntime::mono_log_handler(char const*, char const*, char const*, int, void*)+144) (BuildId: 29c5a3805a0bedee1eede9b6668d7c676aa63371)
	A/DEBUG:       #2 pc 00000000002680bc  /data/app/myapp.name-B9t_3dF9i8mDxJEKodZw5w==/split_config.arm64_v8a.apk!libmonosgen-2.0.so (offset 0x109b000) (BuildId: 4a5dd4396e8816b7f69881838bd549285213d53b)
	A/DEBUG:       #3 pc 00000000002681e8  /data/app/myapp.name-B9t_3dF9i8mDxJEKodZw5w==/split_config.arm64_v8a.apk!libmonosgen-2.0.so (offset 0x109b000) (BuildId: 4a5dd4396e8816b7f69881838bd549285213d53b)
	A/DEBUG:       #4 pc 000000000008555c  /data/app/myapp.name-B9t_3dF9i8mDxJEKodZw5w==/split_config.arm64_v8a.apk!libmonosgen-2.0.so (offset 0x109b000) (mono_metadata_string_heap+188) (BuildId: 4a5dd4396e8816b7f69881838bd549285213d53b)
	…

My guess is that LZ4 either uses the output buffer as a scratchpad
area when decompressing or that it initializes/modifies the buffer
before writing actual data in it.  With overlapped decompression, it
may lead to one thread overwriting valid data previously written by
another thread, so that when the latter returns the buffer it thought
to have had valid data may contain certain bytes temporarily
overwritten by the decompression session in the other, still running,
thread.  It may happen that MonoVM reads the corrupted data just when
it is still invalid (before the still running decompression session
actually writes the valid data), a classic race condition.

To fix this, the decompression block is now protected with a startup-
aware mutex.  Mutex will be held only after the initial startup phase
is completed, so there should not be much loss of startup performance.
  • Loading branch information
grendello authored and jonpryor committed Jan 28, 2023
1 parent 4b24223 commit a200af1
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,55 @@
"Size": 3032
},
"assemblies/Java.Interop.dll": {
"Size": 58990
"Size": 58920
},
"assemblies/Mono.Android.dll": {
"Size": 87490
"Size": 87624
},
"assemblies/Mono.Android.Runtime.dll": {
"Size": 5860
"Size": 5862
},
"assemblies/rc.bin": {
"Size": 1182
},
"assemblies/System.Console.dll": {
"Size": 6627
"Size": 6594
},
"assemblies/System.Linq.dll": {
"Size": 9252
"Size": 9259
},
"assemblies/System.Private.CoreLib.dll": {
"Size": 470830
"Size": 478515
},
"assemblies/System.Runtime.dll": {
"Size": 2626
"Size": 2632
},
"assemblies/System.Runtime.InteropServices.dll": {
"Size": 2267
"Size": 2272
},
"assemblies/UnnamedProject.dll": {
"Size": 3629
"Size": 3633
},
"classes.dex": {
"Size": 18968
},
"lib/arm64-v8a/libmonodroid.so": {
"Size": 434632
"Size": 379008
},
"lib/arm64-v8a/libmonosgen-2.0.so": {
"Size": 3076080
"Size": 3077928
},
"lib/arm64-v8a/libSystem.IO.Compression.Native.so": {
"Size": 723840
},
"lib/arm64-v8a/libSystem.Native.so": {
"Size": 94136
"Size": 94232
},
"lib/arm64-v8a/libSystem.Security.Cryptography.Native.Android.so": {
"Size": 149552
},
"lib/arm64-v8a/libxamarin-app.so": {
"Size": 16128
"Size": 16336
},
"META-INF/BNDLTOOL.RSA": {
"Size": 1213
Expand All @@ -65,19 +65,19 @@
"Size": 2667
},
"res/drawable-hdpi-v4/icon.png": {
"Size": 4762
"Size": 2178
},
"res/drawable-mdpi-v4/icon.png": {
"Size": 2200
"Size": 1490
},
"res/drawable-xhdpi-v4/icon.png": {
"Size": 7462
"Size": 3098
},
"res/drawable-xxhdpi-v4/icon.png": {
"Size": 13092
"Size": 4674
},
"res/drawable-xxxhdpi-v4/icon.png": {
"Size": 20118
"Size": 6832
},
"res/layout/main.xml": {
"Size": 544
Expand All @@ -89,5 +89,5 @@
"Size": 1904
}
},
"PackageSize": 2590859
"PackageSize": 2549899
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
"Size": 68913
},
"assemblies/Mono.Android.dll": {
"Size": 265169
"Size": 265160
},
"assemblies/mscorlib.dll": {
"Size": 769018
"Size": 769019
},
"assemblies/System.Core.dll": {
"Size": 28199
"Size": 28198
},
"assemblies/System.dll": {
"Size": 9180
"Size": 9179
},
"assemblies/UnnamedProject.dll": {
"Size": 2882
"Size": 2880
},
"classes.dex": {
"Size": 370828
Expand All @@ -32,7 +32,7 @@
"Size": 750976
},
"lib/arm64-v8a/libmonodroid.so": {
"Size": 332936
"Size": 277520
},
"lib/arm64-v8a/libmonosgen-2.0.so": {
"Size": 4039176
Expand Down Expand Up @@ -74,5 +74,5 @@
"Size": 1724
}
},
"PackageSize": 4003540
"PackageSize": 3987156
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
"Size": 7314
},
"assemblies/Java.Interop.dll": {
"Size": 66843
"Size": 66793
},
"assemblies/Mono.Android.dll": {
"Size": 444299
"Size": 444690
},
"assemblies/Mono.Android.Runtime.dll": {
"Size": 5921
"Size": 5862
},
"assemblies/mscorlib.dll": {
"Size": 3860
"Size": 3859
},
"assemblies/netstandard.dll": {
"Size": 5576
"Size": 5575
},
"assemblies/rc.bin": {
"Size": 1182
Expand All @@ -29,106 +29,106 @@
"Size": 10725
},
"assemblies/System.Collections.dll": {
"Size": 15465
"Size": 15460
},
"assemblies/System.Collections.NonGeneric.dll": {
"Size": 7638
"Size": 7633
},
"assemblies/System.ComponentModel.dll": {
"Size": 2158
"Size": 2157
},
"assemblies/System.ComponentModel.Primitives.dll": {
"Size": 2649
"Size": 2648
},
"assemblies/System.ComponentModel.TypeConverter.dll": {
"Size": 6206
"Size": 6205
},
"assemblies/System.Console.dll": {
"Size": 6760
"Size": 6759
},
"assemblies/System.Core.dll": {
"Size": 1988
"Size": 1987
},
"assemblies/System.Diagnostics.TraceSource.dll": {
"Size": 6745
"Size": 6742
},
"assemblies/System.dll": {
"Size": 2344
"Size": 2343
},
"assemblies/System.Drawing.dll": {
"Size": 2029
"Size": 2028
},
"assemblies/System.Drawing.Primitives.dll": {
"Size": 12160
"Size": 12095
},
"assemblies/System.IO.Compression.dll": {
"Size": 16797
"Size": 16984
},
"assemblies/System.IO.IsolatedStorage.dll": {
"Size": 10155
"Size": 10162
},
"assemblies/System.Linq.dll": {
"Size": 19493
"Size": 19494
},
"assemblies/System.Linq.Expressions.dll": {
"Size": 163967
"Size": 163942
},
"assemblies/System.Net.Http.dll": {
"Size": 67013
"Size": 65586
},
"assemblies/System.Net.Primitives.dll": {
"Size": 22021
},
"assemblies/System.Net.Requests.dll": {
"Size": 3745
"Size": 3743
},
"assemblies/System.ObjectModel.dll": {
"Size": 8173
"Size": 8166
},
"assemblies/System.Private.CoreLib.dll": {
"Size": 775898
"Size": 780751
},
"assemblies/System.Private.DataContractSerialization.dll": {
"Size": 192465
"Size": 192055
},
"assemblies/System.Private.Uri.dll": {
"Size": 42550
"Size": 42554
},
"assemblies/System.Private.Xml.dll": {
"Size": 215747
"Size": 215680
},
"assemblies/System.Private.Xml.Linq.dll": {
"Size": 16813
"Size": 16807
},
"assemblies/System.Runtime.dll": {
"Size": 2794
},
"assemblies/System.Runtime.InteropServices.dll": {
"Size": 2273
"Size": 2272
},
"assemblies/System.Runtime.Serialization.dll": {
"Size": 1950
"Size": 1949
},
"assemblies/System.Runtime.Serialization.Formatters.dll": {
"Size": 2683
"Size": 2681
},
"assemblies/System.Runtime.Serialization.Primitives.dll": {
"Size": 3856
"Size": 3854
},
"assemblies/System.Security.Cryptography.dll": {
"Size": 7949
"Size": 7947
},
"assemblies/System.Text.RegularExpressions.dll": {
"Size": 154213
"Size": 155427
},
"assemblies/System.Xml.dll": {
"Size": 1837
},
"assemblies/System.Xml.Linq.dll": {
"Size": 1862
"Size": 1861
},
"assemblies/UnnamedProject.dll": {
"Size": 117372
"Size": 117398
},
"assemblies/Xamarin.AndroidX.Activity.dll": {
"Size": 5872
Expand Down Expand Up @@ -200,22 +200,22 @@
"Size": 3090508
},
"lib/arm64-v8a/libmonodroid.so": {
"Size": 434632
"Size": 379008
},
"lib/arm64-v8a/libmonosgen-2.0.so": {
"Size": 3078808
"Size": 3077928
},
"lib/arm64-v8a/libSystem.IO.Compression.Native.so": {
"Size": 723840
},
"lib/arm64-v8a/libSystem.Native.so": {
"Size": 94136
"Size": 94232
},
"lib/arm64-v8a/libSystem.Security.Cryptography.Native.Android.so": {
"Size": 149552
},
"lib/arm64-v8a/libxamarin-app.so": {
"Size": 333376
"Size": 333336
},
"META-INF/android.support.design_material.version": {
"Size": 12
Expand Down Expand Up @@ -782,7 +782,7 @@
"Size": 470
},
"res/drawable-hdpi-v4/icon.png": {
"Size": 4762
"Size": 2178
},
"res/drawable-hdpi-v4/notification_bg_low_normal.9.png": {
"Size": 212
Expand Down Expand Up @@ -992,7 +992,7 @@
"Size": 309
},
"res/drawable-mdpi-v4/icon.png": {
"Size": 2200
"Size": 1490
},
"res/drawable-mdpi-v4/notification_bg_low_normal.9.png": {
"Size": 215
Expand Down Expand Up @@ -1220,7 +1220,7 @@
"Size": 593
},
"res/drawable-xhdpi-v4/icon.png": {
"Size": 7462
"Size": 3098
},
"res/drawable-xhdpi-v4/notification_bg_low_normal.9.png": {
"Size": 221
Expand Down Expand Up @@ -1385,7 +1385,7 @@
"Size": 868
},
"res/drawable-xxhdpi-v4/icon.png": {
"Size": 13092
"Size": 4674
},
"res/drawable-xxxhdpi-v4/abc_btn_check_to_on_mtrl_000.png": {
"Size": 275
Expand Down Expand Up @@ -1472,7 +1472,7 @@
"Size": 1155
},
"res/drawable-xxxhdpi-v4/icon.png": {
"Size": 20118
"Size": 6832
},
"res/drawable/abc_btn_borderless_material.xml": {
"Size": 588
Expand Down Expand Up @@ -1970,5 +1970,5 @@
"Size": 341228
}
},
"PackageSize": 7971397
"PackageSize": 7930437
}
Loading

0 comments on commit a200af1

Please sign in to comment.