blob: ebdc69bc6add1d464d80f602fa8381b63f0c4ad6 [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"
50
Keun Soo Yimff902242016-03-03 16:23:38 -080051using namespace std;
52using namespace android;
53
54#define INTERFACE_SPEC_LIB_FILENAME "libvts_interfacespecification.so"
Keun Soo Yimfa7ea842016-05-03 16:44:14 -070055#define PASSED_MARKER "[ PASSED ]"
Keun Soo Yimff902242016-03-03 16:23:38 -080056
Keun Soo Yim8103c912016-04-22 20:07:13 -070057// the default epoch count where an epoch is the time for a fuzz test run
58// (e.g., a function call).
59static const int kDefaultEpochCount = 100;
Keun Soo Yimff902242016-03-03 16:23:38 -080060
61// Dumps usage on stderr.
62static void usage() {
63 fprintf(
64 stderr,
65 "Usage: fuzzer [options] <target HAL file path>\n"
66 "\n"
67 "Android fuzzer v0.1. To fuzz Android system.\n"
68 "\n"
69 "Options:\n"
70 "--help\n"
71 " Show this message.\n"
72 "\n"
73 "Recording continues until Ctrl-C is hit or the time limit is reached.\n"
74 "\n");
75}
76
77
78// Parses command args and kicks things off.
79int main(int argc, char* const argv[]) {
80 static const struct option longOptions[] = {
Keun Soo Yim8e07a092016-05-04 16:30:35 -070081 {"help", no_argument, NULL, 'h'},
82 {"class", required_argument, NULL, 'c'},
83 {"type", required_argument, NULL, 't'},
84 {"version", required_argument, NULL, 'v'},
85 {"epoch_count", required_argument, NULL, 'e'},
86 {"spec_dir", required_argument, NULL, 's'},
87 {"service_name", required_argument, NULL, 'n'},
88 {"server", optional_argument, NULL, 'd'},
Keun Soo Yima4a6d532016-06-08 09:11:40 -070089 {"agent_port", optional_argument, NULL, 'p'},
Keun Soo Yim8e07a092016-05-04 16:30:35 -070090 {NULL, 0, NULL, 0}};
Keun Soo Yimff902242016-03-03 16:23:38 -080091 int target_class;
92 int target_type;
93 float target_version = 1.0;
Keun Soo Yim8103c912016-04-22 20:07:13 -070094 int epoch_count = kDefaultEpochCount;
Keun Soo Yimff902242016-03-03 16:23:38 -080095 string spec_dir_path(DEFAULT_SPEC_DIR_PATH);
Keun Soo Yimbf804532016-05-07 22:40:38 -070096 bool server = false;
Keun Soo Yim8e07a092016-05-04 16:30:35 -070097 string service_name(VTS_FUZZER_BINDER_SERVICE_NAME);
Keun Soo Yima4a6d532016-06-08 09:11:40 -070098 int agent_port = -1;
Keun Soo Yimff902242016-03-03 16:23:38 -080099
100 while (true) {
101 int optionIndex = 0;
102 int ic = getopt_long(argc, argv, "", longOptions, &optionIndex);
103 if (ic == -1) {
104 break;
105 }
106
107 switch (ic) {
108 case 'h':
109 usage();
110 return 0;
111 case 'c': {
112 string target_class_str = string(optarg);
113 transform(target_class_str.begin(), target_class_str.end(),
114 target_class_str.begin(), ::tolower);
Keun Soo Yim78446412016-06-16 14:30:13 -0700115 if (!strcmp(target_class_str.c_str(), "hal_conventional")) {
116 target_class = vts::HAL_CONVENTIONAL;
Keun Soo Yimff902242016-03-03 16:23:38 -0800117 } else {
118 target_class = 0;
119 }
120 break;
121 }
122 case 't': {
123 string target_type_str = string(optarg);
124 transform(target_type_str.begin(), target_type_str.end(),
125 target_type_str.begin(), ::tolower);
126 if (!strcmp(target_type_str.c_str(), "camera")) {
127 target_type = vts::CAMERA;
128 } else if (!strcmp(target_type_str.c_str(), "gps")) {
129 target_type = vts::GPS;
130 } else if (!strcmp(target_type_str.c_str(), "audio")) {
131 target_type = vts::AUDIO;
132 } else if (!strcmp(target_type_str.c_str(), "light")) {
133 target_type = vts::LIGHT;
134 } else {
135 target_type = 0;
136 }
137 break;
138 }
139 case 'v':
140 target_version = atof(optarg);
141 break;
Keun Soo Yima4a6d532016-06-08 09:11:40 -0700142 case 'p':
143 agent_port = atoi(optarg);
144 break;
Keun Soo Yim8103c912016-04-22 20:07:13 -0700145 case 'e':
146 epoch_count = atoi(optarg);
147 if (epoch_count <= 0) {
148 fprintf(stderr, "epoch_count must be > 0");
149 return 2;
150 }
151 break;
Keun Soo Yimff902242016-03-03 16:23:38 -0800152 case 's':
153 spec_dir_path = string(optarg);
154 break;
Keun Soo Yim8e07a092016-05-04 16:30:35 -0700155 case 'n':
156 service_name = string(optarg);
157 break;
Keun Soo Yimbf804532016-05-07 22:40:38 -0700158 case 'd':
159 server = true;
160 break;
Keun Soo Yimff902242016-03-03 16:23:38 -0800161 default:
162 if (ic != '?') {
163 fprintf(stderr, "getopt_long returned unexpected value 0x%x\n", ic);
164 }
165 return 2;
166 }
167 }
168
Keun Soo Yim8103c912016-04-22 20:07:13 -0700169 android::vts::SpecificationBuilder spec_builder(
Keun Soo Yima4a6d532016-06-08 09:11:40 -0700170 spec_dir_path, epoch_count, agent_port);
Keun Soo Yimbf804532016-05-07 22:40:38 -0700171 if (!server) {
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700172 if (optind != argc - 1) {
173 fprintf(stderr, "Must specify output file (see --help).\n");
174 return 2;
175 }
176
Keun Soo Yimbf804532016-05-07 22:40:38 -0700177 bool success = spec_builder.Process(
178 argv[optind], INTERFACE_SPEC_LIB_FILENAME, target_class,
179 target_type, target_version);
180 cout << "Result: " << success << endl;
181 if (success) {
182 cout << endl << PASSED_MARKER << endl;
183 }
184 } else {
Keun Soo Yim8e07a092016-05-04 16:30:35 -0700185 android::vts::StartBinderServer(service_name, spec_builder,
186 INTERFACE_SPEC_LIB_FILENAME);
Keun Soo Yimbf804532016-05-07 22:40:38 -0700187 }
188
Keun Soo Yimff902242016-03-03 16:23:38 -0800189 return 0;
190}