Skip to content

Commit

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

Fixes: #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 Feb 27, 2023
1 parent 77c6886 commit 79a0897
Show file tree
Hide file tree
Showing 6 changed files with 562 additions and 560 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,49 @@
"Size": 3032
},
"assemblies/Java.Interop.dll": {
"Size": 58991
"Size": 58989
},
"assemblies/Mono.Android.dll": {
"Size": 88024
"Size": 88028
},
"assemblies/rc.bin": {
"Size": 1182
},
"assemblies/System.Console.dll": {
"Size": 6527
"Size": 6525
},
"assemblies/System.Linq.dll": {
"Size": 9223
"Size": 9224
},
"assemblies/System.Private.CoreLib.dll": {
"Size": 464288
"Size": 464285
},
"assemblies/System.Runtime.dll": {
"Size": 2346
"Size": 2348
},
"assemblies/System.Runtime.InteropServices.dll": {
"Size": 2234
"Size": 2232
},
"assemblies/UnnamedProject.dll": {
"Size": 3631
"Size": 3622
},
"classes.dex": {
"Size": 368636
},
"lib/arm64-v8a/libmonodroid.so": {
"Size": 427328
"Size": 371504
},
"lib/arm64-v8a/libmonosgen-2.0.so": {
"Size": 3074520
"Size": 3075360
},
"lib/arm64-v8a/libSystem.IO.Compression.Native.so": {
"Size": 723840
},
"lib/arm64-v8a/libSystem.Native.so": {
"Size": 93704
"Size": 93768
},
"lib/arm64-v8a/libSystem.Security.Cryptography.Native.Android.so": {
"Size": 148696
"Size": 151624
},
"lib/arm64-v8a/libxamarin-app.so": {
"Size": 9680
Expand All @@ -62,7 +62,7 @@
"Size": 2560
},
"res/drawable-hdpi-v4/icon.png": {
"Size": 4762
"Size": 4791
},
"res/drawable-mdpi-v4/icon.png": {
"Size": 2200
Expand All @@ -86,5 +86,5 @@
"Size": 1904
}
},
"PackageSize": 2639930
"PackageSize": 2627642
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,43 @@
"Size": 2604
},
"assemblies/Java.Interop.dll": {
"Size": 68921
"Size": 69019
},
"assemblies/Mono.Android.dll": {
"Size": 259883
"Size": 264177
},
"assemblies/mscorlib.dll": {
"Size": 769017
"Size": 769014
},
"assemblies/System.Core.dll": {
"Size": 28199
"Size": 28190
},
"assemblies/System.dll": {
"Size": 9179
"Size": 9178
},
"assemblies/UnnamedProject.dll": {
"Size": 2877
"Size": 2873
},
"classes.dex": {
"Size": 362940
"Size": 370828
},
"lib/arm64-v8a/libmono-btls-shared.so": {
"Size": 1613872
},
"lib/arm64-v8a/libmonodroid.so": {
"Size": 277152
},
"lib/arm64-v8a/libmono-native.so": {
"Size": 750976
},
"lib/arm64-v8a/libmonodroid.so": {
"Size": 332128
},
"lib/arm64-v8a/libmonosgen-2.0.so": {
"Size": 4051864
"Size": 4039176
},
"lib/arm64-v8a/libxa-internal-api.so": {
"Size": 66184
},
"lib/arm64-v8a/libxamarin-app.so": {
"Size": 20240
"Size": 20328
},
"META-INF/ANDROIDD.RSA": {
"Size": 1213
Expand Down Expand Up @@ -74,5 +74,5 @@
"Size": 1724
}
},
"PackageSize": 4028116
"PackageSize": 4015828
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"Size": 7114
},
"assemblies/Java.Interop.dll": {
"Size": 66845
"Size": 66840
},
"assemblies/Mono.Android.dll": {
"Size": 445336
"Size": 445271
},
"assemblies/mscorlib.dll": {
"Size": 3829
"Size": 3830
},
"assemblies/netstandard.dll": {
"Size": 5546
"Size": 5550
},
"assemblies/rc.bin": {
"Size": 1182
Expand All @@ -26,7 +26,7 @@
"Size": 10612
},
"assemblies/System.Collections.dll": {
"Size": 15447
"Size": 15446
},
"assemblies/System.Collections.NonGeneric.dll": {
"Size": 7597
Expand All @@ -35,97 +35,97 @@
"Size": 2118
},
"assemblies/System.ComponentModel.Primitives.dll": {
"Size": 2614
"Size": 2615
},
"assemblies/System.ComponentModel.TypeConverter.dll": {
"Size": 6168
"Size": 6169
},
"assemblies/System.Console.dll": {
"Size": 6693
"Size": 6690
},
"assemblies/System.Core.dll": {
"Size": 1953
"Size": 1956
},
"assemblies/System.Diagnostics.TraceSource.dll": {
"Size": 6712
"Size": 6708
},
"assemblies/System.dll": {
"Size": 2309
"Size": 2312
},
"assemblies/System.Drawing.dll": {
"Size": 1991
"Size": 1993
},
"assemblies/System.Drawing.Primitives.dll": {
"Size": 12129
},
"assemblies/System.IO.Compression.dll": {
"Size": 16760
"Size": 16759
},
"assemblies/System.IO.IsolatedStorage.dll": {
"Size": 10114
"Size": 10111
},
"assemblies/System.Linq.dll": {
"Size": 19286
"Size": 19288
},
"assemblies/System.Linq.Expressions.dll": {
"Size": 164020
"Size": 164017
},
"assemblies/System.Net.Http.dll": {
"Size": 66971
"Size": 66969
},
"assemblies/System.Net.Primitives.dll": {
"Size": 21981
"Size": 21983
},
"assemblies/System.Net.Requests.dll": {
"Size": 3716
"Size": 3715
},
"assemblies/System.ObjectModel.dll": {
"Size": 8137
"Size": 8139
},
"assemblies/System.Private.CoreLib.dll": {
"Size": 772506
"Size": 772515
},
"assemblies/System.Private.DataContractSerialization.dll": {
"Size": 192495
"Size": 192548
},
"assemblies/System.Private.Uri.dll": {
"Size": 42378
"Size": 42377
},
"assemblies/System.Private.Xml.dll": {
"Size": 215549
"Size": 215543
},
"assemblies/System.Private.Xml.Linq.dll": {
"Size": 16806
},
"assemblies/System.Runtime.dll": {
"Size": 2545
"Size": 2548
},
"assemblies/System.Runtime.InteropServices.dll": {
"Size": 2234
"Size": 2232
},
"assemblies/System.Runtime.Serialization.dll": {
"Size": 1917
"Size": 1918
},
"assemblies/System.Runtime.Serialization.Formatters.dll": {
"Size": 2647
"Size": 2646
},
"assemblies/System.Runtime.Serialization.Primitives.dll": {
"Size": 3819
"Size": 3820
},
"assemblies/System.Security.Cryptography.dll": {
"Size": 7913
"Size": 7915
},
"assemblies/System.Text.RegularExpressions.dll": {
"Size": 154174
"Size": 154283
},
"assemblies/System.Xml.dll": {
"Size": 1805
"Size": 1806
},
"assemblies/System.Xml.Linq.dll": {
"Size": 1827
"Size": 1828
},
"assemblies/UnnamedProject.dll": {
"Size": 117372
"Size": 117367
},
"assemblies/Xamarin.AndroidX.Activity.dll": {
"Size": 5872
Expand Down Expand Up @@ -197,19 +197,19 @@
"Size": 3480884
},
"lib/arm64-v8a/libmonodroid.so": {
"Size": 427328
"Size": 371504
},
"lib/arm64-v8a/libmonosgen-2.0.so": {
"Size": 3074520
"Size": 3075360
},
"lib/arm64-v8a/libSystem.IO.Compression.Native.so": {
"Size": 723840
},
"lib/arm64-v8a/libSystem.Native.so": {
"Size": 93704
"Size": 93768
},
"lib/arm64-v8a/libSystem.Security.Cryptography.Native.Android.so": {
"Size": 148696
"Size": 151624
},
"lib/arm64-v8a/libxamarin-app.so": {
"Size": 99272
Expand Down Expand Up @@ -779,7 +779,7 @@
"Size": 470
},
"res/drawable-hdpi-v4/icon.png": {
"Size": 4762
"Size": 4791
},
"res/drawable-hdpi-v4/notification_bg_low_normal.9.png": {
"Size": 212
Expand Down Expand Up @@ -1967,5 +1967,5 @@
"Size": 341228
}
},
"PackageSize": 7991796
"PackageSize": 7975412
}
Loading

0 comments on commit 79a0897

Please sign in to comment.