-
Notifications
You must be signed in to change notification settings - Fork 161
minimal_rt: fix intrinsics to correctly return original pointer #2041
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a bug in the memcpy implementation in minimal_rt where the function was returning the modified destination pointer instead of the original destination pointer as required by the C API standard.
- Removes the
mutkeyword from thedestparameter to prevent accidental modification - Changes the inline assembly output to discard the clobbered
rdiregister value instead of capturing it
|
Check memset, ARM impls, and any other similar functions too? |
smalis-msft
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am amazed we got this far before this became an issue.
|
I know right? Seems like pure dumb luck if anything. But I am wondering if in a full optimized build with fat LTO, if the compiler actually knows that |
…osoft#2041) The implementation for intrinsics in minimal_rt was incorrect. For example, the inline asm block for `memcpy` correctly describes that `rdi` will be clobbered by `rep movsb`, but `memcpy`'s API requires that the return value is the _original_ destination pointer, not the incremented pointer. Fix this by removing the mut on `dest` and indicating to the compiler to discard the clobbered value. It seems that in most cases, the compiler ignored the return value of memcpy, except when microsoft#2031 was being added. Certain compilation options or function implementations would cause the compiler to use the inlined returned value of `memcpy` directly from the call to realloc, which would then cause a later an assertion failure due to the pointer not being what was expected and overlapping with the new destination. Fix all the intrinsics in `minimal_rt` to correctly return the unmodified `dest` or `ptr` as they should.
The implementation for intrinsics in minimal_rt was incorrect. For example, the inline asm block for
memcpycorrectly describes thatrdiwill be clobbered byrep movsb, butmemcpy's API requires that the return value is the original destination pointer, not the incremented pointer. Fix this by removing the mut ondestand indicating to the compiler to discard the clobbered value.It seems that in most cases, the compiler ignored the return value of memcpy, except when #2031 was being added. Certain compilation options or function implementations would cause the compiler to use the inlined returned value of
memcpydirectly from the call to realloc, which would then cause a later an assertion failure due to the pointer not being what was expected and overlapping with the new destination.Fix all the intrinsics in
minimal_rtto correctly return the unmodifieddestorptras they should.