1

Do gcc's function attributes extensions have any effect when used on the type of function pointers?

The function attributes can be used when declaring the type of function pointers but at least some of them seem to have no effect.

https://stackoverflow.com/a/28740576/1128289

The gcc documentation itself does not address this issue.

Community
  • 1
  • 1
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
  • It would be pretty illogical if it did - Once you declare a thing, it's defined. Any pointer to it then points to "the thing as defined" (True for data types, structures, **and functions**. The only thing you can do to with fiddling with the pointer to it is *pretending* it pointed to something different. Think about some non-size-aligned structure, for example, and define a pointer to it that would pretend it's 128-byte-aligned. Would you expect the structure to "move" in memory? Note also pointers to objects can be defined outside the current compilation unit and what follows from there. – tofro Dec 11 '16 at 08:48
  • @tofro That's a reasonable guess but I'm hoping for an answer. It does look like some versions of gcc give a warning when mixing certain function pointers and functions. – Praxeolitic Dec 11 '16 at 09:06
  • Actually, I don't agree it's a guess. It's how pointers work in C. – tofro Dec 11 '16 at 09:42
  • @tofro gcc's function attributes are an extension. They're not part of how C works. – Praxeolitic Dec 11 '16 at 09:43
  • 1
    Basically, attributes that affect calling conventions or memory model (fastcall, pascal, far) are part of the function type and thus affect pointers. Others are not, and do not. – n. m. could be an AI Dec 11 '16 at 11:02

1 Answers1

1

Very generally, the C standard basically says that handling an object over a pointer whose type is not aligned with the type of the object itself generates undefined behaviour - There are many exceptions to this general rule, but, apparently, none of them seems to apply to your case.

That means we're moving on very unsafe grounds here, in the first place.

First, you need to distinguish function attributes into two classes:

  1. Function attributes that actually change something in the behavior or location of the function itself like aligned or interrupt- A function that is not attributed that way will not change its inner code once you declare a pointer to it interrupt, for example (the code that is generated for the function would need to dynamically change - for example, a "return from interrupt" instruction replaced by a "return normally" one - depending on along what type of pointer it would have been called). This is obviously not possible.
  2. Function attributes that tell the calling code something about the behaviour that can be expected from the function - like noreturn or malloc, for example. Those attributes sometimes might not modify the function code itself (you'll never know, however...), but rather tell the calling code something about assumptions it can make in order to optimise. These assumptions will affect the calling function only and thus can be triggered by tweaking a pointer (you don't even need a pointer to do that, a modified function prototype should suffice - for the language, that would actually turn out to have the same effect). If you tell the compiler it can make such assumptions and those turn out not to be true, this will, however, lead to all sorts of things go wrong. After all, C makes the programmer responsible for making sure a pointer points to the right type of thing.

There are, however, function attributes that actually restrict the amount of assumptions a calling function would be allowed to make (the most obvious would be malloc that tells calling code it returns yet untyped and uninitialized memory). Those should be relatively safe to use (I do, however, fail to come up with a use case atm)

Without very detailed knowledge on what belongs into (1) or (2) above, and what exactly a function attribute might affect in both called and calling code (which would be very difficult to achieve, because I don't recall to ever have seen that documented in detail), you thus will not be able to decide whether this pointer tweaking actually is possible and what side effects it might generate.

Also, in my opinion, there is not much of a difference between tweaking a pointer to a function and calling an external function with a (deliberately) wrong prototype. This might give you some insight on what you are actually trying to do...

tofro
  • 5,640
  • 14
  • 31
  • I don't expect pointers with attributes to somehow change the pointees. Your #2 is what I'm getting at. – Praxeolitic Dec 11 '16 at 10:42
  • @Praxeolitic the #1 point is: You'll never know whether you are facing case (1) or (2). #2 point is: It's still undefined behaviour – tofro Dec 11 '16 at 10:47