blob: 3b07c076f413f09d283ec9c447598f1bdcf1bf26 [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/*
18 * Example usage:
19 * $ fuzzer --class=hal --type=light --version=1.0 /system/lib/hw/lights.gce_x86.so
20 * $ fuzzer --class=hal --type=gps --version=1.0 /system/lib/hw/gps.gce_x86.so
21 * $ fuzzer --class=hal --type=camera --version=1.0 /system/lib/hw/camera.gce_x86.so
22 */
23
24#include <getopt.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29
30#include <algorithm>
31#include <string>
32#include <iostream>
33
34#include "specification_parser/InterfaceSpecificationParser.h"
35#include "specification_parser/SpecificationBuilder.h"
36
37using namespace std;
38using namespace android;
39
40#define INTERFACE_SPEC_LIB_FILENAME "libvts_interfacespecification.so"
Keun Soo Yimfa7ea842016-05-03 16:44:14 -070041#define PASSED_MARKER "[ PASSED ]"
Keun Soo Yimff902242016-03-03 16:23:38 -080042
Keun Soo Yim8103c912016-04-22 20:07:13 -070043// the default epoch count where an epoch is the time for a fuzz test run
44// (e.g., a function call).
45static const int kDefaultEpochCount = 100;
Keun Soo Yimff902242016-03-03 16:23:38 -080046
47// Dumps usage on stderr.
48static void usage() {
49 fprintf(
50 stderr,
51 "Usage: fuzzer [options] <target HAL file path>\n"
52 "\n"
53 "Android fuzzer v0.1. To fuzz Android system.\n"
54 "\n"
55 "Options:\n"
56 "--help\n"
57 " Show this message.\n"
58 "\n"
59 "Recording continues until Ctrl-C is hit or the time limit is reached.\n"
60 "\n");
61}
62
63
64// Parses command args and kicks things off.
65int main(int argc, char* const argv[]) {
66 static const struct option longOptions[] = {
Keun Soo Yim8103c912016-04-22 20:07:13 -070067 {"help", no_argument, NULL, 'h'},
68 {"class", required_argument, NULL, 'c'},
69 {"type", required_argument, NULL, 't'},
70 {"version", required_argument, NULL, 'v'},
71 {"epoch_count", optional_argument, NULL, 'e'},
72 {"spec_dir", required_argument, NULL, 's'},
73 {NULL, 0, NULL, 0}};
Keun Soo Yimff902242016-03-03 16:23:38 -080074 int target_class;
75 int target_type;
76 float target_version = 1.0;
Keun Soo Yim8103c912016-04-22 20:07:13 -070077 int epoch_count = kDefaultEpochCount;
Keun Soo Yimff902242016-03-03 16:23:38 -080078 string spec_dir_path(DEFAULT_SPEC_DIR_PATH);
79
80 while (true) {
81 int optionIndex = 0;
82 int ic = getopt_long(argc, argv, "", longOptions, &optionIndex);
83 if (ic == -1) {
84 break;
85 }
86
87 switch (ic) {
88 case 'h':
89 usage();
90 return 0;
91 case 'c': {
92 string target_class_str = string(optarg);
93 transform(target_class_str.begin(), target_class_str.end(),
94 target_class_str.begin(), ::tolower);
95 if (!strcmp(target_class_str.c_str(), "hal")) {
96 target_class = vts::HAL;
97 } else {
98 target_class = 0;
99 }
100 break;
101 }
102 case 't': {
103 string target_type_str = string(optarg);
104 transform(target_type_str.begin(), target_type_str.end(),
105 target_type_str.begin(), ::tolower);
106 if (!strcmp(target_type_str.c_str(), "camera")) {
107 target_type = vts::CAMERA;
108 } else if (!strcmp(target_type_str.c_str(), "gps")) {
109 target_type = vts::GPS;
110 } else if (!strcmp(target_type_str.c_str(), "audio")) {
111 target_type = vts::AUDIO;
112 } else if (!strcmp(target_type_str.c_str(), "light")) {
113 target_type = vts::LIGHT;
114 } else {
115 target_type = 0;
116 }
117 break;
118 }
119 case 'v':
120 target_version = atof(optarg);
121 break;
Keun Soo Yim8103c912016-04-22 20:07:13 -0700122 case 'e':
123 epoch_count = atoi(optarg);
124 if (epoch_count <= 0) {
125 fprintf(stderr, "epoch_count must be > 0");
126 return 2;
127 }
128 break;
Keun Soo Yimff902242016-03-03 16:23:38 -0800129 case 's':
130 spec_dir_path = string(optarg);
131 break;
132 default:
133 if (ic != '?') {
134 fprintf(stderr, "getopt_long returned unexpected value 0x%x\n", ic);
135 }
136 return 2;
137 }
138 }
139
140 if (optind != argc - 1) {
141 fprintf(stderr, "Must specify output file (see --help).\n");
142 return 2;
143 }
144
Keun Soo Yim8103c912016-04-22 20:07:13 -0700145 android::vts::SpecificationBuilder spec_builder(
146 spec_dir_path, epoch_count);
Keun Soo Yimff902242016-03-03 16:23:38 -0800147 cout << "Result: "
148 << spec_builder.Process(argv[optind],
149 INTERFACE_SPEC_LIB_FILENAME,
150 target_class,
151 target_type,
152 target_version) << endl;
Keun Soo Yimfa7ea842016-05-03 16:44:14 -0700153 cout << endl << PASSED_MARKER << endl;
Keun Soo Yimff902242016-03-03 16:23:38 -0800154 return 0;
155}