Fix undefined behavior from function pointer type casts flagged by UBSan#619
Open
bjosv wants to merge 3 commits intoawslabs:mainfrom
Open
Fix undefined behavior from function pointer type casts flagged by UBSan#619bjosv wants to merge 3 commits intoawslabs:mainfrom
bjosv wants to merge 3 commits intoawslabs:mainfrom
Conversation
Replace (aws_hash_callback_eq_fn *) and (aws_hash_callback_destroy_fn *) casts with properly-typed static wrapper functions. Casting function pointers to a different type and calling through that pointer is undefined behavior in C. UBSan reports these at hash_table.c:68 in s_safe_eq_check() where the equals_fn callback is invoked. Signed-off-by: Björn Svensson <bjorn.a.svensson@est.tech>
Add void (*)(void *) wrapper functions for destroy callbacks that have concrete struct pointer signatures. These are passed to aws_ref_count_init() which expects aws_simple_completion_callback (void (*)(void *)). UBSan reports these at ref_count.c:29 when the destroy callback is invoked through the mismatched function pointer type. Signed-off-by: Björn Svensson <bjorn.a.svensson@est.tech>
…stroy Add void (*)(void *) wrapper functions for destroy callbacks registered via aws_ref_count_init() that have concrete struct pointer signatures. Signed-off-by: Björn Svensson <bjorn.a.svensson@est.tech>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Replace function pointer casts with properly-typed static wrapper functions throughout the codebase.
Casting function pointers to a different type and calling through that pointer is undefined behavior in C (C11
§6.5.2.2¶6), even when the underlying ABI happens to work.
Three categories of casts are fixed:
Each cast is replaced with a small static void wrapper function that forwards to the original.
How to reproduce:
Build as described in https://github.com/awslabs/aws-c-s3?tab=readme-ov-file#building but with UBSan enabled:
-DCMAKE_C_FLAGS="-fsanitize=undefined -fno-sanitize-recover=undefined"-DCMAKE_EXE_LINKER_FLAGS="-fsanitize=undefined"When running tests UBSan will report errors like:
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.