blob: 686bd539773342e03cb8f31e61b794f5e5c49663 [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):
19 * $ fuzzer --class=hal --type=light --version=1.0 /system/lib64/hw/lights.angler.so
20 * $ fuzzer --class=hal --type=gps --version=1.0 /system/lib64/hw/gps.msm8994.so
21 *
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 Yimff902242016-03-03 16:23:38 -080030 * $ fuzzer --class=hal --type=light --version=1.0 /system/lib/hw/lights.gce_x86.so
31 * $ fuzzer --class=hal --type=gps --version=1.0 /system/lib/hw/gps.gce_x86.so
Keun Soo Yim8e07a092016-05-04 16:30:35 -070032 * $ fuzzer --class=hal --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'},
89 {NULL, 0, NULL, 0}};
Keun Soo Yimff902242016-03-03 16:23:38 -080090 int target_class;
91 int target_type;
92 float target_version = 1.0;
Keun Soo Yim8103c912016-04-22 20:07:13 -070093 int epoch_count = kDefaultEpochCount;
Keun Soo Yimff902242016-03-03 16:23:38 -080094 string spec_dir_path(DEFAULT_SPEC_DIR_PATH);
Keun Soo Yimbf804532016-05-07 22:40:38 -070095 bool server = false;
Keun Soo Yim8e07a092016-05-04 16:30:35 -070096 string service_name(VTS_FUZZER_BINDER_SERVICE_NAME);
Keun Soo Yimff902242016-03-03 16:23:38 -080097
98 while (true) {
99 int optionIndex = 0;
100 int ic = getopt_long(argc, argv, "", longOptions, &optionIndex);
101 if (ic == -1) {
102 break;
103 }
104
105 switch (ic) {
106 case 'h':
107 usage();
108 return 0;
109 case 'c': {
110 string target_class_str = string(optarg);
111 transform(target_class_str.begin(), target_class_str.end(),
112 target_class_str.begin(), ::tolower);
113 if (!strcmp(target_class_str.c_str(), "hal")) {
114 target_class = vts::HAL;
115 } else {
116 target_class = 0;
117 }
118 break;
119 }
120 case 't': {
121 string target_type_str = string(optarg);
122 transform(target_type_str.begin(), target_type_str.end(),
123 target_type_str.begin(), ::tolower);
124 if (!strcmp(target_type_str.c_str(), "camera")) {
125 target_type = vts::CAMERA;
126 } else if (!strcmp(target_type_str.c_str(), "gps")) {
127 target_type = vts::GPS;
128 } else if (!strcmp(target_type_str.c_str(), "audio")) {
129 target_type = vts::AUDIO;
130 } else if (!strcmp(target_type_str.c_str(), "light")) {
131 target_type = vts::LIGHT;
132 } else {
133 target_type = 0;
134 }
135 break;
136 }
137 case 'v':
138 target_version = atof(optarg);
139 break;
Keun Soo Yim8103c912016-04-22 20:07:13 -0700140 case 'e':
141 epoch_count = atoi(optarg);
142 if (epoch_count <= 0) {
143 fprintf(stderr, "epoch_count must be > 0");
144 return 2;
145 }
146 break;
Keun Soo Yimff902242016-03-03 16:23:38 -0800147 case 's':
148 spec_dir_path = string(optarg);
149 break;
Keun Soo Yim8e07a092016-05-04 16:30:35 -0700150 case 'n':
151 service_name = string(optarg);
152 break;
Keun Soo Yimbf804532016-05-07 22:40:38 -0700153 case 'd':
154 server = true;
155 break;
Keun Soo Yimff902242016-03-03 16:23:38 -0800156 default:
157 if (ic != '?') {
158 fprintf(stderr, "getopt_long returned unexpected value 0x%x\n", ic);
159 }
160 return 2;
161 }
162 }
163
Keun Soo Yim8103c912016-04-22 20:07:13 -0700164 android::vts::SpecificationBuilder spec_builder(
165 spec_dir_path, epoch_count);
Keun Soo Yimbf804532016-05-07 22:40:38 -0700166 if (!server) {
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700167 if (optind != argc - 1) {
168 fprintf(stderr, "Must specify output file (see --help).\n");
169 return 2;
170 }
171
Keun Soo Yimbf804532016-05-07 22:40:38 -0700172 bool success = spec_builder.Process(
173 argv[optind], INTERFACE_SPEC_LIB_FILENAME, target_class,
174 target_type, target_version);
175 cout << "Result: " << success << endl;
176 if (success) {
177 cout << endl << PASSED_MARKER << endl;
178 }
179 } else {
Keun Soo Yim8e07a092016-05-04 16:30:35 -0700180 android::vts::StartBinderServer(service_name, spec_builder,
181 INTERFACE_SPEC_LIB_FILENAME);
Keun Soo Yimbf804532016-05-07 22:40:38 -0700182 }
183
Keun Soo Yimff902242016-03-03 16:23:38 -0800184 return 0;
185}