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

[TySan] false positive with unions and placement-new #120987

Open
Jannik2099 opened this issue Dec 23, 2024 · 0 comments
Open

[TySan] false positive with unions and placement-new #120987

Jannik2099 opened this issue Dec 23, 2024 · 0 comments
Labels
compiler-rt:tysan Type sanitizer false-positive Warning fires when it should not

Comments

@Jannik2099
Copy link

Jannik2099 commented Dec 23, 2024

According to https://eel.is/c++draft/class.union#general-6 , the active union member in an union of non-POD types can be switched by invoking the dtor of the old member, and placement new-ing into the new member.

However, tysan does not seem to like this https://godbolt.org/z/nvnjzjGez

#include <cstdio>
#include <new>

class Foo {
   private:
    short i{};

   public:
    Foo() { printf("Foo nontrivial ctor\n"); }
    ~Foo() { printf("Foo nontrivial dtor\n"); }
};

class Bar {
   private:
    int i{};

   public:
    Bar() { printf("Bar nontrivial ctor\n"); }
    ~Bar() { printf("Bar nontrivial dtor\n"); }
};

union U {
    Foo foo;
    Bar bar;
    ~U() {}
};

int main() { 
    U u{ .foo = {}};
    u.foo.~Foo();
    new (&u.bar) Bar;
}
==1==ERROR: TypeSanitizer: type-aliasing-violation on address 0x7ffff9117e2c (pc 0x61eed1b9b89d bp 0x7ffff9117d10 sp 0x7ffff9117cb8 tid 1)
WRITE of size 4 at 0x7ffff9117e2c with type int (in Bar at offset 0) accesses an existing object of type short (in Foo at offset 0)
    #0 0x61eed1b9b89c  (/app/output.s+0x2b89c)

This also happens with an union of POD or trivial types, but I'm not 100% sure on the standard wording if placement-new is supposed to be allowed in these cases.

@github-actions github-actions bot added the false-positive Warning fires when it should not label Dec 23, 2024
@EugeneZelenko EugeneZelenko added the compiler-rt:tysan Type sanitizer label Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-rt:tysan Type sanitizer false-positive Warning fires when it should not
Projects
None yet
Development

No branches or pull requests

2 participants