1 /**
2  * Copyright (C) 2022 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 <dlfcn.h>
18 
19 #include "../includes/common.h"
20 
21 #define private public
22 #include <media/stagefright/rtsp/AAVCAssembler.h>
23 
24 using namespace android;
25 
26 bool isOverloadingEnabled = false;
27 
28 bool isTestInProgress = false;
29 
30 struct sigaction newAction, oldAction;
31 
32 static void *(*realMalloc)(size_t) = nullptr;
33 
malloc(size_t size)34 void *malloc(size_t size) {
35     if (!realMalloc) {
36         realMalloc = (void *(*)(size_t))dlsym(RTLD_NEXT, "malloc");
37         if (!realMalloc) {
38             return nullptr;
39         }
40     }
41     if (isOverloadingEnabled && (size == 0)) {
42         size_t pageSize = sysconf(_SC_PAGE_SIZE);
43         void *ptr = memalign(pageSize, pageSize);
44         mprotect(ptr, pageSize, PROT_NONE);
45         return ptr;
46     }
47     return realMalloc(size);
48 }
49 
sigsegv_handler(int signum,siginfo_t * info,void * context)50 void sigsegv_handler(int signum, siginfo_t *info, void *context) {
51     if (isTestInProgress && info->si_signo == SIGSEGV) {
52         (*oldAction.sa_sigaction)(signum, info, context);
53         return;
54     }
55     _exit(EXIT_FAILURE);
56 }
57 
main()58 int main() {
59     sigemptyset(&newAction.sa_mask);
60     newAction.sa_flags = SA_SIGINFO;
61     newAction.sa_sigaction = sigsegv_handler;
62     sigaction(SIGSEGV, &newAction, &oldAction);
63 
64     sp<ABuffer> buffer(new ABuffer(16));
65     FAIL_CHECK(buffer != nullptr);
66 
67     sp<AMessage> meta = buffer->meta();
68     FAIL_CHECK(meta != nullptr);
69 
70     uint32_t rtpTime = 16;
71     meta->setInt32("rtp-time", rtpTime);
72 
73     AAVCAssembler *assembler = new AAVCAssembler(meta);
74     FAIL_CHECK(assembler != nullptr);
75 
76     isOverloadingEnabled = true;
77     sp<ABuffer> zeroSizedBuffer(new ABuffer(0));
78     isOverloadingEnabled = false;
79 
80     isTestInProgress = true;
81     assembler->checkSpsUpdated(zeroSizedBuffer);
82     isTestInProgress = false;
83 
84     return EXIT_SUCCESS;
85 }
86