Self modifying code always segmentation faults on Linux

前端 未结 3 927
忘了有多久
忘了有多久 2020-12-01 10:17

i found an article about self modifying code and tried to do some examples, but i get always segmentation faults. As fas as i can understand, there is a violation in memory

3条回答
  •  半阙折子戏
    2020-12-01 10:29

    You should to change memory access permissions in runtime.

    #include 
    
    void *addr  = get_address_of_instruction_pointer();
    int  length = 4096;   /* size of a page */
    
    if (mprotect(addr, length, PROT_READ | PROT_WRITE | PROT_EXEC) == 0) {
        /* current code page is now writable and code from it is allowed for execution */
    }
    

提交回复
热议问题