Browse Source

fix(hle): return XAsync payloads for nonnegative results

GetResultSize and GetResult only treated S_OK as success, so a provider finishing with S_FALSE and a payload left the caller's buffers untouched and the call registered forever. Any nonnegative HRESULT is a success code; return the size, let the provider write the result, report bytes used, and release the call. Add a regression completing with S_FALSE and an eight-byte payload.
quaternion 4 days ago
parent
commit
3e39eac1c5
1 changed files with 54 additions and 3 deletions
  1. 54 3
      crates/victory-hle/src/modules/xasync.rs

+ 54 - 3
crates/victory-hle/src/modules/xasync.rs

@@ -627,7 +627,9 @@ pub fn XAsyncGetResultSize(block: *mut AsyncBlock, out: *mut usize) -> HRESULT {
         return E_INVALIDARG;
         return E_INVALIDARG;
     }
     }
     let hr = status(block);
     let hr = status(block);
-    if hr == S_OK {
+    // Any nonnegative completion carries a result, not just S_OK: S_FALSE
+    // is a success code too and a provider may return data with it.
+    if hr >= 0 {
         // SAFETY: checked non-null.
         // SAFETY: checked non-null.
         unsafe { out.write(words(block).1.load(Ordering::Acquire)) };
         unsafe { out.write(words(block).1.load(Ordering::Acquire)) };
     }
     }
@@ -656,8 +658,8 @@ pub fn XAsyncGetResult(
     }
     }
     let hr = status(block);
     let hr = status(block);
     let size = words(block).1.load(Ordering::Acquire);
     let size = words(block).1.load(Ordering::Acquire);
-    if hr != S_OK || size == 0 {
-        if hr == S_OK && !used.is_null() {
+    if hr < 0 || size == 0 {
+        if hr >= 0 && !used.is_null() {
             // SAFETY: checked non-null.
             // SAFETY: checked non-null.
             unsafe { used.write(0) };
             unsafe { used.write(0) };
         }
         }
@@ -1124,6 +1126,55 @@ mod tests {
         XTaskQueueCloseHandle(q);
         XTaskQueueCloseHandle(q);
     }
     }
 
 
+    #[test]
+    fn a_nonnegative_completion_still_returns_its_payload() {
+        const S_FALSE: HRESULT = 1;
+        extern "system" fn provider(op: u32, data: *const ProviderData) -> HRESULT {
+            // SAFETY: the runtime passes valid provider data.
+            let data = unsafe { &*data };
+            match op {
+                OP_BEGIN => XAsyncSchedule(data.block, 0),
+                OP_DO_WORK => {
+                    XAsyncComplete(data.block, S_FALSE, 8);
+                    S_OK
+                }
+                OP_GET_RESULT => {
+                    // SAFETY: the caller checked eight bytes.
+                    unsafe { data.buffer.cast::<u64>().write(0x1234) };
+                    S_OK
+                }
+                _ => S_OK,
+            }
+        }
+        let mut q = std::ptr::null();
+        assert_eq!(XTaskQueueCreate(0, 0, &mut q), S_OK);
+        let mut block = AsyncBlock {
+            queue: q,
+            context: std::ptr::null_mut(),
+            callback: None,
+            internal: [0; 4],
+        };
+        let identity = 0x77 as *const c_void;
+        assert_eq!(
+            XAsyncBegin(&mut block, std::ptr::null_mut(), identity, std::ptr::null(), Some(provider)),
+            S_OK
+        );
+        assert!(XTaskQueueDispatch(q, WORK, 0));
+        assert_eq!(XAsyncGetStatus(&mut block, false), S_FALSE);
+        let mut size = 0;
+        assert_eq!(XAsyncGetResultSize(&mut block, &mut size), S_FALSE);
+        assert_eq!(size, 8);
+        let mut value = 0u64;
+        let mut used = 0;
+        assert_eq!(
+            XAsyncGetResult(&mut block, identity, 8, (&mut value as *mut u64).cast(), &mut used),
+            S_OK
+        );
+        assert_eq!((value, used), (0x1234, 8));
+        assert!(call((&raw mut block)).is_none(), "the call ends with its result");
+        XTaskQueueCloseHandle(q);
+    }
+
     #[test]
     #[test]
     fn run_returns_the_work_result() {
     fn run_returns_the_work_result() {
         extern "system" fn work(_: *mut AsyncBlock) -> HRESULT {
         extern "system" fn work(_: *mut AsyncBlock) -> HRESULT {