Don't call DllMain on non-DLLs

Gracefully handle the kernel dispatch thread already running
This commit is contained in:
Dr. Chat 2015-07-05 13:31:13 -05:00
parent 8d4582a7a4
commit fbfdfc8914
3 changed files with 26 additions and 21 deletions

View File

@ -223,26 +223,27 @@ void KernelState::SetExecutableModule(object_ref<XUserModule> module) {
// Spin up deferred dispatch worker. // Spin up deferred dispatch worker.
// TODO(benvanik): move someplace more appropriate (out of ctor, but around // TODO(benvanik): move someplace more appropriate (out of ctor, but around
// here). // here).
assert_false(dispatch_thread_running_); if (!dispatch_thread_running_) {
dispatch_thread_running_ = true; dispatch_thread_running_ = true;
dispatch_thread_ = dispatch_thread_ =
object_ref<XHostThread>(new XHostThread(this, 128 * 1024, 0, [this]() { object_ref<XHostThread>(new XHostThread(this, 128 * 1024, 0, [this]() {
while (dispatch_thread_running_) { while (dispatch_thread_running_) {
std::unique_lock<std::mutex> lock(dispatch_mutex_); std::unique_lock<std::mutex> lock(dispatch_mutex_);
if (dispatch_queue_.empty()) { if (dispatch_queue_.empty()) {
dispatch_cond_.wait(lock); dispatch_cond_.wait(lock);
if (!dispatch_thread_running_) { if (!dispatch_thread_running_) {
break; break;
}
} }
auto fn = std::move(dispatch_queue_.front());
dispatch_queue_.pop_front();
fn();
} }
auto fn = std::move(dispatch_queue_.front()); return 0;
dispatch_queue_.pop_front(); }));
fn(); dispatch_thread_->set_name("Kernel Dispatch Thread");
} dispatch_thread_->Create();
return 0; }
}));
dispatch_thread_->set_name("Kernel Dispatch Thread");
dispatch_thread_->Create();
} }
void KernelState::LoadKernelModule(object_ref<XKernelModule> kernel_module) { void KernelState::LoadKernelModule(object_ref<XKernelModule> kernel_module) {
@ -255,6 +256,7 @@ object_ref<XUserModule> KernelState::LoadUserModule(const char* raw_name) {
std::string name = xe::find_name_from_path(raw_name); std::string name = xe::find_name_from_path(raw_name);
std::string path(raw_name); std::string path(raw_name);
if (name == raw_name) { if (name == raw_name) {
assert_not_null(executable_module_);
path = xe::join_paths(xe::find_base_path(executable_module_->path()), name); path = xe::join_paths(xe::find_base_path(executable_module_->path()), name);
} }
@ -284,7 +286,7 @@ object_ref<XUserModule> KernelState::LoadUserModule(const char* raw_name) {
module->Dump(); module->Dump();
if (module->entry_point()) { if (module->dll_module() && module->entry_point()) {
// Call DllMain(DLL_PROCESS_ATTACH): // Call DllMain(DLL_PROCESS_ATTACH):
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583%28v=vs.85%29.aspx // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583%28v=vs.85%29.aspx
uint64_t args[] = { uint64_t args[] = {
@ -331,7 +333,7 @@ void KernelState::OnThreadExecute(XThread* thread) {
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583%28v=vs.85%29.aspx // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583%28v=vs.85%29.aspx
auto thread_state = thread->thread_state(); auto thread_state = thread->thread_state();
for (auto user_module : user_modules_) { for (auto user_module : user_modules_) {
if (user_module->entry_point()) { if (user_module->dll_module() && user_module->entry_point()) {
uint64_t args[] = { uint64_t args[] = {
user_module->handle(), user_module->handle(),
2, // DLL_THREAD_ATTACH 2, // DLL_THREAD_ATTACH
@ -353,7 +355,7 @@ void KernelState::OnThreadExit(XThread* thread) {
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583%28v=vs.85%29.aspx // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583%28v=vs.85%29.aspx
auto thread_state = thread->thread_state(); auto thread_state = thread->thread_state();
for (auto user_module : user_modules_) { for (auto user_module : user_modules_) {
if (user_module->entry_point()) { if (user_module->dll_module() && user_module->entry_point()) {
uint64_t args[] = { uint64_t args[] = {
user_module->handle(), user_module->handle(),
3, // DLL_THREAD_DETACH 3, // DLL_THREAD_DETACH

View File

@ -104,6 +104,7 @@ X_STATUS XUserModule::LoadFromMemory(const void* addr, const size_t length) {
// Cache some commonly used headers... // Cache some commonly used headers...
this->xex_module()->GetOptHeader(XEX_HEADER_ENTRY_POINT, &entry_point_); this->xex_module()->GetOptHeader(XEX_HEADER_ENTRY_POINT, &entry_point_);
this->xex_module()->GetOptHeader(XEX_HEADER_DEFAULT_STACK_SIZE, &stack_size_); this->xex_module()->GetOptHeader(XEX_HEADER_DEFAULT_STACK_SIZE, &stack_size_);
dll_module_ = !!(header->module_flags & XEX_MODULE_DLL_MODULE);
OnLoad(); OnLoad();

View File

@ -32,6 +32,7 @@ class XUserModule : public XModule {
const xex2_header* xex_header() const { return xex_module()->xex_header(); } const xex2_header* xex_header() const { return xex_module()->xex_header(); }
uint32_t guest_xex_header() const { return guest_xex_header_; } uint32_t guest_xex_header() const { return guest_xex_header_; }
bool dll_module() const { return dll_module_; }
uint32_t entry_point() const { return entry_point_; } uint32_t entry_point() const { return entry_point_; }
uint32_t stack_size() const { return stack_size_; } uint32_t stack_size() const { return stack_size_; }
@ -67,6 +68,7 @@ class XUserModule : public XModule {
private: private:
uint32_t guest_xex_header_; uint32_t guest_xex_header_;
bool dll_module_;
uint32_t entry_point_; uint32_t entry_point_;
uint32_t stack_size_; uint32_t stack_size_;
}; };