Skip to content

Commit c19897b

Browse files
thughesry
authored andcommitted
Add --max-stack-size flag.
v8 doesn't expose a command-line flag to set the stack size, so this adds a new flag that node understands how to handle and uses v8's ResourceConstraints API.
1 parent 8ec4870 commit c19897b

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

src/node.cc

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static int option_end_index = 0;
7979
static bool use_debug_agent = false;
8080
static bool debug_wait_connect = false;
8181
static int debug_port=5858;
82+
static int max_stack_size = 0;
8283

8384
static ev_check check_tick_watcher;
8485
static ev_prepare prepare_tick_watcher;
@@ -1737,21 +1738,22 @@ static void ParseDebugOpt(const char* arg) {
17371738
static void PrintHelp() {
17381739
printf("Usage: node [options] script.js [arguments] \n"
17391740
"Options:\n"
1740-
" -v, --version print node's version\n"
1741-
" --debug[=port] enable remote debugging via given TCP port\n"
1742-
" without stopping the execution\n"
1743-
" --debug-brk[=port] as above, but break in script.js and\n"
1744-
" wait for remote debugger to connect\n"
1745-
" --v8-options print v8 command line options\n"
1746-
" --vars print various compiled-in variables\n"
1741+
" -v, --version print node's version\n"
1742+
" --debug[=port] enable remote debugging via given TCP port\n"
1743+
" without stopping the execution\n"
1744+
" --debug-brk[=port] as above, but break in script.js and\n"
1745+
" wait for remote debugger to connect\n"
1746+
" --v8-options print v8 command line options\n"
1747+
" --vars print various compiled-in variables\n"
1748+
" --max-stack-size=val set max v8 stack size (bytes)\n"
17471749
"\n"
17481750
"Enviromental variables:\n"
1749-
"NODE_PATH ':'-separated list of directories\n"
1750-
" prefixed to the module search path,\n"
1751-
" require.paths.\n"
1752-
"NODE_DEBUG Print additional debugging output.\n"
1753-
"NODE_MODULE_CONTEXTS Set to 1 to load modules in their own\n"
1754-
" global contexts.\n"
1751+
"NODE_PATH ':'-separated list of directories\n"
1752+
" prefixed to the module search path,\n"
1753+
" require.paths.\n"
1754+
"NODE_DEBUG Print additional debugging output.\n"
1755+
"NODE_MODULE_CONTEXTS Set to 1 to load modules in their own\n"
1756+
" global contexts.\n"
17551757
"NODE_DISABLE_COLORS Set to 1 to disable colors in the REPL\n"
17561758
"\n"
17571759
"Documentation can be found at http://nodejs.org/api.html"
@@ -1775,6 +1777,11 @@ static void ParseArgs(int *argc, char **argv) {
17751777
printf("NODE_PREFIX: %s\n", NODE_PREFIX);
17761778
printf("NODE_CFLAGS: %s\n", NODE_CFLAGS);
17771779
exit(0);
1780+
} else if (strstr(arg, "--max-stack-size=") == arg) {
1781+
const char *p = 0;
1782+
p = 1 + strchr(arg, '=');
1783+
max_stack_size = atoi(p);
1784+
argv[i] = const_cast<char*>("");
17781785
} else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
17791786
PrintHelp();
17801787
exit(0);
@@ -1826,6 +1833,20 @@ int main(int argc, char *argv[]) {
18261833
v8argv[node::option_end_index] = const_cast<char*>("--expose_debug_as");
18271834
v8argv[node::option_end_index + 1] = const_cast<char*>("v8debug");
18281835
}
1836+
1837+
// For the normal stack which moves from high to low addresses when frames
1838+
// are pushed, we can compute the limit as stack_size bytes below the
1839+
// the address of a stack variable (e.g. &stack_var) as an approximation
1840+
// of the start of the stack (we're assuming that we haven't pushed a lot
1841+
// of frames yet).
1842+
if (node::max_stack_size != 0) {
1843+
uint32_t stack_var;
1844+
ResourceConstraints constraints;
1845+
1846+
uint32_t *stack_limit = &stack_var - (node::max_stack_size / sizeof(uint32_t));
1847+
constraints.set_stack_limit(stack_limit);
1848+
SetResourceConstraints(&constraints); // Must be done before V8::Initialize
1849+
}
18291850
V8::SetFlagsFromCommandLine(&v8argc, v8argv, false);
18301851

18311852
// Ignore SIGPIPE

0 commit comments

Comments
 (0)