lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include <cstdint>
#include <iostream>
#include <set>
#include <sstream>
 
#include <android-base/file.h>
 
#include "dexanalyze_bytecode.h"
#include "dexanalyze_experiments.h"
#include "dexanalyze_strings.h"
#include "dex/code_item_accessors-inl.h"
#include "dex/dex_file.h"
#include "dex/dex_file_loader.h"
#include "dex/dex_instruction-inl.h"
 
namespace art {
namespace dexanalyze {
 
class DexAnalyze {
  static constexpr int kExitCodeUsageError = 1;
  static constexpr int kExitCodeFailedToOpenFile = 2;
  static constexpr int kExitCodeFailedToOpenDex = 3;
  static constexpr int kExitCodeFailedToProcessDex = 4;
 
  static void StdoutLogger(android::base::LogId,
                           android::base::LogSeverity,
                           const char*,
                           const char*,
                           unsigned int,
                           const char* message) {
    std::cout << message << std::endl;
  }
 
  static int Usage(char** argv) {
    LOG(ERROR)
        << "Usage " << argv[0] << " [options] <dex files>\n"
        << "    [options] is a combination of the following\n"
        << "    -count_indices (Count dex indices accessed from code items)\n"
        << "    -analyze-strings (Analyze string data)\n"
        << "    -analyze-debug-info (Analyze debug info)\n"
        << "    -new-bytecode (Bytecode optimizations)\n"
        << "    -i (Ignore Dex checksum and verification failures)\n"
        << "    -a (Run all experiments)\n"
        << "    -n <int> (run experiment with 1 .. n as argument)\n"
        << "    -d (Dump on per Dex basis)\n"
        << "    -v (quiet(0) to everything(2))\n";
    return kExitCodeUsageError;
  }
 
  struct Options {
    int Parse(int argc, char** argv) {
      int i;
      for (i = 1; i < argc; ++i) {
        const std::string arg = argv[i];
        if (arg == "-i") {
          verify_checksum_ = false;
          run_dex_file_verifier_ = false;
        } else if (arg == "-v") {
          if (i + 1 >= argc) {
            return Usage(argv);
          }
          std::istringstream iss(argv[i + 1]);
          size_t verbose_level = 0u;
          iss >> verbose_level;
          if (verbose_level > static_cast<size_t>(VerboseLevel::kEverything)) {
            return Usage(argv);
          }
          ++i;
          verbose_level_ = static_cast<VerboseLevel>(verbose_level);
        } else if (arg == "-a") {
          run_all_experiments_ = true;
        } else if (arg == "-n") {
          if (i + 1 >= argc) {
            return Usage(argv);
          }
          std::istringstream iss(argv[i + 1]);
          iss >> experiment_max_;
          ++i;
        } else if (arg == "-count-indices") {
          exp_count_indices_ = true;
        } else if (arg == "-analyze-strings") {
          exp_analyze_strings_ = true;
        } else if (arg == "-analyze-debug-info") {
          exp_debug_info_ = true;
        } else if (arg == "-new-bytecode") {
          exp_bytecode_ = true;
        } else if (arg == "-d") {
          dump_per_input_dex_ = true;
        } else if (!arg.empty() && arg[0] == '-') {
          return Usage(argv);
        } else {
          break;
        }
      }
      filenames_.insert(filenames_.end(), argv + i, argv + argc);
      if (filenames_.empty()) {
        return Usage(argv);
      }
      return 0;
    }
 
    VerboseLevel verbose_level_ = VerboseLevel::kNormal;
    bool verify_checksum_ = true;
    bool run_dex_file_verifier_ = true;
    bool dump_per_input_dex_ = false;
    bool exp_count_indices_ = false;
    bool exp_code_metrics_ = false;
    bool exp_analyze_strings_ = false;
    bool exp_debug_info_ = false;
    bool exp_bytecode_ = false;
    bool run_all_experiments_ = false;
    uint64_t experiment_max_ = 1u;
    std::vector<std::string> filenames_;
  };
 
  class Analysis {
   public:
    explicit Analysis(const Options* options) : options_(options) {
      if (options->run_all_experiments_ || options->exp_count_indices_) {
        experiments_.emplace_back(new CountDexIndices);
      }
      if (options->run_all_experiments_ || options->exp_analyze_strings_) {
        experiments_.emplace_back(new AnalyzeStrings);
      }
      if (options->run_all_experiments_ || options->exp_code_metrics_) {
        experiments_.emplace_back(new CodeMetrics);
      }
      if (options->run_all_experiments_ || options->exp_debug_info_) {
        experiments_.emplace_back(new AnalyzeDebugInfo);
      }
      if (options->run_all_experiments_ || options->exp_bytecode_) {
        for (size_t i = 0; i < options->experiment_max_; ++i) {
          uint64_t exp_value = 0u;
          if (i == 0) {
            exp_value = std::numeric_limits<uint64_t>::max();
          } else if (i == 1) {
            exp_value = 0u;
          } else {
            exp_value = 1u << (i - 2);
          }
          experiments_.emplace_back(new NewRegisterInstructions(exp_value));
        }
      }
      for (const std::unique_ptr<Experiment>& experiment : experiments_) {
        experiment->verbose_level_ = options->verbose_level_;
      }
    }
 
    bool ProcessDexFiles(const std::vector<std::unique_ptr<const DexFile>>& dex_files) {
      for (std::unique_ptr<Experiment>& experiment : experiments_) {
        experiment->ProcessDexFiles(dex_files);
      }
      for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
        total_size_ += dex_file->Size();
      }
      dex_count_ += dex_files.size();
      return true;
    }
 
    void Dump(std::ostream& os) {
      for (std::unique_ptr<Experiment>& experiment : experiments_) {
        experiment->Dump(os, total_size_);
        os << "\n";
      }
    }
 
    const Options* const options_;
    std::vector<std::unique_ptr<Experiment>> experiments_;
    size_t dex_count_ = 0;
    uint64_t total_size_ = 0u;
  };
 
 public:
  static int Run(int argc, char** argv) {
    android::base::SetLogger(StdoutLogger);
 
    Options options;
    int result = options.Parse(argc, argv);
    if (result != 0) {
      return result;
    }
 
    DexFileLoaderErrorCode error_code;
    std::string error_msg;
    Analysis cumulative(&options);
    for (const std::string& filename : options.filenames_) {
      std::string content;
      // TODO: once added, use an API to android::base to read a std::vector<uint8_t>.
      if (!android::base::ReadFileToString(filename.c_str(), &content)) {
        LOG(ERROR) << "ReadFileToString failed for " + filename << std::endl;
        return kExitCodeFailedToOpenFile;
      }
      std::vector<std::unique_ptr<const DexFile>> dex_files;
      const DexFileLoader dex_file_loader;
      if (!dex_file_loader.OpenAll(reinterpret_cast<const uint8_t*>(content.data()),
                                   content.size(),
                                   filename.c_str(),
                                   options.run_dex_file_verifier_,
                                   options.verify_checksum_,
                                   &error_code,
                                   &error_msg,
                                   &dex_files)) {
        LOG(ERROR) << "OpenAll failed for " + filename << " with " << error_msg << std::endl;
        return kExitCodeFailedToOpenDex;
      }
      if (options.dump_per_input_dex_) {
        Analysis current(&options);
        if (!current.ProcessDexFiles(dex_files)) {
          LOG(ERROR) << "Failed to process " << filename << " with error " << error_msg;
          return kExitCodeFailedToProcessDex;
        }
        LOG(INFO) << "Analysis for " << filename << std::endl;
        current.Dump(LOG_STREAM(INFO));
      }
      cumulative.ProcessDexFiles(dex_files);
    }
    LOG(INFO) << "Cumulative analysis for " << cumulative.dex_count_ << " DEX files" << std::endl;
    cumulative.Dump(LOG_STREAM(INFO));
    return 0;
  }
};
 
}  // namespace dexanalyze
}  // namespace art
 
int main(int argc, char** argv) {
  return art::dexanalyze::DexAnalyze::Run(argc, argv);
}