Skip to content

Commit a200af1

Browse files
grendellojonpryor
authored andcommitted
[monodroid] Prevent overlapped decompression of embedded assemblies (dotnet#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: dotnet#3 pc 00000000002681e8 /data/app/myapp.name-B9t_3dF9i8mDxJEKodZw5w==/split_config.arm64_v8a.apk!libmonosgen-2.0.so (offset 0x109b000) (BuildId: 4a5dd4396e8816b7f69881838bd549285213d53b) A/DEBUG: dotnet#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.
1 parent 4b24223 commit a200af1

File tree

6 files changed

+114
-111
lines changed

6 files changed

+114
-111
lines changed

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/BuildReleaseArm64SimpleDotNet.apkdesc

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,55 @@
55
"Size": 3032
66
},
77
"assemblies/Java.Interop.dll": {
8-
"Size": 58990
8+
"Size": 58920
99
},
1010
"assemblies/Mono.Android.dll": {
11-
"Size": 87490
11+
"Size": 87624
1212
},
1313
"assemblies/Mono.Android.Runtime.dll": {
14-
"Size": 5860
14+
"Size": 5862
1515
},
1616
"assemblies/rc.bin": {
1717
"Size": 1182
1818
},
1919
"assemblies/System.Console.dll": {
20-
"Size": 6627
20+
"Size": 6594
2121
},
2222
"assemblies/System.Linq.dll": {
23-
"Size": 9252
23+
"Size": 9259
2424
},
2525
"assemblies/System.Private.CoreLib.dll": {
26-
"Size": 470830
26+
"Size": 478515
2727
},
2828
"assemblies/System.Runtime.dll": {
29-
"Size": 2626
29+
"Size": 2632
3030
},
3131
"assemblies/System.Runtime.InteropServices.dll": {
32-
"Size": 2267
32+
"Size": 2272
3333
},
3434
"assemblies/UnnamedProject.dll": {
35-
"Size": 3629
35+
"Size": 3633
3636
},
3737
"classes.dex": {
3838
"Size": 18968
3939
},
4040
"lib/arm64-v8a/libmonodroid.so": {
41-
"Size": 434632
41+
"Size": 379008
4242
},
4343
"lib/arm64-v8a/libmonosgen-2.0.so": {
44-
"Size": 3076080
44+
"Size": 3077928
4545
},
4646
"lib/arm64-v8a/libSystem.IO.Compression.Native.so": {
4747
"Size": 723840
4848
},
4949
"lib/arm64-v8a/libSystem.Native.so": {
50-
"Size": 94136
50+
"Size": 94232
5151
},
5252
"lib/arm64-v8a/libSystem.Security.Cryptography.Native.Android.so": {
5353
"Size": 149552
5454
},
5555
"lib/arm64-v8a/libxamarin-app.so": {
56-
"Size": 16128
56+
"Size": 16336
5757
},
5858
"META-INF/BNDLTOOL.RSA": {
5959
"Size": 1213
@@ -65,19 +65,19 @@
6565
"Size": 2667
6666
},
6767
"res/drawable-hdpi-v4/icon.png": {
68-
"Size": 4762
68+
"Size": 2178
6969
},
7070
"res/drawable-mdpi-v4/icon.png": {
71-
"Size": 2200
71+
"Size": 1490
7272
},
7373
"res/drawable-xhdpi-v4/icon.png": {
74-
"Size": 7462
74+
"Size": 3098
7575
},
7676
"res/drawable-xxhdpi-v4/icon.png": {
77-
"Size": 13092
77+
"Size": 4674
7878
},
7979
"res/drawable-xxxhdpi-v4/icon.png": {
80-
"Size": 20118
80+
"Size": 6832
8181
},
8282
"res/layout/main.xml": {
8383
"Size": 544
@@ -89,5 +89,5 @@
8989
"Size": 1904
9090
}
9191
},
92-
"PackageSize": 2590859
92+
"PackageSize": 2549899
9393
}

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/BuildReleaseArm64SimpleLegacy.apkdesc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
"Size": 68913
99
},
1010
"assemblies/Mono.Android.dll": {
11-
"Size": 265169
11+
"Size": 265160
1212
},
1313
"assemblies/mscorlib.dll": {
14-
"Size": 769018
14+
"Size": 769019
1515
},
1616
"assemblies/System.Core.dll": {
17-
"Size": 28199
17+
"Size": 28198
1818
},
1919
"assemblies/System.dll": {
20-
"Size": 9180
20+
"Size": 9179
2121
},
2222
"assemblies/UnnamedProject.dll": {
23-
"Size": 2882
23+
"Size": 2880
2424
},
2525
"classes.dex": {
2626
"Size": 370828
@@ -32,7 +32,7 @@
3232
"Size": 750976
3333
},
3434
"lib/arm64-v8a/libmonodroid.so": {
35-
"Size": 332936
35+
"Size": 277520
3636
},
3737
"lib/arm64-v8a/libmonosgen-2.0.so": {
3838
"Size": 4039176
@@ -74,5 +74,5 @@
7474
"Size": 1724
7575
}
7676
},
77-
"PackageSize": 4003540
77+
"PackageSize": 3987156
7878
}

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/BuildReleaseArm64XFormsDotNet.apkdesc

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
"Size": 7314
99
},
1010
"assemblies/Java.Interop.dll": {
11-
"Size": 66843
11+
"Size": 66793
1212
},
1313
"assemblies/Mono.Android.dll": {
14-
"Size": 444299
14+
"Size": 444690
1515
},
1616
"assemblies/Mono.Android.Runtime.dll": {
17-
"Size": 5921
17+
"Size": 5862
1818
},
1919
"assemblies/mscorlib.dll": {
20-
"Size": 3860
20+
"Size": 3859
2121
},
2222
"assemblies/netstandard.dll": {
23-
"Size": 5576
23+
"Size": 5575
2424
},
2525
"assemblies/rc.bin": {
2626
"Size": 1182
@@ -29,106 +29,106 @@
2929
"Size": 10725
3030
},
3131
"assemblies/System.Collections.dll": {
32-
"Size": 15465
32+
"Size": 15460
3333
},
3434
"assemblies/System.Collections.NonGeneric.dll": {
35-
"Size": 7638
35+
"Size": 7633
3636
},
3737
"assemblies/System.ComponentModel.dll": {
38-
"Size": 2158
38+
"Size": 2157
3939
},
4040
"assemblies/System.ComponentModel.Primitives.dll": {
41-
"Size": 2649
41+
"Size": 2648
4242
},
4343
"assemblies/System.ComponentModel.TypeConverter.dll": {
44-
"Size": 6206
44+
"Size": 6205
4545
},
4646
"assemblies/System.Console.dll": {
47-
"Size": 6760
47+
"Size": 6759
4848
},
4949
"assemblies/System.Core.dll": {
50-
"Size": 1988
50+
"Size": 1987
5151
},
5252
"assemblies/System.Diagnostics.TraceSource.dll": {
53-
"Size": 6745
53+
"Size": 6742
5454
},
5555
"assemblies/System.dll": {
56-
"Size": 2344
56+
"Size": 2343
5757
},
5858
"assemblies/System.Drawing.dll": {
59-
"Size": 2029
59+
"Size": 2028
6060
},
6161
"assemblies/System.Drawing.Primitives.dll": {
62-
"Size": 12160
62+
"Size": 12095
6363
},
6464
"assemblies/System.IO.Compression.dll": {
65-
"Size": 16797
65+
"Size": 16984
6666
},
6767
"assemblies/System.IO.IsolatedStorage.dll": {
68-
"Size": 10155
68+
"Size": 10162
6969
},
7070
"assemblies/System.Linq.dll": {
71-
"Size": 19493
71+
"Size": 19494
7272
},
7373
"assemblies/System.Linq.Expressions.dll": {
74-
"Size": 163967
74+
"Size": 163942
7575
},
7676
"assemblies/System.Net.Http.dll": {
77-
"Size": 67013
77+
"Size": 65586
7878
},
7979
"assemblies/System.Net.Primitives.dll": {
8080
"Size": 22021
8181
},
8282
"assemblies/System.Net.Requests.dll": {
83-
"Size": 3745
83+
"Size": 3743
8484
},
8585
"assemblies/System.ObjectModel.dll": {
86-
"Size": 8173
86+
"Size": 8166
8787
},
8888
"assemblies/System.Private.CoreLib.dll": {
89-
"Size": 775898
89+
"Size": 780751
9090
},
9191
"assemblies/System.Private.DataContractSerialization.dll": {
92-
"Size": 192465
92+
"Size": 192055
9393
},
9494
"assemblies/System.Private.Uri.dll": {
95-
"Size": 42550
95+
"Size": 42554
9696
},
9797
"assemblies/System.Private.Xml.dll": {
98-
"Size": 215747
98+
"Size": 215680
9999
},
100100
"assemblies/System.Private.Xml.Linq.dll": {
101-
"Size": 16813
101+
"Size": 16807
102102
},
103103
"assemblies/System.Runtime.dll": {
104104
"Size": 2794
105105
},
106106
"assemblies/System.Runtime.InteropServices.dll": {
107-
"Size": 2273
107+
"Size": 2272
108108
},
109109
"assemblies/System.Runtime.Serialization.dll": {
110-
"Size": 1950
110+
"Size": 1949
111111
},
112112
"assemblies/System.Runtime.Serialization.Formatters.dll": {
113-
"Size": 2683
113+
"Size": 2681
114114
},
115115
"assemblies/System.Runtime.Serialization.Primitives.dll": {
116-
"Size": 3856
116+
"Size": 3854
117117
},
118118
"assemblies/System.Security.Cryptography.dll": {
119-
"Size": 7949
119+
"Size": 7947
120120
},
121121
"assemblies/System.Text.RegularExpressions.dll": {
122-
"Size": 154213
122+
"Size": 155427
123123
},
124124
"assemblies/System.Xml.dll": {
125125
"Size": 1837
126126
},
127127
"assemblies/System.Xml.Linq.dll": {
128-
"Size": 1862
128+
"Size": 1861
129129
},
130130
"assemblies/UnnamedProject.dll": {
131-
"Size": 117372
131+
"Size": 117398
132132
},
133133
"assemblies/Xamarin.AndroidX.Activity.dll": {
134134
"Size": 5872
@@ -200,22 +200,22 @@
200200
"Size": 3090508
201201
},
202202
"lib/arm64-v8a/libmonodroid.so": {
203-
"Size": 434632
203+
"Size": 379008
204204
},
205205
"lib/arm64-v8a/libmonosgen-2.0.so": {
206-
"Size": 3078808
206+
"Size": 3077928
207207
},
208208
"lib/arm64-v8a/libSystem.IO.Compression.Native.so": {
209209
"Size": 723840
210210
},
211211
"lib/arm64-v8a/libSystem.Native.so": {
212-
"Size": 94136
212+
"Size": 94232
213213
},
214214
"lib/arm64-v8a/libSystem.Security.Cryptography.Native.Android.so": {
215215
"Size": 149552
216216
},
217217
"lib/arm64-v8a/libxamarin-app.so": {
218-
"Size": 333376
218+
"Size": 333336
219219
},
220220
"META-INF/android.support.design_material.version": {
221221
"Size": 12
@@ -782,7 +782,7 @@
782782
"Size": 470
783783
},
784784
"res/drawable-hdpi-v4/icon.png": {
785-
"Size": 4762
785+
"Size": 2178
786786
},
787787
"res/drawable-hdpi-v4/notification_bg_low_normal.9.png": {
788788
"Size": 212
@@ -992,7 +992,7 @@
992992
"Size": 309
993993
},
994994
"res/drawable-mdpi-v4/icon.png": {
995-
"Size": 2200
995+
"Size": 1490
996996
},
997997
"res/drawable-mdpi-v4/notification_bg_low_normal.9.png": {
998998
"Size": 215
@@ -1220,7 +1220,7 @@
12201220
"Size": 593
12211221
},
12221222
"res/drawable-xhdpi-v4/icon.png": {
1223-
"Size": 7462
1223+
"Size": 3098
12241224
},
12251225
"res/drawable-xhdpi-v4/notification_bg_low_normal.9.png": {
12261226
"Size": 221
@@ -1385,7 +1385,7 @@
13851385
"Size": 868
13861386
},
13871387
"res/drawable-xxhdpi-v4/icon.png": {
1388-
"Size": 13092
1388+
"Size": 4674
13891389
},
13901390
"res/drawable-xxxhdpi-v4/abc_btn_check_to_on_mtrl_000.png": {
13911391
"Size": 275
@@ -1472,7 +1472,7 @@
14721472
"Size": 1155
14731473
},
14741474
"res/drawable-xxxhdpi-v4/icon.png": {
1475-
"Size": 20118
1475+
"Size": 6832
14761476
},
14771477
"res/drawable/abc_btn_borderless_material.xml": {
14781478
"Size": 588
@@ -1970,5 +1970,5 @@
19701970
"Size": 341228
19711971
}
19721972
},
1973-
"PackageSize": 7971397
1973+
"PackageSize": 7930437
19741974
}

0 commit comments

Comments
 (0)