Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 7 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jul 20, 2012
@ivmai Code refactoring of GC_x_printf (move shared code to macro)
* misc.c (GC_PRINTF_IMPL): New macro (used by GC_[err/log_]printf
only); copy code from GC_err_printf parametrizing output stream.
* misc.c (GC_printf, GC_err_printf, GC_log_printf): Replace shared
code with GC_PRINTF_IMPL macro usage.
035af26
@ivmai Disable find-leak GC_gcollect on GC abnormal EXIT
* misc.c (GC_exit_check): Test GC_find_leak, if it is switched off
then do not invoke GC_gcollect.
* misc.c (GC_default_on_abort): Explicitly turn off GC_find_leak to
prevent redundant garbage collection on EXIT (caused by some GC
failure).
2a95ef5
@ivmai Include pthread_np.h from pthread_stop_world.c on OpenBSD
* pthread_stop_world.c: Include pthread_np.h if GC_OPENBSD_THREADS to
get pthread_suspend_np() and pthread_resume_np() declarations.
67932bb
@ivmai Use memcpy (BCOPY) instead of strcpy (to suppress GCC warning)
* cord/cordbscs.c (CORD_from_fn, CORD_substr_checked): Use memcpy
instead of strcpy (to suppress GCC/ld warning
"strcpy() is almost always misused").
* dbg_mlc.c (GC_debug_strdup): Replace strcpy and memcpy with
cross-platform BCOPY (with reversed order of "src" and "dest"
arguments).
* mallocx.c (GC_strdup): Likewise.
* misc.c (GC_envfile_init, GC_CreateLogFile): Likewise.
* os_dep.c (GC_print_callers): Likewise.
943c0eb
@ivmai Suppress GCC warning in GC_get_main_stack_base (OpenBSD)
* include/private/gcconfig.h (STACKBOTTOM): Cast USRSTACK to ptr_t if
OPENBSD (to suppress GCC warning in GC_get_main_stack_base).
c640c6f
@ivmai Replace sprintf with defensive snprintf
* dyn_load.c (GC_register_dynamic_libraries): Replace sprintf with
snprintf add put traling '\0' to buffer to prevent buffer overrun;
cast snprintf result to void (to suppress compiler warning).
* cord/tests/cordtest.c (test_printf): Likewise.
* os_dep.c (GC_print_callers): Likewise.
* os_dep.c (GC_dirty_init): Likewise (if PROC_VDB).
* dyn_load.c (GC_dyld_image_add, GC_dyld_image_remove): Put trailing
'\0' to buffer and cast snprintf result to void (Darwin).
edfd845
@ivmai Test SP obtained from OpenBSD-specific pthread context in GC_suspend_all
* pthread_stop_world.c (GC_suspend_all): Abort if stop_info.stack_ptr
obtained from OpenBSD-specific pthread context is NULL; add FIXME for
GC_OPENBSD_THREADS case (the implementation should probably use
pthread_sp_np).
f8d4f15
Showing with 79 additions and 80 deletions.
  1. +4 −4 cord/cordbscs.c
  2. +3 −1 cord/tests/cordtest.c
  3. +1 −6 dbg_mlc.c
  4. +6 −3 dyn_load.c
  5. +9 −9 include/private/gcconfig.h
  6. +1 −6 mallocx.c
  7. +28 −37 misc.c
  8. +17 −10 os_dep.c
  9. +10 −4 pthread_stop_world.c
View
8 cord/cordbscs.c
@@ -290,10 +290,10 @@ CORD CORD_from_fn(CORD_fn fn, void * client_data, size_t len)
if (c == '\0') goto gen_case;
buf[i] = c;
}
- buf[i] = '\0';
+
result = GC_MALLOC_ATOMIC(len+1);
if (result == 0) OUT_OF_MEMORY;
- strcpy(result, buf);
+ memcpy(result, buf, len);
result[len] = '\0';
return((CORD) result);
}
@@ -440,10 +440,10 @@ CORD CORD_substr_checked(CORD x, size_t i, size_t n)
}
*p++ = c;
}
- *p = '\0';
result = GC_MALLOC_ATOMIC(n+1);
if (result == 0) OUT_OF_MEMORY;
- strcpy(result, buf);
+ memcpy(result, buf, n);
+ result[n] = '\0';
return(result);
}
}
View
4 cord/tests/cordtest.c
@@ -221,7 +221,9 @@ void test_printf(void)
x = CORD_cat(x,x);
if (CORD_sprintf(&result, "->%-120.78r!\n", x) != 124)
ABORT("CORD_sprintf failed 3");
- (void) sprintf(result2, "->%-120.78s!\n", CORD_to_char_star(x));
+ (void)snprintf(result2, sizeof(result2), "->%-120.78s!\n",
+ CORD_to_char_star(x));
+ result2[sizeof(result2) - 1] = '\0';
if (CORD_cmp(result, result2) != 0)ABORT("CORD_sprintf goofed 5");
}
View
7 dbg_mlc.c
@@ -662,12 +662,7 @@ GC_API char * GC_CALL GC_debug_strdup(const char *str, GC_EXTRA_PARAMS)
# endif
return NULL;
}
-# ifndef MSWINCE
- strcpy(copy, str);
-# else
- /* strcpy() is deprecated in WinCE */
- memcpy(copy, str, lb);
-# endif
+ BCOPY(str, copy, lb);
return copy;
}
View
9 dyn_load.c
@@ -741,7 +741,8 @@ GC_INNER void GC_register_dynamic_libraries(void)
# endif /* SOLARISDL */
if (fd < 0) {
- sprintf(buf, "/proc/%ld", (long)getpid());
+ (void)snprintf(buf, sizeof(buf), "/proc/%ld", (long)getpid());
+ buf[sizeof(buf) - 1] = '\0';
/* The above generates a lint complaint, since pid_t varies. */
/* It's unclear how to improve this. */
fd = open(buf, O_RDONLY);
@@ -1279,7 +1280,8 @@ STATIC void GC_dyld_image_add(const struct GC_MACH_HEADER *hdr,
fmt = GC_dyld_add_sect_fmts[j];
/* Add our manufactured aligned BSS sections. */
for (i = 0; i <= L2_MAX_OFILE_ALIGNMENT; i++) {
- snprintf(secnam, sizeof(secnam), fmt, (unsigned)i);
+ (void)snprintf(secnam, sizeof(secnam), fmt, (unsigned)i);
+ secnam[sizeof(secnam) - 1] = '\0';
sec = GC_GETSECTBYNAME(hdr, SEG_DATA, secnam);
if (sec == NULL || sec->size == 0)
continue;
@@ -1330,7 +1332,8 @@ STATIC void GC_dyld_image_remove(const struct GC_MACH_HEADER *hdr,
for (j = 0; j < sizeof(GC_dyld_add_sect_fmts) / sizeof(char *); j++) {
fmt = GC_dyld_add_sect_fmts[j];
for (i = 0; i <= L2_MAX_OFILE_ALIGNMENT; i++) {
- snprintf(secnam, sizeof(secnam), fmt, (unsigned)i);
+ (void)snprintf(secnam, sizeof(secnam), fmt, (unsigned)i);
+ secnam[sizeof(secnam) - 1] = '\0';
sec = GC_GETSECTBYNAME(hdr, SEG_DATA, secnam);
if (sec == NULL || sec->size == 0)
continue;
View
18 include/private/gcconfig.h
@@ -878,7 +878,7 @@
# else
# include <sys/param.h>
# include <uvm/uvm_extern.h>
-# define STACKBOTTOM USRSTACK
+# define STACKBOTTOM ((ptr_t) USRSTACK)
# endif
extern int __data_start[];
# define DATASTART ((ptr_t)__data_start)
@@ -1080,7 +1080,7 @@
# else
# include <sys/param.h>
# include <uvm/uvm_extern.h>
-# define STACKBOTTOM USRSTACK
+# define STACKBOTTOM ((ptr_t) USRSTACK)
# endif
extern int __data_start[];
# define DATASTART ((ptr_t)__data_start)
@@ -1357,7 +1357,7 @@
# else
# include <sys/param.h>
# include <uvm/uvm_extern.h>
-# define STACKBOTTOM USRSTACK
+# define STACKBOTTOM ((ptr_t) USRSTACK)
# endif
extern int __data_start[];
# define DATASTART ((ptr_t)__data_start)
@@ -1594,7 +1594,7 @@
# else
# include <sys/param.h>
# include <uvm/uvm_extern.h>
-# define STACKBOTTOM USRSTACK
+# define STACKBOTTOM ((ptr_t) USRSTACK)
# endif
extern int _fdata[];
# define DATASTART ((ptr_t)_fdata)
@@ -1672,7 +1672,7 @@
# else
# include <sys/param.h>
# include <uvm/uvm_extern.h>
-# define STACKBOTTOM USRSTACK
+# define STACKBOTTOM ((ptr_t) USRSTACK)
# endif
extern int __data_start[];
# define DATASTART ((ptr_t)__data_start)
@@ -1704,7 +1704,7 @@
# else
# include <sys/param.h>
# include <uvm/uvm_extern.h>
-# define STACKBOTTOM USRSTACK
+# define STACKBOTTOM ((ptr_t) USRSTACK)
# endif
extern int __data_start[];
# define DATASTART ((ptr_t)__data_start)
@@ -2014,7 +2014,7 @@
# else
# include <sys/param.h>
# include <uvm/uvm_extern.h>
-# define STACKBOTTOM USRSTACK
+# define STACKBOTTOM ((ptr_t) USRSTACK)
# endif
extern int __data_start[];
# define DATASTART ((ptr_t)__data_start)
@@ -2073,7 +2073,7 @@
# else
# include <sys/param.h>
# include <uvm/uvm_extern.h>
-# define STACKBOTTOM USRSTACK
+# define STACKBOTTOM ((ptr_t) USRSTACK)
# endif
extern int __data_start[];
# define DATASTART ((ptr_t)__data_start)
@@ -2139,7 +2139,7 @@
# else
# include <sys/param.h>
# include <uvm/uvm_extern.h>
-# define STACKBOTTOM USRSTACK
+# define STACKBOTTOM ((ptr_t) USRSTACK)
# endif
extern int __data_start[];
# define DATASTART ((ptr_t)__data_start)
View
7 mallocx.c
@@ -568,12 +568,7 @@ GC_API char * GC_CALL GC_strdup(const char *s)
# endif
return NULL;
}
-# ifndef MSWINCE
- strcpy(copy, s);
-# else
- /* strcpy() is deprecated in WinCE */
- memcpy(copy, s, lb);
-# endif
+ BCOPY(s, copy, lb);
return copy;
}
View
65 misc.c
@@ -566,7 +566,7 @@ GC_API void GC_CALL GC_get_heap_usage_safe(GC_word *pheap_size,
if (len > 4 && path[len - 4] == (TCHAR)'.') {
len -= 4; /* strip executable file extension */
}
- memcpy(&path[len], TEXT(".gc.env"), sizeof(TEXT(".gc.env")));
+ BCOPY(TEXT(".gc.env"), &path[len], sizeof(TEXT(".gc.env")));
hFile = CreateFile(path, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL /* lpSecurityAttributes */, OPEN_EXISTING,
@@ -644,7 +644,9 @@ GC_INNER GC_bool GC_is_initialized = FALSE;
STATIC void GC_exit_check(void)
{
- GC_gcollect();
+ if (GC_find_leak) {
+ GC_gcollect();
+ }
}
#if defined(UNIX_LIKE) && !defined(NO_DEBUGGING)
@@ -1216,9 +1218,8 @@ GC_API void GC_CALL GC_enable_incremental(void)
if (len > 4 && pathBuf[len - 4] == (TCHAR)'.') {
len -= 4; /* strip executable file extension */
}
- /* strcat/wcscat() are deprecated on WinCE, so use memcpy() */
- memcpy(&pathBuf[len], TEXT("." GC_LOG_STD_NAME),
- sizeof(TEXT("." GC_LOG_STD_NAME)));
+ BCOPY(TEXT("." GC_LOG_STD_NAME), &pathBuf[len],
+ sizeof(TEXT("." GC_LOG_STD_NAME)));
# endif
}
@@ -1369,52 +1370,40 @@ GC_API void GC_CALL GC_enable_incremental(void)
# define vsnprintf _vsnprintf
# endif
#endif
+
/* A version of printf that is unlikely to call malloc, and is thus safer */
/* to call from the collector in case malloc has been bound to GC_malloc. */
-/* Floating point arguments and formats should be avoided, since fp */
-/* conversion is more likely to allocate. */
+/* Floating point arguments and formats should be avoided, since FP */
+/* conversion is more likely to allocate memory. */
/* Assumes that no more than BUFSZ-1 characters are written at once. */
+#define GC_PRINTF_IMPL(f, f_name, format) { \
+ va_list args; \
+ char buf[BUFSZ + 1]; \
+ va_start(args, format); \
+ buf[BUFSZ] = 0x15; \
+ (void)vsnprintf(buf, BUFSZ, format, args); \
+ va_end(args); \
+ if (buf[BUFSZ] != 0x15) \
+ ABORT("GC_printf clobbered stack"); \
+ if (WRITE(f, buf, strlen(buf)) < 0) \
+ ABORT("write to " f_name " failed"); \
+ }
+
void GC_printf(const char *format, ...)
{
- va_list args;
- char buf[BUFSZ+1];
-
if (GC_quiet) return;
- va_start(args, format);
- buf[BUFSZ] = 0x15;
- (void) vsnprintf(buf, BUFSZ, format, args);
- va_end(args);
- if (buf[BUFSZ] != 0x15) ABORT("GC_printf clobbered stack");
- if (WRITE(GC_stdout, buf, strlen(buf)) < 0)
- ABORT("write to stdout failed");
+
+ GC_PRINTF_IMPL(GC_stdout, "stdout", format);
}
void GC_err_printf(const char *format, ...)
{
- va_list args;
- char buf[BUFSZ+1];
-
- va_start(args, format);
- buf[BUFSZ] = 0x15;
- (void) vsnprintf(buf, BUFSZ, format, args);
- va_end(args);
- if (buf[BUFSZ] != 0x15) ABORT("GC_printf clobbered stack");
- if (WRITE(GC_stderr, buf, strlen(buf)) < 0)
- ABORT("write to stderr failed");
+ GC_PRINTF_IMPL(GC_stderr, "stderr", format);
}
void GC_log_printf(const char *format, ...)
{
- va_list args;
- char buf[BUFSZ+1];
-
- va_start(args, format);
- buf[BUFSZ] = 0x15;
- (void) vsnprintf(buf, BUFSZ, format, args);
- va_end(args);
- if (buf[BUFSZ] != 0x15) ABORT("GC_printf clobbered stack");
- if (WRITE(GC_log, buf, strlen(buf)) < 0)
- ABORT("write to log failed");
+ GC_PRINTF_IMPL(GC_log, "log", format);
}
/* This is equivalent to GC_err_printf("%s",s). */
@@ -1472,6 +1461,8 @@ GC_API GC_warn_proc GC_CALL GC_get_warn_proc(void)
/* and from EXIT() macro (msg is NULL in that case). */
STATIC void GC_CALLBACK GC_default_on_abort(const char *msg)
{
+ GC_find_leak = FALSE; /* disable at-exit GC_gcollect() */
+
if (msg != NULL) {
# if defined(MSWIN32)
# ifndef DONT_USE_USER32_DLL
View
27 os_dep.c
@@ -3671,7 +3671,8 @@ GC_INNER void GC_dirty_init(void)
+ GC_bytes_allocd_before_gc));
}
- sprintf(buf, "/proc/%ld", (long)getpid());
+ (void)snprintf(buf, sizeof(buf), "/proc/%ld", (long)getpid());
+ buf[sizeof(buf) - 1] = '\0';
fd = open(buf, O_RDONLY);
if (fd < 0) {
ABORT("/proc open failed");
@@ -4678,7 +4679,8 @@ GC_INNER void GC_print_callers(struct callinfo info[NFRAMES])
# else
char buf[40];
char *name = buf;
- sprintf(buf, "##PC##= 0x%lx", info[i].ci_pc);
+ (void)snprintf(buf, sizeof(buf), "##PC##= 0x%lx", info[i].ci_pc);
+ buf[sizeof(buf) - 1] = '\0';
# endif
# if defined(LINUX) && !defined(SMALL_CONFIG)
/* Try for a line number. */
@@ -4713,15 +4715,18 @@ GC_INNER void GC_print_callers(struct callinfo info[NFRAMES])
/* Then we use popen to start addr2line -e <exe> <addr> */
/* There are faster ways to do this, but hopefully this */
/* isn't time critical. */
- sprintf(cmd_buf, "/usr/bin/addr2line -f -e %s 0x%lx", exe_name,
- (unsigned long)info[i].ci_pc);
+ (void)snprintf(cmd_buf, sizeof(cmd_buf),
+ "/usr/bin/addr2line -f -e %s 0x%lx",
+ exe_name, (unsigned long)info[i].ci_pc);
+ cmd_buf[sizeof(cmd_buf) - 1] = '\0';
old_preload = GETENV("LD_PRELOAD");
if (0 != old_preload) {
- if (strlen (old_preload) >= PRELOAD_SZ) {
+ size_t old_len = strlen(old_preload);
+ if (old_len >= PRELOAD_SZ) {
will_fail = TRUE;
goto out;
}
- strcpy (preload_buf, old_preload);
+ BCOPY(old_preload, preload_buf, old_len + 1);
unsetenv ("LD_PRELOAD");
}
pipe = popen(cmd_buf, "r");
@@ -4730,8 +4735,8 @@ GC_INNER void GC_print_callers(struct callinfo info[NFRAMES])
WARN("Failed to reset LD_PRELOAD\n", 0);
}
if (pipe == NULL
- || (result_len = fread(result_buf, 1, RESULT_SZ - 1, pipe))
- == 0) {
+ || (result_len = fread(result_buf, 1,
+ RESULT_SZ - 1, pipe)) == 0) {
if (pipe != NULL) pclose(pipe);
will_fail = TRUE;
goto out;
@@ -4757,8 +4762,10 @@ GC_INNER void GC_print_callers(struct callinfo info[NFRAMES])
}
if (result_len < RESULT_SZ - 25) {
/* Add in hex address */
- sprintf(result_buf + result_len, " [0x%lx]",
- (unsigned long)info[i].ci_pc);
+ (void)snprintf(&result_buf[result_len],
+ sizeof(result_buf) - result_len,
+ " [0x%lx]", (unsigned long)info[i].ci_pc);
+ result_buf[sizeof(result_buf) - 1] = '\0';
}
name = result_buf;
pclose(pipe);
View
14 pthread_stop_world.c
@@ -35,7 +35,11 @@ GC_INNER __thread GC_thread GC_nacl_gc_thread_self = NULL;
int GC_nacl_thread_parked[MAX_NACL_GC_THREADS];
int GC_nacl_thread_used[MAX_NACL_GC_THREADS];
-#elif !defined(GC_OPENBSD_THREADS)
+#elif defined(GC_OPENBSD_THREADS)
+
+# include <pthread_np.h>
+
+#else /* !GC_OPENBSD_THREADS && !NACL */
#include <signal.h>
#include <semaphore.h>
@@ -473,13 +477,15 @@ STATIC int GC_suspend_all(void)
# ifdef GC_OPENBSD_THREADS
if (pthread_suspend_np(p -> id) != 0)
ABORT("pthread_suspend_np failed");
- /* This will only work for userland pthreads. It will */
- /* fail badly on rthreads. Perhaps we should consider */
- /* a pthread_sp_np() function that returns the stack */
+ /* FIXME: This will only work for userland pthreads. */
+ /* It will fail badly on rthreads. Perhaps we should */
+ /* consider pthread_sp_np() that returns the stack */
/* pointer for a suspended thread and implement in both */
/* pthreads and rthreads. */
p -> stop_info.stack_ptr =
*(ptr_t *)((char *)p -> id + UTHREAD_SP_OFFSET);
+ if (p -> stop_info.stack_ptr == NULL)
+ ABORT("NULL pointer at pthread_context UTHREAD_SP_OFFSET");
# else
# ifndef PLATFORM_ANDROID
result = pthread_kill(p -> id, GC_sig_suspend);

No commit comments for this range

Something went wrong with that request. Please try again.