| 548 | } |
| 549 | |
| 550 | bool ExecutorX86::SetStackPlacement(void* start, void* end, unsigned int flagMemoryAllocated) |
| 551 | { |
| 552 | #ifdef __linux |
| 553 | if(NULLC::stackManaged && NULLC::stackBaseAddress) |
| 554 | { |
| 555 | char *p = (char*)((intptr_t)((char*)NULLC::stackEndAddress + PAGESIZE - 1) & ~(PAGESIZE - 1)) - PAGESIZE; |
| 556 | if(mprotect(p - 8192, PAGESIZE, PROT_READ | PROT_WRITE)) |
| 557 | asm("int $0x3"); |
| 558 | // If stack is managed by Executor, free memory |
| 559 | NULLC::alignedDealloc(NULLC::stackBaseAddress); |
| 560 | }else if(NULLC::stackEndAddress){ |
| 561 | // Otherwise, remove page guard, restoring old protection value |
| 562 | char *p = (char*)((intptr_t)((char*)NULLC::stackEndAddress + PAGESIZE - 1) & ~(PAGESIZE - 1)) - PAGESIZE; |
| 563 | if(mprotect(p - 8192, PAGESIZE, PROT_READ | PROT_WRITE)) |
| 564 | asm("int $0x3"); |
| 565 | } |
| 566 | #else |
| 567 | // If old memory was allocated here using VirtualAlloc |
| 568 | if(NULLC::stackManaged) |
| 569 | { |
| 570 | // If stack is managed by Executor, free memory |
| 571 | VirtualFree(NULLC::stackBaseAddress, 0, MEM_RELEASE); |
| 572 | }else{ |
| 573 | // Otherwise, remove page guard, restoring old protection value |
| 574 | VirtualProtect((char*)NULLC::stackEndAddress - 8192, 4096, NULLC::stackProtect, &NULLC::stackProtect); |
| 575 | } |
| 576 | #endif |
| 577 | NULLC::stackBaseAddress = start; |
| 578 | NULLC::stackEndAddress = end; |
| 579 | NULLC::stackManaged = !flagMemoryAllocated; |
| 580 | if(NULLC::stackManaged) |
| 581 | { |
| 582 | NULLC::stackGrowSize = 128 * 4096; |
| 583 | NULLC::stackGrowCommit = 64 * 4096; |
| 584 | |
| 585 | char *paramData = NULL; |
| 586 | // Request memory at address |
| 587 | #ifdef __linux |
| 588 | if(NULLC::stackEndAddress) |
| 589 | NULLC::stackGrowCommit = NULLC::stackGrowSize = int((char*)end - (char*)start); |
| 590 | if(NULL == (paramData = (char*)NULLC::alignedAlloc(NULLC::stackGrowSize))) |
| 591 | { |
| 592 | strcpy(execError, "ERROR: failed to allocate memory"); |
| 593 | return false; |
| 594 | } |
| 595 | NULLC::stackBaseAddress = paramData; |
| 596 | NULLC::stackEndAddress = (char*)paramData + NULLC::stackGrowSize; |
| 597 | char *p = (char*)((intptr_t)((char*)NULLC::stackEndAddress + PAGESIZE - 1) & ~(PAGESIZE - 1)) - PAGESIZE; |
| 598 | if(mprotect(p - 8192, PAGESIZE, 0)) |
| 599 | asm("int $0x3"); |
| 600 | #else |
| 601 | if(NULL == (paramData = (char*)VirtualAlloc(NULLC::stackBaseAddress, NULLC::stackGrowSize, MEM_RESERVE, PAGE_NOACCESS))) |
| 602 | { |
| 603 | strcpy(execError, "ERROR: failed to reserve memory"); |
| 604 | return false; |
| 605 | } |
| 606 | if(!VirtualAlloc(NULLC::stackBaseAddress, NULLC::stackGrowCommit, MEM_COMMIT, PAGE_READWRITE)) |
| 607 | { |
no test coverage detected