From nobody@FreeBSD.org  Fri Feb  3 02:09:11 2012
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 45BCA1065673
	for <freebsd-gnats-submit@FreeBSD.org>; Fri,  3 Feb 2012 02:09:11 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id 29DD38FC1B
	for <freebsd-gnats-submit@FreeBSD.org>; Fri,  3 Feb 2012 02:09:11 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id q1329AOF031537
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 3 Feb 2012 02:09:10 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id q1329AZC031536;
	Fri, 3 Feb 2012 02:09:10 GMT
	(envelope-from nobody)
Message-Id: <201202030209.q1329AZC031536@red.freebsd.org>
Date: Fri, 3 Feb 2012 02:09:10 GMT
From: Pedro Giffuni <giffunip@tutopia.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: Signal bug in Dtrace
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         164724
>Category:       kern
>Synopsis:       [dtrace] [patch] Signal bug in Dtrace
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    gnn
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Fri Feb 03 02:10:12 UTC 2012
>Closed-Date:    Mon Jan 13 19:20:03 UTC 2014
>Last-Modified:  Mon Jan 13 19:20:03 UTC 2014
>Originator:     Pedro Giffuni
>Release:        9.0-release
>Organization:
>Environment:
FreeBSD pcbsd-8714 9.0-RELEASE FreeBSD 9.0-RELEASE #3: Tue Dec 27 14:14:29 PST 2011     root@build9x64.pcbsd.org:/usr/obj/builds/amd64/pcbsd-build90/fbsd-source/9.0/sys/GENERIC  amd64
>Description:
Last year Bryan Cantrill found a nasty bug in Dtrace:

http://dtrace.org/blogs/bmc/2011/03/09/when-magic-collides/

He warns "you are not expected to understand this", and not
really being used to Dtrace I haven't really reproduced it.

The fix, however, was relatively easy so I adapted the patch
here:

http://dtrace.org/resources/bmc/dtrace-signal.patch

to work on FreeBSD's port.
>How-To-Repeat:

>Fix:
Patch attached

Patch attached with submission follows:

Index: cddl/dev/dtrace/i386/dtrace_subr.c
===================================================================
--- cddl/dev/dtrace/i386/dtrace_subr.c	(revision 230923)
+++ cddl/dev/dtrace/i386/dtrace_subr.c	(working copy)
@@ -27,6 +27,10 @@
  * Use is subject to license terms.
  */
 
+/*
+ * Copyright (c) 2011, Joyent, Inc. All rights reserved.
+ */
+
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/types.h>
@@ -298,14 +302,15 @@
 	}
 
 	/*
-	 * If we've executed the original instruction, but haven't performed
-	 * the jmp back to t->t_dtrace_npc or the clean up of any registers
-	 * used to emulate %rip-relative instructions in 64-bit mode, do that
-	 * here and take the signal right away. We detect this condition by
-	 * seeing if the program counter is the range [scrpc + isz, astpc).
+	 * If we have executed the original instruction, but we have performed
+	 * neither the jmp back to t->t_dtrace_npc nor the clean up of any
+	 * registers used to emulate %rip-relative instructions in 64-bit mode,
+	 * we'll save ourselves some effort by doing that here and taking the
+	 * signal right away.  We detect this condition by seeing if the program
+	 * counter is the range [scrpc + isz, astpc).
 	 */
-	if (t->t_dtrace_astpc - rp->r_pc <
-	    t->t_dtrace_astpc - t->t_dtrace_scrpc - isz) {
+	if (rp->r_pc >= t->t_dtrace_scrpc + isz &&
+	    rp->r_pc < t->t_dtrace_astpc) {
 #ifdef __amd64
 		/*
 		 * If there is a scratch register and we're on the
Index: cddl/dev/dtrace/amd64/dtrace_subr.c
===================================================================
--- cddl/dev/dtrace/amd64/dtrace_subr.c	(revision 230923)
+++ cddl/dev/dtrace/amd64/dtrace_subr.c	(working copy)
@@ -27,6 +27,10 @@
  * Use is subject to license terms.
  */
 
+/*
+ * Copyright (c) 2011, Joyent, Inc. All rights reserved.
+ */
+
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/types.h>
@@ -297,14 +301,15 @@
 	}
 
 	/*
-	 * If we've executed the original instruction, but haven't performed
-	 * the jmp back to t->t_dtrace_npc or the clean up of any registers
-	 * used to emulate %rip-relative instructions in 64-bit mode, do that
-	 * here and take the signal right away. We detect this condition by
-	 * seeing if the program counter is the range [scrpc + isz, astpc).
+	 * If we have executed the original instruction, but we have performed
+	 * neither the jmp back to t->t_dtrace_npc nor the clean up of any
+	 * registers used to emulate %rip-relative instructions in 64-bit mode,
+	 * we'll save ourselves some effort by doing that here and taking the
+	 * signal right away.  We detect this condition by seeing if the program
+	 * counter is the range [scrpc + isz, astpc).
 	 */
-	if (t->t_dtrace_astpc - rp->r_pc <
-	    t->t_dtrace_astpc - t->t_dtrace_scrpc - isz) {
+	if (rp->r_pc >= t->t_dtrace_scrpc + isz &&
+	    rp->r_pc < t->t_dtrace_astpc) {
 #ifdef __amd64
 		/*
 		 * If there is a scratch register and we're on the


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->gnn 
Responsible-Changed-By: gnn 
Responsible-Changed-When: Fri Apr 13 20:27:28 UTC 2012 
Responsible-Changed-Why:  
Take this bug and try the fix. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=164724 
State-Changed-From-To: open->patched 
State-Changed-By: gnn 
State-Changed-When: Mon Jun 4 16:28:24 UTC 2012 
State-Changed-Why:  
Patched in: http://svn.freebsd.org/changeset/base/236567 

http://www.freebsd.org/cgi/query-pr.cgi?pr=164724 
State-Changed-From-To: patched->closed 
State-Changed-By: gnn 
State-Changed-When: Mon Jan 13 19:19:25 UTC 2014 
State-Changed-Why:  
Bug fixed and verified. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=164724 
>Unformatted:
