-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
gh-127563: fix UBSan failure in dictobject.c
#127568
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
Merged
encukou
merged 4 commits into
python:main
from
picnixz:fix/ubsan/dict/misaligned-loads-127563
Dec 10, 2024
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't understand this. The only change is the explicit cast to
const void *
?Uh oh!
There was an error while loading. Please reload this page.
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.
IIUC this would silence the UBSan misaligned warning, since by definition a
void*
can be read from any location.But IMHO this is dangerous, because the UBSan warning given in the issue states that a
PyDictUnicodeEntry *
is going to be read from 0x5f7d064233d1, with an alignment of 8 (so this seems to stem from a 64 bit build).Since this results from
_DK_ENTRIES
, this can only result fromdk_log2_index_bytes=0
due toIf I read the code correctly, this is only set via
new_keys_object
which has a protectiveso that further down in the code
dk_log2_index_bytes
should never be smaller thanPyDict_LOG_MINSIZE=3
.Hence, I assume this is not a debug build.
Luckily,
new_keys_object
has only four callers, and two of them pass constants (PyDict_LOG_MINSIZE
andNEXT_LOG2_SHARED_KEYS_MAX_SIZE=6
), this leavesdict_new_presized
anddictresize
, where the latter unfortunately has many callers :(Maybe this helps and hopefully I am not completely mistaken ...
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.
Py_EMPTY_KEYS has dk_log2_index_bytes=0. But I don't know why it is passed to dictresize.
Can we get stacktrace for the UBSan error?
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.
Py_EMPTY_KEYS
as the keys objectdictresize
)oldkeys
isPy_EMPTY_KEYS
andoldentries
is a misalignedPyDictUnicodeEntry *
memcpy
is called withnumentries=0
and an invalid/misaligned pointer foroldentries
.count
is zero.DK_UNICODE_ENTRIES()
onPy_EMPTY_KEYS
is UB.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.
insert_to_emptydict()
is optimized for inserting into dict using Py_EMPTY_KEYS.stacktrace can help to find where
insert_to_emptydict()
can be used.Anyway, dk_log2_index_bytes in Py_EMPTY_DICT can be 3 instead of 0.
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.
Sorry to be pesky, I knew this from C++
But for https://en.cppreference.com/w/c/language/pointer I do not see this or anything similar.
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.
See the C11 standard draft, 6.5.6 bullet points 7 and 8. There are probably other references as well.
Uh oh!
There was an error while loading. Please reload this page.
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 think this is documented in Section 6.5.9.7 from the C20 (emphasis mine):
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.
There are also some examples in https://en.cppreference.com/w/c/language/struct involving structs with flexible array members. The examples are probably not authoritative, but I tend to trust the site:
Note that constructing
dp
is okay even if evaluating*dp
is UB.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.
Yeah - flexible arrays to the rescue. Luckily, we have it here in
_dictkeysobject
, which is internal, since in C++ they are not (yet) allowed :)