cache incoherence issue

Hi,
I am testing some codes protection mechanism with my application by altering the shared library and restore it after the .so is loaded.
Although the application will alway run without error on my x86_64 Ubuntu, it will fail 2 out of 5 times on the Tx1, reporting either “segment fault” or “invalid instruction”.

Basically, when I found the segment of my .so before entering main(), I do this:
if(0 != mprotect(void* address, size, PROT_READ | PROT_WRITE | PROT_EXEC){
perror(" 1:“):
}
/restore the codes in address/
if(0 != mprotect(void* address, size, PROT_READ | PROT_EXEC){
perror(” 2 :"):
}

I am wondering if this is due to cache incoherence? How can I fix this?

PS: if I add a delay, i.e.usleep(50000),before calling the function in .so, then segment fault issue is gone.

Hi HooverLv,

Thanks for telling us this error. What do you mean “by altering the shared library and restore it after the .so is loaded.”?? Changing the content of a shared library?

Any simpler way to reproduce the error?

Could you share your sample codes to us for debugging?

Hi,

One example of “Changing the content of a shared library”, is to flip every bit in the text segment in the .so file.
then after the so is loaded, flip those bits in memory to restore it before entering main().

Could you share the sample? You can share it through private message if don’t want to reveal.

Does any other mprotect sample also hit such error?

no error after sleep for 10 ms. so I will accept this workaround

Great. But it would be better if you could share a easy way or sample codes to reproduce your problem.