File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -132,7 +132,19 @@ StackBounds StackBounds::currentThreadStackBoundsInternal()
132132 // account for a guard page
133133 size -= static_cast <rlim_t >(sysconf (_SC_PAGESIZE));
134134 void * bound = static_cast <char *>(origin) - size;
135- return StackBounds { origin, bound };
135+
136+ static char ** oldestEnviron = environ;
137+
138+ // In 32bit architecture, it is possible that environment variables are having a characters which looks like a pointer,
139+ // and conservative GC will find it as a live pointer. We would like to avoid that to precisely exclude non user stack
140+ // data region from this stack bounds. As the article (https://lwn.net/Articles/631631/) and the elf loader implementation
141+ // explain how Linux main thread stack is organized, environment variables vector is placed on the stack, so we can exclude
142+ // environment variables if we use `environ` global variable as a origin of the stack.
143+ // But `setenv` / `putenv` may alter `environ` variable's content. So we record the oldest `environ` variable content, and use it.
144+ StackBounds stackBounds { origin, bound };
145+ if (stackBounds.contains (oldestEnviron))
146+ stackBounds = { oldestEnviron, bound };
147+ return stackBounds;
136148 }
137149#endif
138150 return ret;
You can’t perform that action at this time.
0 commit comments