Pointer-Bounds-Checker-builtins.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Pointer Bounds Checker builtins</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Pointer Bounds Checker builtins">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Pointer Bounds Checker builtins">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
  30. <link href="Cilk-Plus-Builtins.html#Cilk-Plus-Builtins" rel="next" title="Cilk Plus Builtins">
  31. <link href="Object-Size-Checking.html#Object-Size-Checking" rel="prev" title="Object Size Checking">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Pointer-Bounds-Checker-builtins"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Cilk-Plus-Builtins.html#Cilk-Plus-Builtins" accesskey="n" rel="next">Cilk Plus Builtins</a>, Previous: <a href="Object-Size-Checking.html#Object-Size-Checking" accesskey="p" rel="prev">Object Size Checking</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Pointer-Bounds-Checker-Built_002din-Functions"></a>
  69. <h3 class="section">6.57 Pointer Bounds Checker Built-in Functions</h3>
  70. <a name="index-Pointer-Bounds-Checker-builtins"></a>
  71. <a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fset_005fptr_005fbounds"></a>
  72. <a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fnarrow_005fptr_005fbounds"></a>
  73. <a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fcopy_005fptr_005fbounds"></a>
  74. <a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005finit_005fptr_005fbounds"></a>
  75. <a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fnull_005fptr_005fbounds"></a>
  76. <a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fstore_005fptr_005fbounds"></a>
  77. <a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fchk_005fptr_005flbounds"></a>
  78. <a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fchk_005fptr_005fubounds"></a>
  79. <a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fchk_005fptr_005fbounds"></a>
  80. <a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fget_005fptr_005flbound"></a>
  81. <a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fget_005fptr_005fubound"></a>
  82. <p>GCC provides a set of built-in functions to control Pointer Bounds Checker
  83. instrumentation. Note that all Pointer Bounds Checker builtins can be used
  84. even if you compile with Pointer Bounds Checker off
  85. (<samp>-fno-check-pointer-bounds</samp>).
  86. The behavior may differ in such case as documented below.
  87. </p>
  88. <dl>
  89. <dt><a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fset_005fptr_005fbounds-1"></a>Built-in Function: <em>void *</em> <strong>__builtin___bnd_set_ptr_bounds</strong> <em>(const void *<var>q</var>, size_t <var>size</var>)</em></dt>
  90. <dd>
  91. <p>This built-in function returns a new pointer with the value of <var>q</var>, and
  92. associate it with the bounds [<var>q</var>, <var>q</var>+<var>size</var>-1]. With Pointer
  93. Bounds Checker off, the built-in function just returns the first argument.
  94. </p>
  95. <div class="smallexample">
  96. <pre class="smallexample">extern void *__wrap_malloc (size_t n)
  97. {
  98. void *p = (void *)__real_malloc (n);
  99. if (!p) return __builtin___bnd_null_ptr_bounds (p);
  100. return __builtin___bnd_set_ptr_bounds (p, n);
  101. }
  102. </pre></div>
  103. </dd></dl>
  104. <dl>
  105. <dt><a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fnarrow_005fptr_005fbounds-1"></a>Built-in Function: <em>void *</em> <strong>__builtin___bnd_narrow_ptr_bounds</strong> <em>(const void *<var>p</var>, const void *<var>q</var>, size_t <var>size</var>)</em></dt>
  106. <dd>
  107. <p>This built-in function returns a new pointer with the value of <var>p</var>
  108. and associates it with the narrowed bounds formed by the intersection
  109. of bounds associated with <var>q</var> and the bounds
  110. [<var>p</var>, <var>p</var> + <var>size</var> - 1].
  111. With Pointer Bounds Checker off, the built-in function just returns the first
  112. argument.
  113. </p>
  114. <div class="smallexample">
  115. <pre class="smallexample">void init_objects (object *objs, size_t size)
  116. {
  117. size_t i;
  118. /* Initialize objects one-by-one passing pointers with bounds of
  119. an object, not the full array of objects. */
  120. for (i = 0; i &lt; size; i++)
  121. init_object (__builtin___bnd_narrow_ptr_bounds (objs + i, objs,
  122. sizeof(object)));
  123. }
  124. </pre></div>
  125. </dd></dl>
  126. <dl>
  127. <dt><a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fcopy_005fptr_005fbounds-1"></a>Built-in Function: <em>void *</em> <strong>__builtin___bnd_copy_ptr_bounds</strong> <em>(const void *<var>q</var>, const void *<var>r</var>)</em></dt>
  128. <dd>
  129. <p>This built-in function returns a new pointer with the value of <var>q</var>,
  130. and associates it with the bounds already associated with pointer <var>r</var>.
  131. With Pointer Bounds Checker off, the built-in function just returns the first
  132. argument.
  133. </p>
  134. <div class="smallexample">
  135. <pre class="smallexample">/* Here is a way to get pointer to object's field but
  136. still with the full object's bounds. */
  137. int *field_ptr = __builtin___bnd_copy_ptr_bounds (&amp;objptr-&gt;int_field,
  138. objptr);
  139. </pre></div>
  140. </dd></dl>
  141. <dl>
  142. <dt><a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005finit_005fptr_005fbounds-1"></a>Built-in Function: <em>void *</em> <strong>__builtin___bnd_init_ptr_bounds</strong> <em>(const void *<var>q</var>)</em></dt>
  143. <dd>
  144. <p>This built-in function returns a new pointer with the value of <var>q</var>, and
  145. associates it with INIT (allowing full memory access) bounds. With Pointer
  146. Bounds Checker off, the built-in function just returns the first argument.
  147. </p>
  148. </dd></dl>
  149. <dl>
  150. <dt><a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fnull_005fptr_005fbounds-1"></a>Built-in Function: <em>void *</em> <strong>__builtin___bnd_null_ptr_bounds</strong> <em>(const void *<var>q</var>)</em></dt>
  151. <dd>
  152. <p>This built-in function returns a new pointer with the value of <var>q</var>, and
  153. associates it with NULL (allowing no memory access) bounds. With Pointer
  154. Bounds Checker off, the built-in function just returns the first argument.
  155. </p>
  156. </dd></dl>
  157. <dl>
  158. <dt><a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fstore_005fptr_005fbounds-1"></a>Built-in Function: <em>void</em> <strong>__builtin___bnd_store_ptr_bounds</strong> <em>(const void **<var>ptr_addr</var>, const void *<var>ptr_val</var>)</em></dt>
  159. <dd>
  160. <p>This built-in function stores the bounds associated with pointer <var>ptr_val</var>
  161. and location <var>ptr_addr</var> into Bounds Table. This can be useful to propagate
  162. bounds from legacy code without touching the associated pointer&rsquo;s memory when
  163. pointers are copied as integers. With Pointer Bounds Checker off, the built-in
  164. function call is ignored.
  165. </p>
  166. </dd></dl>
  167. <dl>
  168. <dt><a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fchk_005fptr_005flbounds-1"></a>Built-in Function: <em>void</em> <strong>__builtin___bnd_chk_ptr_lbounds</strong> <em>(const void *<var>q</var>)</em></dt>
  169. <dd>
  170. <p>This built-in function checks if the pointer <var>q</var> is within the lower
  171. bound of its associated bounds. With Pointer Bounds Checker off, the built-in
  172. function call is ignored.
  173. </p>
  174. <div class="smallexample">
  175. <pre class="smallexample">extern void *__wrap_memset (void *dst, int c, size_t len)
  176. {
  177. if (len &gt; 0)
  178. {
  179. __builtin___bnd_chk_ptr_lbounds (dst);
  180. __builtin___bnd_chk_ptr_ubounds ((char *)dst + len - 1);
  181. __real_memset (dst, c, len);
  182. }
  183. return dst;
  184. }
  185. </pre></div>
  186. </dd></dl>
  187. <dl>
  188. <dt><a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fchk_005fptr_005fubounds-1"></a>Built-in Function: <em>void</em> <strong>__builtin___bnd_chk_ptr_ubounds</strong> <em>(const void *<var>q</var>)</em></dt>
  189. <dd>
  190. <p>This built-in function checks if the pointer <var>q</var> is within the upper
  191. bound of its associated bounds. With Pointer Bounds Checker off, the built-in
  192. function call is ignored.
  193. </p>
  194. </dd></dl>
  195. <dl>
  196. <dt><a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fchk_005fptr_005fbounds-1"></a>Built-in Function: <em>void</em> <strong>__builtin___bnd_chk_ptr_bounds</strong> <em>(const void *<var>q</var>, size_t <var>size</var>)</em></dt>
  197. <dd>
  198. <p>This built-in function checks if [<var>q</var>, <var>q</var> + <var>size</var> - 1] is within
  199. the lower and upper bounds associated with <var>q</var>. With Pointer Bounds Checker
  200. off, the built-in function call is ignored.
  201. </p>
  202. <div class="smallexample">
  203. <pre class="smallexample">extern void *__wrap_memcpy (void *dst, const void *src, size_t n)
  204. {
  205. if (n &gt; 0)
  206. {
  207. __bnd_chk_ptr_bounds (dst, n);
  208. __bnd_chk_ptr_bounds (src, n);
  209. __real_memcpy (dst, src, n);
  210. }
  211. return dst;
  212. }
  213. </pre></div>
  214. </dd></dl>
  215. <dl>
  216. <dt><a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fget_005fptr_005flbound-1"></a>Built-in Function: <em>const void *</em> <strong>__builtin___bnd_get_ptr_lbound</strong> <em>(const void *<var>q</var>)</em></dt>
  217. <dd>
  218. <p>This built-in function returns the lower bound associated
  219. with the pointer <var>q</var>, as a pointer value.
  220. This is useful for debugging using <code>printf</code>.
  221. With Pointer Bounds Checker off, the built-in function returns 0.
  222. </p>
  223. <div class="smallexample">
  224. <pre class="smallexample">void *lb = __builtin___bnd_get_ptr_lbound (q);
  225. void *ub = __builtin___bnd_get_ptr_ubound (q);
  226. printf (&quot;q = %p lb(q) = %p ub(q) = %p&quot;, q, lb, ub);
  227. </pre></div>
  228. </dd></dl>
  229. <dl>
  230. <dt><a name="index-_005f_005fbuiltin_005f_005f_005fbnd_005fget_005fptr_005fubound-1"></a>Built-in Function: <em>const void *</em> <strong>__builtin___bnd_get_ptr_ubound</strong> <em>(const void *<var>q</var>)</em></dt>
  231. <dd>
  232. <p>This built-in function returns the upper bound (which is a pointer) associated
  233. with the pointer <var>q</var>. With Pointer Bounds Checker off,
  234. the built-in function returns -1.
  235. </p>
  236. </dd></dl>
  237. <hr>
  238. <div class="header">
  239. <p>
  240. Next: <a href="Cilk-Plus-Builtins.html#Cilk-Plus-Builtins" accesskey="n" rel="next">Cilk Plus Builtins</a>, Previous: <a href="Object-Size-Checking.html#Object-Size-Checking" accesskey="p" rel="prev">Object Size Checking</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  241. </div>
  242. </body>
  243. </html>