blob: ec9f8e72188614e16a8536789b99c0031ac4851f [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"
41
Keun Soo Yim8103c912016-04-22 20:07:13 -070042// the default epoch count where an epoch is the time for a fuzz test run
43// (e.g., a function call).
44static const int kDefaultEpochCount = 100;
Keun Soo Yimff902242016-03-03 16:23:38 -080045
46// Dumps usage on stderr.
47static void usage() {
48 fprintf(
49 stderr,
50 "Usage: fuzzer [options] <target HAL file path>\n"
51 "\n"
52 "Android fuzzer v0.1. To fuzz Android system.\n"
53 "\n"
54 "Options:\n"
55 "--help\n"
56 " Show this message.\n"
57 "\n"
58 "Recording continues until Ctrl-C is hit or the time limit is reached.\n"
59 "\n");
60}
61
62
63// Parses command args and kicks things off.
64int main(int argc, char* const argv[]) {
65 static const struct option longOptions[] = {
Keun Soo Yim8103c912016-04-22 20:07:13 -070066 {"help", no_argument, NULL, 'h'},
67 {"class", required_argument, NULL, 'c'},
68 {"type", required_argument, NULL, 't'},
69 {"version", required_argument, NULL, 'v'},
70 {"epoch_count", optional_argument, NULL, 'e'},
71 {"spec_dir", required_argument, NULL, 's'},
72 {NULL, 0, NULL, 0}};
Keun Soo Yimff902242016-03-03 16:23:38 -080073 int target_class;
74 int target_type;
75 float target_version = 1.0;
Keun Soo Yim8103c912016-04-22 20:07:13 -070076 int epoch_count = kDefaultEpochCount;
Keun Soo Yimff902242016-03-03 16:23:38 -080077 string spec_dir_path(DEFAULT_SPEC_DIR_PATH);
78
79 while (true) {
80 int optionIndex = 0;
81 int ic = getopt_long(argc, argv, "", longOptions, &optionIndex);
82 if (ic == -1) {
83 break;
84 }
85
86 switch (ic) {
87 case 'h':
88 usage();
89 return 0;
90 case 'c': {
91 string target_class_str = string(optarg);
92 transform(target_class_str.begin(), target_class_str.end(),
93 target_class_str.begin(), ::tolower);
94 if (!strcmp(target_class_str.c_str(), "hal")) {
95 target_class = vts::HAL;
96 } else {
97 target_class = 0;
98 }
99 break;
100 }
101 case 't': {
102 string target_type_str = string(optarg);
103 transform(target_type_str.begin(), target_type_str.end(),
104 target_type_str.begin(), ::tolower);
105 if (!strcmp(target_type_str.c_str(), "camera")) {
106 target_type = vts::CAMERA;
107 } else if (!strcmp(target_type_str.c_str(), "gps")) {
108 target_type = vts::GPS;
109 } else if (!strcmp(target_type_str.c_str(), "audio")) {
110 target_type = vts::AUDIO;
111 } else if (!strcmp(target_type_str.c_str(), "light")) {
112 target_type = vts::LIGHT;
113 } else {
114 target_type = 0;
115 }
116 break;
117 }
118 case 'v':
119 target_version = atof(optarg);
120 break;
Keun Soo Yim8103c912016-04-22 20:07:13 -0700121 case 'e':
122 epoch_count = atoi(optarg);
123 if (epoch_count <= 0) {
124 fprintf(stderr, "epoch_count must be > 0");
125 return 2;
126 }
127 break;
Keun Soo Yimff902242016-03-03 16:23:38 -0800128 case 's':
129 spec_dir_path = string(optarg);
130 break;
131 default:
132 if (ic != '?') {
133 fprintf(stderr, "getopt_long returned unexpected value 0x%x\n", ic);
134 }
135 return 2;
136 }
137 }
138
139 if (optind != argc - 1) {
140 fprintf(stderr, "Must specify output file (see --help).\n");
141 return 2;
142 }
143
Keun Soo Yim8103c912016-04-22 20:07:13 -0700144 android::vts::SpecificationBuilder spec_builder(
145 spec_dir_path, epoch_count);
Keun Soo Yimff902242016-03-03 16:23:38 -0800146 cout << "Result: "
147 << spec_builder.Process(argv[optind],
148 INTERFACE_SPEC_LIB_FILENAME,
149 target_class,
150 target_type,
151 target_version) << endl;
152 return 0;
153}