Created
March 11, 2014 00:27
-
-
Save Amanieu/9477265 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/engine/framework/VirtualMachine.cpp b/src/engine/framework/VirtualMachine.cpp | |
index b2fe6c7..2ec2493 100644 | |
--- a/src/engine/framework/VirtualMachine.cpp | |
+++ b/src/engine/framework/VirtualMachine.cpp | |
@@ -134,7 +134,7 @@ static std::pair<IPC::OSHandleType, IPC::Socket> InternalLoadModule(std::pair<IP | |
PROCESS_INFORMATION processInfo; | |
memset(&startupInfo, 0, sizeof(startupInfo)); | |
startupInfo.cb = sizeof(startupInfo); | |
- if (!CreateProcessW(NULL, &wcmdline[0], NULL, NULL, TRUE, CREATE_SUSPENDED | CREATE_BREAKAWAY_FROM_JOB | DETACHED_PROCESS, NULL, NULL, &startupInfo, &processInfo)) { | |
+ if (!CreateProcessW(NULL, &wcmdline[0], NULL, NULL, TRUE, CREATE_SUSPENDED | CREATE_BREAKAWAY_FROM_JOB | DETACHED_PROCESS, "", NULL, &startupInfo, &processInfo)) { | |
CloseHandle(job); | |
Com_Error(ERR_DROP, "VM: Could create child process: %s", Win32StrError(GetLastError()).c_str()); | |
} | |
@@ -192,7 +192,8 @@ static std::pair<IPC::OSHandleType, IPC::Socket> InternalLoadModule(std::pair<IP | |
close(1); | |
// Load the target executable | |
- execv(args[0], const_cast<char* const*>(args)); | |
+ char* env[] = {nullptr}; | |
+ execve(args[0], const_cast<char* const*>(args), env); | |
// If the exec fails, return errno to the parent through the pipe | |
write(pipefds[1], &errno, sizeof(int)); | |
@@ -222,6 +223,7 @@ std::pair<IPC::OSHandleType, IPC::Socket> CreateNaClVM(std::pair<IPC::Socket, IP | |
std::vector<const char*> args; | |
char rootSocketRedir[32]; | |
std::string module, sel_ldr, irt, bootstrap; | |
+ | |
// Extract the nexe from the pak so that sel_ldr can load it | |
module = name + "-" ARCH_STRING ".nexe"; | |
try { | |
@@ -294,19 +296,21 @@ IPC::Socket CreateInProcessNativeVM(std::pair<IPC::Socket, IPC::Socket> pair, St | |
inProcess.sharedLibHandle = handle; | |
int (*vmMain)(int, const char**) = (int (*)(int, const char**))(Sys_LoadFunction(handle, "main")); | |
- //TODO check dlsym worked | |
+ if (!vmMain) { | |
+ Com_Error(ERR_DROP, "Could not find main function in shared library VM %s", filename.c_str()); | |
+ } | |
std::string vmSocketArg = std::to_string((int)(intptr_t)pair.second.ReleaseHandle()); | |
inProcess.running = true; | |
- inProcess.thread = std::unique_ptr<std::thread>(new std::thread([=, &inProcess](){ | |
+ inProcess.thread = std::thread([vmMain, vmSocketArg, &inProcess]() { | |
const char* args[2] = {nullptr, vmSocketArg.c_str()}; | |
vmMain(2, args); | |
- std::unique_lock<std::mutex> lock(inProcess.mutex); | |
+ std::lock_guard<std::mutex> lock(inProcess.mutex); | |
inProcess.running = false; | |
inProcess.condition.notify_one(); | |
- })); | |
+ }); | |
return std::move(pair.first); | |
} | |
@@ -342,7 +346,7 @@ int VMBase::Create(Str::StringRef name, vmType_t type) | |
} | |
void VMBase::FreeInProcessVM() { | |
- if (inProcess.thread) { | |
+ if (inProcess.thread.joinable()) { | |
bool wait = true; | |
if (inProcess.running) { | |
std::unique_lock<std::mutex> lock(inProcess.mutex); | |
@@ -354,13 +358,11 @@ void VMBase::FreeInProcessVM() { | |
if (wait) { | |
Com_Printf("Waiting for the VM thread..."); | |
- inProcess.thread->join(); | |
+ inProcess.thread.join(); | |
} else { | |
Com_Printf("The VM thread doesn't seem to stop, detaching it (bad things WILL ensue)"); | |
- inProcess.thread->detach(); | |
+ inProcess.thread.detach(); | |
} | |
- | |
- inProcess.thread = nullptr; | |
} | |
if (inProcess.sharedLibHandle) { | |
@@ -386,11 +388,11 @@ void VMBase::Free() | |
kill(processHandle, SIGKILL); | |
waitpid(processHandle, NULL, 0); | |
#endif | |
+ processHandle = IPC::INVALID_HANDLE; | |
} else if (vmType == TYPE_NATIVE_DLL) { | |
FreeInProcessVM(); | |
} | |
- processHandle = IPC::INVALID_HANDLE; | |
} | |
} // namespace VM | |
diff --git a/src/engine/framework/VirtualMachine.h b/src/engine/framework/VirtualMachine.h | |
index 412904e..398cb5b 100644 | |
--- a/src/engine/framework/VirtualMachine.h | |
+++ b/src/engine/framework/VirtualMachine.h | |
@@ -52,7 +52,7 @@ enum vmType_t { | |
class VMBase { | |
public: | |
VMBase() | |
- : processHandle(IPC::INVALID_HANDLE), inProcess{nullptr, {}, {}, nullptr, false} {} | |
+ : processHandle(IPC::INVALID_HANDLE), inProcess{{}, {}, {}, nullptr, false} {} | |
// Create the VM for the named module. Returns the ABI version reported | |
// by the module. | |
@@ -64,7 +64,7 @@ public: | |
// Check if the VM is active | |
bool IsActive() const | |
{ | |
- return processHandle != IPC::INVALID_HANDLE || inProcess.thread; | |
+ return processHandle != IPC::INVALID_HANDLE || inProcess.thread.joinable(); | |
} | |
// Make sure the VM is closed on exit | |
@@ -82,7 +82,7 @@ public: | |
} | |
struct InProcessInfo { | |
- std::unique_ptr<std::thread> thread; | |
+ std::thread thread; | |
std::mutex mutex; | |
std::condition_variable condition; | |
void* sharedLibHandle; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment