Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/nle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <assert.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>

#include <tmt.h>

Expand All @@ -22,6 +23,24 @@

#define STACK_SIZE (1 << 16) /* 64KiB */

static size_t
effective_stack_size(void)
{
/* create_fcontext_stack() uses the first page as a guard page.
*
* On systems with 64KiB pages (e.g. some aarch64 kernels),
* STACK_SIZE=64KiB results in a single page mapping, leaving no usable
* stack space after the guard page. Ensure at least 2 pages (guard +
* usable).
*/
size_t stack_size = STACK_SIZE;
long page_size = sysconf(_SC_PAGESIZE);
if (page_size > 0 && stack_size < 2u * (size_t) page_size) {
stack_size = 2u * (size_t) page_size;
}
return stack_size;
}

#ifndef __has_feature
#define __has_feature(x) 0 /* Compatibility with non-clang compilers. */
#endif
Expand Down Expand Up @@ -402,7 +421,7 @@ nle_start(nle_obs *obs, FILE *ttyrec, nle_settings *settings_p)
/* Initialise the level generation RNG */
nle_init_lgen_rng();

nle->stack = create_fcontext_stack(STACK_SIZE);
nle->stack = create_fcontext_stack(effective_stack_size());
nle->generatorcontext =
make_fcontext(nle->stack.sptr, nle->stack.ssize, mainloop);

Expand Down
Loading