Skip to content

Commit

Permalink
c18n: Rename and generalise c18n_return_address
Browse files Browse the repository at this point in the history
Turn all use sites of __builtin_return_address(0) to use the newly
defined rtld_get_return_address macro, which is overridden to take into
account the existence of trampolines when c18n is enabled.
  • Loading branch information
dpgao committed Dec 11, 2024
1 parent 4fb798d commit bfdd89f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 37 deletions.
38 changes: 7 additions & 31 deletions libexec/rtld-elf/rtld.c
Original file line number Diff line number Diff line change
Expand Up @@ -4482,15 +4482,8 @@ do_dlsym(void *handle, const char *name, void *retaddr, const Ver_Entry *ve,
void *
dlsym(void *handle, const char *name)
{
void *retaddr;

#ifdef CHERI_LIB_C18N
retaddr = c18n_return_address();
#else
retaddr = __builtin_return_address(0);
#endif

return (do_dlsym(handle, name, retaddr, NULL, SYMLOOK_DLSYM));
return (do_dlsym(handle, name, rtld_get_return_address(), NULL,
SYMLOOK_DLSYM));
}

dlfunc_t
Expand All @@ -4500,36 +4493,23 @@ dlfunc(void *handle, const char *name)
void *d;
dlfunc_t f;
} rv;
void *retaddr;

#ifdef CHERI_LIB_C18N
retaddr = c18n_return_address();
#else
retaddr = __builtin_return_address(0);
#endif

rv.d = do_dlsym(handle, name, retaddr, NULL, SYMLOOK_DLSYM);
rv.d = do_dlsym(handle, name, rtld_get_return_address(), NULL,
SYMLOOK_DLSYM);
return (rv.f);
}

void *
dlvsym(void *handle, const char *name, const char *version)
{
Ver_Entry ventry;
void *retaddr;

ventry.name = version;
ventry.file = NULL;
ventry.hash = elf_hash(version);
ventry.flags= 0;

#ifdef CHERI_LIB_C18N
retaddr = c18n_return_address();
#else
retaddr = __builtin_return_address(0);
#endif

return (do_dlsym(handle, name, retaddr, &ventry, SYMLOOK_DLSYM));
return (do_dlsym(handle, name, rtld_get_return_address(), &ventry,
SYMLOOK_DLSYM));
}

int
Expand Down Expand Up @@ -4635,11 +4615,7 @@ dlinfo(void *handle, int request, void *p)
if (handle == NULL || handle == RTLD_SELF) {
void *retaddr;

#ifdef CHERI_LIB_C18N
retaddr = c18n_return_address();
#else
retaddr = __builtin_return_address(0); /* __GNUC__ only */
#endif
retaddr = rtld_get_return_address();
if ((obj = obj_from_addr(retaddr)) == NULL)
_rtld_error("Cannot determine caller's shared object");
} else
Expand Down
2 changes: 2 additions & 0 deletions libexec/rtld-elf/rtld.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ __BEGIN_DECLS
typeof (Y) y_ = (Y); \
(x_ > y_) ? x_ : y_; })

#define rtld_get_return_address() __builtin_return_address(0)

#define NEW(type) ((type *) xmalloc(sizeof(type)))
#define CNEW(type) ((type *) xcalloc(1, sizeof(type)))

Expand Down
19 changes: 13 additions & 6 deletions libexec/rtld-elf/rtld_c18n.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ pop_dummy_rtld_trusted_frame(struct trusted_frame *tf)
/*
* Stack unwinding
*/
#ifdef rtld_get_return_address
#undef rtld_get_return_address
#define rtld_get_return_address() ({ \
void *__retaddr = __builtin_return_address(0); \
if (C18N_ENABLED) { \
struct trusted_frame *__tf = get_trusted_stk(); \
if (c18n_is_tramp((uintptr_t)__retaddr, __tf)) \
__retaddr = __tf->state.pc; \
} \
__retaddr; \
})
#endif

int c18n_is_tramp(uintptr_t, const struct trusted_frame *);

/*
Expand Down Expand Up @@ -263,12 +276,6 @@ func_sig_legal(struct func_sig sig)
/*
* APIs
*/
/*
* This macro can only be used in a function directly invoked by a trampoline.
*/
#define c18n_return_address() (C18N_ENABLED ? \
get_trusted_stk()->state.pc : __builtin_return_address(0))

void *_rtld_sandbox_code(void *, struct func_sig);
void *_rtld_safebox_code(void *, struct func_sig);

Expand Down

0 comments on commit bfdd89f

Please sign in to comment.