Bug 277236 - devel/qt5-script: clang optimization miscompiles qtscript
Summary: devel/qt5-script: clang optimization miscompiles qtscript
Status: Closed FIXED
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-kde (group)
URL:
Keywords:
Depends on:
Blocks: 265989
  Show dependency treegraph
 
Reported: 2024-02-23 10:00 UTC by ice
Modified: 2024-03-22 21:21 UTC (History)
3 users (show)

See Also:
arrowd: maintainer-feedback+


Attachments
devel/qt5-script diff fixing optimisation issue with Clang (1.75 KB, patch)
2024-02-23 10:00 UTC, ice
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description ice 2024-02-23 10:00:57 UTC
Created attachment 248689 [details]
devel/qt5-script diff fixing optimisation issue with Clang

(or whatever the order of cause and effect is)

Cf. bug #265989

The attached patch (taken from OpenBSD, https://github.com/openbsd/ports/blob/e10f7e1a39f85873183c232462850449f777e16f/x11/qt5/qtscript/patches/patch-src_3rdparty_javascriptcore_JavaScriptCore_interpreter_CallFrame_h) fixes, at least on FreeBSD 14, the problem reported with cad/qcad in bug #265989 (and presumably other things using qtscript).
Comment 1 Yuri Victorovich freebsd_committer freebsd_triage 2024-02-23 10:24:56 UTC
Should this also be reported to Clang, since it's Clang that miscompiles devel/qt5-script.
Comment 2 John F. Carr 2024-02-23 13:36:23 UTC
My first impression is clang is within its rights to break this code.  The JS implementation tries to store metadata bits in the low bits of a pointer type.  The compiler is allowed to assume that the low bits of a pointer value are zero. (Assuming the pointed-to type has alignment rules, which is normally the case.)  LLVM itself uses a wrapper class instead of an illegal pointer value.  The wrapper class stores the pointer+tag combination as an integer and provides a getter method to return an untagged pointer.
Comment 3 Yuri Victorovich freebsd_committer freebsd_triage 2024-02-23 16:43:17 UTC
(In reply to John F. Carr from comment #2)

I see.

In this case this should be reported to the devel/qt5-script upstream. They should stop using hacks and do the same thing properly.


Yuri
Comment 4 John F. Carr 2024-02-23 17:07:39 UTC
Here is an example:

struct S {
  long alignment;
  int this_lowbit();
};
int S::this_lowbit() { return reinterpret_cast<signed long>(this) & 1; }

The method this_lowbit always returns zero with optimization enabled.
Comment 5 commit-hook freebsd_committer freebsd_triage 2024-03-22 21:20:32 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/ports/commit/?id=9e3ff8f0b99e7b3074f3e83c954a0395185f63f0

commit 9e3ff8f0b99e7b3074f3e83c954a0395185f63f0
Author:     Tomas Tevesz <ice@extreme.hu>
AuthorDate: 2024-03-22 21:16:49 +0000
Commit:     Gleb Popov <arrowd@FreeBSD.org>
CommitDate: 2024-03-22 21:18:36 +0000

    devel/qt5-script: Prevent clang from optimizing the code with undefined behavior

    PR:             277236
    Obtained from:  OpenBSD ports

 devel/qt5-script/Makefile                                 |  2 +-
 ...criptcore_JavaScriptCore_interpreter_CallFrame.h (new) | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
Comment 6 Gleb Popov freebsd_committer freebsd_triage 2024-03-22 21:21:10 UTC
Pushed, thanks.