blob: ca8f35203a59cd39892550ab5ea0906c6509a705 [file] [log] [blame]
Keun Soo Yimff902242016-03-03 16:23:38 -08001/*
2 * Copyright 2016 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/*
Keun Soo Yimbf804532016-05-07 22:40:38 -070018 * Example usage (for angler 64-bit devices):
Keun Soo Yim78446412016-06-16 14:30:13 -070019 * $ fuzzer --class=hal_conventional --type=light --version=1.0 /system/lib64/hw/lights.angler.so
20 * $ fuzzer --class=hal_conventional --type=gps --version=1.0 /system/lib64/hw/gps.msm8994.so
Keun Soo Yimbf804532016-05-07 22:40:38 -070021 *
Keun Soo Yim8e07a092016-05-04 16:30:35 -070022 * $ LD_LIBRARY_PATH=/data/local/tmp/64 ./fuzzer64 --class=hal --type=light \
Keun Soo Yim04431a92016-05-24 16:26:42 -070023 * --version=1.0 --spec_dir=/data/local/tmp/spec \
Keun Soo Yim8e07a092016-05-04 16:30:35 -070024 * /data/local/tmp/64/hal/lights.bullhead-vts.so
25 * $ LD_LIBRARY_PATH=/data/local/tmp/32 ./fuzzer32 --class=hal --type=light \
26 * --version=1.0 --spec_dir=/data/local/tmp/spec \
27 * /data/local/tmp/32/hal/camera.bullhead-vts.so
Keun Soo Yim04431a92016-05-24 16:26:42 -070028 *
Keun Soo Yimbf804532016-05-07 22:40:38 -070029 * Example usage (for GCE virtual devices):
Keun Soo Yim78446412016-06-16 14:30:13 -070030 * $ fuzzer --class=hal_conventional --type=light --version=1.0 /system/lib/hw/lights.gce_x86.so
31 * $ fuzzer --class=hal_conventional --type=gps --version=1.0 /system/lib/hw/gps.gce_x86.so
32 * $ fuzzer --class=hal_conventional --type=camera --version=2.1 /system/lib/hw/camera.gce_x86.so
Keun Soo Yimff902242016-03-03 16:23:38 -080033 */
34
35#include <getopt.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40
41#include <algorithm>
42#include <string>
43#include <iostream>
44
Keun Soo Yimbf804532016-05-07 22:40:38 -070045#include "binder/VtsFuzzerBinderService.h"
Keun Soo Yimff902242016-03-03 16:23:38 -080046#include "specification_parser/InterfaceSpecificationParser.h"
47#include "specification_parser/SpecificationBuilder.h"
48
Keun Soo Yimbf804532016-05-07 22:40:38 -070049#include "BinderServer.h"
Keun Soo Yim14f22722016-06-29 09:49:38 -070050#include "SocketServer.h"
Keun Soo Yimbf804532016-05-07 22:40:38 -070051
Keun Soo Yimff902242016-03-03 16:23:38 -080052using namespace std;
53using namespace android;
54
55#define INTERFACE_SPEC_LIB_FILENAME "libvts_interfacespecification.so"
Keun Soo Yimfa7ea842016-05-03 16:44:14 -070056#define PASSED_MARKER "[ PASSED ]"
Keun Soo Yimff902242016-03-03 16:23:38 -080057
Keun Soo Yim8103c912016-04-22 20:07:13 -070058// the default epoch count where an epoch is the time for a fuzz test run
59// (e.g., a function call).
60static const int kDefaultEpochCount = 100;
Keun Soo Yimff902242016-03-03 16:23:38 -080061
62// Dumps usage on stderr.
63static void usage() {
64 fprintf(
65 stderr,
66 "Usage: fuzzer [options] <target HAL file path>\n"
67 "\n"
68 "Android fuzzer v0.1. To fuzz Android system.\n"
69 "\n"
70 "Options:\n"
71 "--help\n"
72 " Show this message.\n"
73 "\n"
74 "Recording continues until Ctrl-C is hit or the time limit is reached.\n"
75 "\n");
76}
77
78
79// Parses command args and kicks things off.
80int main(int argc, char* const argv[]) {
81 static const struct option longOptions[] = {
Keun Soo Yim14f22722016-06-29 09:49:38 -070082 {"help", no_argument, NULL, 'h'},
83 {"class", required_argument, NULL, 'c'},
84 {"type", required_argument, NULL, 't'},
85 {"version", required_argument, NULL, 'v'},
86 {"epoch_count", required_argument, NULL, 'e'},
87 {"spec_dir", required_argument, NULL, 's'},
88#ifndef VTS_AGENT_DRIVER_COMM_BINDER // socket
89 {"socket_port_file", optional_argument, NULL, 'f'},
90#else // binder
91 {"service_name", required_argument, NULL, 'n'},
92#endif
93 {"server", optional_argument, NULL, 'd'},
94 {"agent_port", optional_argument, NULL, 'p'},
95 {NULL, 0, NULL, 0}};
Keun Soo Yimff902242016-03-03 16:23:38 -080096 int target_class;
97 int target_type;
98 float target_version = 1.0;
Keun Soo Yim8103c912016-04-22 20:07:13 -070099 int epoch_count = kDefaultEpochCount;
Keun Soo Yimff902242016-03-03 16:23:38 -0800100 string spec_dir_path(DEFAULT_SPEC_DIR_PATH);
Keun Soo Yimbf804532016-05-07 22:40:38 -0700101 bool server = false;
Keun Soo Yim14f22722016-06-29 09:49:38 -0700102#ifndef VTS_AGENT_DRIVER_COMM_BINDER // socket
103 string socket_port_file;
104#else // binder
Keun Soo Yim8e07a092016-05-04 16:30:35 -0700105 string service_name(VTS_FUZZER_BINDER_SERVICE_NAME);
Keun Soo Yim14f22722016-06-29 09:49:38 -0700106#endif
Keun Soo Yima4a6d532016-06-08 09:11:40 -0700107 int agent_port = -1;
Keun Soo Yimff902242016-03-03 16:23:38 -0800108
109 while (true) {
110 int optionIndex = 0;
111 int ic = getopt_long(argc, argv, "", longOptions, &optionIndex);
112 if (ic == -1) {
113 break;
114 }
115
116 switch (ic) {
117 case 'h':
118 usage();
119 return 0;
120 case 'c': {
121 string target_class_str = string(optarg);
122 transform(target_class_str.begin(), target_class_str.end(),
123 target_class_str.begin(), ::tolower);
Keun Soo Yim78446412016-06-16 14:30:13 -0700124 if (!strcmp(target_class_str.c_str(), "hal_conventional")) {
125 target_class = vts::HAL_CONVENTIONAL;
Keun Soo Yimff902242016-03-03 16:23:38 -0800126 } else {
127 target_class = 0;
128 }
129 break;
130 }
131 case 't': {
132 string target_type_str = string(optarg);
133 transform(target_type_str.begin(), target_type_str.end(),
134 target_type_str.begin(), ::tolower);
135 if (!strcmp(target_type_str.c_str(), "camera")) {
136 target_type = vts::CAMERA;
137 } else if (!strcmp(target_type_str.c_str(), "gps")) {
138 target_type = vts::GPS;
139 } else if (!strcmp(target_type_str.c_str(), "audio")) {
140 target_type = vts::AUDIO;
141 } else if (!strcmp(target_type_str.c_str(), "light")) {
142 target_type = vts::LIGHT;
143 } else {
144 target_type = 0;
145 }
146 break;
147 }
148 case 'v':
149 target_version = atof(optarg);
150 break;
Keun Soo Yima4a6d532016-06-08 09:11:40 -0700151 case 'p':
152 agent_port = atoi(optarg);
153 break;
Keun Soo Yim8103c912016-04-22 20:07:13 -0700154 case 'e':
155 epoch_count = atoi(optarg);
156 if (epoch_count <= 0) {
157 fprintf(stderr, "epoch_count must be > 0");
158 return 2;
159 }
160 break;
Keun Soo Yimff902242016-03-03 16:23:38 -0800161 case 's':
162 spec_dir_path = string(optarg);
163 break;
Keun Soo Yim14f22722016-06-29 09:49:38 -0700164#ifndef VTS_AGENT_DRIVER_COMM_BINDER // socket
165 case 'f':
166 socket_port_file = string(optarg);
167 break;
168#else // binder
Keun Soo Yim8e07a092016-05-04 16:30:35 -0700169 case 'n':
170 service_name = string(optarg);
171 break;
Keun Soo Yim14f22722016-06-29 09:49:38 -0700172#endif
Keun Soo Yimbf804532016-05-07 22:40:38 -0700173 case 'd':
174 server = true;
175 break;
Keun Soo Yimff902242016-03-03 16:23:38 -0800176 default:
177 if (ic != '?') {
178 fprintf(stderr, "getopt_long returned unexpected value 0x%x\n", ic);
179 }
180 return 2;
181 }
182 }
183
Keun Soo Yim8103c912016-04-22 20:07:13 -0700184 android::vts::SpecificationBuilder spec_builder(
Keun Soo Yima4a6d532016-06-08 09:11:40 -0700185 spec_dir_path, epoch_count, agent_port);
Keun Soo Yimbf804532016-05-07 22:40:38 -0700186 if (!server) {
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700187 if (optind != argc - 1) {
188 fprintf(stderr, "Must specify output file (see --help).\n");
189 return 2;
190 }
191
Keun Soo Yimbf804532016-05-07 22:40:38 -0700192 bool success = spec_builder.Process(
193 argv[optind], INTERFACE_SPEC_LIB_FILENAME, target_class,
194 target_type, target_version);
195 cout << "Result: " << success << endl;
196 if (success) {
197 cout << endl << PASSED_MARKER << endl;
198 }
199 } else {
Keun Soo Yim14f22722016-06-29 09:49:38 -0700200#ifndef VTS_AGENT_DRIVER_COMM_BINDER // socket
201 android::vts::StartSocketServer(socket_port_file, spec_builder,
202 INTERFACE_SPEC_LIB_FILENAME);
203#else // binder
Keun Soo Yim8e07a092016-05-04 16:30:35 -0700204 android::vts::StartBinderServer(service_name, spec_builder,
205 INTERFACE_SPEC_LIB_FILENAME);
Keun Soo Yim14f22722016-06-29 09:49:38 -0700206#endif
Keun Soo Yimbf804532016-05-07 22:40:38 -0700207 }
208
Keun Soo Yimff902242016-03-03 16:23:38 -0800209 return 0;
210}