bytedance / bhook

:fire: ByteHook is an Android PLT hook library which supports armeabi-v7a, arm64-v8a, x86 and x86_64.
https://github.com/bytedance/bhook/tree/main/doc#readme
MIT License
2.05k stars 315 forks source link

一个小提问 #21

Closed Mr-JingShi closed 2 years ago

Mr-JingShi commented 2 years ago

https://github.com/bytedance/bhook/blob/19f99d96c5e561b8e3598498024ef1285ee59c83/bytehook/src/main/cpp/bytesig.c#L319 已经做了compare,为什么 https://github.com/bytedance/bhook/blob/19f99d96c5e561b8e3598498024ef1285ee59c83/bytehook/src/main/cpp/bytesig.c#L322 还要使用__atomic_compare_exchange_n?

caikelun commented 2 years ago

这段逻辑是无锁的,所以要用cas来保证“比较和赋值”是一个原子操作。前面的预先判断只是为了减少一些cas操作,提升性能。