<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<p style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-size:
15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,
'Helvetica Neue', Helvetica, sans-serif; font-style: normal;
font-variant: normal; font-weight: normal; letter-spacing: normal;
line-height: 19.5px; orphans: auto; text-align: left; text-indent:
0px; text-transform: none; white-space: normal; widows: 1;
word-spacing: 0px; -webkit-text-stroke-width: 0px;
background-color: rgb(255, 255, 255);">I want to detect bad casts
in C++ code by using the Clang compiler. The approach is similar
to what Caver and TypeSan do but without using the compiler-rt.</p>
<p style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-size:
15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,
'Helvetica Neue', Helvetica, sans-serif; font-style: normal;
font-variant: normal; font-weight: normal; letter-spacing: normal;
line-height: 19.5px; orphans: auto; text-align: left; text-indent:
0px; text-transform: none; white-space: normal; widows: 1;
word-spacing: 0px; -webkit-text-stroke-width: 0px;
background-color: rgb(255, 255, 255);">Caver and TypeSan:</p>
<p style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-size:
15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,
'Helvetica Neue', Helvetica, sans-serif; font-style: normal;
font-variant: normal; font-weight: normal; letter-spacing: normal;
line-height: 19.5px; orphans: auto; text-align: left; text-indent:
0px; text-transform: none; white-space: normal; widows: 1;
word-spacing: 0px; -webkit-text-stroke-width: 0px;
background-color: rgb(255, 255, 255);"><a
href="https://www.usenix.org/system/files/conference/usenixsecurity15/sec15-paper-lee.pdf"
rel="nofollow noreferrer" style="margin: 0px; padding: 0px;
border: 0px; font-size: 15px; color: rgb(0, 89, 153);
text-decoration: none; cursor: pointer;"><a class="moz-txt-link-freetext" href="https://www.usenix.org/system/files/conference/usenixsecurity15/sec15-paper-lee.pdf">https://www.usenix.org/system/files/conference/usenixsecurity15/sec15-paper-lee.pdf</a></a><a
href="https://nebelwelt.net/publications/files/16CCS2.pdf"
rel="nofollow noreferrer" style="margin: 0px; padding: 0px;
border: 0px; font-size: 15px; color: rgb(0, 89, 153);
text-decoration: none; cursor: pointer;"><a class="moz-txt-link-freetext" href="https://nebelwelt.net/publications/files/16CCS2.pdf">https://nebelwelt.net/publications/files/16CCS2.pdf</a></a></p>
<p style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-size:
15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,
'Helvetica Neue', Helvetica, sans-serif; font-style: normal;
font-variant: normal; font-weight: normal; letter-spacing: normal;
line-height: 19.5px; orphans: auto; text-align: left; text-indent:
0px; text-transform: none; white-space: normal; widows: 1;
word-spacing: 0px; -webkit-text-stroke-width: 0px;
background-color: rgb(255, 255, 255);">For example if I have the
following C++ code snippet where I want to cast object<span
class="Apple-converted-space"> </span><code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);">b</code><span
class="Apple-converted-space"> </span>into object<span
class="Apple-converted-space"> </span><code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);">D</code>.</p>
<pre class="lang-cpp prettyprint prettyprinted" style="margin: 0px 0px 1em; padding: 5px; border: 0px; font-size: 13px; width: auto; max-height: 600px; overflow: auto; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; display: block; color: rgb(57, 51, 24); word-wrap: normal; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(239, 240, 241);"><code style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit; background-color: rgb(239, 240, 241);"><span class="pln" style="margin: 0px; padding: 0px; b
o
rder: 0px; font-size: 13px; color: rgb(48, 51, 54);">D</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; color: rgb(48, 51, 54);">*</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; color: rgb(48, 51, 54);"> obj </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; color: rgb(48, 51, 54);">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; color: rgb(48, 51, 54);"> </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; color: rgb(16, 16, 148);">static_cast</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; color: rgb(48, 51, 54);"><</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; color: rgb(48, 51, 54);">D</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; color: rgb(48, 51, 54);">*>(<
/
span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; color: rgb(48, 51, 54);">b</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; font-size: 13px; color: rgb(48, 51, 54);">);</span></code></pre>
<p style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-size:
15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,
'Helvetica Neue', Helvetica, sans-serif; font-style: normal;
font-variant: normal; font-weight: normal; letter-spacing: normal;
line-height: 19.5px; orphans: auto; text-align: left; text-indent:
0px; text-transform: none; white-space: normal; widows: 1;
word-spacing: 0px; -webkit-text-stroke-width: 0px;
background-color: rgb(255, 255, 255);">from where (inside Clang,
LTO, thinLTO, etc.) can I get the base class of<span
class="Apple-converted-space"> </span><code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);">D</code><span
class="Apple-converted-space"> </span>and the base class of<span
class="Apple-converted-space"> </span><code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);">b</code>.
Is this available in the Clang compiler or LTO?</p>
<p style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-size:
15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,
'Helvetica Neue', Helvetica, sans-serif; font-style: normal;
font-variant: normal; font-weight: normal; letter-spacing: normal;
line-height: 19.5px; orphans: auto; text-align: left; text-indent:
0px; text-transform: none; white-space: normal; widows: 1;
word-spacing: 0px; -webkit-text-stroke-width: 0px;
background-color: rgb(255, 255, 255);">Also, if b is an object of
a virtual Class (class with inherited or its own virtual
functions) can I get its virtual pointer at compile time by using
the LTO?</p>
<p style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-size:
15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,
'Helvetica Neue', Helvetica, sans-serif; font-style: normal;
font-variant: normal; font-weight: normal; letter-spacing: normal;
line-height: 19.5px; orphans: auto; text-align: left; text-indent:
0px; text-transform: none; white-space: normal; widows: 1;
word-spacing: 0px; -webkit-text-stroke-width: 0px;
background-color: rgb(255, 255, 255);">I found out that in<span
class="Apple-converted-space"> </span><code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);">CodeGen/CGExpr.cpp</code><span
class="Apple-converted-space"> </span>the explicit case
"BaseToDerived" is handled and you can retrieve<span
class="Apple-converted-space"> </span><code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);">TypeSourceInfo</code><span
class="Apple-converted-space"> </span>using the function<span
class="Apple-converted-space"> </span><code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);">getTypeInfoAsWritten()</code>.
It seems that this information is enough to get the exact type of
this cast at compile-time and should also work for Template
programming, as my, understanding is that Clang should have
abstracted the code into, specific types at this point.</p>
<p style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-size:
15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,
'Helvetica Neue', Helvetica, sans-serif; font-style: normal;
font-variant: normal; font-weight: normal; letter-spacing: normal;
line-height: 19.5px; orphans: auto; text-align: left; text-indent:
0px; text-transform: none; white-space: normal; widows: 1;
word-spacing: 0px; -webkit-text-stroke-width: 0px;
background-color: rgb(255, 255, 255);">Am I wrong here?</p>
<p style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-size:
15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,
'Helvetica Neue', Helvetica, sans-serif; font-style: normal;
font-variant: normal; font-weight: normal; letter-spacing: normal;
line-height: 19.5px; orphans: auto; text-align: left; text-indent:
0px; text-transform: none; white-space: normal; widows: 1;
word-spacing: 0px; -webkit-text-stroke-width: 0px;
background-color: rgb(255, 255, 255);">Is there any case where the
type of the objects used inside the cast not known at compile
time? Can the, 'real' type be hidden behind a pointer?<br>
</p>
<p style="margin: 0px 0px 1em; padding: 0px; border: 0px; font-size:
15px; clear: both; color: rgb(36, 39, 41); font-family: Arial,
'Helvetica Neue', Helvetica, sans-serif; font-style: normal;
font-variant: normal; font-weight: normal; letter-spacing: normal;
line-height: 19.5px; orphans: auto; text-align: left; text-indent:
0px; text-transform: none; white-space: normal; widows: 1;
word-spacing: 0px; -webkit-text-stroke-width: 0px;
background-color: rgb(255, 255, 255);">see also:<br>
<a class="moz-txt-link-freetext" href="http://stackoverflow.com/questions/41104659/llvm-clang-get-cast-type-during-compile-time?sem=2">http://stackoverflow.com/questions/41104659/llvm-clang-get-cast-type-during-compile-time?sem=2</a><br>
</p>
</body>
</html>