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 | 8e07a09 | 2016-05-04 16:30:35 -0700 | [diff] [blame^] | 22 | * $ LD_LIBRARY_PATH=/data/local/tmp/64 ./fuzzer64 --class=hal --type=light \ |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame] | 23 | * --version=1.0 --spec_dir=/data/local/tmp/spec \ |
Keun Soo Yim | 8e07a09 | 2016-05-04 16:30:35 -0700 | [diff] [blame^] | 24 | * /data/local/tmp/64/hal/lights.bullhead-vts.so |
| 25 | * $ LD_LIBRARY_PATH=/data/local/tmp/32 ./fuzzer32 --class=hal --type=light \ |
| 26 | * --version=1.0 --spec_dir=/data/local/tmp/spec \ |
| 27 | * /data/local/tmp/32/hal/camera.bullhead-vts.so |
Keun Soo Yim | 04431a9 | 2016-05-24 16:26:42 -0700 | [diff] [blame] | 28 | * |
Keun Soo Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 29 | * Example usage (for GCE virtual devices): |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 30 | * $ fuzzer --class=hal --type=light --version=1.0 /system/lib/hw/lights.gce_x86.so |
| 31 | * $ fuzzer --class=hal --type=gps --version=1.0 /system/lib/hw/gps.gce_x86.so |
Keun Soo Yim | 8e07a09 | 2016-05-04 16:30:35 -0700 | [diff] [blame^] | 32 | * $ fuzzer --class=hal --type=camera --version=2.1 /system/lib/hw/camera.gce_x86.so |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | #include <getopt.h> |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <unistd.h> |
| 40 | |
| 41 | #include <algorithm> |
| 42 | #include <string> |
| 43 | #include <iostream> |
| 44 | |
Keun Soo Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 45 | #include "binder/VtsFuzzerBinderService.h" |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 46 | #include "specification_parser/InterfaceSpecificationParser.h" |
| 47 | #include "specification_parser/SpecificationBuilder.h" |
| 48 | |
Keun Soo Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 49 | #include "BinderServer.h" |
| 50 | |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 51 | using namespace std; |
| 52 | using namespace android; |
| 53 | |
| 54 | #define INTERFACE_SPEC_LIB_FILENAME "libvts_interfacespecification.so" |
Keun Soo Yim | fa7ea84 | 2016-05-03 16:44:14 -0700 | [diff] [blame] | 55 | #define PASSED_MARKER "[ PASSED ]" |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 56 | |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 57 | // the default epoch count where an epoch is the time for a fuzz test run |
| 58 | // (e.g., a function call). |
| 59 | static const int kDefaultEpochCount = 100; |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 60 | |
| 61 | // Dumps usage on stderr. |
| 62 | static void usage() { |
| 63 | fprintf( |
| 64 | stderr, |
| 65 | "Usage: fuzzer [options] <target HAL file path>\n" |
| 66 | "\n" |
| 67 | "Android fuzzer v0.1. To fuzz Android system.\n" |
| 68 | "\n" |
| 69 | "Options:\n" |
| 70 | "--help\n" |
| 71 | " Show this message.\n" |
| 72 | "\n" |
| 73 | "Recording continues until Ctrl-C is hit or the time limit is reached.\n" |
| 74 | "\n"); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | // Parses command args and kicks things off. |
| 79 | int main(int argc, char* const argv[]) { |
| 80 | static const struct option longOptions[] = { |
Keun Soo Yim | 8e07a09 | 2016-05-04 16:30:35 -0700 | [diff] [blame^] | 81 | {"help", no_argument, NULL, 'h'}, |
| 82 | {"class", required_argument, NULL, 'c'}, |
| 83 | {"type", required_argument, NULL, 't'}, |
| 84 | {"version", required_argument, NULL, 'v'}, |
| 85 | {"epoch_count", required_argument, NULL, 'e'}, |
| 86 | {"spec_dir", required_argument, NULL, 's'}, |
| 87 | {"service_name", required_argument, NULL, 'n'}, |
| 88 | {"server", optional_argument, NULL, 'd'}, |
| 89 | {NULL, 0, NULL, 0}}; |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 90 | int target_class; |
| 91 | int target_type; |
| 92 | float target_version = 1.0; |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 93 | int epoch_count = kDefaultEpochCount; |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 94 | string spec_dir_path(DEFAULT_SPEC_DIR_PATH); |
Keun Soo Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 95 | bool server = false; |
Keun Soo Yim | 8e07a09 | 2016-05-04 16:30:35 -0700 | [diff] [blame^] | 96 | string service_name(VTS_FUZZER_BINDER_SERVICE_NAME); |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 97 | |
| 98 | while (true) { |
| 99 | int optionIndex = 0; |
| 100 | int ic = getopt_long(argc, argv, "", longOptions, &optionIndex); |
| 101 | if (ic == -1) { |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | switch (ic) { |
| 106 | case 'h': |
| 107 | usage(); |
| 108 | return 0; |
| 109 | case 'c': { |
| 110 | string target_class_str = string(optarg); |
| 111 | transform(target_class_str.begin(), target_class_str.end(), |
| 112 | target_class_str.begin(), ::tolower); |
| 113 | if (!strcmp(target_class_str.c_str(), "hal")) { |
| 114 | target_class = vts::HAL; |
| 115 | } else { |
| 116 | target_class = 0; |
| 117 | } |
| 118 | break; |
| 119 | } |
| 120 | case 't': { |
| 121 | string target_type_str = string(optarg); |
| 122 | transform(target_type_str.begin(), target_type_str.end(), |
| 123 | target_type_str.begin(), ::tolower); |
| 124 | if (!strcmp(target_type_str.c_str(), "camera")) { |
| 125 | target_type = vts::CAMERA; |
| 126 | } else if (!strcmp(target_type_str.c_str(), "gps")) { |
| 127 | target_type = vts::GPS; |
| 128 | } else if (!strcmp(target_type_str.c_str(), "audio")) { |
| 129 | target_type = vts::AUDIO; |
| 130 | } else if (!strcmp(target_type_str.c_str(), "light")) { |
| 131 | target_type = vts::LIGHT; |
| 132 | } else { |
| 133 | target_type = 0; |
| 134 | } |
| 135 | break; |
| 136 | } |
| 137 | case 'v': |
| 138 | target_version = atof(optarg); |
| 139 | break; |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 140 | case 'e': |
| 141 | epoch_count = atoi(optarg); |
| 142 | if (epoch_count <= 0) { |
| 143 | fprintf(stderr, "epoch_count must be > 0"); |
| 144 | return 2; |
| 145 | } |
| 146 | break; |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 147 | case 's': |
| 148 | spec_dir_path = string(optarg); |
| 149 | break; |
Keun Soo Yim | 8e07a09 | 2016-05-04 16:30:35 -0700 | [diff] [blame^] | 150 | case 'n': |
| 151 | service_name = string(optarg); |
| 152 | break; |
Keun Soo Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 153 | case 'd': |
| 154 | server = true; |
| 155 | break; |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 156 | default: |
| 157 | if (ic != '?') { |
| 158 | fprintf(stderr, "getopt_long returned unexpected value 0x%x\n", ic); |
| 159 | } |
| 160 | return 2; |
| 161 | } |
| 162 | } |
| 163 | |
Keun Soo Yim | 8103c91 | 2016-04-22 20:07:13 -0700 | [diff] [blame] | 164 | android::vts::SpecificationBuilder spec_builder( |
| 165 | spec_dir_path, epoch_count); |
Keun Soo Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 166 | if (!server) { |
Keun Soo Yim | feceb4d | 2016-05-11 20:01:00 -0700 | [diff] [blame] | 167 | if (optind != argc - 1) { |
| 168 | fprintf(stderr, "Must specify output file (see --help).\n"); |
| 169 | return 2; |
| 170 | } |
| 171 | |
Keun Soo Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 172 | bool success = spec_builder.Process( |
| 173 | argv[optind], INTERFACE_SPEC_LIB_FILENAME, target_class, |
| 174 | target_type, target_version); |
| 175 | cout << "Result: " << success << endl; |
| 176 | if (success) { |
| 177 | cout << endl << PASSED_MARKER << endl; |
| 178 | } |
| 179 | } else { |
Keun Soo Yim | 8e07a09 | 2016-05-04 16:30:35 -0700 | [diff] [blame^] | 180 | android::vts::StartBinderServer(service_name, spec_builder, |
| 181 | INTERFACE_SPEC_LIB_FILENAME); |
Keun Soo Yim | bf80453 | 2016-05-07 22:40:38 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Keun Soo Yim | ff90224 | 2016-03-03 16:23:38 -0800 | [diff] [blame] | 184 | return 0; |
| 185 | } |