Skip to content

Commit

Permalink
cg: fix return value of copyin()
Browse files Browse the repository at this point in the history
The copyin() function was returning the resolved alloca pointer rather
than the offset into scratchmem (as it should have).  Now that the
function is correctly marked as returning an alloca-pointer, it needs
to return the native representation of an alloca-pointer, i.e. as an
offset into scratchmem.

Signed-off-by: Kris Van Hees <kris.van.hees@oracle.com>
Reviewed-by: Nick Alcock <nick.alcock@oracle.com>
  • Loading branch information
kvanhees committed Feb 20, 2023
1 parent 0903985 commit 267527c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
12 changes: 8 additions & 4 deletions libdtrace/dt_cg.c
Original file line number Diff line number Diff line change
Expand Up @@ -4492,6 +4492,9 @@ dt_cg_subr_copyin(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)

/* Allocate scratch space. */
dt_cg_subr_alloca_impl(dnp, size, dlp, drp);
/* Push the native alloca value to be used as return value. */
dt_cg_push_stack(dnp->dn_reg, dlp, drp);
/* Turn it into a proper alloca pointer. */
dt_cg_alloca_ptr(dlp, drp, dnp->dn_reg, dnp->dn_reg);

dt_cg_node(src, dlp, drp);
Expand All @@ -4506,16 +4509,17 @@ dt_cg_subr_copyin(dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp)
emit(dlp, BPF_CALL_HELPER(dtp->dt_bpfhelper[BPF_FUNC_probe_read_user]));
dt_regset_free_args(drp);
emit(dlp, BPF_BRANCH_IMM(BPF_JEQ, BPF_REG_0, 0, lbl_ok));
dt_regset_free(drp, BPF_REG_0);
dt_cg_probe_error(yypcb, DTRACEFLT_BADADDR, DT_ISREG, src->dn_reg);
dt_regset_free(drp, src->dn_reg);
emitl(dlp, lbl_badsize,
BPF_NOP());
dt_cg_probe_error(yypcb, DTRACEFLT_BADSIZE, DT_ISREG, size->dn_reg);
dt_regset_free(drp, size->dn_reg);
emitl(dlp, lbl_ok,
BPF_NOP());
dt_regset_free(drp, BPF_REG_0);

dt_regset_free(drp, src->dn_reg);
dt_regset_free(drp, size->dn_reg);
/* Pop the native alloca value as our value. */
dt_cg_pop_stack(dnp->dn_reg, dlp, drp);

TRACE_REGSET(" subr-copyin:End ");
}
Expand Down
31 changes: 31 additions & 0 deletions test/unittest/funcs/copyin/tst.copyin-retval-ok.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Oracle Linux DTrace.
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/

/*
* ASSERTION: Ensure the alloca()'d pointer return value of copyin() is valid.
*
* SECTION: Actions and Subroutines/copyin()
*/

#pragma D option quiet
#pragma D option destructive

BEGIN
{
system("echo dtrace-copyin-test");
}

syscall::write:entry
{
((uint8_t *)copyin(arg1, 32))[0];
exit(0);
}

ERROR
{
exit(1);
}

0 comments on commit 267527c

Please sign in to comment.