[botan-devel] Multithreaded Execution Crash - access violation in get_cipher(...)

Raja kalx91 at gmail.com
Sun Jul 3 20:07:37 EDT 2011

Visual Studio 2010
Compiling against pre-built library for Botan-1.8.11


>From my understanding, using Botan in a multithreaded fashion has two
1. The LibraryInitializer needs to be initalized with the
'thread_safe' parameter, eg:
2. Explicit locking must be used if I create and share Botan objects
across multiple threads

In my test program, I initialize the LibraryInitializer as indicated
above at the start of the program's main(). I am then launching two
threads to perform AES/CBC/PKCS7 encryption. Each thread has it's own
encryption helper class instance with it's own set of Botan objects
(SymmetricKey, InitializationVector, Pipe, etc). When each thread
finishes, I launch another thread to repeat. Threads are all
encrypting the same source file but outputting to different target

//Class Members
Botan::SecureVector<Botan::byte> *m_pRawKey;
Botan::SymmetricKey *m_pKey;
Botan::SecureVector<Botan::byte> *m_pRawIV;
Botan::InitializationVector *m_pIV;
Botan::Pipe *m_pProcessPipe;

//Example init function
bool AESEncryptor::initialize(unsigned char * iv, int ivLen, unsigned
char * key, int keyLen)
	// iv points to a valid unsigned char[16] buffer, ivLen is always 16
	m_pRawIV = new Botan::SecureVector<Botan::byte>(iv, ivLen);
	m_pIV = new Botan::InitializationVector(*m_pRawIV);

	// key points to a valid unsigned char[16] buffer, keyLen is always 16
	m_pRawKey = new Botan::SecureVector<Botan::byte>(key, keyLen));
	m_pKey = new Botan::SymmetricKey(*m_pRawKey);

	// create encryptor
	m_pProcessPipe = new Botan::Pipe(Botan::get_cipher("AES/CBC/PKCS7",
*m_pKey, *m_pIV, Botan::ENCRYPTION)); //Crashing line

I am seeing what appears to be a random Access Violation crash in the
call to Botan::get_cipher(...) above. Are there any known issues with
this method and multi-threading? Do I need to lock around it?

After further testing, I am able to repro the crash 90% of the time if
I make the two threads break on the get_cipher(..) line, freeze them,
and then continue execution (by freezing both threads there and
continuing execution, my assumption is I'm increasing the likeliness
both threads enter the get_ciper function at the same time thus
resulting in the crash). The crash only seems to occur at the
beginning on the first set of execution threads. If it makes it past
that, none of the future launched threads seem to have any problems,
even when using the freeze trick I mentioned. Is there some shared
object being allocated inside get_cipher(...)?

I am compiling against a prebuilt Botan lib so I wasn't able to step
in to the Botan source, however when the Access Violation occurs,
Visual Studio shows the thread as being inside
"Default_Engine::get_cipher()". Disassembly shows:
01150C9D  call        Botan::`anonymous namespace'::get_bc_pad (10DD87Eh)
01150CA2  mov         edx,dword ptr [esi]
01150CA4  add         esp,4
01150CA7  push        eax
---> 01150CA8  mov         eax,dword ptr [edx+0Ch]  <--- Debugger
points to this instruction execution. EDX appears to have a bad value
01150CAB  mov         ecx,esi
01150CAD  call        eax

Thanks in advance for any help in this matter. :)


More information about the botan-devel mailing list