This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 411
Add Fiber's guard page in Posix as well #1698
Merged
MartinNowak
merged 6 commits into
dlang:master
from
nemanja-boric-sociomantic:fiber-protection
May 6, 2017
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6ac9c09
Add Fiber's guard page in Posix as well
nemanja-boric-sociomantic 3e964f0
Make Fiber's stack protection-page's size configurable
nemanja-boric-sociomantic bd719ea
change guard_page_size to camelCase
MartinNowak 3b7117b
add test for Fiber guard page
MartinNowak a5c0dc2
do not write to guard page
MartinNowak 6be1794
Simplify fiber guard page test
nemanja-boric-sociomantic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Make fiber stack protection-page size configurable | ||
|
|
||
| It is now possible to change the guard page size by using | ||
| the new Fiber's constructor argument - guard_page_size. It defaults to | ||
| `PAGE_SIZE` (the same it used to be on Windows), and specifying 0 will | ||
| turn this feature off. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| Add Fiber's stack-protection page for Posix. | ||
|
|
||
| The feature already existing for Windows' fiber implementation is now added to | ||
| the systems using mmap to allocate fibers' stacks: After (or before) the last | ||
| page used for the Fiber's stack, the page is allocate which is protected for | ||
| any kind of access. This will cause system to trap immediately on the fiber's | ||
| stack overflow. If in debugger session, one can inspect contents of the memory | ||
| before or after stack pointer and it can be seen if it contains END OF FIBER | ||
| string pattern. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| include ../common.mak | ||
|
|
||
| TESTS:=fiber_guard_page | ||
|
|
||
| .PHONY: all clean | ||
| all: $(addprefix $(ROOT)/,$(addsuffix .done,$(TESTS))) | ||
|
|
||
| # segfault || bus error (OSX) | ||
| $(ROOT)/fiber_guard_page.done: $(ROOT)/%.done : $(ROOT)/% | ||
| @echo Testing $* | ||
| $(QUIET)$(TIMELIMIT)$(ROOT)/$* $(RUN_ARGS); rc=$$?; [ $$rc -eq 139 ] || [ $$rc -eq 138 ] | ||
| @touch $@ | ||
|
|
||
| $(ROOT)/%: $(SRC)/%.d | ||
| $(QUIET)$(DMD) $(DFLAGS) -of$@ $< | ||
|
|
||
| clean: | ||
| rm -rf $(GENERATED) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import core.thread; | ||
| import core.sys.posix.sys.mman; | ||
|
|
||
| // this should be true for most architectures | ||
| // (taken from core.thread) | ||
| version = StackGrowsDown; | ||
|
|
||
| enum stackSize = 4096; | ||
|
|
||
| // Simple method that causes a stack overflow | ||
| void stackMethod() | ||
| { | ||
| // Over the stack size, so it overflows the stack | ||
| int[stackSize/int.sizeof+100] x; | ||
| } | ||
|
|
||
| void main() | ||
| { | ||
| auto test_fiber = new Fiber(&stackMethod, stackSize); | ||
|
|
||
| // allocate a page below (above) the fiber's stack to make stack overflows possible (w/o segfaulting) | ||
| version (StackGrowsDown) | ||
| { | ||
| static assert(__traits(identifier, test_fiber.tupleof[8]) == "m_pmem"); | ||
| auto stackBottom = test_fiber.tupleof[8]; | ||
| auto p = mmap(stackBottom - 8 * stackSize, 8 * stackSize, | ||
| PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); | ||
| assert(p !is null, "failed to allocate page"); | ||
| } | ||
| else | ||
| { | ||
| auto m_sz = test_fiber.tupleof[7]; | ||
| auto m_pmem = test_fiber.tupleof[8]; | ||
| static assert(__traits(identifier, test_fiber.tupleof[7]) == "m_size"); | ||
| static assert(__traits(identifier, test_fiber.tupleof[8]) == "m_pmem"); | ||
|
|
||
| auto stackTop = m_pmem + m_sz; | ||
| auto p = mmap(stackTop, 8 * stackSize, | ||
| PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); | ||
| assert(p !is null, "failed to allocate page"); | ||
| } | ||
|
|
||
| // the guard page should prevent a mem corruption by stack | ||
| // overflow and cause a segfault instead (or generate SIGBUS on *BSD flavors) | ||
| test_fiber.call(); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't true anymore, see #1821 for a fix.