IQuasarHost::LoadSourceModule() leads to crash for some .q files

Private: Q&ACategory: BugIQuasarHost::LoadSourceModule() leads to crash for some .q files
Frank Vernaillen asked 8 years ago

I am building a C++-based image processing application that uses Quasar as engine. In the current experimental phase I would like to be able to load .Quasar scripts (.q files) in a Quasar host, have it compile the script on the fly, and then be able to call some functions defined in that .q file.
It turns out that loading some Quasar source files leads to memory corruption. Consider the following example:


#include <cstdio>
#include <cstdlib>
#include <cassert>

#include "quasar_dsl.h"

int main(int argc, char *argv[])
{
	quasar::ref host = quasar::IQuasarHost::Create(L"cpu", true);
	
	LPCWSTR module = L"e:\\Program Files\\Quasar\\Library\\linalg.q";  // crash
	//LPCWSTR module = L"e:\\Program Files\\Quasar\\Library\\dwt.q"; // works fine

	LPCWSTR errorMsg;
	bool loaded = host->LoadSourceModule(module, &errorMsg);
	assert(loaded);

	printf("Source module loaded!\n");
	return 0;
}

If I compile it as follows on my Windows 10 machine with VS2015 x64:

cl /DEBUG /EHsc /Zi /I  "%QUASAR_PATH%\include" quasar_load_module.cpp "%QUASAR_PATH%\include\quasar_host.cpp"

and run the resulting quasar_load_module.exe, the Quasar module gets compiled and apparently loaded, but when the C++ program ends, a crash occurs:


Load and start the .NET runtime v4.0.30319
Initializing Quasar runtime system...
Initializing Quasar compiler system...
Initializing computation device...
Initializing interpreter...
Invoking C++ compiler...
INFO: Could not find files for the given pattern(s).
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24210 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

system_4GM.cpp
E:\Program Files\Quasar\include\quasar.h(742): warning C4273: 'erf': inconsistent dll linkage
C:\Program Files (x86)\Windows Kits\10\include\10.0.14393.0\ucrt\math.h(499): note: see previous definition of 'erf'
C:\Users\frv\AppData\Local\Temp\Quasar\Intermediate\system_4GM.cpp(17): warning C4101: '_L_p2': unreferenced local variable
...
...
C:\Users\frv\AppData\Local\Temp\Quasar\Intermediate\linalg_4GM.cpp(1845): warning C4101: '_nThreads': unreferenced local variable
Microsoft (R) Incremental Linker Version 14.00.24210.0
Copyright (C) Microsoft Corporation. All rights reserved.

/dll
/implib:C:\Users\frv\AppData\Local\Temp\Quasar\Intermediate\linalg_4GM.64.lib
/ltcg
/debug
/out:C:\Users\frv\AppData\Local\Temp\Quasar\Intermediate\linalg_4GM.64.dll
C:\Users\frv\AppData\Local\Temp\Quasar\Intermediate\linalg_4GM.obj
Creating library C:\Users\frv\AppData\Local\Temp\Quasar\Intermediate\linalg_4GM.64.lib and object C:\Users\frv\AppData\Local\Temp\Quasar\Intermediate\linalg_4GM.64.exp
Generating code
Finished generating code


Output file successfully created: 82432 bytes.
Source module loaded!
============ Memory subprogram DEBUG diagnostics ============

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at Quasar.Computations.CPU.CpuDynKernelMemoryMgr.GetTotalUsed()
at Quasar.Computations.CPU.CpuEngine.GetStatistics()
at Quasar.RuntimeSystem.TerminationChecks(Boolean showDebugInfo)
at Quasar.NativeInterface.Close()

The crash does not seem to occur for all Quasar scripts. It happens for linalg.q but not for dwt.q for example.
I have Quasar 1.0.6089.28264 – build date 02-Sep-16 3:42:07 PM – 64-bit.