NetBSD Problem Report #59129
From www@netbsd.org Tue Mar 4 19:10:49 2025
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256
client-signature RSA-PSS (2048 bits) client-digest SHA256)
(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
by mollari.NetBSD.org (Postfix) with ESMTPS id DB0F41A923D
for <gnats-bugs@gnats.NetBSD.org>; Tue, 4 Mar 2025 19:10:49 +0000 (UTC)
Message-Id: <20250304191048.6F7861A923F@mollari.NetBSD.org>
Date: Tue, 4 Mar 2025 19:10:48 +0000 (UTC)
From: campbell+netbsd@mumble.net
Reply-To: campbell+netbsd@mumble.net
To: gnats-bugs@NetBSD.org
Subject: futex(3): missing sign extension in FUTEX_WAKE_OP
X-Send-Pr-Version: www-1.0
>Number: 59129
>Category: kern
>Synopsis: futex(3): missing sign extension in FUTEX_WAKE_OP
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: kern-bug-people
>State: needs-pullups
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Tue Mar 04 19:15:00 +0000 2025
>Closed-Date:
>Last-Modified: Wed Mar 05 14:05:02 +0000 2025
>Originator: Taylor R Campbell
>Release: current, 10
>Organization:
The FutexBSigneD Extension
>Environment:
>Description:
According to the Linux man page at https://www.man7.org/linux/man-pages/man2/futex.2.html, Linux's FUTEX_WAKE_OP performs an operation of the form:
uint32_t oldval = *(uint32_t *) uaddr2;
*(uint32_t *) uaddr2 = oldval op oparg;
futex(uaddr, FUTEX_WAKE, val, 0, 0, 0);
if (oldval cmp cmparg)
futex(uaddr2, FUTEX_WAKE, val2, 0, 0, 0);
The operation is parametrized by a 32-bit word that contains four parts: cmp, cmparg, op, oparg. The layout, again from the man page, is:
+---+---+-----------+-----------+
|op |cmp| oparg | cmparg |
+---+---+-----------+-----------+
4 4 12 12 <== # of bits
Expressed in code, the encoding is:
#define FUTEX_OP(op, oparg, cmp, cmparg) \
(((op & 0xf) << 28) | \
((cmp & 0xf) << 24) | \
((oparg & 0xfff) << 12) | \
(cmparg & 0xfff))
Left unstated in this documentation -- and any other documentation I can find -- is that oparg and cmparg are interpreted by Linux as _signed_ 12-bit quantities. But currently NetBSD's futex implementation interprets them as _unsigned_ 12-bit quantities.
>How-To-Repeat:
#include <sys/syscall.h>
#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#if defined __NetBSD__
#include <sys/futex.h>
static inline int
futex(volatile int *uaddr, int op, int val, const struct timespec *timeout,
volatile int *uaddr2, int val2, int val3)
{
return syscall(SYS___futex, uaddr, op, val, timeout, uaddr2,
val2, val3);
}
#elif defined __linux__
#include <linux/futex.h>
#include <sys/time.h>
#include <assert.h>
static inline int
futex(volatile int *uaddr, int op, int val, const struct timespec *timeout,
volatile int *uaddr2, int val2, int val3)
{
switch (op & FUTEX_CMD_MASK) {
case FUTEX_WAIT:
case FUTEX_LOCK_PI:
case FUTEX_WAIT_BITSET:
case FUTEX_WAIT_REQUEUE_PI:
break;
case FUTEX_WAKE:
case FUTEX_FD:
case FUTEX_REQUEUE:
case FUTEX_CMP_REQUEUE:
case FUTEX_WAKE_OP:
case FUTEX_UNLOCK_PI:
case FUTEX_TRYLOCK_PI:
case FUTEX_WAKE_BITSET:
case FUTEX_CMP_REQUEUE_PI:
assert(timeout == NULL);
timeout = (void *)(intptr_t)val;
break;
}
return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3);
}
#endif
static int
futex_cas11s(volatile int *word, int old, int new)
{
volatile int word1;
return futex(/*uaddr*/&word1, /*op*/FUTEX_WAKE_OP,
/*nr_wake=val*/INT_MAX, /*timeout*/NULL, /*uaddr2*/word,
/*nr_wake2=val2*/INT_MAX,
/*op=val3*/FUTEX_OP(FUTEX_OP_SET, new, FUTEX_OP_CMP_EQ, old));
}
int
main(void)
{
volatile int word = 0;
if (futex_cas11s(&word, 0, 4095) == -1)
err(1, "futex(FUTEX_WAKE_OP)");
printf("%d\n", word);
fflush(stdout);
return ferror(stdout);
}
Linux prints -1; NetBSD>=10 prints 4095.
>Fix:
1. Add some tests for this.
2. Sign-extend in futex_compute_cmp and futex_compute_op.
>Release-Note:
>Audit-Trail:
From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc:
Subject: PR/59129 CVS commit: src/tests/lib/libc/sys
Date: Wed, 5 Mar 2025 00:02:47 +0000
Module Name: src
Committed By: riastradh
Date: Wed Mar 5 00:02:47 UTC 2025
Modified Files:
src/tests/lib/libc/sys: t_futex_ops.c
Log Message:
t_futex_ops: Spruce up diagnostics.
- Use RL to report errno for any syscalls that set it.
- Sprinkle messages to show intermediate quantities.
Preparation for:
PR kern/59129: futex(3): missing sign extension in FUTEX_WAKE_OP
To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/tests/lib/libc/sys/t_futex_ops.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc:
Subject: PR/59129 CVS commit: src/tests/lib/libc/sys
Date: Wed, 5 Mar 2025 00:02:58 +0000
Module Name: src
Committed By: riastradh
Date: Wed Mar 5 00:02:58 UTC 2025
Modified Files:
src/tests/lib/libc/sys: t_futex_ops.c
Log Message:
t_futex_ops: Relax various ATF_REQUIRE_* to ATF_CHECK_*.
This makes the test output more usable for browsing diagnostics when
multi-part tests partially fail.
Preparation for:
PR kern/59129: futex(3): missing sign extension in FUTEX_WAKE_OP
To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/tests/lib/libc/sys/t_futex_ops.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc:
Subject: PR/59129 CVS commit: src/tests/lib/libc/sys
Date: Wed, 5 Mar 2025 00:03:13 +0000
Module Name: src
Committed By: riastradh
Date: Wed Mar 5 00:03:13 UTC 2025
Modified Files:
src/tests/lib/libc/sys: t_futex_ops.c
Log Message:
t_futex_ops: Test sign-extension of WAKE_OP oparg/cmparg.
PR kern/59129: futex(3): missing sign extension in FUTEX_WAKE_OP
To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/tests/lib/libc/sys/t_futex_ops.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc:
Subject: PR/59129 CVS commit: src
Date: Wed, 5 Mar 2025 12:02:00 +0000
Module Name: src
Committed By: riastradh
Date: Wed Mar 5 12:02:00 UTC 2025
Modified Files:
src/sys/kern: sys_futex.c
src/sys/sys: futex.h
src/tests/lib/libc/sys: t_futex_ops.c
Log Message:
futex(2): Sign-extend FUTEX_WAKE_OP oparg/cmparg as Linux does.
Also mask off bits in the FUTEX_OP macro as Linux does so that passing
negative arguments works like in Linux.
PR kern/59129: futex(3): missing sign extension in FUTEX_WAKE_OP
To generate a diff of this commit:
cvs rdiff -u -r1.22 -r1.23 src/sys/kern/sys_futex.c
cvs rdiff -u -r1.6 -r1.7 src/sys/sys/futex.h
cvs rdiff -u -r1.13 -r1.14 src/tests/lib/libc/sys/t_futex_ops.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
State-Changed-From-To: open->needs-pullups
State-Changed-By: riastradh@NetBSD.org
State-Changed-When: Wed, 05 Mar 2025 12:07:40 +0000
State-Changed-Why:
fixed in HEAD, needs pullup-10
From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc:
Subject: PR/59129 CVS commit: src/sys/kern
Date: Wed, 5 Mar 2025 14:01:21 +0000
Module Name: src
Committed By: riastradh
Date: Wed Mar 5 14:01:20 UTC 2025
Modified Files:
src/sys/kern: sys_futex.c
Log Message:
futex(2): Fix some comments to match the usual argument order.
No functional change intenteded.
Prompted by:
PR kern/59129: futex(3): missing sign extension in FUTEX_WAKE_OP
To generate a diff of this commit:
cvs rdiff -u -r1.23 -r1.24 src/sys/kern/sys_futex.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc:
Subject: PR/59129 CVS commit: src/sys/kern
Date: Wed, 5 Mar 2025 14:01:34 +0000
Module Name: src
Committed By: riastradh
Date: Wed Mar 5 14:01:34 UTC 2025
Modified Files:
src/sys/kern: sys_futex.c
Log Message:
futex(2): Rename various parameters to clarify correspondence.
No functional change intended.
I have spent way too much time puzzling over what val/val2/val3 mean
for each operation; let's just give them all meaningful names and
write down the correspondence in the dispatch switch in do_futex.
Prompted by how much time I spent scratching my head for:
PR kern/59129: futex(3): missing sign extension in FUTEX_WAKE_OP
To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/sys/kern/sys_futex.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
>Unformatted:
(Contact us)
$NetBSD: query-full-pr,v 1.47 2022/09/11 19:34:41 kim Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2025
The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.