Is void* compatible with function pointers?

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 24 08:36:26 PDT 2014


On Tue, 24 Jun 2014 00:24:50 -0400, deadalnix <deadalnix at gmail.com> wrote:

> On Tuesday, 24 June 2014 at 01:41:13 UTC, Steven Schveighoffer wrote:
>>> This is because most CPUs consider the instructions as immutable.
>>> Even x86 do not provide any guarantee (which makes it very hard
>>> to swap implementation outside of a VM).
>>
>> Remember, these are not functions, but function pointers. You are not  
>> modifying the function at all.
>>
>
> void* is not a function and can come from anything that is mutable.
>
>>> Casting from void* to function is pretty guaranteed to be
>>> undefined behavior on all plateforms, unless you emit the proper
>>> barriers.
>>
>> Can you demonstrate a C example that will fail on ARM?
>
> Anything that JIT is affected. Consider the pseudocode:
>
> void JIT() {
>      void* code = malloc(XXX);
>      // Write instructions into the allocated chunk of memory.
>
>      membar(); // Memory barrier to avoid reordering.
>
>      auto fun = cast(void function()) code;
>      fun(); // Even if your codegen is correct, anything can happen this  
> point.
> }

This is not what we are talking about.

We are talking about whether a valid function pointer can be cast to void  
* and back again. It is undefined for C/C++. Should it be defined for D?

-Steve


More information about the Digitalmars-d mailing list