-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
bpo-33955: Support USE_STACKCHECK on macOS #8046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
07d8e26
bpo-33955: Support USE_STACKCHECK on macOS
corona10 c967a08
bpo-33955: Remove the estimation code
corona10 d4465d6
bpo-33955: Apply code review
corona10 c8fe461
bpo-33955: Apply code review
corona10 6a3879c
bpo-33955: Fix
corona10 79b10e6
bpo-33955: lint
corona10 60c3f0f
bpo-33955: Apply codereview
corona10 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
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
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
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core and Builtins/2018-07-02-18-53-31.bpo-33955.2FKRM2.rst
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,2 @@ | ||
| A interpreter on macOS system now supports USE_STACKCHECK. | ||
| The interpreter detects running out of stack space and raise MemoryError. |
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 |
|---|---|---|
|
|
@@ -1649,6 +1649,76 @@ PyOS_CheckStack(void) | |
|
|
||
| #endif /* WIN32 && _MSC_VER */ | ||
|
|
||
| #if defined(__APPLE__) | ||
|
|
||
| #define MAC_OS_10_9 13 | ||
| #define MAC_OS_10_10 14 | ||
| #define MAC_OS_10_11 15 | ||
|
|
||
| #include <sys/utsname.h> | ||
|
|
||
| static size_t | ||
| Get_Stack_Check_Size(const pthread_t *thread_self) | ||
|
corona10 marked this conversation as resolved.
Outdated
|
||
| { | ||
| size_t stack_space; | ||
| if (pthread_main_np() == 1) { | ||
| // On macOS 10.09 - 10.11 pthread_get_stacksize_np | ||
| // returns wrong stack size on main thread. | ||
| // ref: https://github.com/rust-lang/rust/issues/43347#issuecomment-316783599 | ||
| // ref: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8020753 | ||
| struct utsname sysinfo; | ||
| uname(&sysinfo); | ||
| const int major_version = atoi(sysinfo.release); | ||
| if (major_version == MAC_OS_10_9 || | ||
| major_version == MAC_OS_10_10 || | ||
| major_version == MAC_OS_10_11) { | ||
| struct rlimit limit; | ||
| getrlimit(RLIMIT_STACK, &limit); | ||
| stack_space = limit.rlim_cur; | ||
| } | ||
| else { | ||
| stack_space = pthread_get_stacksize_np(*thread_self); | ||
| } | ||
| } | ||
| else { | ||
| stack_space = pthread_get_stacksize_np(*thread_self); | ||
| } | ||
| return stack_space; | ||
| } | ||
|
|
||
| /* | ||
| * Return non-zero when we run out of memory on the stack; zero otherwise. | ||
| */ | ||
| int | ||
| PyOS_CheckStack(void) | ||
| { | ||
| PyThreadState *tstate = _PyThreadState_GET(); | ||
| if (tstate->stack_space == 0) { | ||
| const pthread_t thread_self = pthread_self(); | ||
| size_t stack_space = Get_Stack_Check_Size(&thread_self); | ||
| tstate->stack_space = stack_space; | ||
| tstate->last_stack_remain = stack_space; | ||
| } | ||
| const uintptr_t end = (uintptr_t)pthread_get_stackaddr_np(pthread_self()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know if it's implemented in pure userspace, or if it's a system call? |
||
| const uintptr_t frame = (uintptr_t)__builtin_frame_address(0); | ||
|
corona10 marked this conversation as resolved.
Outdated
|
||
| const size_t remains = tstate->stack_space - (end - frame); | ||
| size_t required_stack_space; | ||
|
|
||
| if (remains > tstate->last_stack_remain) { | ||
| required_stack_space = remains - tstate->last_stack_remain; | ||
| } | ||
| else { | ||
| required_stack_space = tstate->last_stack_remain - remains; | ||
| } | ||
|
|
||
| tstate->last_stack_remain = remains; | ||
| if (remains >= required_stack_space) { | ||
| return 0; | ||
| } | ||
| return 1; | ||
| } | ||
| #endif /* __APPLE__ */ | ||
|
|
||
| /* Alternate implementations can be added here... */ | ||
|
|
||
| #endif /* USE_STACKCHECK */ | ||
|
|
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.
I have to check with a native speaker, but this seems to suggest that USE_STACKCHECK is only enabled on macOS when using the Microsoft compiler, which is not what you're trying to say here.
That said, I'm not a native english speaker and might interpret this sentence too strictly.