bsd-user: Add system independent stack, data and text limiting

Eliminate the x86 specific stack stuff in favor of more generic control
over the process size:
    target_maxtsiz  max text size
    target_dfldsiz  initial data size limit
    target_maxdsiz  max data size
    target_dflssiz  initial stack size limit
    target_maxssiz  max stack size
    target_sgrowsiz amount to grow stack
These can be set on a per-arch basis, and the stack size can be set
on the command line. Adjust the stack size parameters at startup.

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
Warner Losh 2021-08-06 18:48:37 -06:00 committed by Warner Losh
parent 82792244da
commit 312a0b1cbf
3 changed files with 45 additions and 16 deletions

View File

@ -204,7 +204,7 @@ static abi_ulong setup_arg_pages(abi_ulong p, struct bsd_binprm *bprm,
/* Create enough stack to hold everything. If we don't use
* it for args, we'll use it for something else...
*/
size = x86_stack_size;
size = target_dflssiz;
if (size < MAX_ARG_PAGES * TARGET_PAGE_SIZE)
size = MAX_ARG_PAGES * TARGET_PAGE_SIZE;
error = target_mmap(0,

View File

@ -18,6 +18,11 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include "qemu/osdep.h"
#include "qemu-common.h"
#include "qemu/units.h"
@ -44,8 +49,6 @@
#include "host-os.h"
#include "target_arch_cpu.h"
#include <sys/sysctl.h>
int singlestep;
unsigned long mmap_min_addr;
uintptr_t guest_base;
@ -57,12 +60,12 @@ const char *qemu_uname_release;
enum BSDType bsd_type;
char qemu_proc_pathname[PATH_MAX]; /* full path to exeutable */
/*
* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
* we allocate a bigger stack. Need a better solution, for example
* by remapping the process stack directly at the right place
*/
unsigned long x86_stack_size = 512 * 1024;
unsigned long target_maxtsiz = TARGET_MAXTSIZ; /* max text size */
unsigned long target_dfldsiz = TARGET_DFLDSIZ; /* initial data size limit */
unsigned long target_maxdsiz = TARGET_MAXDSIZ; /* max data size */
unsigned long target_dflssiz = TARGET_DFLSSIZ; /* initial data size limit */
unsigned long target_maxssiz = TARGET_MAXSSIZ; /* max stack size */
unsigned long target_sgrowsiz = TARGET_SGROWSIZ; /* amount to grow stack */
void gemu_log(const char *fmt, ...)
{
@ -112,7 +115,6 @@ static void usage(void)
"-d item1[,...] enable logging of specified items\n"
" (use '-d help' for a list of log items)\n"
"-D logfile write logs to 'logfile' (default stderr)\n"
"-p pagesize set the host page size to 'pagesize'\n"
"-singlestep always run in singlestep mode\n"
"-strace log system calls\n"
"-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
@ -132,7 +134,7 @@ static void usage(void)
,
TARGET_NAME,
interp_prefix,
x86_stack_size);
target_dflssiz);
exit(1);
}
@ -161,6 +163,23 @@ void init_task_state(TaskState *ts)
ts->sigqueue_table[i].next = NULL;
}
static void
adjust_ssize(void)
{
struct rlimit rl;
if (getrlimit(RLIMIT_STACK, &rl) != 0) {
return;
}
target_maxssiz = MIN(target_maxssiz, rl.rlim_max);
target_dflssiz = MIN(MAX(target_dflssiz, rl.rlim_cur), target_maxssiz);
rl.rlim_max = target_maxssiz;
rl.rlim_cur = target_dflssiz;
setrlimit(RLIMIT_STACK, &rl);
}
static void save_proc_pathname(char *argv0)
{
int mib[4];
@ -197,6 +216,8 @@ int main(int argc, char **argv)
envlist_t *envlist = NULL;
bsd_type = HOST_DEFAULT_BSD_TYPE;
adjust_ssize();
if (argc <= 1) {
usage();
}
@ -257,14 +278,17 @@ int main(int argc, char **argv)
}
} else if (!strcmp(r, "s")) {
r = argv[optind++];
rv = qemu_strtoul(r, &r, 0, &x86_stack_size);
if (rv < 0 || x86_stack_size <= 0) {
rv = qemu_strtoul(r, &r, 0, &target_dflssiz);
if (rv < 0 || target_dflssiz <= 0) {
usage();
}
if (*r == 'M') {
x86_stack_size *= MiB;
target_dflssiz *= 1024 * 1024;
} else if (*r == 'k' || *r == 'K') {
x86_stack_size *= KiB;
target_dflssiz *= 1024;
}
if (target_dflssiz > target_maxssiz) {
usage();
}
} else if (!strcmp(r, "L")) {
interp_prefix = argv[optind++];

View File

@ -219,7 +219,12 @@ void mmap_fork_end(int child);
/* main.c */
extern char qemu_proc_pathname[];
extern unsigned long x86_stack_size;
extern unsigned long target_maxtsiz;
extern unsigned long target_dfldsiz;
extern unsigned long target_maxdsiz;
extern unsigned long target_dflssiz;
extern unsigned long target_maxssiz;
extern unsigned long target_sgrowsiz;
/* user access */