This method uses no special files. In fact it doesn't store data anywhere.
To set up your randomize_va_space
should be set to zero (linux).
You can check this with:
sysctl -anr "e_v"
I won't tell you how to set it to 0
since in general this should not be set to 0
, and setting it to 0
represents a possible security risk.
Don't play with your kernel unless you know what you are doing or have nothing to lose on the device.
import Unsafe.Coerce
main=([[interact id],[pure()]]>>=([0..7]>>))!!mod(unsafeCoerce(+))16
Try it online!
TIO is stuck on cat and I have no way to reset it. You can switch the +
to a :
to see a version that is stuck on the noop.
To reset it locally it should be enough to set the randomize_va_space
to 2
run the program once and then set it back to 0
.
Explanation
More detailed explanation here
In Haskell, all complex objects including functions are internally represented as a pointer. This is a number in binary that "points" to a specific location in memory. This is because we want to pass these values by reference, since copying the whole thing is expensive, and since Haskell disallows mutation we can with no problem.
However simple objects like ints are passed by value since they are so small that copying them is about as expensive as copying a pointer would be. unsafeCoerce is a super unsafe function which just takes the raw bytes from one object and reinterprets it as the raw bytes for another type.
So if we use unsafeCoerce from a function to an Int
, the resulting Int
is just the value of the pointer to the object. And the value of that simple object is dependent only on where the complex object is located not anything about what it is.
When randomize_va_space
is on, this means the value of unsafeCoerce(+)
is a random multiple of 8. However when it's off, it is fixed to one value which depends on some factors about the machine.