Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 1 | /* |
| 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 Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 18 | * 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 Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame^] | 22 | * $ 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 Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 26 | * Example usage (for GCE virtual devices): |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 27 | * $ 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 Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 42 | #include "binder/VtsFuzzerBinderService.h" |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 43 | #include "specification_parser/InterfaceSpecificationParser.h" |
| 44 | #include "specification_parser/SpecificationBuilder.h" |
| 45 | |
Keun Soo Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 46 | #include "BinderServer.h" |
| 47 | |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 48 | using namespace std; |
| 49 | using namespace android; |
| 50 | |
| 51 | #define INTERFACE_SPEC_LIB_FILENAME "libvts_interfacespecification.so" |
Keun Soo Yim | fa7ea84 | 2016-05-03 16:44:14 -0700 | [diff] [blame] | 52 | #define PASSED_MARKER "[ PASSED ]" |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 53 | |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 54 | // the default epoch count where an epoch is the time for a fuzz test run |
| 55 | // (e.g., a function call). |
| 56 | static const int kDefaultEpochCount = 100; |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 57 | |
| 58 | // Dumps usage on stderr. |
| 59 | static 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. |
| 76 | int main(int argc, char* const argv[]) { |
| 77 | static const struct option longOptions[] = { |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 78 | {"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 Yim | 072337b | 2016-05-13 15:58:26 -0700 | [diff] [blame] | 82 | {"epoch_count", required_argument, NULL, 'e'}, |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 83 | {"spec_dir", required_argument, NULL, 's'}, |
Keun Soo Yim | 072337b | 2016-05-13 15:58:26 -0700 | [diff] [blame] | 84 | {"server", optional_argument, NULL, 'd'}, |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 85 | {NULL, 0, NULL, 0}}; |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 86 | int target_class; |
| 87 | int target_type; |
| 88 | float target_version = 1.0; |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 89 | int epoch_count = kDefaultEpochCount; |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 90 | string spec_dir_path(DEFAULT_SPEC_DIR_PATH); |
Keun Soo Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 91 | bool server = false; |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 92 | |
| 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 Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 135 | 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 Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 142 | case 's': |
| 143 | spec_dir_path = string(optarg); |
| 144 | break; |
Keun Soo Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 145 | case 'd': |
| 146 | server = true; |
| 147 | break; |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 148 | 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 Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 156 | android::vts::SpecificationBuilder spec_builder( |
| 157 | spec_dir_path, epoch_count); |
Keun Soo Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 158 | if (!server) { |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 159 | if (optind != argc - 1) { |
| 160 | fprintf(stderr, "Must specify output file (see --help).\n"); |
| 161 | return 2; |
| 162 | } |
| 163 | |
Keun Soo Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 164 | 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 Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 172 | android::vts::StartBinderServer(spec_builder, INTERFACE_SPEC_LIB_FILENAME); |
Keun Soo Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 175 | return 0; |
| 176 | } |