Skip to content

Commit

Permalink
i#58 MacOS: syscalls monitored by DR, part 1
Browse files Browse the repository at this point in the history
Progress on syscalls monitored by DR:
+ No SYS_mmap2: just SYS_mmap
+ No SYS_uselib
+ SYS_brk and SYS_mremap are both Linux-only and not present on Mac.
  Thus dr_raw_mremap and dr_raw_brk are both Linux-only as well.

SVN-Revision: 2420
  • Loading branch information
derekbruening committed Dec 5, 2013
1 parent 33bb5db commit 7bb27f2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
30 changes: 23 additions & 7 deletions core/unix/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,12 @@ DECLARE_CXTSWPROT_VAR(static mutex_t client_tls_lock, INIT_LOCK_FREE(client_tls_
static void handle_execve_post(dcontext_t *dcontext);
static bool os_switch_lib_tls(dcontext_t *dcontext, bool to_app);
static bool os_switch_seg_to_context(dcontext_t *dcontext, reg_id_t seg, bool to_app);
#ifdef LINUX
static bool handle_app_mremap(dcontext_t *dcontext, byte *base, size_t size,
byte *old_base, size_t old_size,
uint old_prot, uint old_type);
static void handle_app_brk(dcontext_t *dcontext, byte *old_brk, byte *new_brk);
#endif

/* full path to our own library, used for execve */
static char dynamorio_library_path[MAXIMUM_PATH];
Expand Down Expand Up @@ -2150,7 +2152,7 @@ os_raw_mem_alloc(void *preferred, size_t size, uint prot, uint flags,
return p;
}

#ifdef CLIENT_INTERFACE
#if defined(CLIENT_INTERFACE) && defined(LINUX)
DR_API
/* XXX: could add dr_raw_mem_realloc() instead of dr_raw_mremap() -- though there
* is no realloc for Windows: supposed to reserve yourself and then commit in
Expand Down Expand Up @@ -2198,7 +2200,7 @@ dr_raw_brk(void *new_address)
return res;
}
}
#endif
#endif /* CLIENT_INTERFACE && LINUX */

/* caller is required to handle thread synchronization and to update dynamo vm areas */
void
Expand Down Expand Up @@ -3801,13 +3803,17 @@ ignorable_system_call(int num)
case SYS_exit_group:
#endif
case SYS_exit:
#ifdef LINUX
case SYS_brk:
#endif
case SYS_mmap:
#ifndef X64
#if !defined(X64) && !defined(MACOS)
case SYS_mmap2:
#endif
case SYS_munmap:
#ifdef LINUX
case SYS_mremap:
#endif
case SYS_mprotect:
case SYS_execve:
case SYS_clone:
Expand Down Expand Up @@ -4928,7 +4934,7 @@ pre_system_call(dcontext_t *dcontext)
break;
}
#endif
case IF_X64_ELSE(SYS_mmap,SYS_mmap2): {
case IF_MACOS_ELSE(SYS_mmap,IF_X64_ELSE(SYS_mmap,SYS_mmap2)): {
/* in /usr/src/linux/arch/i386/kernel/sys_i386.c:
asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
unsigned long prot, unsigned long flags,
Expand Down Expand Up @@ -5022,6 +5028,7 @@ pre_system_call(dcontext_t *dcontext)
#endif
break;
}
#ifdef LINUX
case SYS_mremap: {
/* in /usr/src/linux/mm/mmap.c:
asmlinkage unsigned long sys_mremap(unsigned long addr,
Expand Down Expand Up @@ -5056,6 +5063,7 @@ pre_system_call(dcontext_t *dcontext)
});
break;
}
#endif
case SYS_mprotect: {
/* in /usr/src/linux/mm/mprotect.c:
asmlinkage long sys_mprotect(unsigned long start, uint len,
Expand Down Expand Up @@ -5126,6 +5134,7 @@ pre_system_call(dcontext_t *dcontext)
}
break;
}
#ifdef LINUX
case SYS_brk: {
/* i#91/PR 396352: need to watch SYS_brk to maintain all_memory_areas.
* We store the old break in the param1 slot.
Expand All @@ -5143,6 +5152,7 @@ pre_system_call(dcontext_t *dcontext)
ASSERT_NOT_IMPLEMENTED(false);
break;
}
#endif

/****************************************************************************/
/* SPAWNING */
Expand Down Expand Up @@ -5837,6 +5847,7 @@ process_mmap(dcontext_t *dcontext, app_pc base, size_t size, uint prot,
#endif
}

#ifdef LINUX
/* Call right after the system call.
* i#173: old_prot and old_type should be from before the system call
*/
Expand Down Expand Up @@ -5902,6 +5913,7 @@ handle_app_brk(dcontext_t *dcontext, byte *old_brk, byte *new_brk)
}
IF_NO_MEMQUERY(memcache_handle_app_brk(old_brk, new_brk));
}
#endif

/* Returns false if system call should NOT be executed
* Returns true if system call should go ahead
Expand Down Expand Up @@ -6004,7 +6016,7 @@ post_system_call(dcontext_t *dcontext)
}
#endif

#ifndef X64
#if defined(LINUX) && !defined(X64)
case SYS_mmap2:
#endif
case SYS_mmap: {
Expand Down Expand Up @@ -6081,6 +6093,7 @@ post_system_call(dcontext_t *dcontext)
}
break;
}
#ifdef LINUX
case SYS_mremap: {
app_pc old_base = (app_pc) dcontext->sys_param0;
size_t old_size = (size_t) dcontext->sys_param1;
Expand All @@ -6106,6 +6119,7 @@ post_system_call(dcontext_t *dcontext)
goto exit_post_system_call;
break;
}
#endif
case SYS_mprotect: {
base = (app_pc) dcontext->sys_param0;
size = dcontext->sys_param1;
Expand Down Expand Up @@ -6168,6 +6182,7 @@ post_system_call(dcontext_t *dcontext)
}
break;
}
#ifdef LINUX
case SYS_brk: {
/* i#91/PR 396352: need to watch SYS_brk to maintain all_memory_areas.
* This code should work regardless of whether syscall failed
Expand All @@ -6177,18 +6192,19 @@ post_system_call(dcontext_t *dcontext)
app_pc old_brk = (app_pc) dcontext->sys_param1;
app_pc new_brk = (app_pc) result;
DEBUG_DECLARE(app_pc req_brk = (app_pc) dcontext->sys_param0;);
#ifdef DEBUG
# ifdef DEBUG
if (DYNAMO_OPTION(early_inject) &&
req_brk != NULL /* Ignore calls that don't increase brk. */) {
DO_ONCE({
ASSERT_CURIOSITY(new_brk > old_brk && "i#1004: first brk() "
"allocation failed with -early_inject");
});
}
#endif
# endif
handle_app_brk(dcontext, old_brk, new_brk);
break;
}
#endif

/****************************************************************************/
/* SPAWNING -- fork mostly handled above */
Expand Down
4 changes: 3 additions & 1 deletion core/x86/instrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -2032,12 +2032,13 @@ DR_API
bool
dr_raw_mem_free(void *addr, size_t size);

#ifdef UNIX
#ifdef LINUX
DR_API
/**
* Calls mremap with the specified parameters and returns the result.
* The old memory must be non-DR memory, and the new memory is also
* considered to be non-DR memory (see #DR_ALLOC_NON_DR).
* \note Linux-only.
*/
void *
dr_raw_mremap(void *old_address, size_t old_size, size_t new_size,
Expand All @@ -2049,6 +2050,7 @@ DR_API
* system call and returns the result. This is the application's
* program break, so use this system call only when deliberately
* changing the application's behavior.
* \note Linux-only.
*/
void *
dr_raw_brk(void *new_address);
Expand Down
4 changes: 4 additions & 0 deletions suite/tests/client-interface/alloc.dll.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,18 +451,22 @@ void custom_unix_test(void)
dr_fprintf(STDERR, "error: unable to mmap\n");
write_array(array);

# ifdef LINUX
array = dr_raw_mremap(array, PAGE_SIZE, PAGE_SIZE*2, MREMAP_MAYMOVE, NULL);
if ((ptr_int_t)array <= 0 && (ptr_int_t)array >= -PAGE_SIZE)
dr_fprintf(STDERR, "error: unable to mremap\n");
write_array(array);
# endif

ok = dr_raw_mem_free(array, PAGE_SIZE*2);
if (!ok)
dr_fprintf(STDERR, "error: failed to munmap\n");

# ifdef LINUX
array = dr_raw_brk(0);
if (array == NULL)
dr_fprintf(STDERR, "error: unable to query brk\n");
# endif

dr_fprintf(STDERR, "success\n");
}
Expand Down

0 comments on commit 7bb27f2

Please sign in to comment.