blob: 9eb1cf22d57ecf050f2fe713ecf91681633ddd39 [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 Yim04431a92016-05-24 16:26:42 -070022 * $ LD_LIBRARY_PATH=/data/local/tmp ./fuzzer --class=hal --type=light \
23 * --version=1.0 --spec_dir=/data/local/tmp/spec \
24 * /data/local/tmp/hw64/lights.bullhead-vts.so
25 *
Keun Soo Yimbf804532016-05-07 22:40:38 -070026 * Example usage (for GCE virtual devices):
Keun Soo Yimff902242016-03-03 16:23:38 -080027 * $ fuzzer --class=hal --type=light --version=1.0 /system/lib/hw/lights.gce_x86.so
28 * $ fuzzer --class=hal --type=gps --version=1.0 /system/lib/hw/gps.gce_x86.so
29 * $ fuzzer --class=hal --type=camera --version=1.0 /system/lib/hw/camera.gce_x86.so
30 */
31
32#include <getopt.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37
38#include <algorithm>
39#include <string>
40#include <iostream>
41
Keun Soo Yimbf804532016-05-07 22:40:38 -070042#include "binder/VtsFuzzerBinderService.h"
Keun Soo Yimff902242016-03-03 16:23:38 -080043#include "specification_parser/InterfaceSpecificationParser.h"
44#include "specification_parser/SpecificationBuilder.h"
45
Keun Soo Yimbf804532016-05-07 22:40:38 -070046#include "BinderServer.h"
47
Keun Soo Yimff902242016-03-03 16:23:38 -080048using namespace std;
49using namespace android;
50
51#define INTERFACE_SPEC_LIB_FILENAME "libvts_interfacespecification.so"
Keun Soo Yimfa7ea842016-05-03 16:44:14 -070052#define PASSED_MARKER "[ PASSED ]"
Keun Soo Yimff902242016-03-03 16:23:38 -080053
Keun Soo Yim8103c912016-04-22 20:07:13 -070054// the default epoch count where an epoch is the time for a fuzz test run
55// (e.g., a function call).
56static const int kDefaultEpochCount = 100;
Keun Soo Yimff902242016-03-03 16:23:38 -080057
58// Dumps usage on stderr.
59static void usage() {
60 fprintf(
61 stderr,
62 "Usage: fuzzer [options] <target HAL file path>\n"
63 "\n"
64 "Android fuzzer v0.1. To fuzz Android system.\n"
65 "\n"
66 "Options:\n"
67 "--help\n"
68 " Show this message.\n"
69 "\n"
70 "Recording continues until Ctrl-C is hit or the time limit is reached.\n"
71 "\n");
72}
73
74
75// Parses command args and kicks things off.
76int main(int argc, char* const argv[]) {
77 static const struct option longOptions[] = {
Keun Soo Yim8103c912016-04-22 20:07:13 -070078 {"help", no_argument, NULL, 'h'},
79 {"class", required_argument, NULL, 'c'},
80 {"type", required_argument, NULL, 't'},
81 {"version", required_argument, NULL, 'v'},
Keun Soo Yim072337b2016-05-13 15:58:26 -070082 {"epoch_count", required_argument, NULL, 'e'},
Keun Soo Yim8103c912016-04-22 20:07:13 -070083 {"spec_dir", required_argument, NULL, 's'},
Keun Soo Yim072337b2016-05-13 15:58:26 -070084 {"server", optional_argument, NULL, 'd'},
Keun Soo Yim8103c912016-04-22 20:07:13 -070085 {NULL, 0, NULL, 0}};
Keun Soo Yimff902242016-03-03 16:23:38 -080086 int target_class;
87 int target_type;
88 float target_version = 1.0;
Keun Soo Yim8103c912016-04-22 20:07:13 -070089 int epoch_count = kDefaultEpochCount;
Keun Soo Yimff902242016-03-03 16:23:38 -080090 string spec_dir_path(DEFAULT_SPEC_DIR_PATH);
Keun Soo Yimbf804532016-05-07 22:40:38 -070091 bool server = false;
Keun Soo Yimff902242016-03-03 16:23:38 -080092
93 while (true) {
94 int optionIndex = 0;
95 int ic = getopt_long(argc, argv, "", longOptions, &optionIndex);
96 if (ic == -1) {
97 break;
98 }
99
100 switch (ic) {
101 case 'h':
102 usage();
103 return 0;
104 case 'c': {
105 string target_class_str = string(optarg);
106 transform(target_class_str.begin(), target_class_str.end(),
107 target_class_str.begin(), ::tolower);
108 if (!strcmp(target_class_str.c_str(), "hal")) {
109 target_class = vts::HAL;
110 } else {
111 target_class = 0;
112 }
113 break;
114 }
115 case 't': {
116 string target_type_str = string(optarg);
117 transform(target_type_str.begin(), target_type_str.end(),
118 target_type_str.begin(), ::tolower);
119 if (!strcmp(target_type_str.c_str(), "camera")) {
120 target_type = vts::CAMERA;
121 } else if (!strcmp(target_type_str.c_str(), "gps")) {
122 target_type = vts::GPS;
123 } else if (!strcmp(target_type_str.c_str(), "audio")) {
124 target_type = vts::AUDIO;
125 } else if (!strcmp(target_type_str.c_str(), "light")) {
126 target_type = vts::LIGHT;
127 } else {
128 target_type = 0;
129 }
130 break;
131 }
132 case 'v':
133 target_version = atof(optarg);
134 break;
Keun Soo Yim8103c912016-04-22 20:07:13 -0700135 case 'e':
136 epoch_count = atoi(optarg);
137 if (epoch_count <= 0) {
138 fprintf(stderr, "epoch_count must be > 0");
139 return 2;
140 }
141 break;
Keun Soo Yimff902242016-03-03 16:23:38 -0800142 case 's':
143 spec_dir_path = string(optarg);
144 break;
Keun Soo Yimbf804532016-05-07 22:40:38 -0700145 case 'd':
146 server = true;
147 break;
Keun Soo Yimff902242016-03-03 16:23:38 -0800148 default:
149 if (ic != '?') {
150 fprintf(stderr, "getopt_long returned unexpected value 0x%x\n", ic);
151 }
152 return 2;
153 }
154 }
155
Keun Soo Yim8103c912016-04-22 20:07:13 -0700156 android::vts::SpecificationBuilder spec_builder(
157 spec_dir_path, epoch_count);
Keun Soo Yimbf804532016-05-07 22:40:38 -0700158 if (!server) {
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700159 if (optind != argc - 1) {
160 fprintf(stderr, "Must specify output file (see --help).\n");
161 return 2;
162 }
163
Keun Soo Yimbf804532016-05-07 22:40:38 -0700164 bool success = spec_builder.Process(
165 argv[optind], INTERFACE_SPEC_LIB_FILENAME, target_class,
166 target_type, target_version);
167 cout << "Result: " << success << endl;
168 if (success) {
169 cout << endl << PASSED_MARKER << endl;
170 }
171 } else {
Keun Soo Yimfeceb4d2016-05-11 20:01:00 -0700172 android::vts::StartBinderServer(spec_builder, INTERFACE_SPEC_LIB_FILENAME);
Keun Soo Yimbf804532016-05-07 22:40:38 -0700173 }
174
Keun Soo Yimff902242016-03-03 16:23:38 -0800175 return 0;
176}