1 /**
2 * Copyright (C) 2020 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 <IMediaExtractor.h>
18 #include <dlfcn.h>
19 #include "../includes/common.h"
20 #include "../includes/memutils.h"
21
22 #if _32_BIT
23 #define LIBNAME "/system/lib/extractors/libmidiextractor.so"
24 #define LIBNAME_APEX "/apex/com.android.media/lib/extractors/libmidiextractor.so"
25 #elif _64_BIT
26 #define LIBNAME "/system/lib64/extractors/libmidiextractor.so"
27 #define LIBNAME_APEX "/apex/com.android.media/lib64/extractors/libmidiextractor.so"
28 #endif
29
30 char enable_selective_overload = ENABLE_NONE;
31
32 using namespace android;
33
34 bool isTestInProgress = false;
35
36 struct sigaction new_action, old_action;
37
38 int fdData, fdInfo;
39
40 void *libHandle = nullptr;
41
sigsegv_handler(int signum,siginfo_t * info,void * context)42 void sigsegv_handler(int signum, siginfo_t *info, void *context) {
43 if (isTestInProgress && info->si_signo == SIGSEGV) {
44 (*old_action.sa_sigaction)(signum, info, context);
45 return;
46 }
47 _exit(EXIT_FAILURE);
48 }
49
50 class XMFDataSource : public DataSource {
51 public:
52 int mFdData;
53 int mFdInfo;
XMFDataSource(int fdData,int fdInfo)54 XMFDataSource(int fdData, int fdInfo) {
55 mFdData = fdData;
56 mFdInfo = fdInfo;
57 }
58
59 ~XMFDataSource() = default;
60
readAt(off64_t offset,void * data,size_t size)61 virtual ssize_t readAt(off64_t offset __attribute__((unused)), void *data, size_t size) {
62 uint32_t infoOffset, infoSize;
63 read(mFdInfo, &infoSize, sizeof(int32_t));
64 read(mFdInfo, &infoOffset, sizeof(int32_t));
65 lseek(mFdData, infoOffset, SEEK_SET);
66 read(mFdData, data, infoSize);
67 return size;
68 }
69
getSize(off64_t * size)70 virtual status_t getSize(off64_t *size) {
71 *size = 0x10000;
72 return 0;
73 }
initCheck() const74 virtual status_t initCheck() const { return 0; }
75 };
76
close_resources()77 void close_resources() {
78 if (fdData >= 0) {
79 ::close(fdData);
80 }
81 if (fdInfo >= 0) {
82 ::close(fdInfo);
83 }
84 if (libHandle) {
85 dlclose(libHandle);
86 }
87 }
88
main(int argc,char ** argv)89 int main(int argc, char **argv) {
90 atexit(close_resources);
91
92 sigemptyset(&new_action.sa_mask);
93 new_action.sa_flags = SA_SIGINFO;
94 new_action.sa_sigaction = sigsegv_handler;
95 sigaction(SIGSEGV, &new_action, &old_action);
96
97 FAIL_CHECK(argc == 3);
98 libHandle = dlopen(LIBNAME, RTLD_NOW | RTLD_LOCAL);
99 if (!libHandle) {
100 libHandle = dlopen(LIBNAME_APEX, RTLD_NOW | RTLD_LOCAL);
101 FAIL_CHECK(libHandle);
102 }
103
104 GetExtractorDef getDef = (GetExtractorDef)dlsym(libHandle, "GETEXTRACTORDEF");
105 FAIL_CHECK(getDef);
106
107 fdData = open(argv[1], O_RDONLY);
108 FAIL_CHECK(fdData >= 0);
109
110 fdInfo = open(argv[2], O_RDONLY);
111 FAIL_CHECK(fdInfo >= 0);
112
113 sp<DataSource> dataSource = (sp<DataSource>)new XMFDataSource(fdData, fdInfo);
114 FAIL_CHECK(dataSource);
115
116 enable_selective_overload = ENABLE_ALL;
117 isTestInProgress = true;
118
119 void *meta = nullptr;
120 FreeMetaFunc freeMeta = nullptr;
121 float confidence = 0.0f;
122 if (getDef().def_version == EXTRACTORDEF_VERSION_NDK_V1) {
123 getDef().u.v2.sniff(dataSource->wrap(), &confidence, &meta, &freeMeta);
124 } else if (getDef().def_version == EXTRACTORDEF_VERSION_NDK_V2) {
125 getDef().u.v3.sniff(dataSource->wrap(), &confidence, &meta, &freeMeta);
126 }
127
128 isTestInProgress = false;
129 enable_selective_overload = ENABLE_FREE_CHECK | ENABLE_REALLOC_CHECK;
130 return EXIT_SUCCESS;
131 }
132