Is void* compatible with function pointers?
John Colvin via Digitalmars-d
digitalmars-d at puremagic.com
Mon Jun 23 22:39:49 PDT 2014
On Tuesday, 24 June 2014 at 04:24:51 UTC, deadalnix 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.
> }
That's one thing, but casting a function pointer to void* and
then back again later, without modification, must be defined, no?
That's a very different situation to forging your own executable
code.
More information about the Digitalmars-d
mailing list