[HB] Improve debug output and fix mprotect bug

This commit is contained in:
Behdad Esfahbod 2009-08-05 15:20:34 -04:00
parent ec9f8db0d3
commit 7acb389569
3 changed files with 64 additions and 24 deletions

View File

@ -173,6 +173,11 @@ hb_blob_lock (hb_blob_t *blob)
if (!HB_OBJECT_IS_INERT (blob))
(void) _hb_reference_count_inc (blob->lock);
#if HB_DEBUG
fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
HB_REFERENCE_COUNT_GET_VALUE (blob->lock), blob->data);
#endif
return blob->data;
}
@ -183,6 +188,11 @@ hb_blob_unlock (hb_blob_t *blob)
int old_lock = _hb_reference_count_inc (blob->lock);
assert (old_lock > 0);
}
#if HB_DEBUG
fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
HB_REFERENCE_COUNT_GET_VALUE (blob->lock), blob->data);
#endif
}
hb_bool_t
@ -195,20 +205,44 @@ hb_bool_t
hb_blob_try_writeable_inplace (hb_blob_t *blob)
{
if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITEABLE) {
int pagesize;
unsigned int length;
unsigned int pagesize, mask, length;
const char *addr;
pagesize = sysconf(_SC_PAGE_SIZE);
if (-1 == pagesize)
#if HB_DEBUG
fprintf (stderr, "%p %s: making writeable\n", blob, __FUNCTION__);
#endif
pagesize = (unsigned int) sysconf(_SC_PAGE_SIZE);
if ((unsigned int) -1 == pagesize) {
#if HB_DEBUG
fprintf (stderr, "%p %s: %s\n", blob, __FUNCTION__, strerror (errno));
#endif
return FALSE;
}
#if HB_DEBUG
fprintf (stderr, "%p %s: pagesize is %u\n", blob, __FUNCTION__, pagesize);
#endif
addr = (const char *) (((size_t) blob->data) & pagesize);
length = (const char *) (((size_t) blob->data + blob->length + pagesize-1) & pagesize) - addr;
if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE))
mask = ~(pagesize-1);
addr = (const char *) (((size_t) blob->data) & mask);
length = (const char *) (((size_t) blob->data + blob->length + pagesize-1) & mask) - addr;
#if HB_DEBUG
fprintf (stderr, "%p %s: calling mprotect on [%p..%p] (%d bytes)\n",
blob, __FUNCTION__,
addr, addr+length, length);
#endif
if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
#if HB_DEBUG
fprintf (stderr, "%p %s: %s\n", blob, __FUNCTION__, strerror (errno));
#endif
return FALSE;
}
blob->mode = HB_MEMORY_MODE_WRITEABLE;
#if HB_DEBUG
fprintf (stderr, "%p %s: successfully made [%p..%p] (%d bytes) writeable\n",
blob, __FUNCTION__,
addr, addr+length, length);
#endif
}
return blob->mode == HB_MEMORY_MODE_WRITEABLE;
@ -224,11 +258,18 @@ hb_blob_try_writeable (hb_blob_t *blob)
{
char *new_data;
#if HB_DEBUG
fprintf (stderr, "%p %s (%d) -> %p\n", blob, __FUNCTION__,
HB_REFERENCE_COUNT_GET_VALUE (blob->lock), blob->data);
#endif
if (HB_REFERENCE_COUNT_HAS_REFERENCE (blob->lock))
return FALSE;
new_data = malloc (blob->length);
if (new_data) {
#if HB_DEBUG
fprintf (stderr, "%p %s: dupped successfully -> %p\n", blob, __FUNCTION__, blob->data);
#endif
memcpy (new_data, blob->data, blob->length);
blob->data = new_data;
blob->mode = HB_MEMORY_MODE_WRITEABLE;

View File

@ -203,16 +203,16 @@ struct Null <Type> \
* Sanitize
*/
#if HB_DEBUG
#if HB_DEBUG >= 5
#define SANITIZE_DEBUG_ARG_DEF , unsigned int sanitize_depth
#define SANITIZE_DEBUG_ARG , sanitize_depth + 1
#define SANITIZE_DEBUG_ARG_INIT , 0
#define SANITIZE_DEBUG() \
HB_STMT_START { \
printf ("SANITIZE(%p) %-*d-> %s\n", \
(CONST_CHARP (this) == NullPool) ? 0 : this, \
sanitize_depth+1, sanitize_depth, \
__PRETTY_FUNCTION__); \
fprintf (stderr, "SANITIZE(%p) %-*d-> %s\n", \
(CONST_CHARP (this) == NullPool) ? 0 : this, \
sanitize_depth+1, sanitize_depth, \
__PRETTY_FUNCTION__); \
} HB_STMT_END
#else
#define SANITIZE_DEBUG_ARG_DEF
@ -239,9 +239,8 @@ _hb_sanitize_init (hb_sanitize_context_t *context,
context->edit_count = 0;
#if HB_DEBUG
printf ("sanitize %p init [%p..%p] (%u bytes)\n",
context->blob, context->start, context->end, context->start - context->end);
fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
context->blob, context->start, context->end, context->start - context->end);
#endif
}
@ -250,9 +249,8 @@ _hb_sanitize_fini (hb_sanitize_context_t *context,
bool unlock)
{
#if HB_DEBUG
printf ("sanitize %p fini [%p..%p] %u edit requests\n",
context->blob, context->start, context->end, context->edit_count);
fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
context->blob, context->start, context->end, context->edit_count);
#endif
if (unlock)
@ -268,8 +266,8 @@ _hb_sanitize_edit (hb_sanitize_context_t *context,
context->edit_count++;
#if HB_DEBUG
printf ("sanitize %p edit %u requested for [%p..%p] (%d bytes) in [%p..%p] -> %s\n",
context->blob,
fprintf (stderr, "sanitize %p edit %u requested for [%p..%p] (%d bytes) in [%p..%p] -> %s\n",
context->blob,
context->edit_count,
base, base+len, len,
context->start, context->end,
@ -316,7 +314,7 @@ struct Sanitizer
retry:
#if HB_DEBUG
printf ("Sanitizer %p start %s\n", blob, __PRETTY_FUNCTION__);
fprintf (stderr, "Sanitizer %p start %s\n", blob, __PRETTY_FUNCTION__);
#endif
_hb_sanitize_init (&context, blob);
@ -340,14 +338,14 @@ struct Sanitizer
if (edit_count && !hb_blob_is_writeable (blob) && hb_blob_try_writeable (blob)) {
/* ok, we made it writeable by relocating. try again */
#if HB_DEBUG
printf ("Sanitizer %p retry %s\n", blob, __PRETTY_FUNCTION__);
fprintf (stderr, "Sanitizer %p retry %s\n", blob, __PRETTY_FUNCTION__);
#endif
goto retry;
}
}
#if HB_DEBUG
printf ("Sanitizer %p %s %s\n", blob, sane ? "passed" : "failed", __PRETTY_FUNCTION__);
fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "failed", __PRETTY_FUNCTION__);
#endif
if (sane)
return blob;

View File

@ -40,7 +40,8 @@
#include <string.h>
#include <assert.h>
#if HB_DEBUG
#include <stdio.h> /* XXX */
#include <stdio.h>
#include <errno.h>
#endif
#include "hb-common.h"