当前位置:网站首页>Strictmode jamming and leakage detection -strictmode principle (2)
Strictmode jamming and leakage detection -strictmode principle (2)
2022-07-01 01:28:00 【Strange uncle Lori Kong】
List of articles
2. StrictMode Turn off the detection principle
StrictMode The closing detection of is actually relatively simple , Basically, it is to bury piles when opening , Check whether the pile is closed when releasing , If it is not closed, it will be detected as leakage .
2.1 CloseGuard
Here is a preliminary knowledge , That's it CloseGuard, It's a StrictMode Classes designed to handle such things , It mainly includes three functions :
- open Start to bury piles
- close Pile removal
- warnIfOpen The explosion
SystemApi(client = MODULE_LIBRARIES)
@libcore.api.IntraCoreApi
public final class CloseGuard {
private static volatile boolean stackAndTrackingEnabled = true;
private static volatile Reporter reporter = new DefaultReporter();
private static volatile Tracker currentTracker = null; // Disabled by default.
@UnsupportedAppUsage
@SystemApi(client = MODULE_LIBRARIES)
public static void setEnabled(boolean enabled) {
CloseGuard.stackAndTrackingEnabled = enabled;
}
public static boolean isEnabled() {
return stackAndTrackingEnabled;
}
@UnsupportedAppUsage(trackingBug=111170242)
@SystemApi(client = MODULE_LIBRARIES)
@libcore.api.IntraCoreApi
public void open(String closer) {
openWithCallSite(closer, null /* callsite */);
}
// Before closing ,
@UnsupportedAppUsage
@SystemApi(client = MODULE_LIBRARIES)
@libcore.api.IntraCoreApi
public void close() {
Tracker tracker = currentTracker;
if (tracker != null && closerNameOrAllocationInfo instanceof Throwable) {
// Invoke tracker on close only if we invoked it on open. Tracker may have changed.
tracker.close((Throwable) closerNameOrAllocationInfo);
}
closerNameOrAllocationInfo = null;
}
public void warnIfOpen() {
if (closerNameOrAllocationInfo != null) {
if (closerNameOrAllocationInfo instanceof Throwable) {
reporter.report(MESSAGE, (Throwable) closerNameOrAllocationInfo);
} else if (stackAndTrackingEnabled) {
reporter.report(MESSAGE + " Callsite: " + closerNameOrAllocationInfo);
} else {
System.logW("A resource failed to call "
+ (String) closerNameOrAllocationInfo + ". ");
}
}
}
}
2.1 IO Turn off detection
Review the previous examples
findViewById<Button>(R.id.io_not_close_btn).setOnClickListener {
var outputStream = FileOutputStream(File(getExternalFilesDir("")?.path + "hello.json"))
outputStream.write("hello world".toByteArray())
outputStream = FileOutputStream(File(getExternalFilesDir("")?.path + "hello.json"))
Runtime.getRuntime().gc()
Runtime.getRuntime().gc()
}
Bury a bomb - First, let's look at the constructor :
private final CloseGuard guard = CloseGuard.get();
public FileOutputStream(File file, boolean append)
throws FileNotFoundException
{
String name = (file != null ? file.getPath() : null);
.......
// Android-added: CloseGuard support. Android Added
guard.open("close");
}
Dismantle the bomb - Close output stream
public void close() throws IOException {
synchronized (closeLock) {
if (closed) {
return;
}
closed = true;
}
// Android-added: CloseGuard support.
guard.close();
}
The explosion - There is no call close Is recycled
protected void finalize() throws IOException {
// Android-added: CloseGuard support.
if (guard != null) {
guard.warnIfOpen();
}
...
}
Similarly, it is inferred that other streams will also have other mechanisms to detect leakage .
2.2 IO Carton test
Look before Caton , We need to prepare some preliminary knowledge first ,IO Preliminary knowledge of reading and writing :
FileInputStream/FileOutputStream -> IOBridge -> ForwardingOs -> BlockGuardOS -> LibCore.OS
We need to focus on BlockGuardOS This floor , Let's look at the source code first :
public class BlockGuardOs extends ForwardingOs {
@Override public FileDescriptor open(String path, int flags, int mode) throws ErrnoException {
BlockGuard.getThreadPolicy().onReadFromDisk();// testing IO read
BlockGuard.getVmPolicy().onPathAccess(path);// testing VM Policy path access
if ((flags & O_ACCMODE) != O_RDONLY) {
BlockGuard.getThreadPolicy().onWriteToDisk(); // testing IO Write
}
....
}
@UnsupportedAppUsage
@Override public void close(FileDescriptor fd) throws ErrnoException {
try {
if (fd.isSocket$()) {
if (isLingerSocket(fd)) {
BlockGuard.getThreadPolicy().onNetwork(); // Whether the main thread accesses the network
}
}
} catch (ErrnoException ignored) {
}
super.close(fd);
}
}
Here we can see clearly ,BlockGuardOs Internal agent BlockGuard, Before each function call , Will check whether there is a stuck problem . And then finally back to StrictMode.
public void onReadFromDisk() {
if ((mThreadPolicyMask & DETECT_THREAD_DISK_READ) == 0) {
return;
}
if (tooManyViolationsThisLoop()) {
return;
}
// This function will print
startHandlingViolationException(new DiskReadViolation());
}
2.3 SqliteCursor Leak detection
location:frameworks/base/core/java/android/database/sqlite/SQLiteCursor.java
protected void finalize() {
try {
// if the cursor hasn't been closed yet, close it first
if (mWindow != null) {
// Report original sql statement
if (StrictMode.vmSqliteObjectLeaksEnabled()) {
String sql = mQuery.getSql();
int len = sql.length();
StrictMode.onSqliteObjectLeaked(
"Finalizing a Cursor that has not been deactivated or closed. "
+ "database = " + mQuery.getDatabase().getLabel()
+ ", table = " + mEditTable
+ ", query = " + sql.substring(0, (len > 1000) ? 1000 : len),
null);
}
}
} finally {
super.finalize();
}
}
It is obvious that , When mWindow Isn't empty , The leaked information will be reported . there window refer to cursor The query window of , Executed move The function assigns a value
边栏推荐
- 分割链表[先取next再斩断链表防止断链]
- 基础知识之一——STA基础概述
- Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
- Unhandled Exception: MissingPluginException(No implementation found for method launch on channel)
- 解读创客教育所蕴含的科技素养
- Service grid ASM year end summary: how do end users use the service grid?
- K210 site helmet
- sort自定义函数
- 基础知识之三——标准单元库
- [leetcode] climb stairs [70]
猜你喜欢

Windows环境下安装MongoDB数据库

探索互联网时代STEAM教育创新之路

"Open math input panel" in MathType editing in win11 is gray and cannot be edited

ESP8266 RC522

Solve idea:class' xxx 'not found in module' xxx‘

Basic knowledge II - Basic definitions related to sta

K210工地安全帽

Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
![Parity linked list [two general directions of linked list operation]](/img/4e/ce860bc172bb75f456427ba26a7842.png)
Parity linked list [two general directions of linked list operation]
![[问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号](/img/51/e48e222c14f4a4e9f2be91a677033f.png)
[问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号
随机推荐
C# 自定义并动态切换光标
编译安装oh-my-zsh
[leetcode] climb stairs [70]
文件服务设计
为什么要搭建个人博客
visual studio 2019 快捷键备忘
【Qt5-基础篇】随机数显示屏展示
【qt5-tab标签精讲】Tab标签及内容分层解析
visual studio 2019 下载
【学习笔记】构造
蒹葭苍苍,白露为霜。
DX-11Q信号继电器
dc_labs--lab1的学习与总结
Visual studio 2019 shortcut notes
系统设置大页
qt5-MVC:数据可视化的层次揭秘
解读创客教育所蕴含的科技素养
双位置继电器ST2-2L/AC220V
Xjy-220/43ac220v static signal relay
[问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号