LookupObject'ing code.

This commit is contained in:
Ben Vanik 2015-05-24 21:37:28 -07:00
parent 018e9a96e1
commit a2b66f9109
8 changed files with 164 additions and 221 deletions

View File

@ -328,18 +328,15 @@ void KernelState::CompleteOverlappedEx(uint32_t overlapped_ptr, X_RESULT result,
XOverlappedSetLength(ptr, length); XOverlappedSetLength(ptr, length);
X_HANDLE event_handle = XOverlappedGetEvent(ptr); X_HANDLE event_handle = XOverlappedGetEvent(ptr);
if (event_handle) { if (event_handle) {
XEvent* ev = nullptr; auto ev = object_table()->LookupObject<XEvent>(event_handle);
if (XSUCCEEDED(object_table()->GetObject( if (ev) {
event_handle, reinterpret_cast<XObject**>(&ev)))) {
ev->Set(0, false); ev->Set(0, false);
ev->Release();
} }
} }
if (XOverlappedGetCompletionRoutine(ptr)) { if (XOverlappedGetCompletionRoutine(ptr)) {
X_HANDLE thread_handle = XOverlappedGetContext(ptr); X_HANDLE thread_handle = XOverlappedGetContext(ptr);
XThread* thread = nullptr; auto thread = object_table()->LookupObject<XThread>(thread_handle);
if (XSUCCEEDED(object_table()->GetObject( if (thread) {
thread_handle, reinterpret_cast<XObject**>(&thread)))) {
uint32_t routine = XOverlappedGetCompletionRoutine(ptr); uint32_t routine = XOverlappedGetCompletionRoutine(ptr);
uint64_t args[] = { uint64_t args[] = {
result, length, overlapped_ptr, result, length, overlapped_ptr,
@ -350,8 +347,6 @@ void KernelState::CompleteOverlappedEx(uint32_t overlapped_ptr, X_RESULT result,
// THIS IS WRONG, for testing only: // THIS IS WRONG, for testing only:
processor()->Execute(XThread::GetCurrentThread()->thread_state(), routine, processor()->Execute(XThread::GetCurrentThread()->thread_state(), routine,
args, xe::countof(args)); args, xe::countof(args));
thread->Release();
} }
} }
} }

View File

@ -169,8 +169,8 @@ SHIM_CALL XamEnumerate_shim(PPCContext* ppc_state, KernelState* state) {
XELOGD("XamEnumerate(%.8X, %d, %.8X, %d, %.8X, %.8X)", handle, zero, XELOGD("XamEnumerate(%.8X, %d, %.8X, %d, %.8X, %.8X)", handle, zero,
buffer_ptr, buffer_length, item_count_ptr, overlapped_ptr); buffer_ptr, buffer_length, item_count_ptr, overlapped_ptr);
XEnumerator* e = nullptr; auto e = state->object_table()->LookupObject<XEnumerator>(handle);
if (XFAILED(state->object_table()->GetObject(handle, (XObject**)&e))) { if (!e) {
if (overlapped_ptr) { if (overlapped_ptr) {
state->CompleteOverlappedImmediateEx(overlapped_ptr, 0, state->CompleteOverlappedImmediateEx(overlapped_ptr, 0,
X_ERROR_INVALID_HANDLE, 0); X_ERROR_INVALID_HANDLE, 0);
@ -198,8 +198,6 @@ SHIM_CALL XamEnumerate_shim(PPCContext* ppc_state, KernelState* state) {
result = X_ERROR_INVALID_PARAMETER; result = X_ERROR_INVALID_PARAMETER;
} }
e->Release();
SHIM_SET_RETURN_64(result); SHIM_SET_RETURN_64(result);
} }

View File

@ -104,11 +104,9 @@ SHIM_CALL XMsgCancelIORequest_shim(PPCContext* ppc_state, KernelState* state) {
X_HANDLE event_handle = XOverlappedGetEvent(SHIM_MEM_ADDR(overlapped_ptr)); X_HANDLE event_handle = XOverlappedGetEvent(SHIM_MEM_ADDR(overlapped_ptr));
if (event_handle && wait) { if (event_handle && wait) {
XEvent* ev = nullptr; auto ev = state->object_table()->LookupObject<XEvent>(event_handle);
if (XSUCCEEDED(state->object_table()->GetObject( if (ev) {
event_handle, reinterpret_cast<XObject**>(&ev)))) {
ev->Wait(0, 0, true, nullptr); ev->Wait(0, 0, true, nullptr);
ev->Release();
} }
} }

View File

@ -51,8 +51,8 @@ SHIM_CALL XNotifyGetNext_shim(PPCContext* ppc_state, KernelState* state) {
} }
// Grab listener. // Grab listener.
XNotifyListener* listener = NULL; auto listener = state->object_table()->LookupObject<XNotifyListener>(handle);
if (XFAILED(state->object_table()->GetObject(handle, (XObject**)&listener))) { if (!listener) {
SHIM_SET_RETURN_64(0); SHIM_SET_RETURN_64(0);
return; return;
} }
@ -69,10 +69,6 @@ SHIM_CALL XNotifyGetNext_shim(PPCContext* ppc_state, KernelState* state) {
dequeued = listener->DequeueNotification(&id, &param); dequeued = listener->DequeueNotification(&id, &param);
} }
if (listener) {
listener->Release();
}
if (dequeued) { if (dequeued) {
SHIM_SET_MEM_32(id_ptr, id); SHIM_SET_MEM_32(id_ptr, id);
SHIM_SET_MEM_32(param_ptr, param); SHIM_SET_MEM_32(param_ptr, param);

View File

@ -88,12 +88,12 @@ X_STATUS NtCreateFile(PPCContext* ppc_state, KernelState* state,
FileSystem* fs = state->file_system(); FileSystem* fs = state->file_system();
std::unique_ptr<Entry> entry; std::unique_ptr<Entry> entry;
XFile* root_file = NULL; object_ref<XFile> root_file;
if (object_attrs->root_directory != 0xFFFFFFFD && // ObDosDevices if (object_attrs->root_directory != 0xFFFFFFFD && // ObDosDevices
object_attrs->root_directory != 0) { object_attrs->root_directory != 0) {
result = state->object_table()->GetObject(object_attrs->root_directory, root_file = state->object_table()->LookupObject<XFile>(
(XObject**)&root_file); object_attrs->root_directory);
assert_true(XSUCCEEDED(result)); assert_not_null(root_file);
assert_true(root_file->type() == XObject::Type::kTypeFile); assert_true(root_file->type() == XObject::Type::kTypeFile);
// Resolve the file using the device the root directory is part of. // Resolve the file using the device the root directory is part of.
@ -242,16 +242,18 @@ SHIM_CALL NtReadFile_shim(PPCContext* ppc_state, KernelState* state) {
uint32_t info = 0; uint32_t info = 0;
// Grab event to signal. // Grab event to signal.
XEvent* ev = NULL;
bool signal_event = false; bool signal_event = false;
if (event_handle) { auto ev = event_handle
result = state->object_table()->GetObject(event_handle, (XObject**)&ev); ? state->object_table()->LookupObject<XEvent>(event_handle)
: object_ref<XEvent>();
if (event_handle && !ev) {
result = X_STATUS_INVALID_HANDLE;
} }
// Grab file. // Grab file.
XFile* file = NULL; auto file = state->object_table()->LookupObject<XFile>(file_handle);
if (XSUCCEEDED(result)) { if (!file) {
result = state->object_table()->GetObject(file_handle, (XObject**)&file); result = X_STATUS_INVALID_HANDLE;
} }
// Execute read. // Execute read.
@ -300,11 +302,8 @@ SHIM_CALL NtReadFile_shim(PPCContext* ppc_state, KernelState* state) {
SHIM_SET_MEM_32(io_status_block_ptr + 4, info); // Information SHIM_SET_MEM_32(io_status_block_ptr + 4, info); // Information
} }
if (ev) { if (ev && signal_event) {
if (signal_event) { ev->Set(0, false);
ev->Set(0, false);
}
ev->Release();
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -333,16 +332,18 @@ SHIM_CALL NtWriteFile_shim(PPCContext* ppc_state, KernelState* state) {
uint32_t info = 0; uint32_t info = 0;
// Grab event to signal. // Grab event to signal.
XEvent* ev = NULL;
bool signal_event = false; bool signal_event = false;
if (event_handle) { auto ev = event_handle
result = state->object_table()->GetObject(event_handle, (XObject**)&ev); ? state->object_table()->LookupObject<XEvent>(event_handle)
: object_ref<XEvent>();
if (event_handle && !ev) {
result = X_STATUS_INVALID_HANDLE;
} }
// Grab file. // Grab file.
XFile* file = NULL; auto file = state->object_table()->LookupObject<XFile>(file_handle);
if (XSUCCEEDED(result)) { if (!ev) {
result = state->object_table()->GetObject(file_handle, (XObject**)&file); result = X_STATUS_INVALID_HANDLE;
} }
// Execute write. // Execute write.
@ -383,14 +384,8 @@ SHIM_CALL NtWriteFile_shim(PPCContext* ppc_state, KernelState* state) {
SHIM_SET_MEM_32(io_status_block_ptr + 4, info); // Information SHIM_SET_MEM_32(io_status_block_ptr + 4, info); // Information
} }
if (file) { if (ev && signal_event) {
file->Release(); ev->Set(0, false);
}
if (ev) {
if (signal_event) {
ev->Set(0, false);
}
ev->Release();
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -410,11 +405,8 @@ SHIM_CALL NtSetInformationFile_shim(PPCContext* ppc_state, KernelState* state) {
uint32_t info = 0; uint32_t info = 0;
// Grab file. // Grab file.
XFile* file = NULL; auto file = state->object_table()->LookupObject<XFile>(file_handle);
result = state->object_table()->GetObject(file_handle, (XObject**)&file); if (file) {
if (XSUCCEEDED(result)) {
result = X_STATUS_SUCCESS;
switch (file_info_class) { switch (file_info_class) {
case XFileDispositionInformation: { case XFileDispositionInformation: {
// Used to set deletion flag. Which we don't support. Probably? // Used to set deletion flag. Which we don't support. Probably?
@ -444,20 +436,15 @@ SHIM_CALL NtSetInformationFile_shim(PPCContext* ppc_state, KernelState* state) {
info = 0; info = 0;
break; break;
} }
} else {
result = X_STATUS_INVALID_HANDLE;
} }
if (XFAILED(result)) {
info = 0;
}
if (io_status_block_ptr) { if (io_status_block_ptr) {
SHIM_SET_MEM_32(io_status_block_ptr, result); // Status SHIM_SET_MEM_32(io_status_block_ptr, result); // Status
SHIM_SET_MEM_32(io_status_block_ptr + 4, info); // Information SHIM_SET_MEM_32(io_status_block_ptr + 4, info); // Information
} }
if (file) {
file->Release();
}
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
} }
@ -476,11 +463,8 @@ SHIM_CALL NtQueryInformationFile_shim(PPCContext* ppc_state,
uint32_t info = 0; uint32_t info = 0;
// Grab file. // Grab file.
XFile* file = NULL; auto file = state->object_table()->LookupObject<XFile>(file_handle);
result = state->object_table()->GetObject(file_handle, (XObject**)&file); if (file) {
if (XSUCCEEDED(result)) {
result = X_STATUS_SUCCESS;
switch (file_info_class) { switch (file_info_class) {
case XFileInternalInformation: case XFileInternalInformation:
// Internal unique file pointer. Not sure why anyone would want this. // Internal unique file pointer. Not sure why anyone would want this.
@ -541,20 +525,15 @@ SHIM_CALL NtQueryInformationFile_shim(PPCContext* ppc_state,
info = 0; info = 0;
break; break;
} }
} else {
result = X_STATUS_INVALID_HANDLE;
} }
if (XFAILED(result)) {
info = 0;
}
if (io_status_block_ptr) { if (io_status_block_ptr) {
SHIM_SET_MEM_32(io_status_block_ptr, result); // Status SHIM_SET_MEM_32(io_status_block_ptr, result); // Status
SHIM_SET_MEM_32(io_status_block_ptr + 4, info); // Information SHIM_SET_MEM_32(io_status_block_ptr + 4, info); // Information
} }
if (file) {
file->Release();
}
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
} }
@ -572,11 +551,11 @@ SHIM_CALL NtQueryFullAttributesFile_shim(PPCContext* ppc_state,
X_STATUS result = X_STATUS_NO_SUCH_FILE; X_STATUS result = X_STATUS_NO_SUCH_FILE;
XFile* root_file = NULL; object_ref<XFile> root_file;
if (attrs.root_directory != 0xFFFFFFFD) { // ObDosDevices if (attrs.root_directory != 0xFFFFFFFD) { // ObDosDevices
result = state->object_table()->GetObject(attrs.root_directory, root_file =
(XObject**)&root_file); state->object_table()->LookupObject<XFile>(attrs.root_directory);
assert_true(XSUCCEEDED(result)); assert_not_null(root_file);
assert_true(root_file->type() == XObject::Type::kTypeFile); assert_true(root_file->type() == XObject::Type::kTypeFile);
assert_always(); assert_always();
} }
@ -612,11 +591,8 @@ SHIM_CALL NtQueryVolumeInformationFile_shim(PPCContext* ppc_state,
uint32_t info = 0; uint32_t info = 0;
// Grab file. // Grab file.
XFile* file = NULL; auto file = state->object_table()->LookupObject<XFile>(file_handle);
result = state->object_table()->GetObject(file_handle, (XObject**)&file); if (file) {
if (XSUCCEEDED(result)) {
result = X_STATUS_SUCCESS;
switch (fs_info_class) { switch (fs_info_class) {
case 1: { // FileFsVolumeInformation case 1: { // FileFsVolumeInformation
auto volume_info = (X_FILE_FS_VOLUME_INFORMATION*)calloc(length, 1); auto volume_info = (X_FILE_FS_VOLUME_INFORMATION*)calloc(length, 1);
@ -648,17 +624,19 @@ SHIM_CALL NtQueryVolumeInformationFile_shim(PPCContext* ppc_state,
free(fs_attribute_info); free(fs_attribute_info);
break; break;
} }
case 2: // FileFsLabelInformation case 2: // FileFsLabelInformation
case 4: // FileFsDeviceInformation case 4: // FileFsDeviceInformation
case 6: // FileFsControlInformation case 6: // FileFsControlInformation
case 7: // FileFsFullSizeInformation case 7: // FileFsFullSizeInformation
case 8: // FileFsObjectIdInformation case 8: // FileFsObjectIdInformation
default: default:
// Unsupported, for now. // Unsupported, for now.
assert_always(); assert_always();
info = 0; info = 0;
break; break;
} }
} else {
result = X_STATUS_NO_SUCH_FILE;
} }
if (XFAILED(result)) { if (XFAILED(result)) {
@ -669,10 +647,6 @@ SHIM_CALL NtQueryVolumeInformationFile_shim(PPCContext* ppc_state,
SHIM_SET_MEM_32(io_status_block_ptr + 4, info); // Information SHIM_SET_MEM_32(io_status_block_ptr + 4, info); // Information
} }
if (file) {
file->Release();
}
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
} }
@ -710,9 +684,8 @@ SHIM_CALL NtQueryDirectoryFile_shim(PPCContext* ppc_state, KernelState* state) {
X_STATUS result = X_STATUS_UNSUCCESSFUL; X_STATUS result = X_STATUS_UNSUCCESSFUL;
uint32_t info = 0; uint32_t info = 0;
XFile* file = NULL; auto file = state->object_table()->LookupObject<XFile>(file_handle);
result = state->object_table()->GetObject(file_handle, (XObject**)&file); if (file) {
if (XSUCCEEDED(result)) {
X_FILE_DIRECTORY_INFORMATION* dir_info = (X_FILE_DIRECTORY_INFORMATION*)calloc(length, 1); X_FILE_DIRECTORY_INFORMATION* dir_info = (X_FILE_DIRECTORY_INFORMATION*)calloc(length, 1);
result = result =
file->QueryDirectory(dir_info, length, file_name, restart_scan != 0); file->QueryDirectory(dir_info, length, file_name, restart_scan != 0);
@ -721,6 +694,8 @@ SHIM_CALL NtQueryDirectoryFile_shim(PPCContext* ppc_state, KernelState* state) {
info = length; info = length;
} }
free(dir_info); free(dir_info);
} else {
result = X_STATUS_NO_SUCH_FILE;
} }
if (XFAILED(result)) { if (XFAILED(result)) {
@ -731,10 +706,6 @@ SHIM_CALL NtQueryDirectoryFile_shim(PPCContext* ppc_state, KernelState* state) {
SHIM_SET_MEM_32(io_status_block_ptr + 4, info); // Information SHIM_SET_MEM_32(io_status_block_ptr + 4, info); // Information
} }
if (file) {
file->Release();
}
free(file_name); free(file_name);
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
} }

View File

@ -183,10 +183,9 @@ SHIM_CALL XexGetModuleSection_shim(PPCContext* ppc_state, KernelState* state) {
XELOGD("XexGetModuleSection(%.8X, %s, %.8X, %.8X)", handle, name, data_ptr, XELOGD("XexGetModuleSection(%.8X, %s, %.8X, %.8X)", handle, name, data_ptr,
size_ptr); size_ptr);
X_STATUS result = X_STATUS_INVALID_HANDLE; X_STATUS result = X_STATUS_SUCCESS;
XModule* module = nullptr; auto module = state->object_table()->LookupObject<XModule>(handle);
state->object_table()->GetObject(handle, (XObject**)&module);
if (module) { if (module) {
uint32_t section_data = 0; uint32_t section_data = 0;
uint32_t section_size = 0; uint32_t section_size = 0;
@ -195,7 +194,8 @@ SHIM_CALL XexGetModuleSection_shim(PPCContext* ppc_state, KernelState* state) {
SHIM_SET_MEM_32(data_ptr, section_data); SHIM_SET_MEM_32(data_ptr, section_data);
SHIM_SET_MEM_32(size_ptr, section_size); SHIM_SET_MEM_32(size_ptr, section_size);
} }
module->Release(); } else {
result = X_STATUS_INVALID_HANDLE;
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -266,15 +266,13 @@ SHIM_CALL XexGetProcedureAddress_shim(PPCContext* ppc_state,
X_STATUS result = X_STATUS_INVALID_HANDLE; X_STATUS result = X_STATUS_INVALID_HANDLE;
XModule* module = NULL; object_ref<XModule> module;
if (!module_handle) { if (!module_handle) {
module = state->GetExecutableModule().get(); module = state->GetExecutableModule();
} else { } else {
result = module = state->object_table()->LookupObject<XModule>(module_handle);
state->object_table()->GetObject(module_handle, (XObject**)&module);
} }
if (module) {
if (XSUCCEEDED(result)) {
uint32_t ptr; uint32_t ptr;
if (is_string_name) { if (is_string_name) {
ptr = module->GetProcAddressByName(string_name); ptr = module->GetProcAddressByName(string_name);
@ -291,10 +289,6 @@ SHIM_CALL XexGetProcedureAddress_shim(PPCContext* ppc_state,
} }
} }
if (module) {
module->Release();
}
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
} }

View File

@ -56,11 +56,10 @@ SHIM_CALL ObReferenceObjectByHandle_shim(PPCContext* ppc_state,
XELOGD("ObReferenceObjectByHandle(%.8X, %.8X, %.8X)", handle, object_type_ptr, XELOGD("ObReferenceObjectByHandle(%.8X, %.8X, %.8X)", handle, object_type_ptr,
out_object_ptr); out_object_ptr);
X_STATUS result = X_STATUS_INVALID_HANDLE; X_STATUS result = X_STATUS_SUCCESS;
XObject* object = NULL; auto object = state->object_table()->LookupObject<XObject>(handle);
result = state->object_table()->GetObject(handle, &object); if (object) {
if (XSUCCEEDED(result)) {
// TODO(benvanik): verify type with object_type_ptr // TODO(benvanik): verify type with object_type_ptr
// TODO(benvanik): get native value, if supported. // TODO(benvanik): get native value, if supported.
@ -74,7 +73,7 @@ SHIM_CALL ObReferenceObjectByHandle_shim(PPCContext* ppc_state,
XEvent* ev = (XEvent*)object; XEvent* ev = (XEvent*)object;
} break;*/ } break;*/
case XObject::kTypeThread: { case XObject::kTypeThread: {
XThread* thread = (XThread*)object; auto thread = object.get<XThread>();
native_ptr = thread->thread_state_ptr(); native_ptr = thread->thread_state_ptr();
} break; } break;
default: { default: {
@ -89,7 +88,7 @@ SHIM_CALL ObReferenceObjectByHandle_shim(PPCContext* ppc_state,
native_ptr = 0xDEADF00D; native_ptr = 0xDEADF00D;
} break; } break;
case 0xD01BBEEF: { // ExThreadObjectType case 0xD01BBEEF: { // ExThreadObjectType
XThread* thread = (XThread*)object; auto thread = object.get<XThread>();
native_ptr = thread->thread_state_ptr(); native_ptr = thread->thread_state_ptr();
} break; } break;
default: { default: {
@ -98,9 +97,14 @@ SHIM_CALL ObReferenceObjectByHandle_shim(PPCContext* ppc_state,
} break; } break;
} }
// Caller takes the reference.
// It's released in ObDereferenceObject.
object->Retain();
if (out_object_ptr) { if (out_object_ptr) {
SHIM_SET_MEM_32(out_object_ptr, native_ptr); SHIM_SET_MEM_32(out_object_ptr, native_ptr);
} }
} else {
result = X_STATUS_INVALID_HANDLE;
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -142,22 +146,21 @@ SHIM_CALL NtDuplicateObject_shim(PPCContext* ppc_state, KernelState* state) {
// So we just fake it and properly reference count but not actually make // So we just fake it and properly reference count but not actually make
// different handles. // different handles.
X_STATUS result = X_STATUS_INVALID_HANDLE; X_STATUS result = X_STATUS_SUCCESS;
XObject* obj = 0; auto object = state->object_table()->LookupObject<XObject>(handle);
result = state->object_table()->GetObject(handle, &obj); if (object) {
if (XSUCCEEDED(result)) { object->RetainHandle();
obj->RetainHandle(); uint32_t new_handle = object->handle();
uint32_t new_handle = obj->handle();
if (new_handle_ptr) { if (new_handle_ptr) {
SHIM_SET_MEM_32(new_handle_ptr, new_handle); SHIM_SET_MEM_32(new_handle_ptr, new_handle);
} }
if (options == 1 /* DUPLICATE_CLOSE_SOURCE */) { if (options == 1 /* DUPLICATE_CLOSE_SOURCE */) {
// Always close the source object. // Always close the source object.
state->object_table()->RemoveHandle(handle); state->object_table()->RemoveHandle(handle);
} }
obj->Release(); } else {
result = X_STATUS_INVALID_HANDLE;
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);

View File

@ -153,18 +153,15 @@ SHIM_CALL NtResumeThread_shim(PPCContext* ppc_state, KernelState* state) {
XELOGD("NtResumeThread(%.8X, %.8X)", handle, suspend_count_ptr); XELOGD("NtResumeThread(%.8X, %.8X)", handle, suspend_count_ptr);
XThread* thread = NULL; X_RESULT result = X_STATUS_INVALID_HANDLE;
X_STATUS result = uint32_t suspend_count = 0;
state->object_table()->GetObject(handle, (XObject**)&thread);
uint32_t suspend_count; auto thread = state->object_table()->LookupObject<XThread>(handle);
if (XSUCCEEDED(result)) { if (thread) {
result = thread->Resume(&suspend_count); result = thread->Resume(&suspend_count);
thread->Release();
} }
if (XSUCCEEDED(result)) { if (suspend_count_ptr) {
if (suspend_count_ptr) { SHIM_SET_MEM_32(suspend_count_ptr, suspend_count);
SHIM_SET_MEM_32(suspend_count_ptr, suspend_count);
}
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -175,11 +172,13 @@ SHIM_CALL KeResumeThread_shim(PPCContext* ppc_state, KernelState* state) {
XELOGD("KeResumeThread(%.8X)", thread_ptr); XELOGD("KeResumeThread(%.8X)", thread_ptr);
X_STATUS result; X_STATUS result = X_STATUS_SUCCESS;
auto thread = auto thread =
XObject::GetNativeObject<XThread>(state, SHIM_MEM_ADDR(thread_ptr)); XObject::GetNativeObject<XThread>(state, SHIM_MEM_ADDR(thread_ptr));
if (thread) { if (thread) {
result = thread->Resume(); result = thread->Resume();
} else {
result = X_STATUS_INVALID_HANDLE;
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -191,19 +190,18 @@ SHIM_CALL NtSuspendThread_shim(PPCContext* ppc_state, KernelState* state) {
XELOGD("NtSuspendThread(%.8X, %.8X)", handle, suspend_count_ptr); XELOGD("NtSuspendThread(%.8X, %.8X)", handle, suspend_count_ptr);
XThread* thread = NULL; X_RESULT result = X_STATUS_SUCCESS;
X_STATUS result = uint32_t suspend_count = 0;
state->object_table()->GetObject(handle, (XObject**)&thread);
uint32_t suspend_count; auto thread = state->object_table()->LookupObject<XThread>(handle);
if (XSUCCEEDED(result)) { if (thread) {
result = thread->Suspend(&suspend_count); result = thread->Suspend(&suspend_count);
thread->Release(); } else {
result = X_STATUS_INVALID_HANDLE;
} }
if (XSUCCEEDED(result)) { if (suspend_count_ptr) {
if (suspend_count_ptr) { SHIM_SET_MEM_32(suspend_count_ptr, suspend_count);
SHIM_SET_MEM_32(suspend_count_ptr, suspend_count);
}
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -250,22 +248,21 @@ SHIM_CALL KeSetBasePriorityThread_shim(PPCContext* ppc_state,
int32_t prev_priority = 0; int32_t prev_priority = 0;
XThread* thread = NULL; object_ref<XThread> thread;
if (thread_ptr < 0x1000) { if (thread_ptr < 0x1000) {
// They passed in a handle (for some reason) // They passed in a handle (for some reason)
X_STATUS result = thread = state->object_table()->LookupObject<XThread>(thread_ptr);
state->object_table()->GetObject(thread_ptr, (XObject**)&thread);
// Log it in case this is the source of any problems in the future // Log it in case this is the source of any problems in the future
XELOGD("KeSetBasePriorityThread - Interpreting thread ptr as handle!"); XELOGD("KeSetBasePriorityThread - Interpreting thread ptr as handle!");
} else { } else {
thread = XObject::GetNativeObject<XThread>(state, SHIM_MEM_ADDR(thread_ptr)).release(); thread =
XObject::GetNativeObject<XThread>(state, SHIM_MEM_ADDR(thread_ptr));
} }
if (thread) { if (thread) {
prev_priority = thread->QueryPriority(); prev_priority = thread->QueryPriority();
thread->SetPriority(increment); thread->SetPriority(increment);
thread->Release();
} }
SHIM_SET_RETURN_32(prev_priority); SHIM_SET_RETURN_32(prev_priority);
@ -507,15 +504,14 @@ SHIM_CALL NtSetEvent_shim(PPCContext* ppc_state, KernelState* state) {
X_STATUS result = X_STATUS_SUCCESS; X_STATUS result = X_STATUS_SUCCESS;
XEvent* ev = NULL; auto ev = state->object_table()->LookupObject<XEvent>(event_handle);
result = state->object_table()->GetObject(event_handle, (XObject**)&ev); if (ev) {
if (XSUCCEEDED(result)) {
int32_t was_signalled = ev->Set(0, false); int32_t was_signalled = ev->Set(0, false);
if (previous_state_ptr) { if (previous_state_ptr) {
SHIM_SET_MEM_32(previous_state_ptr, was_signalled); SHIM_SET_MEM_32(previous_state_ptr, was_signalled);
} }
} else {
ev->Release(); result = X_STATUS_INVALID_HANDLE;
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -547,15 +543,14 @@ SHIM_CALL NtPulseEvent_shim(PPCContext* ppc_state, KernelState* state) {
X_STATUS result = X_STATUS_SUCCESS; X_STATUS result = X_STATUS_SUCCESS;
XEvent* ev = NULL; auto ev = state->object_table()->LookupObject<XEvent>(event_handle);
result = state->object_table()->GetObject(event_handle, (XObject**)&ev); if (ev) {
if (XSUCCEEDED(result)) {
int32_t was_signalled = ev->Pulse(0, false); int32_t was_signalled = ev->Pulse(0, false);
if (previous_state_ptr) { if (previous_state_ptr) {
SHIM_SET_MEM_32(previous_state_ptr, was_signalled); SHIM_SET_MEM_32(previous_state_ptr, was_signalled);
} }
} else {
ev->Release(); result = X_STATUS_INVALID_HANDLE;
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -584,11 +579,11 @@ SHIM_CALL NtClearEvent_shim(PPCContext* ppc_state, KernelState* state) {
X_STATUS result = X_STATUS_SUCCESS; X_STATUS result = X_STATUS_SUCCESS;
XEvent* ev = NULL; auto ev = state->object_table()->LookupObject<XEvent>(event_handle);
result = state->object_table()->GetObject(event_handle, (XObject**)&ev); if (ev) {
if (XSUCCEEDED(result)) {
ev->Reset(); ev->Reset();
ev->Release(); } else {
result = X_STATUS_INVALID_HANDLE;
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -674,16 +669,16 @@ SHIM_CALL NtReleaseSemaphore_shim(PPCContext* ppc_state, KernelState* state) {
previous_count_ptr); previous_count_ptr);
X_STATUS result = X_STATUS_SUCCESS; X_STATUS result = X_STATUS_SUCCESS;
int32_t previous_count = 0;
XSemaphore* sem = NULL; auto sem = state->object_table()->LookupObject<XSemaphore>(sem_handle);
result = state->object_table()->GetObject(sem_handle, (XObject**)&sem); if (sem) {
if (XSUCCEEDED(result)) { previous_count = sem->ReleaseSemaphore(release_count);
int32_t previous_count = sem->ReleaseSemaphore(release_count); } else {
sem->Release(); result = X_STATUS_INVALID_HANDLE;
}
if (previous_count_ptr) { if (previous_count_ptr) {
SHIM_SET_MEM_32(previous_count_ptr, previous_count); SHIM_SET_MEM_32(previous_count_ptr, previous_count);
}
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -736,11 +731,11 @@ SHIM_CALL NtReleaseMutant_shim(PPCContext* ppc_state, KernelState* state) {
X_STATUS result = X_STATUS_SUCCESS; X_STATUS result = X_STATUS_SUCCESS;
XMutant* mutant = NULL; auto mutant = state->object_table()->LookupObject<XMutant>(mutant_handle);
result = state->object_table()->GetObject(mutant_handle, (XObject**)&mutant); if (mutant) {
if (XSUCCEEDED(result)) {
result = mutant->ReleaseMutant(priority_increment, abandon, wait); result = mutant->ReleaseMutant(priority_increment, abandon, wait);
mutant->Release(); } else {
result = X_STATUS_INVALID_HANDLE;
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -798,12 +793,12 @@ SHIM_CALL NtSetTimerEx_shim(PPCContext* ppc_state, KernelState* state) {
X_STATUS result = X_STATUS_SUCCESS; X_STATUS result = X_STATUS_SUCCESS;
XTimer* timer = NULL; auto timer = state->object_table()->LookupObject<XTimer>(timer_handle);
result = state->object_table()->GetObject(timer_handle, (XObject**)&timer); if (timer) {
if (XSUCCEEDED(result)) {
result = timer->SetTimer(due_time, period_ms, routine, routine_arg, result = timer->SetTimer(due_time, period_ms, routine, routine_arg,
resume ? true : false); resume ? true : false);
timer->Release(); } else {
result = X_STATUS_INVALID_HANDLE;
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -817,15 +812,14 @@ SHIM_CALL NtCancelTimer_shim(PPCContext* ppc_state, KernelState* state) {
X_STATUS result = X_STATUS_SUCCESS; X_STATUS result = X_STATUS_SUCCESS;
XTimer* timer = NULL; auto timer = state->object_table()->LookupObject<XTimer>(timer_handle);
result = state->object_table()->GetObject(timer_handle, (XObject**)&timer); if (timer) {
if (XSUCCEEDED(result)) {
result = timer->Cancel(); result = timer->Cancel();
timer->Release(); } else {
result = X_STATUS_INVALID_HANDLE;
if (current_state_ptr) { }
SHIM_SET_MEM_32(current_state_ptr, 0); if (current_state_ptr) {
} SHIM_SET_MEM_32(current_state_ptr, 0);
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -869,13 +863,13 @@ SHIM_CALL NtWaitForSingleObjectEx_shim(PPCContext* ppc_state,
X_STATUS result = X_STATUS_SUCCESS; X_STATUS result = X_STATUS_SUCCESS;
XObject* object = NULL; auto object = state->object_table()->LookupObject<XObject>(object_handle);
result = state->object_table()->GetObject(object_handle, &object); if (object) {
if (XSUCCEEDED(result)) {
uint64_t timeout = timeout_ptr ? SHIM_MEM_64(timeout_ptr) : 0; uint64_t timeout = timeout_ptr ? SHIM_MEM_64(timeout_ptr) : 0;
result = result =
object->Wait(3, wait_mode, alertable, timeout_ptr ? &timeout : NULL); object->Wait(3, wait_mode, alertable, timeout_ptr ? &timeout : nullptr);
object->Release(); } else {
result = X_STATUS_INVALID_HANDLE;
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
@ -936,21 +930,21 @@ SHIM_CALL NtWaitForMultipleObjectsEx_shim(PPCContext* ppc_state,
X_STATUS result = X_STATUS_SUCCESS; X_STATUS result = X_STATUS_SUCCESS;
XObject** objects = (XObject**)alloca(sizeof(XObject*) * count); std::vector<object_ref<XObject>> objects(count);
for (uint32_t n = 0; n < count; n++) { for (uint32_t n = 0; n < count; n++) {
uint32_t object_handle = SHIM_MEM_32(handles_ptr + n * 4); uint32_t object_handle = SHIM_MEM_32(handles_ptr + n * 4);
XObject* object = NULL; auto object = state->object_table()->LookupObject<XObject>(object_handle);
result = state->object_table()->GetObject(object_handle, &object); if (!object) {
if (XFAILED(result)) {
SHIM_SET_RETURN_32(X_STATUS_INVALID_PARAMETER); SHIM_SET_RETURN_32(X_STATUS_INVALID_PARAMETER);
return; return;
} }
objects[n] = object; objects[n] = std::move(object);
} }
uint64_t timeout = timeout_ptr ? SHIM_MEM_64(timeout_ptr) : 0; uint64_t timeout = timeout_ptr ? SHIM_MEM_64(timeout_ptr) : 0;
result = XObject::WaitMultiple(count, objects, wait_type, 6, wait_mode, result = XObject::WaitMultiple(
alertable, timeout_ptr ? &timeout : NULL); count, reinterpret_cast<XObject**>(objects.data()), wait_type, 6,
wait_mode, alertable, timeout_ptr ? &timeout : nullptr);
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);
} }
@ -968,22 +962,16 @@ SHIM_CALL NtSignalAndWaitForSingleObjectEx_shim(PPCContext* ppc_state,
X_STATUS result = X_STATUS_SUCCESS; X_STATUS result = X_STATUS_SUCCESS;
XObject* signal_object = NULL; auto signal_object =
XObject* wait_object = NULL; state->object_table()->LookupObject<XObject>(signal_handle);
result = state->object_table()->GetObject(signal_handle, &signal_object); auto wait_object = state->object_table()->LookupObject<XObject>(wait_handle);
if (XSUCCEEDED(result)) { if (signal_object && wait_object) {
result = state->object_table()->GetObject(wait_handle, &wait_object);
}
if (XSUCCEEDED(result)) {
uint64_t timeout = timeout_ptr ? SHIM_MEM_64(timeout_ptr) : 0; uint64_t timeout = timeout_ptr ? SHIM_MEM_64(timeout_ptr) : 0;
result = XObject::SignalAndWait(signal_object, wait_object, 3, 1, alertable, result =
timeout_ptr ? &timeout : NULL); XObject::SignalAndWait(signal_object.get(), wait_object.get(), 3, 1,
} alertable, timeout_ptr ? &timeout : nullptr);
if (signal_object) { } else {
signal_object->Release(); result = X_STATUS_INVALID_HANDLE;
}
if (wait_object) {
wait_object->Release();
} }
SHIM_SET_RETURN_32(result); SHIM_SET_RETURN_32(result);