blob: aca19beb0d1b5ceaeba79a3d5b055890db973cdc [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 *
22 * Example usage (for GCE virtual devices):
Keun Soo Yimff902242016-03-03 16:23:38 -080023 * $ fuzzer --class=hal --type=light --version=1.0 /system/lib/hw/lights.gce_x86.so
24 * $ fuzzer --class=hal --type=gps --version=1.0 /system/lib/hw/gps.gce_x86.so
25 * $ fuzzer --class=hal --type=camera --version=1.0 /system/lib/hw/camera.gce_x86.so
26 */
27
28#include <getopt.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33
34#include <algorithm>
35#include <string>
36#include <iostream>
37
Keun Soo Yimbf804532016-05-07 22:40:38 -070038#include "binder/VtsFuzzerBinderService.h"
Keun Soo Yimff902242016-03-03 16:23:38 -080039#include "specification_parser/InterfaceSpecificationParser.h"
40#include "specification_parser/SpecificationBuilder.h"
41
Keun Soo Yimbf804532016-05-07 22:40:38 -070042#include "BinderServer.h"
43
Keun Soo Yimff902242016-03-03 16:23:38 -080044using namespace std;
45using namespace android;
46
47#define INTERFACE_SPEC_LIB_FILENAME "libvts_interfacespecification.so"
Keun Soo Yimfa7ea842016-05-03 16:44:14 -070048#define PASSED_MARKER "[ PASSED ]"
Keun Soo Yimff902242016-03-03 16:23:38 -080049
Keun Soo Yim8103c912016-04-22 20:07:13 -070050// the default epoch count where an epoch is the time for a fuzz test run
51// (e.g., a function call).
52static const int kDefaultEpochCount = 100;
Keun Soo Yimff902242016-03-03 16:23:38 -080053
54// Dumps usage on stderr.
55static void usage() {
56 fprintf(
57 stderr,
58 "Usage: fuzzer [options] <target HAL file path>\n"
59 "\n"
60 "Android fuzzer v0.1. To fuzz Android system.\n"
61 "\n"
62 "Options:\n"
63 "--help\n"
64 " Show this message.\n"
65 "\n"
66 "Recording continues until Ctrl-C is hit or the time limit is reached.\n"
67 "\n");
68}
69
70
71// Parses command args and kicks things off.
72int main(int argc, char* const argv[]) {
73 static const struct option longOptions[] = {
Keun Soo Yim8103c912016-04-22 20:07:13 -070074 {"help", no_argument, NULL, 'h'},
75 {"class", required_argument, NULL, 'c'},
76 {"type", required_argument, NULL, 't'},
77 {"version", required_argument, NULL, 'v'},
78 {"epoch_count", optional_argument, NULL, 'e'},
79 {"spec_dir", required_argument, NULL, 's'},
Keun Soo Yimbf804532016-05-07 22:40:38 -070080 {"server", no_argument, NULL, 'd'},
Keun Soo Yim8103c912016-04-22 20:07:13 -070081 {NULL, 0, NULL, 0}};
Keun Soo Yimff902242016-03-03 16:23:38 -080082 int target_class;
83 int target_type;
84 float target_version = 1.0;
Keun Soo Yim8103c912016-04-22 20:07:13 -070085 int epoch_count = kDefaultEpochCount;
Keun Soo Yimff902242016-03-03 16:23:38 -080086 string spec_dir_path(DEFAULT_SPEC_DIR_PATH);
Keun Soo Yimbf804532016-05-07 22:40:38 -070087 bool server = false;
Keun Soo Yimff902242016-03-03 16:23:38 -080088
89 while (true) {
90 int optionIndex = 0;
91 int ic = getopt_long(argc, argv, "", longOptions, &optionIndex);
92 if (ic == -1) {
93 break;
94 }
95
96 switch (ic) {
97 case 'h':
98 usage();
99 return 0;
100 case 'c': {
101 string target_class_str = string(optarg);
102 transform(target_class_str.begin(), target_class_str.end(),
103 target_class_str.begin(), ::tolower);
104 if (!strcmp(target_class_str.c_str(), "hal")) {
105 target_class = vts::HAL;
106 } else {
107 target_class = 0;
108 }
109 break;
110 }
111 case 't': {
112 string target_type_str = string(optarg);
113 transform(target_type_str.begin(), target_type_str.end(),
114 target_type_str.begin(), ::tolower);
115 if (!strcmp(target_type_str.c_str(), "camera")) {
116 target_type = vts::CAMERA;
117 } else if (!strcmp(target_type_str.c_str(), "gps")) {
118 target_type = vts::GPS;
119 } else if (!strcmp(target_type_str.c_str(), "audio")) {
120 target_type = vts::AUDIO;
121 } else if (!strcmp(target_type_str.c_str(), "light")) {
122 target_type = vts::LIGHT;
123 } else {
124 target_type = 0;
125 }
126 break;
127 }
128 case 'v':
129 target_version = atof(optarg);
130 break;
Keun Soo Yim8103c912016-04-22 20:07:13 -0700131 case 'e':
132 epoch_count = atoi(optarg);
133 if (epoch_count <= 0) {
134 fprintf(stderr, "epoch_count must be > 0");
135 return 2;
136 }
137 break;
Keun Soo Yimff902242016-03-03 16:23:38 -0800138 case 's':
139 spec_dir_path = string(optarg);
140 break;
Keun Soo Yimbf804532016-05-07 22:40:38 -0700141 case 'd':
142 server = true;
143 break;
Keun Soo Yimff902242016-03-03 16:23:38 -0800144 default:
145 if (ic != '?') {
146 fprintf(stderr, "getopt_long returned unexpected value 0x%x\n", ic);
147 }
148 return 2;
149 }
150 }
151
152 if (optind != argc - 1) {
153 fprintf(stderr, "Must specify output file (see --help).\n");
154 return 2;
155 }
156
Keun Soo Yim8103c912016-04-22 20:07:13 -0700157 android::vts::SpecificationBuilder spec_builder(
158 spec_dir_path, epoch_count);
Keun Soo Yimbf804532016-05-07 22:40:38 -0700159 if (!server) {
160 bool success = spec_builder.Process(
161 argv[optind], INTERFACE_SPEC_LIB_FILENAME, target_class,
162 target_type, target_version);
163 cout << "Result: " << success << endl;
164 if (success) {
165 cout << endl << PASSED_MARKER << endl;
166 }
167 } else {
168 android::vts::StartBinderServer();
169 }
170
Keun Soo Yimff902242016-03-03 16:23:38 -0800171 return 0;
172}