feat: 精准测试(OC、Swift)

This commit is contained in:
杭城小刘
2024-05-08 21:57:14 +08:00
parent f5e57d1d25
commit 1a8659e143
59 changed files with 1711 additions and 122 deletions

69
Chapter1 - iOS/1.134.md Normal file
View File

@@ -0,0 +1,69 @@
# 去除无用代码
通用方案
Mach—O
__DATA __objc_selrefs 标记方法被调用信息
otools -v -s _DATA _objc_selrefs Mach-O
linkmap - selfrefs = 无用方法
问题不准OC 语言,动态性)
## clang plugin
重载
RecursiveASTVistor::visitDecl
RecursiveASTVistor::visitStmt
上线前,通过静态方式去查找。不安全、不全面
## 运行时查找
Code Coverage
clang -fprofile-instr-generate -fcoverafe-mapping a.m -o a
swiftc -profile-generate -peofile-coverage-mapping a.swift
缺点:难以定制
## Fuzzing 方案
## Sanitizee Coverage
缺点: 编译慢、且无法进一步定制,包体积负向影响
## 自定义 llvm Pass
针对 LLVM IR 进行处理。
低级别编程语言,类似 RISC 指令集。和高级语言对应LLVM 利用一些列 Pass 对 IR 进行优化。
LLVM 的优化是由 Pass 完成的,每个 Pass 完成特定的优化
自己开发 Pass 是独立的,不会影响 LLVM 的结构
Pass 之间可以有关联,也可分租
LLVM 有c/c++ 接口,还可以用 c/Swift 编写 Pass
c 接口较稳定C++ 接口更新较新
LLVM 内置 Pass
memcpyopt memset 指令替换 memcpy
always_inline: 总是内联用 alwaysinline 修饰的函数
dce死代码消除
loop_deletion:删除未使用的循环
Pass 生成:
静态:在 LLVM 工程中设置 CMake重新构建 opt
动态opt 用 `-load-pass-plugin` 选项加载
怎么写 Pass
对 IR 分析,继承 AnalysisInfoMixin
对 IR 转换,继承 PassInfoMixin