From 39152b4f449273467e8e34dae67ff62674628249 Mon Sep 17 00:00:00 2001 From: cheloha Date: Tue, 26 Jul 2022 04:07:13 +0000 Subject: [PATCH] moncontrol(3): remove hertz() fallback function In the moncontrol(3) code in libc there is a fallback function, hertz(). The idea is, if getting kern.clockrate from sysctl(2) fails, we fall back to deriving the value of hz(9) using setitimer(2)'s rounding behavior. This is extremely clever, but it actually sucks. Calling setitimer(2) quietly cancels any extant ITIMER_REAL timer, so moncontrol(3) cannot be safely used alongside setitimer(2). This fact is not documented. kern.clockrate is not blocked by pledge(2), so outside of stack corruption (which we can't do anything about anyway) I don't see a way for the sysctl(2) call to ever fail on OpenBSD. So hertz() is also pointless. Hence this patch: get rid of hertz(). Thread: https://marc.info/?l=openbsd-tech&m=163881542813633&w=2 ok guenther@ --- lib/libc/gmon/gmon.c | 37 +++++++------------------------------ 1 file changed, 7 insertions(+), 30 deletions(-) diff --git a/lib/libc/gmon/gmon.c b/lib/libc/gmon/gmon.c index 1ce0a1c289e..f09ffd91837 100644 --- a/lib/libc/gmon/gmon.c +++ b/lib/libc/gmon/gmon.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gmon.c,v 1.32 2020/10/12 22:08:33 deraadt Exp $ */ +/* $OpenBSD: gmon.c,v 1.33 2022/07/26 04:07:13 cheloha Exp $ */ /*- * Copyright (c) 1983, 1992, 1993 * The Regents of the University of California. All rights reserved. @@ -50,7 +50,6 @@ static int s_scale; PROTO_NORMAL(moncontrol); PROTO_DEPRECATED(monstartup); -static int hertz(void); void monstartup(u_long lowpc, u_long highpc) @@ -159,17 +158,15 @@ _mcleanup(void) if (p->state == GMON_PROF_ERROR) ERR("_mcleanup: tos overflow\n"); + /* + * There is nothing we can do if sysctl(2) fails or if + * clockinfo.hz is unset. + */ size = sizeof(clockinfo); if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) == -1) { - /* - * Best guess - */ - clockinfo.profhz = hertz(); + clockinfo.profhz = 0; } else if (clockinfo.profhz == 0) { - if (clockinfo.hz != 0) - clockinfo.profhz = clockinfo.hz; - else - clockinfo.profhz = hertz(); + clockinfo.profhz = clockinfo.hz; /* best guess */ } moncontrol(0); @@ -304,23 +301,3 @@ moncontrol(int mode) } } DEF_WEAK(moncontrol); - -/* - * discover the tick frequency of the machine - * if something goes wrong, we return 0, an impossible hertz. - */ -static int -hertz(void) -{ - struct itimerval tim; - - tim.it_interval.tv_sec = 0; - tim.it_interval.tv_usec = 1; - tim.it_value.tv_sec = 0; - tim.it_value.tv_usec = 0; - setitimer(ITIMER_REAL, &tim, 0); - setitimer(ITIMER_REAL, 0, &tim); - if (tim.it_interval.tv_usec < 2) - return(0); - return (1000000 / tim.it_interval.tv_usec); -} -- 2.20.1