• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.compatibility.common.util;
18 
19 import com.android.compatibility.common.util.CrashUtils.Config.BacktraceFilterPattern;
20 import com.google.common.collect.ImmutableList;
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import org.json.JSONArray;
25 import org.json.JSONException;
26 import org.json.JSONObject;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.JUnit4;
32 import java.util.List;
33 import java.util.regex.Pattern;
34 
35 /** Unit tests for {@link CrashUtils}. */
36 @RunWith(JUnit4.class)
37 public class CrashUtilsTest {
38 
39     private JSONArray mCrashes;
40 
41     @Before
setUp()42     public void setUp() throws IOException {
43         try (BufferedReader txtReader =
44                 new BufferedReader(
45                         new InputStreamReader(
46                                 getClass().getClassLoader().getResourceAsStream("logcat.txt")))) {
47             StringBuffer input = new StringBuffer();
48             String tmp;
49             while ((tmp = txtReader.readLine()) != null) {
50                 input.append(tmp + "\n");
51             }
52             mCrashes = CrashUtils.addAllCrashes(input.toString(), new JSONArray());
53         }
54     }
55 
56     @Test
testGetAllCrashes()57     public void testGetAllCrashes() throws Exception {
58         JSONArray expectedResults = new JSONArray();
59         expectedResults.put(
60                 createCrashJson(
61                         11071,
62                         11189,
63                         "AudioOut_D",
64                         "/system/bin/audioserver",
65                         "e9380000",
66                         "SIGSEGV",
67                         null,
68                         ImmutableList.of(
69                                 stackFrame(
70                                         "/system/lib/libaudioutils.so",
71                                         "memcpy_to_float_from_i16+5"),
72                                 stackFrame("/system/lib/libaudioflinger.so", null),
73                                 stackFrame("/system/lib/libaudioflinger.so", null),
74                                 stackFrame("/system/lib/libaudioflinger.so", null),
75                                 stackFrame("/system/lib/libaudioflinger.so", null),
76                                 stackFrame("/system/lib/libaudioflinger.so", null),
77                                 stackFrame(
78                                         "/system/lib/libutils.so",
79                                         "_ZN7android6Thread11_threadLoopEPv+140"),
80                                 stackFrame("/system/lib/libc.so", "_ZL15__pthread_startPv+22"),
81                                 stackFrame("/system/lib/libc.so", "__start_thread+6"))));
82         expectedResults.put(
83                 createCrashJson(
84                         12736,
85                         12761,
86                         "Binder:12736_2",
87                         "/system/bin/audioserver",
88                         "0",
89                         "SIGSEGV",
90                         null,
91                         ImmutableList.of(
92                                 stackFrame("/system/lib/libc.so", "strlen+71"),
93                                 stackFrame("/system/lib/libc.so", "__strlen_chk+4"),
94                                 stackFrame(
95                                         "/system/lib/libutils.so", "_ZN7android7String8C2EPKc+12"),
96                                 stackFrame(
97                                         "/system/lib/libaudiopolicymanagerdefault.so",
98                                         "_ZNK7android18HwModuleCollection19getDeviceDescriptorEjPKcS2_b+458"),
99                                 stackFrame(
100                                         "/system/lib/libaudiopolicymanagerdefault.so",
101                                         "_ZN7android18AudioPolicyManager27setDeviceConnectionStateIntEj24"
102                                             + "audio_policy_dev_state_tPKcS3_+178"),
103                                 stackFrame("/system/lib/libaudiopolicyservice.so", null),
104                                 stackFrame(
105                                         "/system/lib/libmedia.so",
106                                         "_ZN7android20BnAudioPolicyService10onTransactEjRKNS_6ParcelEPS1_j+1256"),
107                                 stackFrame(
108                                         "/system/lib/libbinder.so",
109                                         "_ZN7android7BBinder8transactEjRKNS_6ParcelEPS1_j+70"),
110                                 stackFrame(
111                                         "/system/lib/libbinder.so",
112                                         "_ZN7android14IPCThreadState14executeCommandEi+702"),
113                                 stackFrame(
114                                         "/system/lib/libbinder.so",
115                                         "_ZN7android14IPCThreadState20getAndExecuteCommandEv+114"),
116                                 stackFrame(
117                                         "/system/lib/libbinder.so",
118                                         "_ZN7android14IPCThreadState14joinThreadPoolEb+46"),
119                                 stackFrame("/system/lib/libbinder.so", null),
120                                 stackFrame(
121                                         "/system/lib/libutils.so",
122                                         "_ZN7android6Thread11_threadLoopEPv+140"),
123                                 stackFrame("/system/lib/libc.so", "_ZL15__pthread_startPv+22"),
124                                 stackFrame("/system/lib/libc.so", "__start_thread+6"))));
125         expectedResults.put(
126                 createCrashJson(
127                         26201,
128                         26227,
129                         "Binder:26201_3",
130                         "/system/bin/audioserver",
131                         "0",
132                         "SIGSEGV",
133                         null,
134                         ImmutableList.of(
135                                 stackFrame("/system/lib/libc.so", "strlen+71"),
136                                 stackFrame("/system/lib/libc.so", "__strlen_chk+4"),
137                                 stackFrame(
138                                         "/system/lib/libutils.so", "_ZN7android7String8C2EPKc+12"),
139                                 stackFrame(
140                                         "/system/lib/libaudiopolicymanagerdefault.so",
141                                         "_ZNK7android18HwModuleCollection19getDeviceDescriptorEjPKcS2_b+458"),
142                                 stackFrame(
143                                         "/system/lib/libaudiopolicymanagerdefault.so",
144                                         "_ZN7android18AudioPolicyManager27setDeviceConnectionStateIntEj24"
145                                             + "audio_policy_dev_state_tPKcS3_+178"),
146                                 stackFrame("/system/lib/libaudiopolicyservice.so", null),
147                                 stackFrame(
148                                         "/system/lib/libmedia.so",
149                                         "_ZN7android20BnAudioPolicyService10onTransactEjRKNS_6ParcelEPS1_j+1256"),
150                                 stackFrame(
151                                         "/system/lib/libbinder.so",
152                                         "_ZN7android7BBinder8transactEjRKNS_6ParcelEPS1_j+70"),
153                                 stackFrame(
154                                         "/system/lib/libbinder.so",
155                                         "_ZN7android14IPCThreadState14executeCommandEi+702"),
156                                 stackFrame(
157                                         "/system/lib/libbinder.so",
158                                         "_ZN7android14IPCThreadState20getAndExecuteCommandEv+114"),
159                                 stackFrame(
160                                         "/system/lib/libbinder.so",
161                                         "_ZN7android14IPCThreadState14joinThreadPoolEb+46"),
162                                 stackFrame("/system/lib/libbinder.so", null),
163                                 stackFrame(
164                                         "/system/lib/libutils.so",
165                                         "_ZN7android6Thread11_threadLoopEPv+140"),
166                                 stackFrame("/system/lib/libc.so", "_ZL15__pthread_startPv+22"),
167                                 stackFrame("/system/lib/libc.so", "__start_thread+6"))));
168         expectedResults.put(
169                 createCrashJson(
170                         26246,
171                         26282,
172                         "Binder:26246_5",
173                         "/system/bin/audioserver",
174                         "0",
175                         "SIGSEGV",
176                         null,
177                         ImmutableList.of(
178                                 stackFrame("/system/lib/libc.so", "strlen+71"),
179                                 stackFrame("/system/lib/libc.so", "__strlen_chk+4"),
180                                 stackFrame(
181                                         "/system/lib/libutils.so", "_ZN7android7String8C2EPKc+12"),
182                                 stackFrame(
183                                         "/system/lib/libaudiopolicymanagerdefault.so",
184                                         "_ZNK7android18HwModuleCollection19getDeviceDescriptorEjPKcS2_b+458"),
185                                 stackFrame(
186                                         "/system/lib/libaudiopolicymanagerdefault.so",
187                                         "_ZN7android18AudioPolicyManager27setDeviceConnectionStateIntEj24"
188                                             + "audio_policy_dev_state_tPKcS3_+178"),
189                                 stackFrame("/system/lib/libaudiopolicyservice.so", null),
190                                 stackFrame(
191                                         "/system/lib/libmedia.so",
192                                         "_ZN7android20BnAudioPolicyService10onTransactEjRKNS_6ParcelEPS1_j+1256"),
193                                 stackFrame(
194                                         "/system/lib/libbinder.so",
195                                         "_ZN7android7BBinder8transactEjRKNS_6ParcelEPS1_j+70"),
196                                 stackFrame(
197                                         "/system/lib/libbinder.so",
198                                         "_ZN7android14IPCThreadState14executeCommandEi+702"),
199                                 stackFrame(
200                                         "/system/lib/libbinder.so",
201                                         "_ZN7android14IPCThreadState20getAndExecuteCommandEv+114"),
202                                 stackFrame(
203                                         "/system/lib/libbinder.so",
204                                         "_ZN7android14IPCThreadState14joinThreadPoolEb+46"),
205                                 stackFrame("/system/lib/libbinder.so", null),
206                                 stackFrame(
207                                         "/system/lib/libutils.so",
208                                         "_ZN7android6Thread11_threadLoopEPv+140"),
209                                 stackFrame("/system/lib/libc.so", "_ZL15__pthread_startPv+22"),
210                                 stackFrame("/system/lib/libc.so", "__start_thread+6"))));
211         expectedResults.put(
212                 createCrashJson(
213                         245,
214                         245,
215                         "installd",
216                         "/system/bin/installd",
217                         null,
218                         "SIGABRT",
219                         "'utils.cpp:67] Check failed: is_valid_package_name(package_name) == 0 '",
220                         ImmutableList.of(
221                                 stackFrame("/system/lib64/libc.so", "tgkill+8"),
222                                 stackFrame("/system/lib64/libc.so", "pthread_kill+64"),
223                                 stackFrame("/system/lib64/libc.so", "raise+24"),
224                                 stackFrame("/system/lib64/libc.so", "abort+52"),
225                                 stackFrame(
226                                         "/system/lib64/libbase.so",
227                                         "_ZN7android4base10LogMessageD1Ev+1084"),
228                                 stackFrame("/system/bin/installd", null),
229                                 stackFrame("/system/bin/installd", null),
230                                 stackFrame("/system/bin/installd", null),
231                                 stackFrame("/system/bin/installd", null),
232                                 stackFrame("/system/lib64/libc.so", "__libc_init+88"),
233                                 stackFrame("/system/bin/installd", null))));
234         expectedResults.put(
235                 createCrashJson(
236                         6371,
237                         8072,
238                         "media.codec",
239                         "omx@1.0-service",
240                         "ed000000",
241                         "SIGSEGV",
242                         null,
243                         ImmutableList.of(
244                                 stackFrame("/system/lib/libstagefright_soft_hevcdec.so", null))));
245         expectedResults.put(
246                 createCrashJson(
247                         8373,
248                         8414,
249                         "loo",
250                         "com.android.bluetooth",
251                         null,
252                         "SIGABRT",
253                         "'[FATAL:allocation_tracker.cc(143)] Check failed: map_entry !="
254                                 + " allocations.end().",
255                         ImmutableList.of(
256                                 stackFrame("/system/lib64/libc.so", "abort+120"),
257                                 stackFrame(
258                                         "/system/lib64/libchrome.so",
259                                         "base::debug::BreakDebugger()+20"),
260                                 stackFrame(
261                                         "/system/lib64/libchrome.so",
262                                         "logging::LogMessage::~LogMessage()+1068"),
263                                 stackFrame(
264                                         "/system/lib64/hw/bluetooth.default.so",
265                                         "allocation_tracker_notify_free(unsigned char, void*)+720"),
266                                 stackFrame(
267                                         "/system/lib64/hw/bluetooth.default.so",
268                                         "osi_free(void*)+20"),
269                                 stackFrame(
270                                         "/system/lib64/hw/bluetooth.default.so",
271                                         "l2c_fcr_cleanup(t_l2c_ccb*)+92"),
272                                 stackFrame(
273                                         "/system/lib64/hw/bluetooth.default.so",
274                                         "l2cu_release_ccb(t_l2c_ccb*)+176"),
275                                 stackFrame(
276                                         "/system/lib64/hw/bluetooth.default.so",
277                                         "l2c_csm_execute(t_l2c_ccb*, unsigned short, void*)+1852"),
278                                 stackFrame(
279                                         "/system/lib64/hw/bluetooth.default.so",
280                                         "L2CA_DisconnectRsp(unsigned short)+92"),
281                                 stackFrame(
282                                         "/system/lib64/hw/bluetooth.default.so",
283                                         "sdp_disconnect_ind(unsigned short, bool)+52"),
284                                 stackFrame(
285                                         "/system/lib64/hw/bluetooth.default.so",
286                                         "l2c_csm_execute(t_l2c_ccb*, unsigned short, void*)+3600"),
287                                 stackFrame(
288                                         "/system/lib64/hw/bluetooth.default.so",
289                                         "l2c_rcv_acl_data(BT_HDR*)+3980"),
290                                 stackFrame(
291                                         "/system/lib64/libchrome.so",
292                                         "base::debug::TaskAnnotator::RunTask(char const*, "
293                                                 + "base::PendingTask const&)+188"),
294                                 stackFrame(
295                                         "/system/lib64/libchrome.so",
296                                         "base::MessageLoop::RunTask(base::PendingTask const&)+444"),
297                                 stackFrame(
298                                         "/system/lib64/libchrome.so",
299                                         "base::MessageLoop::DeferOrRunPendingTask(base::PendingTask)+52"),
300                                 stackFrame(
301                                         "/system/lib64/libchrome.so",
302                                         "base::MessageLoop::DoWork()+356"),
303                                 stackFrame(
304                                         "/system/lib64/libchrome.so",
305                                         "base::MessagePumpDefault::Run(base::MessagePump::Delegate*)+220"),
306                                 stackFrame(
307                                         "/system/lib64/libchrome.so", "base::RunLoop::Run()+136"),
308                                 stackFrame(
309                                         "/system/lib64/hw/bluetooth.default.so",
310                                         "btu_message_loop_run(void*)+248"),
311                                 stackFrame(
312                                         "/system/lib64/hw/bluetooth.default.so",
313                                         "work_queue_read_cb(void*)+92"),
314                                 stackFrame(
315                                         "/system/lib64/hw/bluetooth.default.so",
316                                         "run_reactor(reactor_t*, int)+320"),
317                                 stackFrame(
318                                         "/system/lib64/hw/bluetooth.default.so",
319                                         "reactor_start(reactor_t*)+84"),
320                                 stackFrame(
321                                         "/system/lib64/hw/bluetooth.default.so",
322                                         "run_thread(void*)+184"),
323                                 stackFrame("/system/lib64/libc.so", "__pthread_start(void*)+36"),
324                                 stackFrame("/system/lib64/libc.so", "__start_thread+68"))));
325         expectedResults.put(
326                 createCrashJson(
327                         8080,
328                         11665,
329                         "generic",
330                         "/system/bin/mediaserver",
331                         null,
332                         "SIGABRT",
333                         "'frameworks/av/media/libstagefright/MPEG4Extractor.cpp:6853 CHECK_EQ("
334                                 + " (unsigned)ptr[0],1u) failed: 129 vs. 1'",
335                         ImmutableList.of(
336                                 stackFrame("/system/lib/libc.so", "tgkill+12"),
337                                 stackFrame("/system/lib/libc.so", "pthread_kill+32"),
338                                 stackFrame("/system/lib/libc.so", "raise+10"),
339                                 stackFrame("/system/lib/libc.so", "__libc_android_abort+34"),
340                                 stackFrame("/system/lib/libc.so", "abort+4"),
341                                 stackFrame("/system/lib/libcutils.so", "__android_log_assert+86"),
342                                 stackFrame(
343                                         "/system/lib/libstagefright.so",
344                                         "_ZN7android14MPEG4Extractor25avcc_getCodecSpecificInfo"
345                                                 + "ERNS_2spINS_7ABufferEEEPKcj+392"),
346                                 stackFrame(
347                                         "/system/lib/libstagefright.so",
348                                         "_ZN7android14MPEG4Extractor15checkConfigDataEjRKNS_2spINS_8MetaDataEEE+218"),
349                                 stackFrame(
350                                         "/system/lib/libstagefright.so",
351                                         "_ZN7android14MPEG4Extractor12checkSupportEjRKNS_2spINS_8MetaDataEEE+136"),
352                                 stackFrame(
353                                         "/system/lib/libstagefright.so",
354                                         "_ZN7android14MPEG4Extractor10parseChunkEPxi+13060"),
355                                 stackFrame(
356                                         "/system/lib/libstagefright.so",
357                                         "_ZN7android14MPEG4Extractor10parseChunkEPxi+12508"),
358                                 stackFrame(
359                                         "/system/lib/libstagefright.so",
360                                         "_ZN7android14MPEG4Extractor10parseChunkEPxi+6174"),
361                                 stackFrame(
362                                         "/system/lib/libstagefright.so",
363                                         "_ZN7android14MPEG4Extractor10parseChunkEPxi+6174"),
364                                 stackFrame(
365                                         "/system/lib/libstagefright.so",
366                                         "_ZN7android14MPEG4Extractor10parseChunkEPxi+6174"),
367                                 stackFrame(
368                                         "/system/lib/libstagefright.so",
369                                         "_ZN7android14MPEG4Extractor10parseChunkEPxi+6174"),
370                                 stackFrame(
371                                         "/system/lib/libstagefright.so",
372                                         "_ZN7android14MPEG4Extractor10parseChunkEPxi+6174"),
373                                 stackFrame(
374                                         "/system/lib/libstagefright.so",
375                                         "_ZN7android14MPEG4Extractor12readMetaDataEv+94"),
376                                 stackFrame(
377                                         "/system/lib/libstagefright.so",
378                                         "_ZN7android14MPEG4Extractor11getMetaDataEv+10"),
379                                 stackFrame(
380                                         "/system/lib/libmediaplayerservice.so",
381                                         "_ZN7android8NuPlayer13GenericSource18initFromDataSourceEv+386"),
382                                 stackFrame(
383                                         "/system/lib/libmediaplayerservice.so",
384                                         "_ZN7android8NuPlayer13GenericSource14onPrepareAsyncEv+238"),
385                                 stackFrame(
386                                         "/system/lib/libstagefright_foundation.so",
387                                         "_ZN7android8AHandler14deliverMessageERKNS_2spINS_8AMessageEEE+16"),
388                                 stackFrame(
389                                         "/system/lib/libstagefright_foundation.so",
390                                         "_ZN7android8AMessage7deliverEv+54"),
391                                 stackFrame(
392                                         "/system/lib/libstagefright_foundation.so",
393                                         "_ZN7android7ALooper4loopEv+224"),
394                                 stackFrame(
395                                         "/system/lib/libutils.so",
396                                         "_ZN7android6Thread11_threadLoopEPv+112"),
397                                 stackFrame("/system/lib/libc.so", "_ZL15__pthread_startPv+30"),
398                                 stackFrame("/system/lib/libc.so", "__start_thread+6"))));
399         expectedResults.put(
400                 createCrashJson(
401                         11071,
402                         11189,
403                         "synthetic_thread",
404                         "synthetic_process_0",
405                         "e9380000",
406                         "SIGSEGV",
407                         null,
408                         ImmutableList.of(
409                                 stackFrame(
410                                         "/system/lib/libaudioutils.so",
411                                         "memcpy_to_float_from_i16+5"),
412                                 stackFrame("/system/lib/libaudioflinger.so", null),
413                                 stackFrame("/system/lib/libaudioflinger.so", null),
414                                 stackFrame("/system/lib/libaudioflinger.so", null),
415                                 stackFrame("/system/lib/libaudioflinger.so", null),
416                                 stackFrame("/system/lib/libaudioflinger.so", null),
417                                 stackFrame(
418                                         "/system/lib/libutils.so",
419                                         "_ZN7android6Thread11_threadLoopEPv+140"),
420                                 stackFrame("/system/lib/libc.so", "_ZL15__pthread_startPv+22"),
421                                 stackFrame("/system/lib/libc.so", "__start_thread+6"))));
422         expectedResults.put(
423                 createCrashJson(
424                         12736,
425                         12761,
426                         "synthetic_thread",
427                         "synthetic_process_1",
428                         "0",
429                         "SIGSEGV",
430                         null,
431                         ImmutableList.of(
432                                 stackFrame("/system/lib/libc.so", "strlen+71"),
433                                 stackFrame("/system/lib/libc.so", "__strlen_chk+4"),
434                                 stackFrame(
435                                         "/system/lib/libutils.so", "_ZN7android7String8C2EPKc+12"),
436                                 stackFrame(
437                                         "/system/lib/libaudiopolicymanagerdefault.so",
438                                         "_ZNK7android18HwModuleCollection19getDeviceDescriptorEjPKcS2_b+458"),
439                                 stackFrame(
440                                         "/system/lib/libaudiopolicymanagerdefault.so",
441                                         "_ZN7android18AudioPolicyManager27setDeviceConnectionStateIntEj24"
442                                             + "audio_policy_dev_state_tPKcS3_+178"),
443                                 stackFrame("/system/lib/libaudiopolicyservice.so", null),
444                                 stackFrame(
445                                         "/system/lib/libmedia.so",
446                                         "_ZN7android20BnAudioPolicyService10onTransactEjRKNS_6ParcelEPS1_j+1256"),
447                                 stackFrame(
448                                         "/system/lib/libbinder.so",
449                                         "_ZN7android7BBinder8transactEjRKNS_6ParcelEPS1_j+70"),
450                                 stackFrame(
451                                         "/system/lib/libbinder.so",
452                                         "_ZN7android14IPCThreadState14executeCommandEi+702"),
453                                 stackFrame(
454                                         "/system/lib/libbinder.so",
455                                         "_ZN7android14IPCThreadState20getAndExecuteCommandEv+114"),
456                                 stackFrame(
457                                         "/system/lib/libbinder.so",
458                                         "_ZN7android14IPCThreadState14joinThreadPoolEb+46"),
459                                 stackFrame("/system/lib/libbinder.so", null),
460                                 stackFrame(
461                                         "/system/lib/libutils.so",
462                                         "_ZN7android6Thread11_threadLoopEPv+140"),
463                                 stackFrame("/system/lib/libc.so", "_ZL15__pthread_startPv+22"),
464                                 stackFrame("/system/lib/libc.so", "__start_thread+6"))));
465         expectedResults.put(
466                 createCrashJson(
467                         8925,
468                         8925,
469                         "CVE-2021-0684",
470                         "/data/local/tmp/CVE-2021-0684",
471                         "7e772b8cfbe0",
472                         "SIGSEGV",
473                         null,
474                         ImmutableList.of(
475                                 stackFrame(
476                                         "/system/lib64/libinputreader.so",
477                                         "android::TouchInputMapper::assignPointerIds("
478                                                 + "android::TouchInputMapper::RawState const*, "
479                                                 + "android::TouchInputMapper::RawState*)+37"),
480                                 stackFrame(
481                                         "/system/lib64/libinputreader.so",
482                                         "android::TouchInputMapper::sync(long, long)+285"),
483                                 stackFrame(
484                                         "/system/lib64/libinputreader.so",
485                                         "android::MultiTouchInputMapper::process(android::RawEvent"
486                                                 + " const*)+14"),
487                                 stackFrame("/data/local/tmp/CVE-2021-0684", null),
488                                 stackFrame("/data/local/tmp/CVE-2021-0684", null),
489                                 stackFrame(
490                                         "/apex/com.android.runtime/lib64/bionic/libc.so",
491                                         "__libc_init+86"))));
492 
493         Assert.assertEquals(
494                 expectedResults.toString() + "\n" + mCrashes.toString() + "\n",
495                 expectedResults.toString(),
496                 mCrashes.toString());
497     }
498 
499     /** Helper method to shorten code for readability. */
stackFrame(String filename, String method)500     private JSONObject stackFrame(String filename, String method) throws JSONException {
501         return new JSONObject().put(CrashUtils.FILENAME, filename).put(CrashUtils.METHOD, method);
502     }
503 
createCrashJson( int pid, int tid, String name, String process, String faultaddress, String signal, String abortMessage)504     public JSONObject createCrashJson(
505             int pid,
506             int tid,
507             String name,
508             String process,
509             String faultaddress,
510             String signal,
511             String abortMessage) {
512         return createCrashJson(
513                 pid, tid, name, process, faultaddress, signal, abortMessage, ImmutableList.of());
514     }
515 
createCrashJson( int pid, int tid, String name, String process, String faultaddress, String signal, String abortMessage, List<JSONObject> stacktrace)516     public JSONObject createCrashJson(
517             int pid,
518             int tid,
519             String name,
520             String process,
521             String faultaddress,
522             String signal,
523             String abortMessage,
524             List<JSONObject> stacktrace) {
525         JSONObject json = new JSONObject();
526         try {
527             json.put(CrashUtils.PID, pid);
528             json.put(CrashUtils.TID, tid);
529             json.put(CrashUtils.NAME, name);
530             json.put(CrashUtils.PROCESS, process);
531             json.put(CrashUtils.FAULT_ADDRESS, faultaddress);
532             json.put(CrashUtils.SIGNAL, signal);
533             json.put(CrashUtils.ABORT_MESSAGE, abortMessage);
534 
535             JSONArray stacktraceJson = new JSONArray();
536             for (JSONObject stackframe : stacktrace) {
537                 stacktraceJson.put(stackframe);
538             }
539             json.put(CrashUtils.BACKTRACE, stacktraceJson);
540         } catch (JSONException e) {}
541         return json;
542     }
543 
544     @Test
testValidCrash()545     public void testValidCrash() throws Exception {
546         Assert.assertTrue(CrashUtils.securityCrashDetected(mCrashes, new CrashUtils.Config()
547                 .checkMinAddress(true)
548                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))));
549     }
550 
551     @Test
testMissingName()552     public void testMissingName() throws Exception {
553         Assert.assertFalse(CrashUtils.securityCrashDetected(mCrashes, new CrashUtils.Config()
554                 .checkMinAddress(true)
555                 .setProcessPatterns(Pattern.compile(""))));
556     }
557 
558     @Test
testSIGABRT()559     public void testSIGABRT() throws Exception {
560         Assert.assertFalse(CrashUtils.securityCrashDetected(mCrashes, new CrashUtils.Config()
561                 .checkMinAddress(true)
562                 .setProcessPatterns(Pattern.compile("installd"))));
563     }
564 
565     @Test
testFaultAddressBelowMin()566     public void testFaultAddressBelowMin() throws Exception {
567         Assert.assertFalse(CrashUtils.securityCrashDetected(mCrashes, new CrashUtils.Config()
568                 .checkMinAddress(true)
569                 .setProcessPatterns(Pattern.compile("synthetic_process_1"))));
570     }
571 
572     @Test
testIgnoreMinAddressCheck()573     public void testIgnoreMinAddressCheck() throws Exception {
574         Assert.assertTrue(CrashUtils.securityCrashDetected(mCrashes, new CrashUtils.Config()
575                 .checkMinAddress(false)
576                 .setProcessPatterns(Pattern.compile("synthetic_process_1"))));
577     }
578 
579     @Test
testBadAbortMessage()580     public void testBadAbortMessage() throws Exception {
581         Assert.assertFalse(CrashUtils.securityCrashDetected(mCrashes, new CrashUtils.Config()
582                 .checkMinAddress(true)
583                 .setProcessPatterns(Pattern.compile("generic"))));
584     }
585 
586     @Test
testGoodAndBadCrashes()587     public void testGoodAndBadCrashes() throws Exception {
588         Assert.assertTrue(CrashUtils.securityCrashDetected(mCrashes, new CrashUtils.Config()
589                 .checkMinAddress(true)
590                 .setProcessPatterns(
591                         Pattern.compile("synthetic_process_0"),
592                         Pattern.compile("generic"))));
593     }
594 
595     @Test
testNullFaultAddress()596     public void testNullFaultAddress() throws Exception {
597         JSONArray crashes = new JSONArray();
598         crashes.put(
599                 createCrashJson(8373, 8414, "loo", "com.android.bluetooth", null, "SIGSEGV", ""));
600         Assert.assertTrue(CrashUtils.securityCrashDetected(crashes, new CrashUtils.Config()
601                 .checkMinAddress(true)
602                 .setProcessPatterns(Pattern.compile("com\\.android\\.bluetooth"))));
603     }
604 
605     @Test
testAbortMessageInclude()606     public void testAbortMessageInclude() throws Exception {
607         JSONArray crashes = new JSONArray();
608         crashes.put(createCrashJson(8373, 8414, "loo", "com.android.bluetooth", null, "SIGABRT",
609               "'[FATAL:allocation_tracker.cc(143)] Check failed: map_entry != allocations.end()."));
610         Assert.assertTrue(CrashUtils.securityCrashDetected(crashes, new CrashUtils.Config()
611                 .appendSignals(CrashUtils.SIGABRT)
612                 .appendAbortMessageIncludes("Check failed:")
613                 .setProcessPatterns(Pattern.compile("com\\.android\\.bluetooth"))));
614 
615         Assert.assertFalse(CrashUtils.securityCrashDetected(crashes, new CrashUtils.Config()
616                 .appendSignals(CrashUtils.SIGABRT)
617                 .appendAbortMessageIncludes("include not matches")
618                 .setProcessPatterns(Pattern.compile("com\\.android\\.bluetooth"))));
619     }
620 
621     @Test
testAbortMessageExclude()622     public void testAbortMessageExclude() throws Exception {
623         JSONArray crashes = new JSONArray();
624         crashes.put(createCrashJson(8373, 8414, "loo", "com.android.bluetooth", null, "SIGABRT",
625               "'[FATAL:allocation_tracker.cc(143)] Check failed: map_entry != allocations.end()."));
626         Assert.assertFalse(CrashUtils.securityCrashDetected(crashes, new CrashUtils.Config()
627                 .appendSignals(CrashUtils.SIGABRT)
628                 .appendAbortMessageExcludes("Check failed:")
629                 .setProcessPatterns(Pattern.compile("com\\.android\\.bluetooth"))));
630 
631         Assert.assertTrue(CrashUtils.securityCrashDetected(crashes, new CrashUtils.Config()
632                 .appendSignals(CrashUtils.SIGABRT)
633                 .appendAbortMessageExcludes("exclude not matches")
634                 .setProcessPatterns(Pattern.compile("com\\.android\\.bluetooth"))));
635     }
636 
637     @Test
testAbortMessageExcludeCannotLink()638     public void testAbortMessageExcludeCannotLink() throws Exception {
639         JSONArray crashes = new JSONArray();
640         crashes.put(createCrashJson(
641                 18959, 18959, "CVE-2020-0073", "/data/local/tmp/CVE-2020-0073", null, "SIGABRT",
642                 "'CANNOT LINK EXECUTABLE \"/data/local/tmp/CVE-2020-0073\": library "
643                 + "\"libnfc-nci.so\" (\"(default)\", \"/data/local/tmp/CVE-2020-0073\", \"\") "
644                 + "not found'"));
645         Assert.assertFalse(CrashUtils.securityCrashDetected(crashes, new CrashUtils.Config()
646                 .appendSignals(CrashUtils.SIGABRT)
647                 .setProcessPatterns(Pattern.compile("CVE-2020-0073"))));
648 
649         crashes.put(createCrashJson(
650                 5105, 5105, "CVE-2015-6616-2", "/data/local/tmp/CVE-2015-6616-2", null, "SIGABRT",
651                 "'CANNOT LINK EXECUTABLE \"/data/local/tmp/CVE-2015-6616-2\": "
652                 + "cannot locate symbol \""
653                 + "_ZN7android14MediaExtractor17CreateFromServiceERKNS_2spINS_10DataSourceEEEPKc"
654                 + "\" referenced by \"/data/local/tmp/CVE-2015-6616-2\"...'"));
655         Assert.assertFalse(CrashUtils.securityCrashDetected(crashes, new CrashUtils.Config()
656                 .appendSignals(CrashUtils.SIGABRT)
657                 .setProcessPatterns(Pattern.compile("CVE-2015-6616-2"))));
658 
659     }
660 
661     @Test
testBacktraceFilterIncludeFilename()662     public void testBacktraceFilterIncludeFilename() throws Exception {
663         Assert.assertTrue(
664                 CrashUtils.securityCrashDetected(
665                         mCrashes,
666                         new CrashUtils.Config()
667                                 .checkMinAddress(true)
668                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
669                                 .setBacktraceIncludes(
670                                         new BacktraceFilterPattern("libaudioutils", null))));
671         Assert.assertTrue(
672                 CrashUtils.securityCrashDetected(
673                         mCrashes,
674                         new CrashUtils.Config()
675                                 .checkMinAddress(true)
676                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
677                                 .setBacktraceIncludes(
678                                         new BacktraceFilterPattern("libstagefright", null),
679                                         new BacktraceFilterPattern("libaudioflinger\\.so", null))));
680         Assert.assertFalse(
681                 CrashUtils.securityCrashDetected(
682                         mCrashes,
683                         new CrashUtils.Config()
684                                 .checkMinAddress(true)
685                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
686                                 .setBacktraceIncludes(
687                                         new BacktraceFilterPattern("libstagefright", null))));
688     }
689 
690     @Test
testBacktraceFilterExcludeFilename()691     public void testBacktraceFilterExcludeFilename() throws Exception {
692         Assert.assertFalse(
693                 CrashUtils.securityCrashDetected(
694                         mCrashes,
695                         new CrashUtils.Config()
696                                 .checkMinAddress(true)
697                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
698                                 .setBacktraceExcludes(
699                                         new BacktraceFilterPattern("libaudioutils", null))));
700         Assert.assertFalse(
701                 CrashUtils.securityCrashDetected(
702                         mCrashes,
703                         new CrashUtils.Config()
704                                 .checkMinAddress(true)
705                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
706                                 .setBacktraceExcludes(
707                                         new BacktraceFilterPattern("libstagefright", null),
708                                         new BacktraceFilterPattern("libaudioflinger\\.so", null))));
709         Assert.assertTrue(
710                 CrashUtils.securityCrashDetected(
711                         mCrashes,
712                         new CrashUtils.Config()
713                                 .checkMinAddress(true)
714                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
715                                 .setBacktraceExcludes(
716                                         new BacktraceFilterPattern("libstagefright", null))));
717     }
718 
719     @Test
testBacktraceFilterIncludeMethodName()720     public void testBacktraceFilterIncludeMethodName() throws Exception {
721         Assert.assertTrue(
722                 CrashUtils.securityCrashDetected(
723                         mCrashes,
724                         new CrashUtils.Config()
725                                 .checkMinAddress(true)
726                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
727                                 .setBacktraceIncludes(
728                                         new BacktraceFilterPattern(null, "memcpy_to_float"))));
729         Assert.assertTrue(
730                 CrashUtils.securityCrashDetected(
731                         mCrashes,
732                         new CrashUtils.Config()
733                                 .checkMinAddress(true)
734                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
735                                 .setBacktraceIncludes(
736                                         new BacktraceFilterPattern(null, "strlen"),
737                                         new BacktraceFilterPattern(null, "memcpy_[^_]+_float"))));
738         Assert.assertFalse(
739                 CrashUtils.securityCrashDetected(
740                         mCrashes,
741                         new CrashUtils.Config()
742                                 .checkMinAddress(true)
743                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
744                                 .setBacktraceIncludes(new BacktraceFilterPattern(null, "strlen"))));
745     }
746 
747     @Test
testBacktraceFilterExcludeMethodName()748     public void testBacktraceFilterExcludeMethodName() throws Exception {
749         Assert.assertFalse(
750                 CrashUtils.securityCrashDetected(
751                         mCrashes,
752                         new CrashUtils.Config()
753                                 .checkMinAddress(true)
754                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
755                                 .setBacktraceExcludes(
756                                         new BacktraceFilterPattern(null, "memcpy_to_float"))));
757         Assert.assertFalse(
758                 CrashUtils.securityCrashDetected(
759                         mCrashes,
760                         new CrashUtils.Config()
761                                 .checkMinAddress(true)
762                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
763                                 .setBacktraceExcludes(
764                                         new BacktraceFilterPattern(null, "strlen"),
765                                         new BacktraceFilterPattern(null, "memcpy_[^_]+_float"))));
766         Assert.assertTrue(
767                 CrashUtils.securityCrashDetected(
768                         mCrashes,
769                         new CrashUtils.Config()
770                                 .checkMinAddress(true)
771                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
772                                 .setBacktraceExcludes(new BacktraceFilterPattern(null, "strlen"))));
773     }
774 
775     @Test
testBacktraceFilterCombinations()776     public void testBacktraceFilterCombinations() throws Exception {
777         Assert.assertTrue(
778                 CrashUtils.securityCrashDetected(
779                         mCrashes,
780                         new CrashUtils.Config()
781                                 .checkMinAddress(true)
782                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
783                                 .setBacktraceIncludes(new BacktraceFilterPattern(null, null))));
784         Assert.assertTrue(
785                 CrashUtils.securityCrashDetected(
786                         mCrashes,
787                         new CrashUtils.Config()
788                                 .checkMinAddress(true)
789                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
790                                 .setBacktraceIncludes(
791                                         new BacktraceFilterPattern("libaudioutils", "memcpy"))));
792         Assert.assertFalse(
793                 CrashUtils.securityCrashDetected(
794                         mCrashes,
795                         new CrashUtils.Config()
796                                 .checkMinAddress(true)
797                                 .setProcessPatterns(Pattern.compile("synthetic_process_0"))
798                                 .setBacktraceIncludes(
799                                         new BacktraceFilterPattern("libaudioutils", "strlen"))));
800     }
801 }
802