This is an archive of the discontinued LLVM Phabricator instance.

[clang][ubsan] Fix __builtin_assume_aligned incorrect type descriptor and C++ object polymorphic address
ClosedPublic

Authored by yronglin on Sep 9 2022, 8:54 AM.

Details

Summary

Fix __builtin_assume_aligned incorrect type descriptor

example from @rsmith

struct A { int n; };
struct B { int n; };
struct C : A, B {};

void *f(C *c) {
  // Incorrectly returns `c` rather than the address of the B base class.
  return __builtin_assume_aligned((B*)c, 8);
}

Diff Detail

Event Timeline

yronglin created this revision.Sep 9 2022, 8:54 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 9 2022, 8:54 AM
yronglin requested review of this revision.Sep 9 2022, 8:54 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 9 2022, 8:54 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
yronglin added a comment.EditedSep 9 2022, 9:00 AM

Hi, follow D133202 , should I both fix alignment in this patch or in another separate patch? (this seems have different behavior with gcc https://godbolt.org/z/7dvM8zhnh )

Hi, follow D133202 , should I both fix alignment in this patch or in another separate patch? (this seems have different behavior with gcc https://godbolt.org/z/7dvM8zhnh )

I think that's a separate patch -- this one is fixing a mistake with the type-checking related crash fix and the other is about the behavior of the expression itself when it's valid.

The current patch looks reasonable to me, but I'd love a second opinion.

clang/test/Sema/builtin-redecl.cpp
5–6

We can be tricky instead of including a header file.

Hi, follow D133202 , should I both fix alignment in this patch or in another separate patch? (this seems have different behavior with gcc https://godbolt.org/z/7dvM8zhnh )

I think that's a separate patch -- this one is fixing a mistake with the type-checking related crash fix and the other is about the behavior of the expression itself when it's valid.

The current patch looks reasonable to me, but I'd love a second opinion.

Thanks for your comments @aaron.ballman

clang/test/Sema/builtin-redecl.cpp
5–6

Use decltype not works in C , can we use typedef unsigned long size_t instead?

aaron.ballman added inline comments.Sep 15 2022, 11:16 AM
clang/test/Sema/builtin-redecl.cpp
5–6

Oh good catch, I had missed the -x c on a RUN line and was looking at the file extension alone. :-) We can't use that typedef because there are some platforms where that type is wrong, but we can be tricky in a different way: typedef __typeof__(sizeof(0)) size_t;

yronglin updated this revision to Diff 460870.Sep 16 2022, 1:14 PM

Add test case for C++ polymorphism class
Use typedef __typeof__(sizeof(0)) size_t; instead of #include <stddef.h>

Thanks for your suggestion @aaron.ballman , also I have added a new test case for C++ polymorphism classes

clang/test/Sema/builtin-redecl.cpp
5–6

+1

aaron.ballman accepted this revision.Sep 19 2022, 12:30 PM

LGTM! Please give @rjmccall another day to chime in with any concerns he has.

This revision is now accepted and ready to land.Sep 19 2022, 12:30 PM

Thanks for take a review @aaron.ballman @rjmccall , can you land this patch for me? Please use 'yronglin <yronglin777@gmail.com>' to commit the change.

This revision was landed with ongoing or failed builds.Sep 20 2022, 9:35 AM
This revision was automatically updated to reflect the committed changes.

Thanks for take a review @aaron.ballman @rjmccall , can you land this patch for me? Please use 'yronglin <yronglin777@gmail.com>' to commit the change.

Happy to do so! I've landed on your behalf in https://github.com/llvm/llvm-project/commit/8392f1cc78270c7039970b413dfd836bf4def602

Thanks for take a review @aaron.ballman @rjmccall , can you land this patch for me? Please use 'yronglin <yronglin777@gmail.com>' to commit the change.

Happy to do so! I've landed on your behalf in https://github.com/llvm/llvm-project/commit/8392f1cc78270c7039970b413dfd836bf4def602

Thanks very much!