G++: Can __attribute__((__may_alias__)) be used for pointer to class instance rather than to class definition itself?

StackOverflow https://stackoverflow.com/questions/16287329

  •  13-04-2022
  •  | 
  •  

Question

I'm looking to the answer to the following question: is may_alias suitable as attribute for pointer to an object of some class Foo? Or must it be used at class level only?

Consider the following code(it is based on a real-world example which is more complex):

#include <iostream>

using namespace std;

#define alias_hack __attribute__((__may_alias__))

template <typename T>
class Foo
{
private:
    /*alias_hack*/ char Data[sizeof (T)];
public:
    /*alias_hack*/ T& GetT()
    {
        return *((/*alias_hack*/ T*)Data);
    } 
};

struct Bar
{
    int Baz;

    Bar(int baz)
      : Baz(baz)
    {}
} /*alias_hack*/; // <- uncommeting this line apparently solves the problem, but does so on class-level(rather than pointer-level)
// uncommenting previous alias_hack's doesn't help

int main()
{
    Foo<Bar> foo;
    foo.GetT().Baz = 42;
    cout << foo.GetT().Baz << endl;
}

Is there any way to tell gcc that single pointer may_alias some another?

BTW, please note that gcc detection mechanism of such problem is imperfect, so it is very easy to just make this warning go away without actually solving the problem.

Consider the following snippet of code:

#include <iostream>

using namespace std;

int main()
{
    long i = 42;
    long* iptr = &i;
    //(*(short*)&i) = 3; // with warning
    //(*(short*)iptr) = 3; // without warning
    cout << i << endl;
}

Uncomment one of the lines to see the difference in compiler output.

Was it helpful?

Solution 2

Dimitar is correct. may_alias is a type attribute. It can only apply to a type, not an instance of the type. What you'd like is what gcc calls a "variable attribute". It would not be easy to disable optimizations for one specific pointer. What would the compiler do if you call a function with this pointer? The function is potentially already compiled and will behave based on the type passed to the function, not based on the address store in the pointer (you should see now why this is a type attribute)

Now depending on your code something like that might work:

#define define_may_alias_type(X) class X ## _may alias : public X { } attribute ((may_alias));

You'd just pass your pointer as Foo_may_alias * (instead of Foo *) when it might alias. That's hacky though

Wrt your question about the warning, it's because -Wall defaults to -Wstrict-aliasing=3 which is not 100% accurate. Actually, -Wstrict-aliasing is never 100% accurate but depending on the level you'll get more or less false negatives (and false positives). If you pass -Wstrict-aliasing=1 to gcc, you'll see a warning for both

OTHER TIPS

Simple answer - sorry, no.

__attrbite__ gives instructions to the compiler. Objects exist in the memory of the executed program. Hence nothing in __attribute__ list can relate to the run-time execution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top