1 /**
2  * Copyright (C) 2021 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 #include <androidfw/ApkAssets.h>
18 
19 #include <vector>
20 #include "../includes/common.h"
21 #include "../includes/memutils.h"
22 
23 using android::LoadedArsc;
24 
25 bool testInProgress = false;
26 char enable_selective_overload = ENABLE_NONE;
27 FILE *file = nullptr;
28 
29 struct sigaction new_action, old_action;
sigsegv_handler(int signum,siginfo_t * info,void * context)30 void sigsegv_handler(int signum, siginfo_t *info, void *context) {
31     if (testInProgress && info->si_signo == SIGSEGV) {
32         (*old_action.sa_sigaction)(signum, info, context);
33         return;
34     }
35     _exit(EXIT_FAILURE);
36 }
37 
exitHandler(void)38 void exitHandler(void) {
39     if (file) {
40         fclose(file);
41         file = nullptr;
42     }
43 }
44 
main(int argc,char ** argv)45 int main(int argc, char **argv) {
46     atexit(exitHandler);
47     sigemptyset(&new_action.sa_mask);
48     new_action.sa_flags = SA_SIGINFO;
49     new_action.sa_sigaction = sigsegv_handler;
50     sigaction(SIGSEGV, &new_action, &old_action);
51     FAIL_CHECK(argc >= 2);
52     file = fopen(argv[1], "r");
53     FAIL_CHECK(file);
54     fseek(file, 0, SEEK_END);
55     size_t size = ftell(file);
56     fseek(file, 0, SEEK_SET);
57     enable_selective_overload = ENABLE_ALL;
58     std::vector<uint8_t> buffer(size);
59     enable_selective_overload = ENABLE_FREE_CHECK | ENABLE_REALLOC_CHECK;
60     FAIL_CHECK(fread((void *)buffer.data(), 1, size, file) == size);
61     testInProgress = true;
62     LoadedArsc::Load(buffer.data(), size);
63     testInProgress = false;
64     return EXIT_SUCCESS;
65 }
66