From owner-freebsd-current@freebsd.org Thu Apr 29 22:14:01 2021 Return-Path: Delivered-To: freebsd-current@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 8B195637A32 for ; Thu, 29 Apr 2021 22:14:01 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [96.47.72.83]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4FWVCF3TRFz3K1L; Thu, 29 Apr 2021 22:14:01 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from tensor.andric.com (tensor.andric.com [IPv6:2001:470:7a58:1::1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "tensor.andric.com", Issuer "R3" (verified OK)) (Authenticated sender: dim) by smtp.freebsd.org (Postfix) with ESMTPSA id 5128BE7D4; Thu, 29 Apr 2021 22:14:01 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from smtpclient.apple (unknown [IPv6:2001:470:7a58:0:4d6e:3c71:aa0c:7ba0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by tensor.andric.com (Postfix) with ESMTPSA id 096E2513D0; Fri, 30 Apr 2021 00:13:58 +0200 (CEST) From: Dimitry Andric Message-Id: <84FC71CC-FE92-4327-8536-0A04416432C2@FreeBSD.org> Content-Type: multipart/signed; boundary="Apple-Mail=_C2BF98D0-CEEF-4A70-9DA6-304A70CFB90F"; protocol="application/pgp-signature"; micalg=pgp-sha1 Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.80.0.2.43\)) Subject: Re: Undefined compiler behaviour or a compiler bug? Date: Fri, 30 Apr 2021 00:13:52 +0200 In-Reply-To: <16b60c22-be04-cbbf-8fc4-a90c4031c529@selasky.org> Cc: FreeBSD Current , Konstantin Belousov To: Hans Petter Selasky References: <59f81323-74c5-e6a8-a62d-6abdf78902e8@selasky.org> <16b60c22-be04-cbbf-8fc4-a90c4031c529@selasky.org> X-Mailer: Apple Mail (2.3654.80.0.2.43) X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 29 Apr 2021 22:14:01 -0000 --Apple-Mail=_C2BF98D0-CEEF-4A70-9DA6-304A70CFB90F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii On 29 Apr 2021, at 23:11, Hans Petter Selasky wrote: >=20 > On 4/29/21 11:06 PM, Hans Petter Selasky wrote: >> Hi, >> Can someone please explain what C-compiler flag I'm missing, to make = this simple C-program terminate? >> I have a function _abs() which at some point is equal to one, but the = compiler thinks it is not required to emit the test for the while() at = all, looking at the assembly code! A printf() clearly verifies that the = _abs() function has returned one. >=20 >> clang11 -O2 test.c && ./a.out >=20 > Looks like -fwrapv fixes the issue. Indeed, in this case UBSan can help you diagnose the issue. E.g.: % clang -fsanitize=3Dundefined test.c -o test % ./test Y:0x00000001 ABS:0x00000011 Y:0x00000007 ABS:0x00000041 Y:0x00000019 ABS:0x00000281 Y:0x00000027 ABS:0x00000601 Y:0x000000d9 ABS:0x0000b801 Y:0x00000327 ABS:0x0009f001 Y:0x000004d9 ABS:0x00178001 Y:0x00003b27 ABS:0x0dab0001 Y:0x000044d9 ABS:0x12840001 test.c:20:26: runtime error: signed integer overflow: 46341 * 46341 = cannot be represented in type 'int' SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior test.c:20:26 in Y:0x0001bb27 ABS:0xff200001 Y:0x000e44d9 ABS:0x9a400001 Y:0x0011bb27 ABS:0x64000001 Y:0x01ee44d9 ABS:0xc8000001 ^C -Dimitry --Apple-Mail=_C2BF98D0-CEEF-4A70-9DA6-304A70CFB90F Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=signature.asc Content-Type: application/pgp-signature; name=signature.asc Content-Description: Message signed with OpenPGP -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.2 iF0EARECAB0WIQR6tGLSzjX8bUI5T82wXqMKLiCWowUCYIsvoAAKCRCwXqMKLiCW o2ZSAJ98xP+shoXJopMKNkFS3Ydycpz5YgCgm0j8he49bPR6lDyYXM7oGHks4HY= =lDtn -----END PGP SIGNATURE----- --Apple-Mail=_C2BF98D0-CEEF-4A70-9DA6-304A70CFB90F--