Skip to content
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

BUG: Fix -fsanitize=alignment issue in numpy/_core/src/multiarray/arraytypes.c.src #25136

Merged
merged 1 commit into from
Nov 15, 2023

Conversation

hawkinsp
Copy link
Contributor

OBJECT_nonzero may be called with misaligned pointers, manifesting as a -fsanitize=alignment failure. This is UB per C11 6.3.2.3

A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned) for the referenced type, the behavior is undefined.

Nevertheless, Clang only checks alignment when the unaligned pointer is accessed. https://lists.llvm.org/pipermail/llvm-dev/2016-January/094012.html Call memcpy with unaligned arguments instead to work with new Clang (llvm/llvm-project#67766).

Change originally by @MaskRay

…es.c.src

OBJECT_nonzero may be called with misaligned pointers, manifesting as a
-fsanitize=alignment failure. This is UB per C11 6.3.2.3

> A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned) for the referenced type, the behavior is undefined.

Nevertheless, Clang only checks alignment when the unaligned pointer is accessed.
https://lists.llvm.org/pipermail/llvm-dev/2016-January/094012.html
Call memcpy with unaligned arguments instead to work with new Clang
(llvm/llvm-project#67766).
Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

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

LGTM, although I would prefer changing the signature. The ip type is wrong here (as it isn't guaranteed to be aligned), not how it is used.

@@ -2856,7 +2856,7 @@ OBJECT_nonzero (PyObject **ip, PyArrayObject *ap)
}
else {
PyObject *obj;
memcpy(&obj, ip, sizeof(obj));
memcpy(&obj, (void *)ip, sizeof(obj));
Copy link
Member

Choose a reason for hiding this comment

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

Technically, it would be nicer to change top to char ** (or void ** I guess, but char is more common in NumPy, since it works well with strided loops).

@charris charris changed the title Fix -fsanitize=alignment issue in numpy/_core/src/multiarray/arraytypes.c.src BUG: Fix -fsanitize=alignment issue in numpy/_core/src/multiarray/arraytypes.c.src Nov 13, 2023
@charris charris added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported labels Nov 13, 2023
@seberg seberg merged commit 71f7dcf into numpy:main Nov 15, 2023
61 checks passed
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Nov 20, 2023
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.

None yet

3 participants