我靜態鏈接的 CryptoPP 代碼(在 Linux 上通過 Matlab mex 呼叫時)跳轉到 libmwflcryptocryptopp.so 二進制檔案(然后它凍結)。
我的代碼如何跳轉到外部 .so 而不是靜態鏈接庫?
from 函式CryptoPP::SourceTemplate<CryptoPP::FileStore>::PumpAll2在我vcpkg_installed/.../cryptopp/filters.h:1443它CryptoPP::BufferedTransformation::TransferAllTo2從 Matlabs 自己的libmwflcryptocryptopp.so檔案跳轉到。
gdb 堆疊跟蹤
#0 0x00007ffff02e6cab in CryptoPP::BufferedTransformation::Peek(unsigned char&) const () from /usr/local/MATLAB/R2020b/bin/glnxa64/libmwflcryptocryptopp.so
#1 0x00007ffff02e6c19 in CryptoPP::BufferedTransformation::AnyRetrievable() const () from /usr/local/MATLAB/R2020b/bin/glnxa64/libmwflcryptocryptopp.so
#2 0x00007ffff02e6ffe in CryptoPP::BufferedTransformation::TransferMessagesTo2(CryptoPP::BufferedTransformation&, unsigned int&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) () from /usr/local/MATLAB/R2020b/bin/glnxa64/libmwflcryptocryptopp.so
#3 0x00007ffff02e7129 in CryptoPP::BufferedTransformation::TransferAllTo2(CryptoPP::BufferedTransformation&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) () from /usr/local/MATLAB/R2020b/bin/glnxa64/libmwflcryptocryptopp.so
#4 0x00007fff9758aa4d in CryptoPP::SourceTemplate<CryptoPP::FileStore>::PumpAll2 (this=0x7fffdd87fbc0, blocking=<optimized out>)
at /home/keinkoenig/build-o1/vcpkg_installed/x64-linux/include/cryptopp/filters.h:1443
#5 0x00007fff9758a130 in CryptoPP::Source::PumpAll (this=0x7fffdd87fbc0) at /home/keinkoenig/build-o1/vcpkg_installed/x64-linux/include/cryptopp/filters.h:1420
#6 CryptoPP::Source::SourceInitialize (parameters=warning: RTTI symbol not found for class 'CryptoPP::AlgorithmParameters'
..., pumpAll=true, this=0x7fffdd87fbc0) at /home/keinkoenig/build-o1/vcpkg_installed/x64-linux/include/cryptopp/filters.h:1420
#7 CryptoPP::FileSource::FileSource (attachment=0x7fffd6063160, pumpAll=true, in=..., this=0x7fffdd87fbc0) at /home/keinkoenig/build-o1/vcpkg_installed/x64-linux/include/cryptopp/files.h:102
#8 mexFunction (nlhs=<optimized out>, plhs=<optimized out>, nrhs=<optimized out>, prhs=<optimized out>) at /home/keinkoenig/src/CryptoppMinimal/CryptoppMinimal.cpp:18
最小示例代碼
#include <mex.h>
#include <cryptopp/files.h>
#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <fstream>
unsigned char Key[CryptoPP::AES::DEFAULT_KEYLENGTH] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF };
unsigned char IV[CryptoPP::AES::BLOCKSIZE] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF };
extern "C"
{
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
std::ifstream instream(mxArrayToString(prhs[0]));
std::string decryptedString;
CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption cfbDecryption(Key, sizeof(Key), IV);
CryptoPP::FileSource(instream, true, new CryptoPP::StreamTransformationFilter( cfbDecryption, new CryptoPP::StringSink( decryptedString)));
}
}
和 CMakeLists 鏈接
set(PROJECT_NAME CryptoppMinimal)
...
target_link_libraries(${PROJECT_NAME} PRIVATE cryptopp-static)
使用 gdb 啟動控制臺 matlab
/usr/local/MATLAB/R2020b/bin/matlab -nojvm -Dgdb
并在 Matlab 中運行 test mex 函式
addpath ~/build-o1/bin
CryptoppMinimal('/home/keinkoenig/src/encrypted.dat')
uj5u.com熱心網友回復:
TL; 博士; 您的cryptocpp 正在LD_PRELOAD被Matlab 編輯,即使它是靜態鏈接的。
關于正在發生的事情的一個重要提示是 .so 正在加載的路徑:
#3 0x00007ffff02e7129 [...] /usr/local/MATLAB/R2020b/bin/glnxa64/libmwflcryptocryptopp.so
與編譯期間使用的頭檔案相比:
#5 [...] /home/keinkoenig/build-o1/vcpkg_installed/x64-linux/include/cryptopp/filters.h:1420
這表明,cryptocpp 不僅從動態庫運行,而且運行在.so與您編譯的不同的庫中!
這怎么會發生?如果靜態庫是用位置無關代碼編譯的,對庫中函式的呼叫仍然會以與動態庫相同的方式進行調度:使用跳轉表。
與動態庫非常相似,如果在加載代碼時已經填充了跳轉表,則將重用現有的函式指標。
因此,如果:
- crypto-cpp 是用
-fPIC - Matlab 恰好在
.so加載代碼之前加載了一個 crypto-cpp 。
然后,專案中嵌入的加密 cpp 版本將被忽略,而將使用 Matlab 版本。
快速瀏覽一下 crypto-cpp 專案,我們在 Makefile 中發現:
# Add -fPIC for targets *except* X86, X32, Cygwin or MinGW
ifeq ($(IS_X86)$(IS_CYGWIN)$(IS_MINGW),000)
ifeq ($(findstring -fpic,$(CXXFLAGS))$(findstring -fPIC,$(CXXFLAGS)),)
CRYPTOPP_CXXFLAGS = -fPIC
endif
endif
這似乎證實了這是正在發生的事情。
您可以通過從 Matlab 外部加載和運行您的庫來確認這一點,它應該按預期呼叫靜態庫的代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/348801.html
