2009-05-02 05:04:19 -04:00
|
|
|
#include "master.hpp"
|
|
|
|
|
2009-05-04 02:46:13 -04:00
|
|
|
namespace factor
|
|
|
|
{
|
|
|
|
|
2009-05-02 05:04:19 -04:00
|
|
|
HMODULE hFactorDll;
|
|
|
|
|
2009-09-23 14:05:46 -04:00
|
|
|
void factor_vm::init_ffi()
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
|
|
|
hFactorDll = GetModuleHandle(FACTOR_DLL);
|
|
|
|
if(!hFactorDll)
|
2010-01-16 09:43:22 -05:00
|
|
|
fatal_error("GetModuleHandle() failed", 0);
|
2009-05-02 05:04:19 -04:00
|
|
|
}
|
|
|
|
|
2009-09-23 14:05:46 -04:00
|
|
|
void factor_vm::ffi_dlopen(dll *dll)
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
2010-01-16 09:43:22 -05:00
|
|
|
dll->handle = LoadLibraryEx((WCHAR *)alien_offset(dll->path), NULL, 0);
|
2009-05-02 05:04:19 -04:00
|
|
|
}
|
|
|
|
|
2009-09-23 14:05:46 -04:00
|
|
|
void *factor_vm::ffi_dlsym(dll *dll, symbol_char *symbol)
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
2010-01-16 09:43:22 -05:00
|
|
|
return (void *)GetProcAddress(dll ? (HMODULE)dll->handle : hFactorDll, symbol);
|
2009-05-02 05:04:19 -04:00
|
|
|
}
|
|
|
|
|
2009-09-23 14:05:46 -04:00
|
|
|
void factor_vm::ffi_dlclose(dll *dll)
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
2010-01-16 09:43:22 -05:00
|
|
|
FreeLibrary((HMODULE)dll->handle);
|
|
|
|
dll->handle = NULL;
|
2009-05-02 05:04:19 -04:00
|
|
|
}
|
|
|
|
|
2010-01-16 09:43:22 -05:00
|
|
|
BOOL factor_vm::windows_stat(vm_char *path)
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
|
|
|
BY_HANDLE_FILE_INFORMATION bhfi;
|
|
|
|
HANDLE h = CreateFileW(path,
|
|
|
|
GENERIC_READ,
|
|
|
|
FILE_SHARE_READ,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_FLAG_BACKUP_SEMANTICS,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if(h == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
// FindFirstFile is the only call that can stat c:\pagefile.sys
|
|
|
|
WIN32_FIND_DATA st;
|
|
|
|
HANDLE h;
|
|
|
|
|
|
|
|
if(INVALID_HANDLE_VALUE == (h = FindFirstFile(path, &st)))
|
|
|
|
return false;
|
|
|
|
FindClose(h);
|
|
|
|
return true;
|
|
|
|
}
|
2010-01-16 09:43:22 -05:00
|
|
|
BOOL ret = GetFileInformationByHandle(h, &bhfi);
|
2009-05-02 05:04:19 -04:00
|
|
|
CloseHandle(h);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-09-23 14:05:46 -04:00
|
|
|
void factor_vm::windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length)
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
2010-01-16 09:43:22 -05:00
|
|
|
SNWPRINTF(temp_path, length-1, L"%s.image", full_path);
|
2009-09-04 14:52:05 -04:00
|
|
|
temp_path[length - 1] = 0;
|
2009-05-02 05:04:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* You must free() this yourself. */
|
2009-09-23 14:05:46 -04:00
|
|
|
const vm_char *factor_vm::default_image_path()
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
2009-05-04 05:50:24 -04:00
|
|
|
vm_char full_path[MAX_UNICODE_PATH];
|
|
|
|
vm_char *ptr;
|
|
|
|
vm_char temp_path[MAX_UNICODE_PATH];
|
2009-05-02 05:04:19 -04:00
|
|
|
|
|
|
|
if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
|
|
|
|
fatal_error("GetModuleFileName() failed", 0);
|
|
|
|
|
|
|
|
if((ptr = wcsrchr(full_path, '.')))
|
|
|
|
*ptr = 0;
|
|
|
|
|
2010-01-16 09:43:22 -05:00
|
|
|
SNWPRINTF(temp_path, MAX_UNICODE_PATH-1, L"%s.image", full_path);
|
2009-09-04 14:52:05 -04:00
|
|
|
temp_path[MAX_UNICODE_PATH - 1] = 0;
|
2009-05-02 05:04:19 -04:00
|
|
|
|
|
|
|
return safe_strdup(temp_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* You must free() this yourself. */
|
2009-09-23 14:05:46 -04:00
|
|
|
const vm_char *factor_vm::vm_executable_path()
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
2009-05-04 05:50:24 -04:00
|
|
|
vm_char full_path[MAX_UNICODE_PATH];
|
2009-05-02 05:04:19 -04:00
|
|
|
if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
|
|
|
|
fatal_error("GetModuleFileName() failed", 0);
|
|
|
|
return safe_strdup(full_path);
|
|
|
|
}
|
|
|
|
|
2009-09-27 14:42:18 -04:00
|
|
|
void factor_vm::primitive_existsp()
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
2009-12-18 16:59:56 -05:00
|
|
|
vm_char *path = untag_check<byte_array>(ctx->pop())->data<vm_char>();
|
|
|
|
ctx->push(tag_boolean(windows_stat(path)));
|
2009-05-02 05:04:19 -04:00
|
|
|
}
|
|
|
|
|
2009-10-16 05:37:27 -04:00
|
|
|
segment::segment(cell size_, bool executable_p)
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
2009-09-25 22:17:20 -04:00
|
|
|
size = size_;
|
|
|
|
|
2009-05-02 05:04:19 -04:00
|
|
|
char *mem;
|
|
|
|
DWORD ignore;
|
|
|
|
|
|
|
|
if((mem = (char *)VirtualAlloc(NULL, getpagesize() * 2 + size,
|
2009-10-16 05:37:27 -04:00
|
|
|
MEM_COMMIT, executable_p ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE)) == 0)
|
2009-10-07 09:33:54 -04:00
|
|
|
out_of_memory();
|
2009-05-02 05:04:19 -04:00
|
|
|
|
|
|
|
if (!VirtualProtect(mem, getpagesize(), PAGE_NOACCESS, &ignore))
|
2009-05-04 05:50:24 -04:00
|
|
|
fatal_error("Cannot allocate low guard page", (cell)mem);
|
2009-05-02 05:04:19 -04:00
|
|
|
|
|
|
|
if (!VirtualProtect(mem + size + getpagesize(),
|
|
|
|
getpagesize(), PAGE_NOACCESS, &ignore))
|
2009-05-04 05:50:24 -04:00
|
|
|
fatal_error("Cannot allocate high guard page", (cell)mem);
|
2009-05-02 05:04:19 -04:00
|
|
|
|
2009-09-25 22:17:20 -04:00
|
|
|
start = (cell)mem + getpagesize();
|
|
|
|
end = start + size;
|
2009-05-02 05:04:19 -04:00
|
|
|
}
|
|
|
|
|
2009-09-25 22:17:20 -04:00
|
|
|
segment::~segment()
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
|
|
|
SYSTEM_INFO si;
|
|
|
|
GetSystemInfo(&si);
|
2009-09-25 22:17:20 -04:00
|
|
|
if(!VirtualFree((void*)(start - si.dwPageSize), 0, MEM_RELEASE))
|
2009-09-26 03:56:55 -04:00
|
|
|
fatal_error("Segment deallocation failed",0);
|
2009-05-02 05:04:19 -04:00
|
|
|
}
|
|
|
|
|
2009-09-26 03:41:01 -04:00
|
|
|
long getpagesize()
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
|
|
|
static long g_pagesize = 0;
|
2010-02-06 01:45:06 -05:00
|
|
|
if(!g_pagesize)
|
2009-05-02 05:04:19 -04:00
|
|
|
{
|
|
|
|
SYSTEM_INFO system_info;
|
|
|
|
GetSystemInfo (&system_info);
|
|
|
|
g_pagesize = system_info.dwPageSize;
|
|
|
|
}
|
|
|
|
return g_pagesize;
|
|
|
|
}
|
|
|
|
|
2010-02-03 16:12:13 -05:00
|
|
|
void factor_vm::move_file(const vm_char *path1, const vm_char *path2)
|
|
|
|
{
|
2010-02-06 01:45:06 -05:00
|
|
|
if(MoveFileEx((path1),(path2),MOVEFILE_REPLACE_EXISTING) == false)
|
2010-03-27 07:33:28 -04:00
|
|
|
general_error(ERROR_IO,tag_fixnum(GetLastError()),false_object);
|
2009-05-04 02:46:13 -04:00
|
|
|
}
|
2010-02-03 17:03:22 -05:00
|
|
|
|
|
|
|
}
|