<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - wrong code at -O2 for trivial_abi class that inspects its own "this" pointer"
href="https://bugs.llvm.org/show_bug.cgi?id=37319">37319</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>wrong code at -O2 for trivial_abi class that inspects its own "this" pointer
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>-New Bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>arthur.j.odwyer@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>cat > test.cc <<EOF
#include <assert.h>
#include <stdint.h>
#define TRIVIAL_ABI __attribute__((trivial_abi))
template<class T>
class TRIVIAL_ABI trivial_offset_ptr {
intptr_t value_;
public:
trivial_offset_ptr(T *p) : value_((const char*)p - (const char*)this) {}
trivial_offset_ptr(const trivial_offset_ptr& rhs) : value_((const
char*)rhs.get() - (const char*)this) {}
T *get() const { return (T *)((const char *)this + value_); }
trivial_offset_ptr& operator=(const trivial_offset_ptr& rhs) {
value_ = ((const char*)rhs.get() - (const char*)this);
return *this;
}
trivial_offset_ptr& operator+=(int diff) {
value_ += (diff * sizeof (T));
return *this;
}
};
trivial_offset_ptr<int>
incr(trivial_offset_ptr<int> p) {
p += 1;
return p;
}
int main() {
int a[10];
trivial_offset_ptr<int> op = &a[4];
trivial_offset_ptr<int> top = &a[4];
top = incr(top);
assert(top.get() == &a[5]);
}
EOF
clang++ -std=c++17 -O1 test.cc -o a.out
./a.out # works great!
clang++ -std=c++17 -O2 test.cc -o a.out
./a.out
Assertion failed: (top.get() == &a[5]), function main, file test.cc, line 34.
Abort trap: 6
====
This is super sneaky UB-ful code in theory, I'm sure, but IMHO Clang is still
doing something not-conservative-enough here. Especially, notice that if you
remove the unused local variable `op` from `main`, the assertion will pass. Put
the unused variable back, and the assertion will fail.
Documentation for [[trivial_abi]] is here. It does not mention any caveats
about "don't implement offset_ptr with this attribute." I would grudgingly
accept that outcome if it were proposed (but I'd rather the code Just Work).
<a href="https://clang.llvm.org/docs/AttributeReference.html#trivial-abi-clang-trivial-abi">https://clang.llvm.org/docs/AttributeReference.html#trivial-abi-clang-trivial-abi</a>
P.S. I am aware that [[trivial_abi]] is super new, and I love it, and I don't
mean to be harsh at all in this first corner-case bug filed against it. :)</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>