在Android系统中,应用执行效率一直是核心关注点。Android Runtime(ART)的预编译(Ahead-Of-Time, AOT)技术通过在应用安装时或系统空闲时将字节码预先编译为机器码,显著提升了应用运行性能。相较于传统的即时编译(JIT),AOT编译将运行时的编译开销提前到安装或系统空闲阶段,减少了应用启动时间和运行时的解释执行开销。AOT编译系统涉及多个关键组件,包括编译驱动、DEX文件解析器、中间表示(IR)生成器、优化器和目标代码生成器等。这些组件协同工作,将应用的字节码转换为高效的本地机器码,存储在系统中供运行时直接调用。理解AOT编译系统的工作原理,对于优化应用性能、理解ART运行机制具有重要意义。
AOT编译系统的架构设计体现了模块化和可扩展性的特点。在art/compiler/
目录下,可以看到多个核心模块的实现:
// AOT编译系统的核心类结构
class CompilerDriver {
private:
// 编译选项配置
CompilerOptions* options_;
// DEX文件解析器
DexFileLoader* dex_loader_;
// 中间表示生成器
HGraphBuilder* graph_builder_;
// 优化器
OptimizingCompiler* optimizer_;
// 后端代码生成器
BackendCompiler* backend_;
// 编译结果存储管理器
CompiledMethodStorage* storage_;
public:
// 执行AOT编译的主函数
bool CompileApp(const std::vector<std::string>& dex_files,
const std::string& output_filename);
};
这个架构将编译过程分解为多个独立模块,每个模块负责特定的任务,如Dex文件解析、中间表示生成、优化和代码生成等,便于维护和扩展。
AOT编译的流程控制由CompilerDriver
类主导,在art/compiler/driver/compiler_driver.cc
中实现:
// 执行AOT编译的主函数实现
bool CompilerDriver::CompileApp(const std::vector<std::string>& dex_files,
const std::string& output_filename) {
// 初始化编译环境
if (!InitCompiler()) {
return false;
}
// 加载Dex文件
std::vector<const DexFile*> dex_files = dex_loader_->LoadDexFiles(dex_files);
if (dex_files.empty()) {
return false;
}
// 遍历Dex文件中的所有类
for (const DexFile* dex_file : dex_files) {
for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
// 编译单个类
CompileClass(dex_file, class_def);
}
}
// 保存编译结果到文件
return storage_->SaveCompiledMethods(output_filename);
}
这个流程展示了AOT编译从加载Dex文件开始,遍历所有类和方法,然后编译每个方法,最后保存编译结果的完整过程。
AOT编译支持多种编译选项,这些选项会影响编译的速度、生成代码的质量和大小。在art/compiler/options/compiler_options.h
中定义了这些选项:
// 编译选项类
class CompilerOptions {
private:
// 编译优化级别
OptimizationLevel optimization_level_;
// 是否启用调试信息
bool generate_debug_info_;
// 是否启用内联优化
bool enable_inlining_;
// 是否启用循环优化
bool enable_loop_optimization_;
// 目标架构
TargetArchitecture target_arch_;
public:
// 设置编译优化级别
void SetOptimizationLevel(OptimizationLevel level) {
optimization_level_ = level;
}
// 获取是否启用调试信息
bool GenerateDebugInfo() const {
return generate_debug_info_;
}
// 其他选项的getter和setter方法...
};
这些选项允许开发者根据不同的场景和需求,调整AOT编译的行为,例如在开发阶段可以启用调试信息,而在发布版本中可以禁用以减小应用体积。
Dex文件是Android应用的字节码格式,理解其结构对于AOT编译至关重要。Dex文件的基本结构在art/dex_file/dex_file.h
中定义:
// Dex文件的头部结构
struct DexFile::Header {
uint8_t magic[8]; // 魔数,标识Dex文件
uint32_t checksum; // 校验和
uint8_t signature[kSHA1DigestSize]; // SHA-1哈希
uint32_t file_size; // 文件总大小
uint32_t header_size; // 头部大小
uint32_t endian_tag; // 字节序标记
uint32_t link_size; // 链接段大小
uint32_t link_off; // 链接段偏移
uint32_t map_off; // 映射表偏移
uint32_t string_ids_size; // 字符串ID数量
uint32_t string_ids_off; // 字符串ID表偏移
uint32_t type_ids_size; // 类型ID数量
uint32_t type_ids_off; // 类型ID表偏移
uint32_t proto_ids_size; // 原型ID数量
uint32_t proto_ids_off; // 原型ID表偏移
uint32_t field_ids_size; // 字段ID数量
uint32_t field_ids_off; // 字段ID表偏移
uint32_t method_ids_size; // 方法ID数量
uint32_t method_ids_off; // 方法ID表偏移
uint32_t class_defs_size; // 类定义数量
uint32_t class_defs_off; // 类定义表偏移
uint32_t data_size; // 数据段大小
uint32_t data_off; // 数据段偏移
};
Dex文件包含了类、方法、字段等信息,这些信息以特定的格式组织在文件中,供解析器读取和处理。
Dex文件解析器负责从Dex文件中提取各类信息,在art/dex_file/dex_file_loader.cc
中实现:
// 加载Dex文件的函数
std::vector<std::unique_ptr<const DexFile>> DexFileLoader::Open(
const std::string& location,
const std::string& location_checksum,
bool verify,
bool verify_checksum,
std::string* error_msg) {
// 打开Dex文件
std::unique_ptr<File> file(OS::OpenFileForReading(location.c_str()));
if (file.get() == nullptr) {
*error_msg = StringPrintf("Unable to open '%s': %s",
location.c_str(), strerror(errno));
return {};
}
// 映射文件到内存
std::unique_ptr<const uint8_t[]> base;
size_t size;
if (!MapFile(file.get(), &base, &size, error_msg)) {
return {};
}
// 验证Dex文件头部
const Header* header = reinterpret_cast<const Header*>(base.get());
if (!VerifyHeader(header, size, verify_checksum, error_msg)) {
return {};
}
// 解析Dex文件的各个部分
std::vector<std::unique_ptr<const DexFile>> dex_files;
if (!ParseDexFile(base.get(), size, location, location_checksum,
verify, verify_checksum, &dex_files, error_msg)) {
return {};
}
return dex_files;
}
解析器首先验证Dex文件的头部信息,然后依次解析字符串表、类型表、方法表等各个部分,最终构建出内存中的Dex文件表示。
在解析Dex文件的过程中,需要提取类和方法的详细信息。在art/dex_file/dex_file.cc
中:
// 获取类定义信息
const DexFile::ClassDef* DexFile::GetClassDef(size_t index) const {
DCHECK_LT(index, NumClassDefs());
return reinterpret_cast<const ClassDef*>(Begin() + class_defs_off_ +
(index * sizeof(ClassDef)));
}
// 获取方法信息
const DexFile::MethodId* DexFile::GetMethodId(uint32_t index) const {
DCHECK_LT(index, method_ids_size_);
return reinterpret_cast<const MethodId*>(Begin() + method_ids_off_ +
(index * sizeof(MethodId)));
}
// 获取方法代码项
const DexFile::CodeItem* DexFile::GetCodeItem(const MethodId& method_id) const {
const ClassDef* class_def = FindClassDef(method_id.class_idx_);
if (class_def == nullptr) {
return nullptr;
}
const uint8_t* class_data = GetClassData(class_def);
if (class_data == nullptr) {
return nullptr;
}
ClassDataItemIterator it(*this, class_data);
while (it.HasNext()) {
if (it.GetMemberIndex() == method_id.proto_idx_) {
return it.GetMethodCodeItem();
}
it.Next();
}
return nullptr;
}
这些函数展示了如何从Dex文件中提取类和方法的定义信息,以及方法的具体代码项,为后续的编译过程提供必要的数据。
在AOT编译过程中,Dex字节码首先被转换为中间表示(IR),以便进行平台无关的优化。ART使用了多种IR形式,包括高层IR(HIR)和低层IR(LIR)。在art/compiler/optimizing/hir.h
中定义了高层IR的基本结构:
// 高层IR指令的基类
class HInstruction {
public:
// 指令类型枚举
enum InstructionKind {
kAdd,
kSubtract,
kMultiply,
kDivide,
kModulo,
kAnd,
kOr,
kXor,
kNeg,
kNot,
kInvokeVirtual,
kInvokeStatic,
kInvokeDirect,
kReturn,
kIf,
kGoto,
// 其他指令类型...
};
// 构造函数
HInstruction(InstructionKind kind) : kind_(kind) {}
// 获取指令类型
InstructionKind GetKind() const { return kind_; }
// 获取操作数数量
size_t InputCount() const { return inputs_.size(); }
// 获取第n个操作数
HInstruction* InputAt(size_t n) const { return inputs_[n]; }
// 添加操作数
void AddInput(HInstruction* input) { inputs_.push_back(input); }
// 设置结果类型
void SetResultType(Primitive::Type type) { result_type_ = type; }
// 获取结果类型
Primitive::Type GetResultType() const { return result_type_; }
private:
// 指令类型
InstructionKind kind_;
// 操作数列表
std::vector<HInstruction*> inputs_;
// 结果类型
Primitive::Type result_type_;
};
高层IR指令保留了较高的抽象级别,便于进行各种优化操作。
将Dex字节码转换为IR的过程在art/compiler/optimizing/bytecode_translator.cc
中实现:
// 字节码到IR的转换器
class BytecodeTranslator {
public:
// 构造函数
BytecodeTranslator(const DexFile* dex_file,
const DexFile::CodeItem* code_item,
HGraph* graph)
: dex_file_(dex_file), code_item_(code_item), graph_(graph) {}
// 转换整个方法的字节码
void TranslateBytecode() {
const uint16_t* insns = code_item_->insns_;
uint32_t insns_size = code_item_->insns_size_in_code_units_;
for (uint32_t pc = 0; pc < insns_size; ) {
uint16_t opcode = insns[pc] & 0xff;
// 根据操作码处理不同的字节码指令
switch (opcode) {
case dex::OP_CONST:
TranslateConst(pc);
pc += InstructionSize(dex::OP_CONST);
break;
case dex::OP_ADD_INT:
TranslateAddInt(pc);
pc += InstructionSize(dex::OP_ADD_INT);
break;
case dex::OP_INVOKE_VIRTUAL:
TranslateInvokeVirtual(pc);
pc += InstructionSize(dex::OP_INVOKE_VIRTUAL);
break;
case dex::OP_RETURN_VOID:
TranslateReturnVoid(pc);
pc += InstructionSize(dex::OP_RETURN_VOID);
break;
// 处理其他字节码指令...
default:
LOG(FATAL) << "Unsupported opcode: " << opcode;
break;
}
}
}
private:
// 转换CONST指令
void TranslateConst(uint32_t pc) {
uint16_t vA = (insns[pc] >> 8) & 0xff;
int32_t value = DecodeIntConstant(pc);
// 创建常量指令
HConstant* constant = new (graph_->GetArena()) HConstant(value);
graph_->AddInstruction(constant);
// 将常量存入局部变量表
locals_[vA] = constant;
}
// 转换ADD_INT指令
void TranslateAddInt(uint32_t pc) {
uint16_t vA = (insns[pc] >> 8) & 0x0f;
uint16_t vB = (insns[pc] >> 12) & 0x0f;
uint16_t vC = insns[pc + 1] & 0x0f;
// 获取操作数
HInstruction* left = locals_[vB];
HInstruction* right = locals_[vC];
// 创建加法指令
HAdd* add = new (graph_->GetArena()) HAdd(Primitive::kPrimInt, left, right);
graph_->AddInstruction(add);
// 将结果存入局部变量表
locals_[vA] = add;
}
// 其他字节码指令的转换函数...
private:
// Dex文件引用
const DexFile* dex_file_;
// 当前方法的代码项
const DexFile::CodeItem* code_item_;
// 目标HGraph
HGraph* graph_;
// 局部变量表
std::vector<HInstruction*> locals_;
};
这个转换器将Dex字节码逐条转换为对应的高层IR指令,构建出方法的控制流图(CFG)。
在IR生成过程中,控制流图(CFG)的构建是关键步骤。在art/compiler/optimizing/hgraph.cc
中:
// 构建控制流图
void HGraph::BuildControlFlowGraph() {
// 创建入口基本块
HBasicBlock* entry_block = new (arena_) HBasicBlock(arena_, 0);
blocks_.push_back(entry_block);
// 为每个基本块分配ID并添加到图中
size_t block_id = 1;
for (HInstruction* instruction : instructions_) {
if (instruction->IsBlockEnd()) {
// 如果指令是基本块的结束指令(如分支、返回)
HBasicBlock* current_block = blocks_.back();
current_block->AddInstruction(instruction);
// 创建新的基本块
HBasicBlock* new_block = new (arena_) HBasicBlock(arena_, block_id++);
blocks_.push_back(new_block);
// 根据指令类型设置基本块之间的连接关系
if (instruction->IsGoto()) {
// GOTO指令,直接跳转到目标基本块
HInstruction* target = instruction->GetTarget();
HBasicBlock* target_block = FindBlockForInstruction(target);
current_block->AddSuccessor(target_block);
} else if (instruction->IsIf()) {
// IF指令,有两个可能的目标
HInstruction* true_target = instruction->GetTrueTarget();
HInstruction* false_target = instruction->GetFalseTarget();
HBasicBlock* true_block = FindBlockForInstruction(true_target);
HBasicBlock* false_block = FindBlockForInstruction(false_target);
current_block->AddSuccessor(true_block);
current_block->AddSuccessor(false_block);
} else if (instruction->IsReturn()) {
// 返回指令,基本块结束
current_block->SetIsExitBlock(true);
}
} else {
// 普通指令,添加到当前基本块
blocks_.back()->AddInstruction(instruction);
}
}
// 设置基本块的前驱关系
for (HBasicBlock* block : blocks_) {
for (HBasicBlock* succ : block->GetSuccessors()) {
succ->AddPredecessor(block);
}
}
}
控制流图的构建将独立的IR指令组织成具有逻辑关系的基本块集合,为后续的优化和代码生成提供了基础结构。
ART的IR优化器负责对生成的中间表示进行各种优化,提高代码执行效率。优化器架构在art/compiler/optimizing/optimizing_compiler.cc
中实现:
// 优化编译器类
class OptimizingCompiler {
public:
// 执行优化
void Optimize(HGraph* graph, const CompilerOptions& options) {
// 应用一系列优化
ApplyEarlyOptimizations(graph, options);
ApplyMidTierOptimizations(graph, options);
ApplyLateOptimizations(graph, options);
}
private:
// 应用早期优化
void ApplyEarlyOptimizations(HGraph* graph, const CompilerOptions& options) {
// 常量传播
ConstantPropagation(graph);
// 死代码消除
DeadCodeElimination(graph);
// 内联小方法
InlineSmallMethods(graph, options);
}
// 应用中间层优化
void ApplyMidTierOptimizations(HGraph* graph, const CompilerOptions& options) {
// 循环优化
LoopOptimization(graph);
// 数据流分析
DataFlowAnalysis(graph);
// 强度削弱
StrengthReduction(graph);
}
// 应用后期优化
void ApplyLateOptimizations(HGraph* graph, const CompilerOptions& options) {
// 寄存器分配优化
RegisterAllocationOptimization(graph);
// 指令调度优化
InstructionScheduling(graph);
// 代码布局优化
CodeLayoutOptimization(graph);
}
// 各种优化算法的实现...
};
优化器采用多阶段优化策略,每个阶段针对不同类型的问题进行优化,逐步提高代码质量。
常量传播和死代码消除是两种基础且重要的优化技术。在art/compiler/optimizing/constant_propagation.cc
中:
// 常量传播优化
class ConstantPropagation {
public:
// 执行常量传播
void Run(HGraph* graph) {
// 初始化工作列表
std::queue<HInstruction*> worklist;
for (HBasicBlock* block : graph->GetBlocks()) {
for (HInstruction* instruction : block->GetInstructions()) {
worklist.push(instruction);
}
}
// 处理工作列表
while (!worklist.empty()) {
HInstruction* instruction = worklist.front();
worklist.pop();
// 如果指令的所有输入都是常量
if (AreAllInputsConstants(instruction)) {
// 计算常量值
ConstantValue result = EvaluateConstant(instruction);
// 替换指令为常量
ReplaceWithConstant(instruction, result);
// 将使用该指令的后续指令加入工作列表
for (HInstruction* user : instruction->GetUsers()) {
worklist.push(user);
}
}
}
// 执行死代码消除
EliminateDeadCode(graph);
}
private:
// 判断指令的所有输入是否都是常量
bool AreAllInputsConstants(HInstruction* instruction) {
for (size_t i = 0; i < instruction->InputCount(); ++i) {
if (!instruction->InputAt(i)->IsConstant()) {
return false;
}
}
return true;
}
// 计算常量表达式的值
ConstantValue EvaluateConstant(HInstruction* instruction) {
// 根据指令类型计算常量值
switch (instruction->GetKind()) {
case HInstruction::kAdd:
return EvaluateAdd(instruction);
case HInstruction::kSubtract:
return EvaluateSubtract(instruction);
case HInstruction::kMultiply:
return EvaluateMultiply(instruction);
// 其他指令类型的计算...
default:
return ConstantValue::NoValue();
}
}
// 消除死代码
void EliminateDeadCode(HGraph* graph) {
// 标记所有可达指令
std::unordered_set<HInstruction*> live_instructions;
for (HBasicBlock* block : graph->GetExitBlocks()) {
MarkLiveInstructions(block, live_instructions);
}
// 移除不可达指令
for (HBasicBlock* block : graph->GetBlocks()) {
std::vector<HInstruction*> new_instructions;
for (HInstruction* instruction : block->GetInstructions()) {
if (live_instructions.count(instruction) > 0) {
new_instructions.push_back(instruction);
} else {
// 移除死指令
instruction->DisconnectAndDelete();
}
}
block->SetInstructions(new_instructions);
}
}
// 标记所有可达指令
void MarkLiveInstructions(HBasicBlock* block,
std::unordered_set<HInstruction*>& live_instructions) {
for (HInstruction* instruction : block->GetInstructions()) {
if (live_instructions.insert(instruction).second) {
// 指令第一次被标记,递归处理其输入
for (size_t i = 0; i < instruction->InputCount(); ++i) {
MarkLiveInstructions(instruction->InputAt(i), live_instructions);
}
}
}
}
};
常量传播通过识别和替换已知的常量值,减少运行时的计算量;死代码消除则移除那些永远不会被执行的代码,减小代码体积。
循环优化对于提高计算密集型应用的性能至关重要。在art/compiler/optimizing/loop_optimization.cc
中:
// 循环优化类
class LoopOptimization {
public:
// 执行循环优化
void Run(HGraph* graph) {
// 识别循环结构
LoopInformation* loop_info = new LoopInformation(graph);
loop_info->FindLoops();
// 对每个循环应用优化
for (Loop* loop : loop_info->GetLoops()) {
// 循环不变代码外提
HoistLoopInvariants(loop);
// 循环展开
if (ShouldUnrollLoop(loop)) {
UnrollLoop(loop);
}
// 循环强度削弱
StrengthReduction(loop);
}
}
private:
// 循环不变代码外提
void HoistLoopInvariants(Loop* loop) {
// 找到循环前置块
HBasicBlock* pre_header = loop->GetPreHeader();
if (pre_header == nullptr) {
// 创建循环前置块
pre_header = loop->CreatePreHeader();
}
// 遍历循环中的所有指令
for (HBasicBlock* block : loop->GetBlocks()) {
for (auto it = block->GetInstructions().begin();
it != block->GetInstructions().end(); ) {
HInstruction* instruction = *it;
++it;
// 判断指令是否是循环不变量
if (IsLoopInvariant(instruction, loop)) {
// 将不变量指令移动到循环前置块
pre_header->AddInstruction(instruction);
block->RemoveInstruction(instruction);
}
}
}
}
// 循环展开
void UnrollLoop(Loop* loop) {
// 确定展开因子
int unroll_factor = DetermineUnrollFactor(loop);
if (unroll_factor <= 1) {
return;
}
// 复制循环体
std::vector<LoopCopy> copies = loop->CloneLoopBody(unroll_factor);
// 调整循环控制
AdjustLoopControl(loop, copies, unroll_factor);
// 合并基本块
MergeBlocks(loop, copies);
}
// 判断指令是否是循环不变量
bool IsLoopInvariant(HInstruction* instruction, Loop* loop) {
// 指令不能是PHI节点
if (instruction->IsPhi()) {
return false;
}
// 指令的所有输入必须要么是循环不变量,要么不是循环内定义的
for (size_t i = 0; i < instruction->InputCount(); ++i) {
HInstruction* input = instruction->InputAt(i);
if (loop->Contains(input->GetBlock())) {
if (!IsLoopInvariant(input, loop)) {
return false;
}
}
}
return true;
}
};
循环优化通过不变代码外提、循环展开等技术,减少循环内部的计算量和分支预测错误,提高循环执行效率。
ART的后端代码生成器负责将优化后的IR转换为目标架构的机器码。后端架构在art/compiler/backend/
目录下实现,采用架构特定的设计:
// 后端代码生成器的基类
class Backend {
public:
// 构造函数
Backend(const CompilerOptions& options, const InstructionSetFeatures* features)
: options_(options), features_(features) {}
// 生成目标代码
virtual void GenerateCode(HGraph* graph, CodeAllocator* allocator) = 0;
// 获取目标指令集
virtual InstructionSet GetInstructionSet() const = 0;
protected:
// 编译选项
const CompilerOptions& options_;
// 指令集特性
const InstructionSetFeatures* features_;
};
// ARM架构的后端代码生成器
class ArmBackend : public Backend {
public:
// 构造函数
ArmBackend(const CompilerOptions& options, const InstructionSetFeatures* features)
: Backend(options, features) {}
// 生成ARM目标代码
void GenerateCode(HGraph* graph, CodeAllocator* allocator) override {
// 创建指令选择器
ArmInstructionSelector selector(graph, this, allocator);
// 执行指令选择
selector.SelectInstructions();
// 执行寄存器分配
ArmRegisterAllocator register_allocator(graph, this, allocator);
register_allocator.AllocateRegisters();
// 生成最终代码
ArmCodeGenerator code_generator(graph, this, allocator, ®ister_allocator);
code_generator.GenerateCode();
}
// 获取指令集
InstructionSet GetInstructionSet() const override {
return InstructionSet::kArm;
}
};
// x86架构的后端代码生成器
class X86Backend : public Backend {
// 类似ARM后端的实现...
};
这种架构设计允许ART支持多种目标架构,每个架构的后端代码生成器负责处理特定架构的指令集和特性。
指令选择是将IR指令转换为目标架构具体指令的过程。在art/compiler/backend/arm/instruction_selector.cc
中:
// ARM架构的指令选择器
class ArmInstructionSelector {
public:
// 构造函数
ArmInstructionSelector(HGraph* graph, ArmBackend* backend, CodeAllocator* allocator)
: graph_(graph), backend_(backend), allocator_(allocator) {}
// 执行指令选择
void SelectInstructions() {
// 遍历HGraph中的每个基本块
for (HBasicBlock* block : graph_->GetBlocks()) {
// 为基本块创建对应的LIR基本块
LIRBlock* lir_block = allocator_->CreateLIRBlock();
// 遍历基本块中的每个指令
for (HInstruction* instruction : block->GetInstructions()) {
// 根据指令类型选择对应的ARM指令
switch (instruction->GetKind()) {
case HInstruction::kAdd:
SelectAddInstruction(instruction, lir_block);
break;
case HInstruction::kSubtract:
SelectSubtractInstruction(instruction, lir_block);
break;
case HInstruction::kMultiply:
SelectMultiplyInstruction(instruction, lir_block);
break;
case HInstruction::kInvokeVirtual:
SelectInvokeVirtualInstruction(instruction, lir_block);
break;
// 处理其他指令类型...
default:
LOG(FATAL) << "Unsupported instruction kind: " << instruction->GetKind();
break;
}
}
}
}
private:
// 选择ADD指令
void SelectAddInstruction(HInstruction* instruction, LIRBlock* lir_block) {
HAdd* add = instruction->AsAdd();
LIRRegister dst = AllocateRegister(add->GetResultType());
LIRRegister src1 = GetRegisterForInstruction(add->InputAt(0));
LIRRegister src2 = GetRegisterForInstruction(add->InputAt(1));
// 创建ARM的ADD指令
LIR* lir = new (allocator_->GetArena()) LIR(
kArmAdd, dst, src1, src2);
// 添加到LIR基本块
lir_block->AppendInstruction(lir);
// 记录结果寄存器分配
SetRegisterForInstruction(instruction, dst);
}
// 选择乘法指令
void SelectMultiplyInstruction(HInstruction* instruction, LIRBlock* lir_block) {
HMultiply* mul = instruction->AsMultiply();
LIRRegister dst = AllocateRegister(mul->GetResultType());
LIRRegister src1 = GetRegisterForInstruction(mul->InputAt(0));
LIRRegister src2 = GetRegisterForInstruction(mul->InputAt(1));
// 检查是否可以使用ARM的MUL指令
if (src1.IsGeneralPurpose() && src2.IsGeneralPurpose()) {
// 创建ARM的MUL指令
LIR* lir = new (allocator_->GetArena()) LIR(
kArmMul, dst, src1, src2);
lir_block->AppendInstruction(lir);
} else {
// 无法使用MUL指令,生成替代代码序列
GenerateMultiplyFallback(mul, lir_block);
}
// 记录结果寄存器分配
SetRegisterForInstruction(instruction, dst);
}
// 生成乘法的后备代码序列
void GenerateMultiplyFallback(HMultiply* mul, LIRBlock* lir_block) {
// 使用移位和加法实现乘法
// 例如:a * b = (a << 1) + (a << 2) + ...
// 实际实现会更复杂,这里简化处理
}
private:
// HGraph引用
HGraph* graph_;
// 后端引用
ArmBackend* backend_;
// 代码分配器
CodeAllocator* allocator_;
// 寄存器分配映射
std::unordered_map<HInstruction*, LIRRegister> register_map_;
};
指令选择过程需要考虑目标架构的指令集特性,选择最合适的指令来实现IR指令的语义。
寄存器分配是将IR中的虚拟寄存器映射到目标架构的物理寄存器的过程。在art/compiler/backend/arm/register_allocator.cc
中:
// ARM架构的寄存器分配器
class ArmRegisterAllocator {
public:
// 构造函数
ArmRegisterAllocator(HGraph* graph, ArmBackend* backend, CodeAllocator* allocator)
: graph_(graph), backend_(backend), allocator_(allocator) {}
// 执行寄存器分配
void AllocateRegisters() {
// 构建干涉图
BuildInterferenceGraph();
// 着色干涉图,分配物理寄存器
ColorInterferenceGraph();
// 插入溢出代码(如果需要)
InsertSpillCode();
// 更新LIR指令中的寄存器引用
UpdateLIRInstructions();
}
private:
// 构建干涉图
void BuildInterferenceGraph() {
// 遍历所有LIR基本块
for (LIRBlock* block : allocator_->GetLIRBlocks()) {
// 计算活跃变量
ComputeLiveRanges(block);
// 构建干涉关系
BuildInterferences(block);
}
}
// 着色干涉图
void ColorInterferenceGraph() {
// 使用图着色算法分配寄存器
// 实际实现中会使用更复杂的算法
// 这里简化处理
std::vector<LIRRegister> registers = GetAvailableRegisters();
// 遍历所有虚拟寄存器
for (VirtualRegister* vreg : virtual_registers_) {
// 尝试为虚拟寄存器分配物理寄存器
bool allocated = false;
for (LIRRegister phys_reg : registers) {
if (CanAllocate(vreg, phys_reg)) {
vreg->SetPhysicalRegister(phys_reg);
allocated = true;
break;
}
}
// 如果无法分配寄存器,标记为需要溢出
if (!allocated) {
vreg->SetNeedsSpill(true);
}
}
// 插入溢出代码
void InsertSpillCode() {
for (VirtualRegister* vreg : virtual_registers_) {
if (vreg->NeedsSpill()) {
// 找到合适的内存位置存储溢出的寄存器值
MemoryLocation spill_location = AllocateMemoryLocation();
for (LIRBlock* block : allocator_->GetLIRBlocks()) {
for (LIRInstruction* inst : block->GetInstructions()) {
if (inst->UsesRegister(vreg)) {
// 在指令前插入存储指令
LIRStoreInstruction* store_inst = new (allocator_->GetArena()) LIRStoreInstruction(
vreg->GetPhysicalRegister(), spill_location);
block->InsertInstructionBefore(inst, store_inst);
}
if (inst->DefinesRegister(vreg)) {
// 在指令后插入加载指令
LIRLoadInstruction* load_inst = new (allocator_->GetArena()) LIRLoadInstruction(
vreg->GetPhysicalRegister(), spill_location);
block->InsertInstructionAfter(inst, load_inst);
}
}
}
}
}
}
// 更新LIR指令中的寄存器引用
void UpdateLIRInstructions() {
for (LIRBlock* block : allocator_->GetLIRBlocks()) {
for (LIRInstruction* inst : block->GetInstructions()) {
for (size_t i = 0; i < inst->OperandCount(); ++i) {
Register operand = inst->GetOperand(i);
if (operand.IsVirtual()) {
VirtualRegister* vreg = static_cast<VirtualRegister*>(operand.GetRegister());
if (vreg->HasPhysicalRegister()) {
inst->SetOperand(i, vreg->GetPhysicalRegister());
}
}
}
}
}
}
private:
// HGraph引用
HGraph* graph_;
// 后端引用
ArmBackend* backend_;
// 代码分配器
CodeAllocator* allocator_;
// 虚拟寄存器集合
std::vector<VirtualRegister*> virtual_registers_;
// 干涉图
InterferenceGraph interference_graph_;
};
寄存器分配过程通过构建干涉图分析寄存器使用冲突,采用图着色算法或溢出策略,确保生成的机器码正确使用物理寄存器资源。
在完成指令选择和寄存器分配后,最终生成目标机器码并输出。以ARM架构为例,在art/compiler/backend/arm/code_generator.cc
中:
// ARM架构的代码生成器
class ArmCodeGenerator {
public:
ArmCodeGenerator(HGraph* graph, ArmBackend* backend, CodeAllocator* allocator,
RegisterAllocator* reg_allocator)
: graph_(graph), backend_(backend), allocator_(allocator), reg_allocator_(reg_allocator) {}
// 生成最终代码
void GenerateCode() {
// 遍历所有LIR基本块
for (LIRBlock* block : allocator_->GetLIRBlocks()) {
// 生成基本块的入口代码
GenerateBlockPrologue(block);
// 生成指令代码
GenerateInstructions(block);
// 生成基本块的出口代码
GenerateBlockEpilogue(block);
}
// 处理全局数据和跳转表
GenerateGlobalData();
GenerateJumpTable();
}
private:
// 生成基本块入口代码
void GenerateBlockPrologue(LIRBlock* block) {
// 保存需要的寄存器到栈
for (LIRRegister reg : callee_saved_registers_) {
if (block->UsesRegister(reg)) {
GeneratePushInstruction(reg);
}
}
// 调整栈指针
GenerateStackAdjustmentInstruction(block->GetStackUsage());
}
// 生成指令代码
void GenerateInstructions(LIRBlock* block) {
for (LIRInstruction* inst : block->GetInstructions()) {
switch (inst->GetOpcode()) {
case kArmAdd:
GenerateAddInstruction(inst);
break;
case kArmSub:
GenerateSubtractInstruction(inst);
break;
case kArmMul:
GenerateMultiplyInstruction(inst);
break;
case kArmLoad:
GenerateLoadInstruction(inst);
break;
case kArmStore:
GenerateStoreInstruction(inst);
break;
case kArmBranch:
GenerateBranchInstruction(inst);
break;
default:
LOG(FATAL) << "Unsupported LIR opcode: " << inst->GetOpcode();
break;
}
}
}
// 生成ADD指令
void GenerateAddInstruction(LIRInstruction* inst) {
LIRRegister dst = inst->GetDestinationRegister();
LIRRegister src1 = inst->GetOperand(0).GetRegister();
LIRRegister src2 = inst->GetOperand(1).GetRegister();
// ARM指令编码:ADD {条件码}{S} Rd, Rn, Operand2
uint32_t opcode = 0x01000000 | (dst.GetIndex() << 12) | (src1.GetIndex() << 16) | (src2.GetIndex());
allocator_->EmitInstruction(opcode);
}
// 生成基本块出口代码
void GenerateBlockEpilogue(LIRBlock* block) {
// 恢复保存的寄存器
for (LIRRegister reg : callee_saved_registers_) {
if (block->UsesRegister(reg)) {
GeneratePopInstruction(reg);
}
}
// 恢复栈指针
GenerateStackAdjustmentInstruction(-block->GetStackUsage());
// 如果是返回块,生成返回指令
if (block->IsReturnBlock()) {
GenerateReturnInstruction();
}
}
// 生成全局数据
void GenerateGlobalData() {
// 遍历全局变量
for (GlobalVariable* var : graph_->GetGlobalVariables()) {
// 分配内存
MemoryLocation loc = allocator_->AllocateGlobalVariable(var->GetSize());
// 写入初始值
allocator_->EmitData(var->GetInitialValue(), var->GetSize());
}
}
private:
// HGraph引用
HGraph* graph_;
// 后端引用
ArmBackend* backend_;
// 代码分配器
CodeAllocator* allocator_;
// 寄存器分配器
RegisterAllocator* reg_allocator_;
// 被调用者保存的寄存器列表
std::vector<LIRRegister> callee_saved_registers_;
};
代码生成器将LIR指令转换为具体的机器码指令,并处理代码的入口、出口以及全局数据,最终生成可供运行时执行的目标代码。
AOT编译生成的机器码和相关元数据需要以特定格式存储,以便运行时加载。在art/runtime/compiled_method_storage.h
中定义了存储结构:
// 编译结果存储类
class CompiledMethodStorage {
public:
// 构造函数
CompiledMethodStorage() {}
// 添加编译后的方法
void AddCompiledMethod(ArtMethod* method, const void* code, size_t code_size,
const CompiledMethodInfo& info) {
methods_.push_back({method, code, code_size, info});
}
// 保存编译结果到文件
bool SaveToFile(const std::string& filename) {
std::unique_ptr<File> file(OS::OpenFileForWriting(filename.c_str()));
if (file.get() == nullptr) {
return false;
}
// 写入文件头
WriteFileHeader(file.get());
// 写入方法数量
uint32_t method_count = static_cast<uint32_t>(methods_.size());
file->WriteFully(&method_count, sizeof(method_count));
// 写入每个方法的信息和代码
for (const auto& entry : methods_) {
WriteMethodInfo(file.get(), entry.info);
file->WriteFully(entry.code, entry.code_size);
}
return true;
}
// 从文件加载编译结果
bool LoadFromFile(const std::string& filename) {
std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str()));
if (file.get() == nullptr) {
return false;
}
// 验证文件头
if (!VerifyFileHeader(file.get())) {
return false;
}
// 读取方法数量
uint32_t method_count;
file->ReadFully(&method_count, sizeof(method_count));
// 读取每个方法的信息和代码
for (uint32_t i = 0; i < method_count; ++i) {
CompiledMethodInfo info;
ReadMethodInfo(file.get(), &info);
void* code = allocator_.Allocate(info.code_size);
file->ReadFully(code, info.code_size);
ArtMethod* method = info.method;
method->SetJitCode(code);
method->SetJitCodeSize(info.code_size);
method->SetJitCompilationInfo(info);
}
return true;
}
private:
// 写入文件头
void WriteFileHeader(File* file) {
// 文件头包含魔数和版本号
uint32_t magic = kCompiledMethodStorageMagic;
uint32_t version = kCompiledMethodStorageVersion;
file->WriteFully(&magic, sizeof(magic));
file->WriteFully(&version, sizeof(version));
}
// 验证文件头
bool VerifyFileHeader(File* file) {
uint32_t magic, version;
file->ReadFully(&magic, sizeof(magic));
file->ReadFully(&version, sizeof(version));
return magic == kCompiledMethodStorageMagic && version == kCompiledMethodStorageVersion;
}
// 写入方法信息
void WriteMethodInfo(File* file, const CompiledMethodInfo& info) {
// 方法信息包括方法指针、代码大小等
file->WriteFully(&info.method, sizeof(info.method));
uint32_t code_size = static_cast<uint32_t>(info.code_size);
file->WriteFully(&code_size, sizeof(code_size));
// 写入其他元数据
// ...
}
// 读取方法信息
void ReadMethodInfo(File* file, CompiledMethodInfo* info) {
file->ReadFully(&info->method, sizeof(info->method));
uint32_t code_size;
file->ReadFully(&code_size, sizeof(code_size));
info->code_size = static_cast<size_t>(code_size);
// 读取其他元数据
// ...
}
private:
// 编译后的方法列表
std::vector<std::tuple<ArtMethod*, const void*, size_t, CompiledMethodInfo>> methods_;
// 内存分配器
MemoryAllocator allocator_;
// 魔数和版本号
static const uint32_t kCompiledMethodStorageMagic = 0x414f5443; // "AOTC"
static const uint32_t kCompiledMethodStorageVersion = 1;
};
这种存储格式确保了编译结果的完整性和可加载性,运行时可根据存储的元数据快速定位和使用机器码。
AOT编译结果通常存储在设备的持久化存储中,如/data/dalvik-cache/
目录下。在art/runtime/art_storage_manager.cc
中管理存储路径和清理策略:
// ART存储管理器
class ArtStorageManager {
public:
// 获取AOT编译结果存储路径
std::string GetAotStoragePath() {
// 构建路径,如/data/dalvik-cache/arm
std::string arch = GetInstructionSetString(InstructionSet::kArm);
return StringPrintf("/data/dalvik-cache/%s", arch.c_str());
}
// 清理过期的AOT编译结果
void CleanupOldAotFiles() {
std::string storage_path = GetAotStoragePath();
std::vector<std::string> files = ListFilesInDirectory(storage_path);
for (const std::string& file : files) {
std::string file_path = StringPrintf("%s/%s", storage_path.c_str(), file.c_str());
// 获取文件修改时间
time_t modification_time = GetFileModificationTime(file_path);
// 判断是否超过保留期限
if (time(nullptr) - modification_time > kAotFileRetentionPeriod) {
// 删除过期文件
OS::Remove(file_path.c_str());
}
}
}
private:
// 获取文件修改时间
time_t GetFileModificationTime(const std::string& file_path) {
struct stat file_stat;
if (stat(file_path.c_str(), &file_stat) == 0) {
return file_stat.st_mtime;
}
return 0;
}
// 列出目录下的所有文件
std::vector<std::string> ListFilesInDirectory(const std::string& directory) {
std::vector<std::string> files;
DIR* dir = opendir(directory.c_str());
if (dir != nullptr) {
struct dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
if (entry->d_type == DT_REG) {
files.push_back(entry->d_name);
}
}
closedir(dir);
}
return files;
}
private:
// AOT文件保留期限(秒)
static const time_t kAotFileRetentionPeriod = 7 * 24 * 60 * 60; // 7天
};
存储管理器负责管理存储路径,并通过定期清理过期文件,避免占用过多磁盘空间,同时保证存储的编译结果是最新有效的。
运行时需要从存储中加载AOT编译结果,在art/runtime/art_method.cc
和art/runtime/class_linker.cc
中实现加载逻辑:
// ArtMethod类中加载AOT代码的方法
void ArtMethod::LoadAotCode() {
// 获取存储路径
std::string storage_path = ArtStorageManager::GetInstance().GetAotStoragePath();
std::string file_name = GenerateAotFileName(this);
std::string file_path = StringPrintf("%s/%s", storage_path.c_str(), file_name.c_str());
// 从文件加载代码
CompiledMethodStorage storage;
if (storage.LoadFromFile(file_path)) {
SetJitCode(storage.GetCodeForMethod(this));
SetJitCodeSize(storage.GetCodeSizeForMethod(this));
}
}
// ClassLinker类中链接类时加载AOT代码
void ClassLinker::LinkClass(Thread* self, Handle<mirror::Class> clazz) {
// 遍历类中的所有方法
for (size_t i = 0; i < clazz->GetMethodCount(); ++i) {
ArtMethod* method = clazz->GetDirectMethod(i);
if (method->IsAotCompiled()) {
method->LoadAotCode();
}
}
}
运行时在类链接阶段或方法调用前,根据方法标识从存储中加载对应的机器码,将其绑定到方法,确保方法执行时直接调用预编译的高效代码。
AOT编译和即时编译(JIT)在ART中共同工作,取长补短。在art/runtime/jit/jit_compiler.cc
和art/compiler/driver/compiler_driver.cc
中体现协同逻辑:
// JIT编译器
class JitCompiler {
public:
// 执行JIT编译
void CompileMethod(ArtMethod* method) {
// 检查是否已经有AOT编译结果
if (method->HasAotCode()) {
// 如果有AOT代码,直接使用
return;
}
// 执行JIT编译流程
// ...
}
};
// 编译器驱动中处理AOT和JIT编译
class CompilerDriver {
public:
// 编译应用
bool CompileApp(const std::vector<std::string>& dex_files,
const std::string& output_filename) {
// 执行AOT编译
if (!PerformAotCompilation(dex_files, output_filename)) {
return false;
}
// 设置JIT编译策略,优先使用AOT代码
JitCompiler::GetInstance().SetAotFirstMode(true);
return true;
}
};
应用启动时,AOT编译的代码优先使用,减少即时编译开销;对于未被AOT编译的方法,JIT在运行时动态编译,保证所有方法都能高效执行。
AOT编译生成的代码需要与垃圾回收(GC)机制协同工作,确保GC能够正确识别和处理编译代码中的对象引用。在art/runtime/gc/heap.cc
和art/compiler/backend/code_generator.cc
中实现协同逻辑:
// 垃圾回收堆类
class Heap {
public:
// 在GC时扫描AOT编译代码中的对象引用
void ScanAotCodeForRoots() {
// 获取所有已加载的AOT编译方法
std::vector<ArtMethod*> aot_methods = class_linker_->GetAotCompiledMethods();
for (ArtMethod* method : aot_methods) {
if (method->HasAotCode()) {
// 获取方法的GC映射信息
GcMap* gc_map = method->GetGcMap();
if (gc_map != nullptr) {
// 根据GC映射扫描代码中的对象引用
ScanCodeForRoots(method->GetCode(), gc_map);
}
}
}
}
private:
// 根据GC映射扫描代码中的对象引用
void ScanCodeForRoots(const void* code, GcMap* gc_map) {
// 获取代码中的寄存器和栈位置的对象引用
for (size_t i = 0; i < gc_map->GetNumberOfLocations(); ++i) {
Location location = gc_map->GetLocation(i);
if (location.IsRegister()) {
// 扫描寄存器中的对象引用
mirror::Object* object = GetObjectFromRegister(location.GetRegister());
if (object != nullptr) {
MarkObject(object);
}
} else if (location.IsStack()) {
// 扫描栈中的对象引用
mirror::Object* object = GetObjectFromStack(location.GetStackOffset());
if (object != nullptr) {
MarkObject(object);
}
}
}
}
};
// 代码生成器中生成GC映射信息
class CodeGenerator {
public:
// 生成GC映射信息
void GenerateGcMap() {
// 创建新的GC映射
GcMap* gc_map = new (allocator_->GetArena()) GcMap();
// 遍历所有基本块
for (HBasicBlock* block : graph_->GetBlocks()) {
// 遍历基本块中的所有指令
for (HInstruction* instruction : block->GetInstructions()) {
if (instruction->CanTriggerGc()) {
// 记录可能触发GC的指令位置和对象引用信息
size_t code_offset = GetInstructionOffset(instruction);
std::vector<Location> locations = GetObjectLocations(instruction);
gc_map->AddEntry(code_offset, locations);
}
}
}
// 将GC映射关联到编译方法
compiled_method_->SetGcMap(gc_map);
}
private:
// 获取指令中对象引用的位置
std::vector<Location> GetObjectLocations(HInstruction* instruction) {
std::vector<Location> locations;
// 根据指令类型获取对象引用位置
switch (instruction->GetKind()) {
case HInstruction::kInvokeVirtual:
case HInstruction::kInvokeStatic:
case HInstruction::kInvokeDirect:
// 方法调用指令,获取参数中的对象引用
for (size_t i = 0; i < instruction->InputCount(); ++i) {
HInstruction* input = instruction->InputAt(i);
if (input->GetType() == Primitive::kPrimNot) {
// 对象类型参数
locations.push_back(Location::RegisterLocation(input->GetAssignedRegister()));
}
}
break;
case HInstruction::kNewInstance:
case HInstruction::kNewArray:
// 创建对象或数组的指令,获取结果中的对象引用
locations.push_back(Location::RegisterLocation(instruction->GetAssignedRegister()));
break;
// 其他指令类型...
}
return locations;
}
};
AOT编译生成的代码在关键位置插入GC映射信息,这些信息记录了对象引用在寄存器和栈中的位置。垃圾回收时,GC利用这些映射信息准确识别和处理编译代码中的对象引用,确保垃圾回收的正确性。
AOT编译结果需要与类加载机制协同,确保加载的类能够正确使用预编译代码。在art/runtime/class_linker.cc
和art/runtime/dex_file_loader.cc
中实现协同逻辑:
// 类链接器类
class ClassLinker {
public:
// 加载类时检查是否有AOT编译结果
mirror::Class* DefineClass(const char* descriptor, Handle<mirror::ClassLoader> class_loader,
const DexFile& dex_file, const DexFile::ClassDef& class_def) {
// 正常类加载流程
mirror::Class* clazz = nullptr;
// ...
// 检查是否有该类的AOT编译结果
if (IsAotCompilationAvailable(descriptor, class_loader)) {
// 加载AOT编译结果
LoadAotCompilationForClass(clazz, descriptor, class_loader);
}
return clazz;
}
private:
// 检查是否有AOT编译结果
bool IsAotCompilationAvailable(const char* descriptor,
Handle<mirror::ClassLoader> class_loader) {
// 获取AOT编译结果存储路径
std::string storage_path = ArtStorageManager::GetInstance().GetAotStoragePath();
std::string file_name = GenerateAotFileName(descriptor, class_loader);
std::string file_path = StringPrintf("%s/%s", storage_path.c_str(), file_name.c_str());
// 检查文件是否存在
return OS::FileExists(file_path.c_str());
}
// 加载类的AOT编译结果
void LoadAotCompilationForClass(mirror::Class* clazz, const char* descriptor,
Handle<mirror::ClassLoader> class_loader) {
// 获取AOT编译结果文件
std::string storage_path = ArtStorageManager::GetInstance().GetAotStoragePath();
std::string file_name = GenerateAotFileName(descriptor, class_loader);
std::string file_path = StringPrintf("%s/%s", storage_path.c_str(), file_name.c_str());
// 加载编译结果
CompiledMethodStorage storage;
if (storage.LoadFromFile(file_path)) {
// 应用编译结果到类的方法
for (size_t i = 0; i < clazz->GetMethodCount(); ++i) {
ArtMethod* method = clazz->GetDirectMethod(i);
if (storage.HasCodeForMethod(method)) {
method->SetJitCode(storage.GetCodeForMethod(method));
method->SetJitCodeSize(storage.GetCodeSizeForMethod(method));
method->SetEntryPointFromQuickCompiledCode(storage.GetCodeForMethod(method));
}
}
}
}
};
// DEX文件加载器类
class DexFileLoader {
public:
// 加载Dex文件时检查是否有AOT编译结果
std::vector<std::unique_ptr<const DexFile>> OpenDexFiles(
const std::string& location,
const std::string& oat_location,
bool verify,
bool verify_checksum,
std::string* error_msg) {
// 正常加载Dex文件
std::vector<std::unique_ptr<const DexFile>> dex_files =
OpenDexFilesFromOat(location, oat_location, verify, verify_checksum, error_msg);
// 如果找不到OAT文件或OAT文件无效,尝试使用AOT编译结果
if (dex_files.empty()) {
dex_files = OpenDexFilesWithoutOat(location, verify, verify_checksum, error_msg);
// 检查是否有AOT编译结果可用
if (!dex_files.empty() && IsAotCompilationAvailable(location)) {
// 加载AOT编译结果
LoadAotCompilationForDexFiles(dex_files, location);
}
}
return dex_files;
}
private:
// 加载Dex文件的AOT编译结果
void LoadAotCompilationForDexFiles(
const std::vector<std::unique_ptr<const DexFile>>& dex_files,
const std::string& location) {
// 获取AOT编译结果文件
std::string storage_path = ArtStorageManager::GetInstance().GetAotStoragePath();
std::string file_name = GenerateAotFileNameForDex(location);
std::string file_path = StringPrintf("%s/%s", storage_path.c_str(), file_name.c_str());
// 加载编译结果
CompiledMethodStorage storage;
if (storage.LoadFromFile(file_path)) {
// 遍历Dex文件中的所有类
for (const auto& dex_file : dex_files) {
for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
mirror::Class* clazz = FindClass(dex_file->GetClassDescriptor(class_def));
if (clazz != nullptr) {
// 应用编译结果到类的方法
for (size_t j = 0; j < clazz->GetMethodCount(); ++j) {
ArtMethod* method = clazz->GetDirectMethod(j);
if (storage.HasCodeForMethod(method)) {
method->SetJitCode(storage.GetCodeForMethod(method));
method->SetJitCodeSize(storage.GetCodeSizeForMethod(method));
method->SetEntryPointFromQuickCompiledCode(storage.GetCodeForMethod(method));
}
}
}
}
}
}
}
};
类加载过程中,系统会检查是否存在对应类的AOT编译结果。如果存在,则加载并应用这些编译结果到类的方法中,确保类在实例化和方法调用时能够直接使用预编译的高效代码。
AOT编译时间是一个重要的性能指标,过长的编译时间会影响应用安装速度。ART通过多种策略优化编译时间,在art/compiler/driver/compiler_driver.cc
和art/compiler/optimizing/optimizing_compiler.cc
中实现:
// 编译器驱动类
class CompilerDriver {
public:
// 编译应用,支持增量编译
bool CompileApp(const std::vector<std::string>& dex_files,
const std::string& output_filename,
bool incremental = false) {
// 如果支持增量编译,检查哪些方法需要重新编译
if (incremental && CheckForIncrementalCompilation(dex_files, output_filename)) {
std::vector<ArtMethod*> methods_to_compile =
GetChangedMethods(dex_files, output_filename);
// 只编译变化的方法
return CompileSelectedMethods(methods_to_compile, output_filename);
}
// 全量编译
return CompileAllMethods(dex_files, output_filename);
}
private:
// 检查是否支持增量编译
bool CheckForIncrementalCompilation(const std::vector<std::string>& dex_files,
const std::string& output_filename) {
// 检查是否有之前的编译结果
if (!OS::FileExists(output_filename.c_str())) {
return false;
}
// 检查Dex文件是否有变化
return AreDexFilesModified(dex_files, output_filename);
}
// 获取需要重新编译的方法
std::vector<ArtMethod*> GetChangedMethods(const std::vector<std::string>& dex_files,
const std::string& output_filename) {
std::vector<ArtMethod*> changed_methods;
// 比较新旧Dex文件,找出有变化的方法
// ...
return changed_methods;
}
};
// 优化编译器类
class OptimizingCompiler {
public:
// 根据方法热度调整优化级别
void SetOptimizationLevelBasedOnHotness(ArtMethod* method) {
// 获取方法调用计数
uint32_t invocation_count = method->GetInvocationCount();
// 根据调用计数设置优化级别
if (invocation_count > kHighlyOptimizedThreshold) {
// 热点方法,使用最高优化级别
method->SetOptimizationLevel(OptimizationLevel::kHigh);
} else if (invocation_count > kMediumOptimizedThreshold) {
// 中等热度方法,使用中等优化级别
method->SetOptimizationLevel(OptimizationLevel::kMedium);
} else {
// 冷方法,使用最低优化级别或不优化
method->SetOptimizationLevel(OptimizationLevel::kLow);
}
}
};
ART通过增量编译策略,只重新编译发生变化的方法,大幅减少编译时间。同时,根据方法的热度动态调整优化级别,热点方法使用高级优化,冷方法使用低级优化或不优化,平衡编译时间和执行效率。
AOT编译结果的内存占用也是一个关键问题,ART通过多种技术优化内存使用,在art/runtime/jit/jit_compilation_cache.cc
和art/compiler/driver/compiler_options.cc
中实现:
// JIT编译缓存类
class JitCompilationCache {
public:
// 设置缓存大小限制
void SetCacheSizeLimit(size_t size_limit) {
size_limit_ = size_limit;
// 如果当前缓存超过限制,执行清理
ShrinkCacheIfNeeded();
}
// 向缓存中添加编译结果
void AddCompiledMethod(ArtMethod* method, const void* code, size_t code_size) {
// 检查是否有足够空间
if (current_size_ + code_size > size_limit_) {
// 移除最不常用的项以腾出空间
EvictLeastRecentlyUsed();
}
// 添加新项到缓存
CacheEntry* entry = new CacheEntry(method, code, code_size);
cache_map_[method] = entry;
current_size_ += code_size;
// 更新LRU链表
UpdateLruList(entry);
}
private:
// 收缩缓存大小
void ShrinkCacheIfNeeded() {
while (current_size_ > size_limit_ && !cache_map_.empty()) {
EvictLeastRecentlyUsed();
}
}
// 移除最不常用的项
void EvictLeastRecentlyUsed() {
if (lru_list_.empty()) {
return;
}
// 获取最不常用的项
CacheEntry* entry = lru_list_.back();
// 从缓存中移除
cache_map_.erase(entry->method);
// 释放内存
FreeCompiledCode(entry->code, entry->code_size);
current_size_ -= entry->code_size;
// 从LRU链表中移除
lru_list_.pop_back();
delete entry;
}
private:
// 缓存映射表
std::unordered_map<ArtMethod*, CacheEntry*> cache_map_;
// LRU链表
std::list<CacheEntry*> lru_list_;
// 当前缓存大小
size_t current_size_;
// 缓存大小限制
size_t size_limit_;
};
// 编译器选项类
class CompilerOptions {
public:
// 设置是否启用代码压缩
void SetEnableCodeCompression(bool enable) {
enable_code_compression_ = enable;
}
// 获取是否启用代码压缩
bool GetEnableCodeCompression() const {
return enable_code_compression_;
}
private:
// 是否启用代码压缩
bool enable_code_compression_;
};
ART使用LRU(最近最少使用)缓存策略管理编译结果,当缓存空间不足时,优先移除最不常用的编译代码。此外,ART还支持代码压缩技术,对不常用的编译代码进行压缩存储,进一步减少内存占用。
AOT编译需要在编译质量(生成代码的执行效率)和编译时间、内存占用之间找到平衡。在art/compiler/optimizing/optimizing_compiler.cc
和art/compiler/driver/compiler_driver.cc
中实现这种平衡:
// 优化编译器类
class OptimizingCompiler {
public:
// 根据编译模式选择优化策略
void SelectOptimizationStrategy(CompilationMode mode) {
switch (mode) {
case CompilationMode::kQuick:
// 快速编译模式,减少优化以提高编译速度
DisableExpensiveOptimizations();
break;
case CompilationMode::kSpace:
// 空间优化模式,优先减少代码大小
EnableSpaceSavingOptimizations();
break;
case CompilationMode::kSpeed:
// 速度优化模式,启用所有优化以提高执行效率
EnableAllOptimizations();
break;
default:
// 默认模式,平衡编译时间和执行效率
EnableDefaultOptimizations();
break;
}
}
private:
// 禁用耗时的优化
void DisableExpensiveOptimizations() {
enable_loop_unrolling_ = false;
enable_inlining_ = false;
enable_aggressive_inlining_ = false;
}
// 启用节省空间的优化
void EnableSpaceSavingOptimizations() {
enable_loop_unrolling_ = false;
enable_large_method_inlining_ = false;
enable_code_layout_optimization_ = true;
}
// 启用所有优化
void EnableAllOptimizations() {
enable_loop_unrolling_ = true;
enable_inlining_ = true;
enable_aggressive_inlining_ = true;
enable_loop_optimization_ = true;
enable_constant_propagation_ = true;
enable_dead_code_elimination_ = true;
}
private:
// 各种优化选项
bool enable_loop_unrolling_;
bool enable_inlining_;
bool enable_aggressive_inlining_;
bool enable_loop_optimization_;
bool enable_constant_propagation_;
bool enable_dead_code_elimination_;
bool enable_code_layout_optimization_;
};
// 编译器驱动类
class CompilerDriver {
public:
// 根据设备特性选择编译模式
CompilationMode SelectCompilationMode() {
// 获取设备信息
const DeviceInfo& device_info = DeviceInfo::GetInstance();
// 根据设备内存大小选择编译模式
if (device_info.GetTotalMemory() < kLowMemoryThreshold) {
// 低内存设备,使用空间优化模式
return CompilationMode::kSpace;
} else if (device_info.GetCpuCores() > kMultiCoreThreshold) {
// 多核设备,使用速度优化模式
return CompilationMode::kSpeed;
} else {
// 默认使用平衡模式
return CompilationMode::kBalanced;
}
}
};
ART提供多种编译模式,包括快速编译、空间优化和速度优化等。系统会根据设备特性(如内存大小、CPU核心数)自动选择合适的编译模式,在编译时间、内存占用和执行效率之间找到最佳平衡点。
ART提供了多种调试工具帮助开发者理解和调试AOT编译过程,在art/tools/
目录下实现了这些工具:
// 编译过程跟踪工具
class CompilerTracer {
public:
// 开始跟踪编译过程
void StartTracing(const std::string& trace_file) {
trace_file_.reset(OS::OpenFileForWriting(trace_file.c_str()));
if (trace_file_.get() != nullptr) {
is_tracing_ = true;
// 写入跟踪文件头部
WriteTraceHeader();
}
}
// 记录编译事件
void TraceEvent(const std::string& event_name, int64_t start_time, int64_t end_time) {
if (!is_tracing_) {
return;
}
// 计算事件持续时间
int64_t duration = end_time - start_time;
// 写入事件信息到跟踪文件
WriteEventToFile(event_name, start_time, duration);
}
// 结束跟踪
void StopTracing() {
if (is_tracing_) {
// 写入跟踪文件尾部
WriteTraceFooter();
trace_file_.reset();
is_tracing_ = false;
}
}
private:
// 写入跟踪文件头部
void WriteTraceHeader() {
std::string header = StringPrintf(
"{\"traceEvents\":[{\
\"name\":\"CompilerTrace\",\
\"ph\":\"M\",\
\"pid\":%d,\
\"tid\":%d,\
\"ts\":%lld,\
\"args\":{}}\n",
getpid(), pthread_self(), GetCurrentTimeMicros());
trace_file_->WriteFully(header.c_str(), header.size());
}
// 写入事件信息到跟踪文件
void WriteEventToFile(const std::string& event_name, int64_t start_time, int64_t duration) {
std::string event = StringPrintf(
",{\
\"name\":\"%s\",\
\"ph\":\"X\",\
\"pid\":%d,\
\"tid\":%d,\
\"ts\":%lld,\
\"dur\":%lld,\
\"args\":{}}\n",
event_name.c_str(), getpid(), pthread_self(), start_time, duration);
trace_file_->WriteFully(event.c_str(), event.size());
}
private:
// 跟踪文件
std::unique_ptr<File> trace_file_;
// 是否正在跟踪
bool is_tracing_;
};
// 编译结果分析工具
class CompilationAnalyzer {
public:
// 分析编译结果
void AnalyzeCompilationResult(const std::string& compiled_file) {
// 加载编译结果文件
CompiledMethodStorage storage;
if (!storage.LoadFromFile(compiled_file)) {
LOG(ERROR) << "Failed to load compiled file: " << compiled_file;
return;
}
// 分析编译方法统计信息
AnalyzeCompiledMethods(storage);
// 分析代码大小分布
AnalyzeCodeSizeDistribution(storage);
// 生成报告
GenerateAnalysisReport();
}
private:
// 分析编译方法统计信息
void AnalyzeCompiledMethods(CompiledMethodStorage& storage) {
// 获取编译方法数量
size_t method_count = storage.GetMethodCount();
LOG(INFO) << "Total compiled methods: " << method_count;
// 分析优化级别分布
std::unordered_map<OptimizationLevel, size_t> optimization_level_counts;
for (size_t i = 0; i < method_count; ++i) {
OptimizationLevel level = storage.GetMethodOptimizationLevel(i);
optimization_level_counts[level]++;
}
// 输出优化级别分布
LOG(INFO) << "Optimization level distribution:";
for (const auto& pair : optimization_level_counts) {
LOG(INFO) << " " << OptimizationLevelToString(pair.first) << ": " << pair.second;
}
}
// 分析代码大小分布
void AnalyzeCodeSizeDistribution(CompiledMethodStorage& storage) {
// 计算总代码大小
size_t total_code_size = 0;
size_t max_code_size = 0;
size_t min_code_size = SIZE_MAX;
std::vector<size_t> code_sizes;
for (size_t i = 0; i < storage.GetMethodCount(); ++i) {
size_t code_size = storage.GetMethodCodeSize(i);
total_code_size += code_size;
max_code_size = std::max(max_code_size, code_size);
min_code_size = std::min(min_code_size, code_size);
code_sizes.push_back(code_size);
}
// 计算平均代码大小
double average_code_size = static_cast<double>(total_code_size) / code_sizes.size();
// 输出代码大小统计信息
LOG(INFO) << "Code size statistics:";
LOG(INFO) << " Total code size: " << total_code_size << " bytes";
LOG(INFO) << " Average code size: " << average_code_size << " bytes";
LOG(INFO) << " Max code size: " << max_code_size << " bytes";
LOG(INFO) << " Min code size: " << min_code_size << " bytes";
}
private:
// 分析结果
std::unordered_map<std::string, size_t> analysis_results_;
};
这些工具允许开发者跟踪编译过程中的事件和性能数据,分析编译结果的统计信息,帮助识别编译瓶颈和优化机会。
ART提供了运行时监控机制,帮助开发者了解AOT编译代码的执行情况,在art/runtime/jit/jit_stats.h
和art/runtime/profiler/profiler.h
中实现:
// JIT统计信息类
class JitStats {
public:
// 记录方法执行
void RecordMethodExecution(ArtMethod* method, int64_t execution_time) {
// 更新方法执行计数
method_execution_counts_[method]++;
// 更新方法总执行时间
method_total_execution_times_[method] += execution_time;
// 更新方法最大执行时间
method_max_execution_times_[method] = std::max(
method_max_execution_times_[method], execution_time);
}
// 获取热点方法
std::vector<ArtMethod*> GetHotMethods(size_t count) {
// 根据执行时间排序方法
std::vector<std::pair<ArtMethod*, int64_t>> methods;
for (const auto& pair : method_total_execution_times_) {
methods.push_back(pair);
}
// 按执行时间降序排序
std::sort(methods.begin(), methods.end(),
[](const std::pair<ArtMethod*, int64_t>& a,
const std::pair<ArtMethod*, int64_t>& b) {
return a.second > b.second;
});
// 返回前count个热点方法
std::vector<ArtMethod*> hot_methods;
for (size_t i = 0; i < std::min(count, methods.size()); ++i) {
hot_methods.push_back(methods[i].first);
}
return hot_methods;
}
private:
// 方法执行计数
std::unordered_map<ArtMethod*, size_t> method_execution_counts_;
// 方法总执行时间
std::unordered_map<ArtMethod*, int64_t> method_total_execution_times_;
// 方法最大执行时间
std::unordered_map<ArtMethod*, int64_t> method_max_execution_times_;
};
// 性能分析器类
class Profiler {
public:
// 开始分析
void StartProfiling() {
is_profiling_ = true;
// 启动采样线程
StartSamplingThread();
}
// 停止分析
void StopProfiling() {
is_profiling_ = false;
// 停止采样线程
StopSamplingThread();
// 生成分析报告
GenerateProfileReport();
}
// 采样当前线程状态
void SampleThreadState(pthread_t thread) {
if (!is_profiling_) {
return;
}
// 获取当前线程的调用栈
std::vector<ArtMethod*> call_stack = GetThreadCallStack(thread);
if (!call_stack.empty()) {
// 更新调用栈采样计数
UpdateCallStackSampleCount(call_stack);
}
}
private:
// 启动采样线程
void StartSamplingThread() {
// 创建并启动采样线程
sampling_thread_ = std::thread([this]() {
while (is_profiling_) {
// 采样所有线程状态
for (pthread_t thread : GetAllRunningThreads()) {
SampleThreadState(thread);
}
// 等待采样间隔
std::this_thread::sleep_for(std::chrono::milliseconds(sampling_interval_));
}
});
}
// 停止采样线程
void StopSamplingThread() {
if (sampling_thread_.joinable()) {
sampling_thread_.join();
}
}
// 更新调用栈采样计数
void UpdateCallStackSampleCount(const std::vector<ArtMethod*>& call_stack) {
// 将调用栈转换为字符串表示
std::string call_stack_str = GetCallStackString(call_stack);
// 更新采样计数
call_stack_samples_[call_stack_str]++;
}
private:
// 是否正在分析
bool is_profiling_;
// 采样线程
std::thread sampling_thread_;
// 采样间隔(毫秒)
int sampling_interval_ = 10;
// 调用栈采样计数
std::unordered_map<std::string, size_t> call_stack_samples_;
};
这些监控工具允许开发者在运行时收集方法执行统计信息和性能分析数据,帮助识别性能瓶颈和优化方向。
ART的AOT编译系统包含完善的异常处理和日志记录机制,在art/compiler/utils/exception_handler.cc
和art/runtime/runtime_logger.cc
中实现:
// 异常处理类
class ExceptionHandler {
public:
// 处理编译异常
void HandleCompilationException(ArtMethod* method, const char* message) {
// 记录错误日志
LOG(ERROR) << "Compilation failed for method: " << method->PrettyMethod()
<< ", error: " << message;
// 收集方法信息用于调试
CollectMethodDebugInfo(method);
// 决定是否继续编译其他方法
if (should_abort_on_error_) {
throw CompilationException(message);
} else {
// 标记方法编译失败
method->SetCompilationFailed(true);
}
}
// 设置是否在错误时中止
void SetAbortOnError(bool abort) {
should_abort_on_error_ = abort;
}
private:
// 收集方法调试信息
void CollectMethodDebugInfo(ArtMethod* method) {
// 获取方法的Dex代码
const DexFile::CodeItem* code_item = method->GetCodeItem();
if (code_item != nullptr) {
// 记录方法的字节码
std::string dex_code = DisassembleDexCode(method->GetDexFile(), code_item);
debug_info_[method] = dex_code;
}
}
private:
// 是否在错误时中止
bool should_abort_on_error_;
// 方法调试信息
std::unordered_map<ArtMethod*, std::string> debug_info_;
};
// 运行时日志记录器类
class RuntimeLogger {
public:
// 记录编译事件
void LogCompilationEvent(const std::string& event, ArtMethod* method = nullptr) {
if (log_level_ >= LogLevel::kInfo) {
std::string message = StringPrintf("[COMPILATION] %s", event.c_str());
if (method != nullptr) {
message += StringPrintf(" - Method: %s", method->PrettyMethod().c_str());
}
WriteLog(message);
}
}
// 记录性能指标
void LogPerformanceMetric(const std::string& metric_name, double value) {
if (log_level_ >= LogLevel::kPerformance) {
std::string message = StringPrintf("[PERFORMANCE] %s: %.2f",
metric_name.c_str(), value);
WriteLog(message);
}
}
// 设置日志级别
void SetLogLevel(LogLevel level) {
log_level_ = level;
}
private:
// 写入日志
void WriteLog(const std::string& message) {
// 根据配置写入日志到文件或系统日志
if (log_to_file_) {
if (log_file_ == nullptr) {
OpenLogFile();
}
if (log_file_ != nullptr) {
std::string log_line = StringPrintf("[%lld] %s\n",
GetCurrentTimeMillis(), message.c_str());
log_file_->WriteFully(log_line.c_str(), log_line.size());
}
} else {
LOG(INFO) << message;
}
}
// 打开日志文件
void OpenLogFile() {
std::string log_file_name = StringPrintf("/data/local/tmp/art_compilation_%lld.log",
GetCurrentTimeMillis());
log_file_.reset(OS::OpenFileForWriting(log_file_name.c_str()));
}
private:
// 日志级别
LogLevel log_level_;
// 是否记录到文件
bool log_to_file_;
// 日志文件
std::unique_ptr<File> log_file_;
};
这些机制确保在编译过程中出现异常时能够捕获并记录详细信息,帮助开发者快速定位和解决问题,同时提供了灵活的日志记录功能,方便监控和分析编译过程。
ART的AOT编译系统包含代码签名和验证机制,确保编译结果的完整性和安全性,在art/runtime/verifier/verifier.cc
和art/runtime/signature/signer.cc
中实现:
// 代码验证器类
class Verifier {
public:
// 验证编译结果的签名
bool VerifyCompiledCodeSignature(const std::string& compiled_file,
const std::string& signature_file) {
// 读取编译文件内容
std::vector<uint8_t> compiled_code;
if (!ReadFileContents(compiled_file, &compiled_code)) {
LOG(ERROR) << "Failed to read compiled file: " << compiled_file;
return false;
}
// 读取签名文件内容
std::vector<uint8_t> signature;
if (!ReadFileContents(signature_file, &signature)) {
LOG(ERROR) << "Failed to read signature file: " << signature_file;
return false;
}
// 获取公钥
const uint8_t* public_key = GetSystemPublicKey();
size_t public_key_size = GetSystemPublicKeySize();
// 验证签名
return VerifySignature(compiled_code.data(), compiled_code.size(),
signature.data(), signature.size(),
public_key, public_key_size);
}
private:
// 从文件读取内容
bool ReadFileContents(const std::string& file_path, std::vector<uint8_t>* contents) {
std::unique_ptr<File> file(OS::OpenFileForReading(file_path.c_str()));
if (file.get() == nullptr) {
return false;
}
off_t file_size = file->GetLength();
contents->resize(static_cast<size_t>(file_size));
return file->ReadFully(contents->data(), static_cast<size_t>(file_size)) == 0;
}
// 验证签名
bool VerifySignature(const uint8_t* data, size_t data_size,
const uint8_t* signature, size_t signature_size,
const uint8_t* public_key, size_t public_key_size) {
// 使用OpenSSL或系统提供的签名验证库
// 这里简化示意
EVP_PKEY* pkey = d2i_PUBKEY(nullptr, &public_key, public_key_size);
if (pkey == nullptr) {
return false;
}
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
if (mdctx == nullptr) {
EVP_PKEY_free(pkey);
return false;
}
if (EVP_DigestVerifyInit(mdctx, nullptr, EVP_sha256(), nullptr, pkey) != 1) {
EVP_MD_CTX_free(mdctx);
EVP_PKEY_free(pkey);
return false;
}
if (EVP_DigestVerifyUpdate(mdctx, data, data_size) != 1) {
EVP_MD_CTX_free(mdctx);
EVP_PKEY_free(pkey);
return false;
}
int result = EVP_DigestVerifyFinal(mdctx, signature, signature_size);
EVP_MD_CTX_free(mdctx);
EVP_PKEY_free(pkey);
return result == 1;
}
};
// 签名生成类
class Signer {
public:
// 对编译结果进行签名
bool SignCompiledCode(const std::string& compiled_file,
const std::string& signature_file) {
// 读取编译文件内容
std::vector<uint8_t> compiled_code;
if (!ReadFileContents(compiled_file, &compiled_code)) {
LOG(ERROR) << "Failed to read compiled file: " << compiled_file;
return false;
}
// 获取私钥
const uint8_t* private_key = GetSystemPrivateKey();
size_t private_key_size = GetSystemPrivateKeySize();
// 生成签名
std::vector<uint8_t> signature;
if (!GenerateSignature(compiled_code.data(), compiled_code.size(),
&signature, private_key, private_key_size)) {
return false;
}
// 保存签名到文件
return WriteFileContents(signature_file, signature.data(), signature.size());
}
private:
// 生成签名
bool GenerateSignature(const uint8_t* data, size_t data_size,
std::vector<uint8_t>* signature,
const uint8_t* private_key, size_t private_key_size) {
// 使用OpenSSL或系统提供的签名库
// 这里简化示意
EVP_PKEY* pkey = d2i_PrivateKey(nullptr, &private_key, private_key_size);
if (pkey == nullptr) {
return false;
}
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
if (mdctx == nullptr) {
EVP_PKEY_free(pkey);
return false;
}
if (EVP_DigestSignInit(mdctx, nullptr, EVP_sha256(), nullptr, pkey) != 1) {
EVP_MD_CTX_free(mdctx);
EVP_PKEY_free(pkey);
return false;
}
if (EVP_DigestSignUpdate(mdctx, data, data_size) != 1) {
EVP_MD_CTX_free(mdctx);
EVP_PKEY_free(pkey);
return false;
}
// 确定签名长度
unsigned int sig_len;
if (EVP_DigestSignFinal(mdctx, nullptr, &sig_len) != 1) {
EVP_MD_CTX_free(mdctx);
EVP_PKEY_free(pkey);
return false;
}
signature->resize(sig_len);
if (EVP_DigestSignFinal(mdctx, signature->data(), &sig_len) != 1) {
EVP_MD_CTX_free(mdctx);
EVP_PKEY_free(pkey);
return false;
}
EVP_MD_CTX_free(mdctx);
EVP_PKEY_free(pkey);
return true;
}
// 写入文件内容
bool WriteFileContents(const std::string& file_path, const uint8_t* contents, size_t size) {
std::unique_ptr<File> file(OS::OpenFileForWriting(file_path.c_str()));
if (file.get() == nullptr) {
return false;
}
return file->WriteFully(contents, size) == 0;
}
};
通过代码签名与验证机制,确保AOT编译结果在存储和传输过程中未被篡改,只有经过签名验证的代码才能被运行时加载执行,防止恶意代码注入。
在AOT编译过程中,可能涉及应用数据的处理,需保障隐私数据安全。在art/runtime/gc/heap_internals.cc
和art/compiler/optimizing/data_flow_analysis.cc
中体现相关机制:
// 堆内存管理内部类
class HeapInternals {
public:
// 标记隐私敏感数据区域
void MarkSensitiveDataRegions() {
// 遍历堆内存中的所有对象
for (mirror::Object* object : GetAllLiveObjects()) {
if (IsObjectSensitive(object)) {
// 标记对象所在内存区域为敏感区域
MarkRegionAsSensitive(GetObjectRegion(object));
}
}
}
// 检查对象是否包含敏感数据
bool IsObjectSensitive(mirror::Object* object) {
// 根据对象类型和类名判断
if (object->IsString()) {
mirror::String* str = static_cast<mirror::String*>(object);
const char* utf_chars = str->GetChars();
// 检查字符串是否包含敏感信息
for (const char* sensitive_word : kSensitiveWords) {
if (strstr(utf_chars, sensitive_word) != nullptr) {
return true;
}
}
}
// 检查其他类型对象
// ...
return false;
}
private:
// 标记内存区域为敏感区域
void MarkRegionAsSensitive(MemoryRegion region) {
// 记录敏感区域信息
sensitive_regions_.push_back(region);
}
// 敏感数据区域列表
std::vector<MemoryRegion> sensitive_regions_;
// 敏感词列表
static const char* kSensitiveWords[];
};
// 数据流分析类
class DataFlowAnalysis {
public:
// 分析方法中是否涉及敏感数据操作
bool AnalyzeSensitiveDataUsage(HGraph* graph) {
// 遍历图中所有基本块和指令
for (HBasicBlock* block : graph->GetBlocks()) {
for (HInstruction* instruction : block->GetInstructions()) {
if (IsInstructionSensitive(instruction)) {
// 记录涉及敏感数据操作的指令
sensitive_instructions_.push_back(instruction);
}
}
}
return !sensitive_instructions_.empty();
}
// 检查指令是否涉及敏感数据操作
bool IsInstructionSensitive(HInstruction* instruction) {
// 根据指令类型和操作数判断
if (instruction->IsLoad()) {
HLoad* load = instruction->AsLoad();
if (IsOperandSensitive(load->GetOperand())) {
return true;
}
} else if (instruction->IsStore()) {
HStore* store = instruction->AsStore();
if (IsOperandSensitive(store->GetOperand())) {
return true;
}
}
// 检查其他类型指令
// ...
return false;
}
// 检查操作数是否涉及敏感数据
bool IsOperandSensitive(HValue* operand) {
if (operand->IsConstant()) {
HConstant* constant = operand->AsConstant();
// 检查常量值是否为敏感数据
if (IsValueSensitive(constant->GetValue())) {
return true;
}
} else if (operand->IsRegister()) {
HRegister* reg = operand->AsRegister();
// 检查寄存器关联的对象是否包含敏感数据
if (IsRegisterObjectSensitive(reg)) {
return true;
}
}
// 检查其他类型操作数
// ...
return false;
}
private:
// 涉及敏感数据操作的指令列表
std::vector<HInstruction*> sensitive_instructions_;
};
通过标记隐私敏感数据区域和分析数据流中敏感数据的使用,在编译过程中对隐私数据进行特殊处理,避免敏感数据泄露风险。例如,在优化过程中避免将敏感数据暴露在公共内存区域,或在垃圾回收时特殊处理敏感数据对象。
AOT编译系统需防范多种安全漏洞,如缓冲区溢出、代码注入等。在art/compiler/backend/arm/instruction_generator.cc
和art/runtime/verifier/type_checker.cc
中实现防护机制:
// ARM指令生成器类
class ArmInstructionGenerator {
public:
// 生成指令时检查边界条件,防止缓冲区溢出
void GenerateSafeLoadInstruction(HLoad* load, LIRBlock* block) {
LIRRegister dst = AllocateRegister(load->GetResultType());
LIRAddress address = GetAddressForLoad(load);
// 检查加载地址是否越界
if (IsAddressOutOfBounds(address)) {
// 插入边界检查指令
GenerateBoundsCheckInstruction(address, load->GetSize());
}
// 生成加载指令
LIR* lir = new (allocator_->GetArena()) LIRLoadInstruction(dst, address);
block->AppendInstruction(lir);
}
// 检查地址是否越界
bool IsAddressOutOfBounds(LIRAddress address) {
// 根据目标架构的内存限制和地址范围判断
if (address.GetBaseRegister() == kStackRegister) {
int offset = address.GetOffset();
// 检查栈地址是否超出栈空间范围
if (offset < 0 || offset >= kMaxStackSize) {
return true;
}
}
// 检查其他类型地址
// ...
return false;
}
};
// 类型检查器类
class TypeChecker {
public:
// 检查方法调用的类型安全性
bool CheckMethodInvocationSafety(HInvoke* invoke) {
// 获取方法原型和参数类型
const DexFile::ProtoId* proto_id = invoke->GetProtoId();
const DexFile::TypeList* parameter_types = GetParameterTypes(proto_id);
// 检查参数类型是否匹配
for (size_t i = 0; i < parameter_types->size_; ++i) {
HValue* arg = invoke->GetArgument(i);
if (!IsTypeCompatible(arg->GetType(), parameter_types->list_[i])) {
return false;
}
}
return true;
}
// 检查类型兼容性
bool IsTypeCompatible(Primitive::Type arg_type, uint16_t dex_type) {
// 根据Dex类型和原始类型映射表判断
switch (dex_type) {
case kDexTypeBoolean:
return arg_type == Primitive::kPrimBoolean;
case kDexTypeByte:
return arg_type == Primitive::kPrimByte;
case kDexTypeChar:
return arg_type == Primitive::kPrimChar;
case kDexTypeShort:
return arg_type == Primitive::kPrimShort;
case kDexTypeInt:
return arg_type == Primitive::kPrimInt;
case kDexTypeLong:
return arg_type == Primitive::kPrimLong;
case kDexTypeFloat:
return arg_type == Primitive::kPrimFloat;
case kDexTypeDouble:
return arg_type == Primitive::kPrimDouble;
case kDexTypeObject:
case kDexTypeArray:
return arg_type == Primitive::kPrimNot;
default:
return false;
}
}
};
指令生成时通过边界检查防止缓冲区溢出漏洞,类型检查机制则确保方法调用的安全性,避免因类型不匹配导致的运行时错误和潜在的安全风险,提升AOT编译系统的安全性。
未来AOT编译系统将引入更多智能化技术,如机器学习辅助优化。在art/compiler/optimizing/ml_based_optimizer.cc
中可展望其实现方向:
// 基于机器学习的优化器类
class MlBasedOptimizer {
public:
// 训练优化模型
void TrainOptimizationModel() {
// 收集历史编译数据
std::vector<CompilationData> historical_data = CollectHistoricalCompilationData();
// 准备训练数据
std::vector<TrainingSample> samples;
for (const CompilationData& data : historical_data) {
TrainingSample sample;
sample.features = ExtractFeatures(data);
sample.label = data.optimization_result;
samples.push_back(sample);
}
// 使用深度学习框架(如TensorFlow Lite)训练模型
std::unique_ptr<Model> model = CreateModel();
model->Train(samples);
// 保存训练好的模型
SaveModel(model.get());
}
// 根据模型进行优化决策
void MakeOptimizationDecisions(HGraph* graph) {
// 提取当前方法的特征
std::vector<float> features = ExtractFeaturesForGraph(graph);
// 加载训练好的模型
std::unique_ptr<Model> model = LoadModel();
// 使用模型预测最佳优化策略
OptimizationStrategy prediction = model->Predict(features);
// 应用预测的优化策略
ApplyOptimizationStrategy(graph, prediction);
}
private:
// 收集历史编译数据
std::vector<CompilationData> CollectHistoricalCompilationData() {
// 从日志和性能记录中获取数据
// ...
}
// 提取特征
std::vector<float> ExtractFeatures(const CompilationData& data) {
// 提取方法复杂度、调用频率等特征
// ...
}
// 创建模型
std::unique_ptr<Model> CreateModel() {
// 使用神经网络结构
// ...
}
// 加载模型
std::unique_ptr<Model> LoadModel() {
// 从文件加载训练好的模型
// ...
}
};
通过收集历史编译数据训练机器学习模型,未来的AOT编译系统能够根据应用特征和运行环境,智能选择最优的编译优化策略,进一步提升编译质量和执行效率。
随着硬件多样化发展,AOT编译系统将增强跨平台和异构计算支持。在art/compiler/backend/multi_arch_backend.cc
中可预见其架构演进:
// 多架构后端代码生成器类
class MultiArchBackend {
public:
// 构造函数,支持多种目标架构
MultiArchBackend(const std::vector<InstructionSet>& target_archs)
: target_archs_(target_archs) {}
// 生成多架构代码
void GenerateMultiArchCode(HGraph* graph, CodeAllocator* allocator) {
for (InstructionSet arch : target_archs_) {
// 根据架构选择对应的后端生成器
Backend* backend = CreateBackendForArch(arch);
if (backend != nullptr) {
// 生成特定架构的代码
backend->GenerateCode(graph, allocator);
delete backend;
}
}
}
private:
// 根据架构创建后端生成器
Backend* CreateBackendForArch(InstructionSet arch) {
switch (arch) {
case InstructionSet::kArm:
return new ArmBackend(compiler_options_, GetArmFeatures());
case InstructionSet::kX86:
return new X86Backend(compiler_options_, GetX86Features());
case InstructionSet::kMips:
return new MipsBackend(compiler_options_, GetMipsFeatures());
// 支持更多架构
default:
return