From cd61c76608f67a3f495f24fb0c8eb846ba9b8467 Mon Sep 17 00:00:00 2001 From: Chris Hanson Date: Thu, 3 Jan 2008 00:30:47 +0000 Subject: [PATCH] Don't allocate tables based on the values of POSIX configuration variables. These values may be undefined, or unreasonably large. Instead, allocate small tables and grow them as needed. --- v7/src/microcode/ntproc.c | 4 ++- v7/src/microcode/os2proc.c | 4 ++- v7/src/microcode/osio.h | 4 +-- v7/src/microcode/osproc.h | 4 +-- v7/src/microcode/prosio.c | 15 +++------ v7/src/microcode/prosproc.c | 15 +++++---- v7/src/microcode/ux.h | 9 +----- v7/src/microcode/uxio.c | 35 ++++++++++++++------- v7/src/microcode/uxproc.c | 63 +++++++++++++++++++++++-------------- v7/src/microcode/uxterm.c | 17 ++++++++-- 10 files changed, 100 insertions(+), 70 deletions(-) diff --git a/v7/src/microcode/ntproc.c b/v7/src/microcode/ntproc.c index 38859be76..d3bb9b4b7 100644 --- a/v7/src/microcode/ntproc.c +++ b/v7/src/microcode/ntproc.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: ntproc.c,v 1.12 2007/01/05 21:19:25 cph Exp $ +$Id: ntproc.c,v 1.13 2008/01/03 00:30:38 cph Exp $ Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, @@ -310,6 +310,8 @@ OS_process_deallocate (Tprocess process) int OS_process_valid_p (Tprocess process) { + if (process > OS_process_table_size) + return (0); switch (PROCESS_RAW_STATUS (process)) { case process_status_exited: diff --git a/v7/src/microcode/os2proc.c b/v7/src/microcode/os2proc.c index 453158370..702084751 100644 --- a/v7/src/microcode/os2proc.c +++ b/v7/src/microcode/os2proc.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: os2proc.c,v 1.11 2007/01/05 21:19:25 cph Exp $ +$Id: os2proc.c,v 1.12 2008/01/03 00:30:39 cph Exp $ Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, @@ -323,6 +323,8 @@ OS_process_deallocate (Tprocess process) int OS_process_valid_p (Tprocess process) { + if (process > OS_process_table_size) + return (0); switch (PROCESS_RAW_STATUS (process)) { case process_status_exited: diff --git a/v7/src/microcode/osio.h b/v7/src/microcode/osio.h index 20fa30be8..458f65a29 100644 --- a/v7/src/microcode/osio.h +++ b/v7/src/microcode/osio.h @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: osio.h,v 1.22 2007/04/22 16:31:23 cph Exp $ +$Id: osio.h,v 1.23 2008/01/03 00:30:40 cph Exp $ Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, @@ -53,7 +53,7 @@ enum channel_type }; extern size_t OS_channel_table_size; -#define NO_CHANNEL OS_channel_table_size +#define NO_CHANNEL ((size_t) -1) extern int OS_channel_open_p (Tchannel channel); extern void OS_channel_close (Tchannel channel); extern void OS_channel_close_noerror (Tchannel channel); diff --git a/v7/src/microcode/osproc.h b/v7/src/microcode/osproc.h index 181156e00..da69589b7 100644 --- a/v7/src/microcode/osproc.h +++ b/v7/src/microcode/osproc.h @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: osproc.h,v 1.15 2007/04/22 16:31:23 cph Exp $ +$Id: osproc.h,v 1.16 2008/01/03 00:30:41 cph Exp $ Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, @@ -75,7 +75,7 @@ enum process_channel_type }; extern size_t OS_process_table_size; -#define NO_PROCESS OS_process_table_size +#define NO_PROCESS ((size_t) -1) extern enum process_jc_status scheme_jc_status; /* OS_make_subprocess is obsolete; use OS-specific procedure. */ diff --git a/v7/src/microcode/prosio.c b/v7/src/microcode/prosio.c index 083f63597..0282bbec7 100644 --- a/v7/src/microcode/prosio.c +++ b/v7/src/microcode/prosio.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: prosio.c,v 1.28 2007/04/22 16:31:23 cph Exp $ +$Id: prosio.c,v 1.29 2008/01/03 00:30:42 cph Exp $ Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, @@ -36,17 +36,12 @@ USA. #endif Tchannel -arg_to_channel (SCHEME_OBJECT argument, - int arg_number) +arg_to_channel (SCHEME_OBJECT argument, int arg_number) { - if (! ((INTEGER_P (argument)) && (integer_to_ulong_p (argument)))) + unsigned long channel = (arg_ulong_integer (arg_number)); + if (! (channel < OS_channel_table_size)) error_wrong_type_arg (arg_number); - { - unsigned long channel = (integer_to_ulong (argument)); - if (! (channel < OS_channel_table_size)) - error_wrong_type_arg (arg_number); - return (channel); - } + return (channel); } Tchannel diff --git a/v7/src/microcode/prosproc.c b/v7/src/microcode/prosproc.c index cc7fb989e..600e27b6d 100644 --- a/v7/src/microcode/prosproc.c +++ b/v7/src/microcode/prosproc.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: prosproc.c,v 1.25 2007/04/22 16:31:23 cph Exp $ +$Id: prosproc.c,v 1.26 2008/01/03 00:30:43 cph Exp $ Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, @@ -41,9 +41,8 @@ extern Tchannel arg_channel (int); static Tprocess arg_process (int argument_number) { - Tprocess process - = (arg_index_integer (argument_number, OS_process_table_size)); - if (! (OS_process_valid_p (process))) + Tprocess process = (arg_ulong_integer (argument_number)); + if (!OS_process_valid_p (process)) error_bad_range_arg (argument_number); return (process); } @@ -86,15 +85,15 @@ DEFINE_PRIMITIVE ("PROCESS-TABLE", Prim_process_table, 0, 0, obstack_grow ((&scratch_obstack), (&process), (sizeof (Tprocess))); } { - unsigned int n_processes = - ((obstack_object_size ((&scratch_obstack))) / (sizeof (Tprocess))); + unsigned int n_processes + = ((obstack_object_size ((&scratch_obstack))) / (sizeof (Tprocess))); if (n_processes == 0) PRIMITIVE_RETURN (SHARP_F); { Tprocess * processes = (obstack_finish (&scratch_obstack)); Tprocess * scan_processes = processes; - SCHEME_OBJECT vector = - (allocate_marked_vector (TC_VECTOR, n_processes, 1)); + SCHEME_OBJECT vector + = (allocate_marked_vector (TC_VECTOR, n_processes, 1)); SCHEME_OBJECT * scan_vector = (VECTOR_LOC (vector, 0)); SCHEME_OBJECT * end_vector = (scan_vector + n_processes); while (scan_vector < end_vector) diff --git a/v7/src/microcode/ux.h b/v7/src/microcode/ux.h index 041533e33..3bec2bca9 100644 --- a/v7/src/microcode/ux.h +++ b/v7/src/microcode/ux.h @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: ux.h,v 1.86 2007/09/04 03:35:19 riastradh Exp $ +$Id: ux.h,v 1.87 2008/01/03 00:30:44 cph Exp $ Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, @@ -808,7 +808,6 @@ extern int UX_sigsuspend (const sigset_t *); extern cc_t UX_PC_VDISABLE (int fildes); extern clock_t UX_SC_CLK_TCK (void); # define UX_SC_OPEN_MAX() ((size_t) (sysconf (_SC_OPEN_MAX))) -# define UX_SC_CHILD_MAX() ((size_t) (sysconf (_SC_CHILD_MAX))) # ifdef _POSIX_JOB_CONTROL # define UX_SC_JOB_CONTROL() 1 @@ -830,12 +829,6 @@ extern int UX_sigsuspend (const sigset_t *); # endif # endif -# ifdef CHILD_MAX -# define UX_SC_CHILD_MAX() CHILD_MAX -# else -# define UX_SC_CHILD_MAX() 6 -# endif - # ifdef CLK_TCK # define UX_SC_CLK_TCK() CLK_TCK # else diff --git a/v7/src/microcode/uxio.c b/v7/src/microcode/uxio.c index 1047a9736..de19071a1 100644 --- a/v7/src/microcode/uxio.c +++ b/v7/src/microcode/uxio.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: uxio.c,v 1.57 2007/04/22 16:31:23 cph Exp $ +$Id: uxio.c,v 1.58 2008/01/03 00:30:45 cph Exp $ Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, @@ -54,9 +54,9 @@ UX_channel_close_all (void) void UX_initialize_channels (void) { - OS_channel_table_size = (UX_SC_OPEN_MAX ()); - channel_table = - (UX_malloc (OS_channel_table_size * (sizeof (struct channel)))); + OS_channel_table_size = 64; + channel_table + = (UX_malloc (OS_channel_table_size * (sizeof (struct channel)))); if (channel_table == 0) { fprintf (stderr, "\nUnable to allocate channel table.\n"); @@ -89,15 +89,26 @@ UX_reset_channels (void) Tchannel channel_allocate (void) { - Tchannel channel = 0; - while (1) - { - if (channel == OS_channel_table_size) + Tchannel channel; + for (channel = 0; (channel < OS_channel_table_size); channel += 1) + if (CHANNEL_CLOSED_P (channel)) + return (channel); + { + size_t old_size = OS_channel_table_size; + size_t new_size = ((old_size * 5) / 4); + struct channel * new_table + = (UX_realloc (channel_table, (new_size * (sizeof (struct channel))))); + if (new_table == 0) + { error_out_of_channels (); - if (CHANNEL_CLOSED_P (channel)) - return (channel); - channel += 1; - } + return (NO_CHANNEL); + } + OS_channel_table_size = new_size; + channel_table = new_table; + for (channel = old_size; (channel < new_size); channel += 1) + MARK_CHANNEL_CLOSED (channel); + return (old_size); + } } int diff --git a/v7/src/microcode/uxproc.c b/v7/src/microcode/uxproc.c index 191210fda..abe4f2dd8 100644 --- a/v7/src/microcode/uxproc.c +++ b/v7/src/microcode/uxproc.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: uxproc.c,v 1.35 2007/04/22 16:31:23 cph Exp $ +$Id: uxproc.c,v 1.36 2008/01/03 00:30:46 cph Exp $ Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, @@ -151,9 +151,9 @@ block_sigchld (void) void UX_initialize_processes (void) { - OS_process_table_size = (UX_SC_CHILD_MAX ()); - process_table = - (UX_malloc (OS_process_table_size * (sizeof (struct process)))); + OS_process_table_size = 64; + process_table + = (UX_malloc (OS_process_table_size * (sizeof (struct process)))); if (process_table == 0) { fprintf (stderr, "\nUnable to allocate process table.\n"); @@ -210,14 +210,32 @@ process_allocate (void) for (process = 0; (process < OS_process_table_size); process += 1) if ((PROCESS_RAW_STATUS (process)) == process_status_free) { - Tprocess * pp = (dstack_alloc (sizeof (Tprocess))); - (*pp) = process; - transaction_record_action (tat_abort, process_allocate_abort, pp); (PROCESS_RAW_STATUS (process)) = process_status_allocated; - return (process); + break; } - error_out_of_processes (); - return (NO_PROCESS); + if (process == OS_process_table_size) + { + size_t old_size = OS_process_table_size; + size_t new_size = ((old_size * 5) / 4); + struct process * new_table + = (UX_realloc (process_table, (new_size * (sizeof (struct process))))); + if (new_table == 0) + { + error_out_of_processes (); + return (NO_PROCESS); + } + OS_process_table_size = new_size; + process_table = new_table; + for (process = old_size; (process < new_size); process += 1) + OS_process_deallocate (process); + process = old_size; + } + { + Tprocess * pp = (dstack_alloc (sizeof (Tprocess))); + (*pp) = process; + transaction_record_action (tat_abort, process_allocate_abort, pp); + } + return (process); } void @@ -386,20 +404,17 @@ OS_make_subprocess (const char * filename, } { /* Close all other file descriptors. */ - int fd = 0; int open_max = (UX_SC_OPEN_MAX ()); - while (fd < open_max) - { - if ((fd == STDIN_FILENO) - ? (channel_in_type == process_channel_type_none) - : (fd == STDOUT_FILENO) - ? (channel_out_type == process_channel_type_none) - : (fd == STDERR_FILENO) - ? (channel_err_type == process_channel_type_none) - : 1) - UX_close (fd); - fd += 1; - } + int fd; + for (fd = 0; (fd < open_max); fd += 1) + if ((fd == STDIN_FILENO) + ? (channel_in_type == process_channel_type_none) + : (fd == STDOUT_FILENO) + ? (channel_out_type == process_channel_type_none) + : (fd == STDERR_FILENO) + ? (channel_err_type == process_channel_type_none) + : 1) + UX_close (fd); } /* Put the signal mask and handlers in a normal state. */ @@ -427,6 +442,8 @@ DEFUN_PROCESS_ACCESSOR int OS_process_valid_p (Tprocess process) { + if (process > OS_process_table_size) + return (0); switch (PROCESS_RAW_STATUS (process)) { case process_status_exited: diff --git a/v7/src/microcode/uxterm.c b/v7/src/microcode/uxterm.c index df4043a6a..00ff4be41 100644 --- a/v7/src/microcode/uxterm.c +++ b/v7/src/microcode/uxterm.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: uxterm.c,v 1.34 2007/04/22 16:31:23 cph Exp $ +$Id: uxterm.c,v 1.35 2008/01/03 00:30:47 cph Exp $ Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, @@ -59,6 +59,7 @@ struct terminal_state Ttty_state state; }; +static size_t terminal_table_size; static struct terminal_state * terminal_table; #define TERMINAL_BUFFER(channel) ((terminal_table[(channel)]) . buffer) #define TERMINAL_ORIGINAL_STATE(channel) ((terminal_table[(channel)]) . state) @@ -66,8 +67,9 @@ static struct terminal_state * terminal_table; void UX_initialize_terminals (void) { - terminal_table = - (UX_malloc (OS_channel_table_size * (sizeof (struct terminal_state)))); + terminal_table_size = OS_channel_table_size; + terminal_table + = (UX_malloc (terminal_table_size * (sizeof (struct terminal_state)))); if (terminal_table == 0) { fprintf (stderr, "\nUnable to allocate terminal table.\n"); @@ -81,12 +83,21 @@ UX_reset_terminals (void) { UX_free (terminal_table); terminal_table = 0; + terminal_table_size = 0; } /* This is called from the file-opening code. */ void terminal_open (Tchannel channel) { + if (terminal_table_size != OS_channel_table_size) + { + terminal_table_size = OS_channel_table_size; + terminal_table + = (OS_realloc (terminal_table, + (terminal_table_size + * (sizeof (struct terminal_state))))); + } (TERMINAL_BUFFER (channel)) = (-1); get_terminal_state (channel, (& (TERMINAL_ORIGINAL_STATE (channel)))); } -- 2.25.1