Conversation
On Linux if a detour crossed a page boundary we would only change the memory protection of the first page (as we were aligning the address as required, but not taking into account the length). I don't have an easy way to test this but it looks correct. `addr + len` doesn't appear to need to be aligned though, so another option could be to use `(addr - startPage) + length` as len. Also fixed a non-zero offset being passed into CDetour's ApplyPatch function - this is never done internally anywhere, but it doesn't hurt to fix it. Fixes #984
public/CDetour/detourhelpers.h
Outdated
| mprotect(addr2, sysconf(_SC_PAGESIZE), prot); | ||
| long pageSize = sysconf(_SC_PAGESIZE); | ||
| void *startPage = ke::AlignedBase(addr, pageSize); | ||
| void *endPage = ke::AlignedBase((intptr_t)addr + length, pageSize); |
There was a problem hiding this comment.
Should this be addr + length - 1 if the sequence runs right up to the page boundary?
There was a problem hiding this comment.
I did thing that initially but I think this is correct, if you imagine a zero-length sequence doing -1 would take us one page back, and a sequence equal to the length of a page seems like it'll do the right thing here as well (endPage would be the same as startPage, but one more byte would take it to the next page).
There was a problem hiding this comment.
startPage = AlignedBase(0, 4096) = 0
endPage = AlignedBase(0 + 4096, 4096) = 4096
len = endPage - startPage + pageSize = 4096 - 0 + 4096 = 8192
So I think you'd wind up with two pages? That said, it's pretty edge casey and doesn't really matter.
There was a problem hiding this comment.
Ah, yes, that does make sense! Yeah, I think I'd prefer going over in that edge case than trying to special-case zero length.
The code here can only use a maximum length of 20 currently :D
public/CDetour/detourhelpers.h
Outdated
| mprotect(addr2, sysconf(_SC_PAGESIZE), prot); | ||
| long pageSize = sysconf(_SC_PAGESIZE); | ||
| void *startPage = ke::AlignedBase(addr, pageSize); | ||
| void *endPage = ke::AlignedBase((intptr_t)addr + length, pageSize); |
There was a problem hiding this comment.
startPage = AlignedBase(0, 4096) = 0
endPage = AlignedBase(0 + 4096, 4096) = 4096
len = endPage - startPage + pageSize = 4096 - 0 + 4096 = 8192
So I think you'd wind up with two pages? That said, it's pretty edge casey and doesn't really matter.
On Linux if a detour crossed a page boundary we would only change the
memory protection of the first page (as we were aligning the address as
required, but not taking into account the length).
I don't have an easy way to test this but it looks correct.
addr + lendoesn't appear to need to be aligned though, so another option could be
to use
(addr - startPage) + lengthas len.Also fixed a non-zero offset being passed into CDetour's ApplyPatch
function - this is never done internally anywhere, but it doesn't hurt
to fix it.
Fixes #984