Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
cuda-rt-hook(cuda_mock)是一个用于拦截CUDA/XPU Runtime接口(例如,cudaMalloc
和xpu_malloc
)调用的Python库,通过修改PLT(Procedure Linkage Table)来实现动态拦截,无需重新编译PyTorch、Paddle等复杂框架,安装后即可使用,在调用堆栈追踪、调用耗时统计以及Paddle/PyTorch训练和推理的精度调试和性能优化等场景下非常有用。
本项目的灵感来自于plthook项目,项目的初衷是通过拦截CUDA的Runtime调用转为调用mock函数,可以在没有CUDA和GPU环境的情况下运行和调试triton等项目,因而项目取名cuda_mock。后续增加了多个功能,使得cuda_mock项目可以用于模型的调试和性能分析。
pip install cuda_mock
git clone --recursive https://github.com/lipracer/cuda-rt-hook
cd cuda-rt-hook
python setup.py sdist bdist_wheel
pip install dist/*.whl
# 或者:
# python setup.py install
找到Paddle/PyTorch模型的训练/推理脚本入口,在首次import torch
/import paddle
之后添加如下代码:
import paddle
import cuda_mock; cuda_mock.xpu_initialize() # 加入这一行
或者
import torch
import cuda_mock; cuda_mock.xpu_initialize() # 加入这一行
根据实际的需求和场景设置cuda_mock的功能环境变量(参考功能使用演示章节),接着按照训练/推理脚本原有的执行方式运行脚本即可。
目前,支持以下功能的Runtime接口有:
具体的支持情况请查阅xpu_mock.cpp的XpuRuntimeApiHook
LOG_LEVEL=WARN python run.py
在程序运行结束之后会显示:
xpu_wait
的C++、C和Python调用堆栈HOOK_ENABLE_TRACE="xpu_wait=1" python run.py
在程序运行结束之后会显示:
LOG_LEVEL=WARN python run.py
在程序运行结束之后会显示:
LOG_LEVEL=MEMORY=INFO python run.py
在程序运行过程中会显示:
LOG_SYNC_MODE=1 LOG_LEVEL=PROFILE=INFO python run.py
在程序运行过程中会显示:
HOOK_ENABLE_TRACE=xpu_malloc=0b10 python run.py
HOOK_ENABLE_TRACE=xpu_malloc=0x2 python run.py
在程序运行过程中会显示:
which nvcc
-g
)mv /usr/local/bin/nvcc /usr/local/bin/nvcc_b
chmod 777 tools/nvcc
cp tools/nvcc /usr/local/bin/nvcc
环境变量 | 默认值 | 简短说明 |
---|---|---|
LOG_LEVEL | WARN | 设置全局和各个日志模块的日志级别 |
HOOK_ENABLE_TRACE | 全部接口默认值为0(关闭backtrace) | 是否开启backtrace或参数打印 |
LOG_OUTPUT_PATH | "" | 是否将日志重定向到文件 |
LOG_SYNC_MODE | 0 | 是否使用同步日志输出 |
export LOG_LEVEL=WARN,TRACE=INFO
export HOOK_ENABLE_TRACE='xpu_memcpy=1,xpu_set_device=0,xpu_wait=0x1'
HOOK_ENABLE_TRACE
可接收十进制、二进制和十六进制的数字,不同的位作为不同的开关
Bit | 开关说明 |
---|---|
0 | 是否开启backtrace |
1 | 是否开启参数打印 |
export LOG_OUTPUT_PATH='/tmp/'
export LOG_SYNC_MODE=1
hook函数要与被替换函数类型要保持一致,但是函数名字(特别指mangle后的名字)不能一样,否则会替换失败,或者无限递归调用,暂时未定位!
实现自定义hook installer例子:
class PythonHookInstaller(cuda_mock.HookInstaller):
def is_target_lib(self, name):
return name.find("libcuda_mock_impl.so") != -1
def is_target_symbol(self, name):
return name.find("malloc") != -1
lib = cuda_mock.dynamic_obj(cpp_code, True).appen_compile_opts('-g').compile().get_lib()
installer = PythonHookInstaller(lib)
PythonHookInstaller
__origin_
为开头目标symbol
接口的变量中,方便用户拿到原始函数地址 参考:test/py_test/test_import_mock.py:15
处定义)is_target_lib
是否是要hook的目标函数被调用的libraryis_target_symbol
是否是要hook的目标函数名字(上面接口返回True才回调到这个接口)new_symbol_name
构造函数中传入共享库中的新的用于替换的函数名字,参数name
:当前准备替换的函数名字dynamic_obj
可以运行时编译c++ code,支持引用所有模块:logger
、statistics
# 编译
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=`$pwd/`build -DENABLE_BUILD_WITH_GTEST=ON -GNinja
cmake --build build
# 运行单测
cd build
ctest -R
FAQs
mock cuda runtime api
We found that cuda-mock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.