Skip to content

[libc][bug] Fix out of bound write in memcpy w/ software prefetching #90613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

gchatelet
Copy link
Contributor

This bug showed up when running fuzzers newly added fuzzers #90591.

This bug showed up when running fuzzers newly added fuzzers llvm#90591.
@llvmbot llvmbot added the libc label Apr 30, 2024
@gchatelet gchatelet requested a review from legrosbuffle April 30, 2024 13:59
@llvmbot
Copy link
Member

llvmbot commented Apr 30, 2024

@llvm/pr-subscribers-libc

Author: Guillaume Chatelet (gchatelet)

Changes

This bug showed up when running fuzzers newly added fuzzers #90591.


Full diff: https://github.com/llvm/llvm-project/pull/90613.diff

1 Files Affected:

  • (modified) libc/src/string/memory_utils/x86_64/inline_memcpy.h (+13-1)
diff --git a/libc/src/string/memory_utils/x86_64/inline_memcpy.h b/libc/src/string/memory_utils/x86_64/inline_memcpy.h
index ae61b1235bd08c..150ad9536fd4dd 100644
--- a/libc/src/string/memory_utils/x86_64/inline_memcpy.h
+++ b/libc/src/string/memory_utils/x86_64/inline_memcpy.h
@@ -107,7 +107,13 @@ inline_memcpy_x86_sse2_ge64_sw_prefetching(Ptr __restrict dst,
       offset += K_THREE_CACHELINES;
     }
   }
-  return builtin::Memcpy<32>::loop_and_tail_offset(dst, src, count, offset);
+  // We don't use 'loop_and_tail_offset' because it assumes at least one
+  // iteration of the loop.
+  while (offset + 32 <= count) {
+    builtin::Memcpy<32>::block_offset(dst, src, offset);
+    offset += 32;
+  }
+  return builtin::Memcpy<32>::tail(dst, src, count);
 }
 
 [[maybe_unused]] LIBC_INLINE void
@@ -140,6 +146,12 @@ inline_memcpy_x86_avx_ge64_sw_prefetching(Ptr __restrict dst,
     offset += K_THREE_CACHELINES;
   }
   return builtin::Memcpy<64>::loop_and_tail_offset(dst, src, count, offset);
+  // We don't use 'loop_and_tail_offset' because it assumes at least one
+  // iteration of the loop.
+  while (offset + 64 <= count) {
+    builtin::Memcpy<64>::block_offset(dst, src, offset);
+    offset += 64;
+  }
 }
 
 [[maybe_unused]] LIBC_INLINE void

@@ -140,6 +146,12 @@ inline_memcpy_x86_avx_ge64_sw_prefetching(Ptr __restrict dst,
offset += K_THREE_CACHELINES;
}
return builtin::Memcpy<64>::loop_and_tail_offset(dst, src, count, offset);
// We don't use 'loop_and_tail_offset' because it assumes at least one
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is dead code. (return above). I'm surprised this was not caught by tests ?

Copy link
Contributor Author

@gchatelet gchatelet May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is obvioulsy wrong here. It's actually not fixing the out-of-bound write with software prefetchers for AVX. This code path is not tested because it depends on the LIBC_COPT_MEMCPY_X86_USE_SOFTWARE_PREFETCHING flag that is not activated for tests. We should be testing it but it depends on host runner hardware capabilities and so requires some more scaffolding.

Created #92089 to track this.

@gchatelet gchatelet changed the title [libc][bug] Fix out of bound write in memcpy wi software prefetching [libc][bug] Fix out of bound write in memcpy w/ software prefetching May 14, 2024
@gchatelet
Copy link
Contributor Author

As discussed offline, I'm closing this issue, the tests and fix will be done in #90591.

@gchatelet gchatelet closed this May 14, 2024
@gchatelet gchatelet deleted the fix_memcpy_sw_prefetching_avx branch May 14, 2024 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants