Is void* compatible with function pointers?

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 23 21:24:50 PDT 2014


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.
}



More information about the Digitalmars-d mailing list