ubsan.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  1. /* UndefinedBehaviorSanitizer, undefined behavior detector.
  2. Copyright (C) 2013-2015 Free Software Foundation, Inc.
  3. Contributed by Marek Polacek <polacek@redhat.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "options.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "fold-const.h"
  31. #include "stor-layout.h"
  32. #include "stringpool.h"
  33. #include "predict.h"
  34. #include "dominance.h"
  35. #include "cfg.h"
  36. #include "cfganal.h"
  37. #include "basic-block.h"
  38. #include "hash-map.h"
  39. #include "is-a.h"
  40. #include "plugin-api.h"
  41. #include "tm.h"
  42. #include "hard-reg-set.h"
  43. #include "function.h"
  44. #include "ipa-ref.h"
  45. #include "cgraph.h"
  46. #include "tree-pass.h"
  47. #include "tree-ssa-alias.h"
  48. #include "tree-pretty-print.h"
  49. #include "internal-fn.h"
  50. #include "gimple-expr.h"
  51. #include "gimple.h"
  52. #include "gimple-iterator.h"
  53. #include "gimple-ssa.h"
  54. #include "gimple-walk.h"
  55. #include "output.h"
  56. #include "tm_p.h"
  57. #include "toplev.h"
  58. #include "cfgloop.h"
  59. #include "ubsan.h"
  60. #include "c-family/c-common.h"
  61. #include "rtl.h"
  62. #include "hashtab.h"
  63. #include "flags.h"
  64. #include "statistics.h"
  65. #include "real.h"
  66. #include "fixed-value.h"
  67. #include "insn-config.h"
  68. #include "expmed.h"
  69. #include "dojump.h"
  70. #include "explow.h"
  71. #include "calls.h"
  72. #include "emit-rtl.h"
  73. #include "varasm.h"
  74. #include "stmt.h"
  75. #include "expr.h"
  76. #include "tree-ssanames.h"
  77. #include "asan.h"
  78. #include "gimplify-me.h"
  79. #include "intl.h"
  80. #include "realmpfr.h"
  81. #include "dfp.h"
  82. #include "builtins.h"
  83. #include "tree-object-size.h"
  84. #include "tree-eh.h"
  85. #include "tree-cfg.h"
  86. /* Map from a tree to a VAR_DECL tree. */
  87. struct GTY((for_user)) tree_type_map {
  88. struct tree_map_base type;
  89. tree decl;
  90. };
  91. struct tree_type_map_cache_hasher : ggc_cache_hasher<tree_type_map *>
  92. {
  93. static inline hashval_t
  94. hash (tree_type_map *t)
  95. {
  96. return TYPE_UID (t->type.from);
  97. }
  98. static inline bool
  99. equal (tree_type_map *a, tree_type_map *b)
  100. {
  101. return a->type.from == b->type.from;
  102. }
  103. static void
  104. handle_cache_entry (tree_type_map *&m)
  105. {
  106. extern void gt_ggc_mx (tree_type_map *&);
  107. if (m == HTAB_EMPTY_ENTRY || m == HTAB_DELETED_ENTRY)
  108. return;
  109. else if (ggc_marked_p (m->type.from))
  110. gt_ggc_mx (m);
  111. else
  112. m = static_cast<tree_type_map *> (HTAB_DELETED_ENTRY);
  113. }
  114. };
  115. static GTY ((cache))
  116. hash_table<tree_type_map_cache_hasher> *decl_tree_for_type;
  117. /* Lookup a VAR_DECL for TYPE, and return it if we find one. */
  118. static tree
  119. decl_for_type_lookup (tree type)
  120. {
  121. /* If the hash table is not initialized yet, create it now. */
  122. if (decl_tree_for_type == NULL)
  123. {
  124. decl_tree_for_type
  125. = hash_table<tree_type_map_cache_hasher>::create_ggc (10);
  126. /* That also means we don't have to bother with the lookup. */
  127. return NULL_TREE;
  128. }
  129. struct tree_type_map *h, in;
  130. in.type.from = type;
  131. h = decl_tree_for_type->find_with_hash (&in, TYPE_UID (type));
  132. return h ? h->decl : NULL_TREE;
  133. }
  134. /* Insert a mapping TYPE->DECL in the VAR_DECL for type hashtable. */
  135. static void
  136. decl_for_type_insert (tree type, tree decl)
  137. {
  138. struct tree_type_map *h;
  139. h = ggc_alloc<tree_type_map> ();
  140. h->type.from = type;
  141. h->decl = decl;
  142. *decl_tree_for_type->find_slot_with_hash (h, TYPE_UID (type), INSERT) = h;
  143. }
  144. /* Helper routine, which encodes a value in the pointer_sized_int_node.
  145. Arguments with precision <= POINTER_SIZE are passed directly,
  146. the rest is passed by reference. T is a value we are to encode.
  147. IN_EXPAND_P is true if this function is called during expansion. */
  148. tree
  149. ubsan_encode_value (tree t, bool in_expand_p)
  150. {
  151. tree type = TREE_TYPE (t);
  152. const unsigned int bitsize = GET_MODE_BITSIZE (TYPE_MODE (type));
  153. if (bitsize <= POINTER_SIZE)
  154. switch (TREE_CODE (type))
  155. {
  156. case BOOLEAN_TYPE:
  157. case ENUMERAL_TYPE:
  158. case INTEGER_TYPE:
  159. return fold_build1 (NOP_EXPR, pointer_sized_int_node, t);
  160. case REAL_TYPE:
  161. {
  162. tree itype = build_nonstandard_integer_type (bitsize, true);
  163. t = fold_build1 (VIEW_CONVERT_EXPR, itype, t);
  164. return fold_convert (pointer_sized_int_node, t);
  165. }
  166. default:
  167. gcc_unreachable ();
  168. }
  169. else
  170. {
  171. if (!DECL_P (t) || !TREE_ADDRESSABLE (t))
  172. {
  173. /* The reason for this is that we don't want to pessimize
  174. code by making vars unnecessarily addressable. */
  175. tree var = create_tmp_var (type);
  176. tree tem = build2 (MODIFY_EXPR, void_type_node, var, t);
  177. if (in_expand_p)
  178. {
  179. rtx mem
  180. = assign_stack_temp_for_type (TYPE_MODE (type),
  181. GET_MODE_SIZE (TYPE_MODE (type)),
  182. type);
  183. SET_DECL_RTL (var, mem);
  184. expand_assignment (var, t, false);
  185. return build_fold_addr_expr (var);
  186. }
  187. t = build_fold_addr_expr (var);
  188. return build2 (COMPOUND_EXPR, TREE_TYPE (t), tem, t);
  189. }
  190. else
  191. return build_fold_addr_expr (t);
  192. }
  193. }
  194. /* Cached ubsan_get_type_descriptor_type () return value. */
  195. static GTY(()) tree ubsan_type_descriptor_type;
  196. /* Build
  197. struct __ubsan_type_descriptor
  198. {
  199. unsigned short __typekind;
  200. unsigned short __typeinfo;
  201. char __typename[];
  202. }
  203. type. */
  204. static tree
  205. ubsan_get_type_descriptor_type (void)
  206. {
  207. static const char *field_names[3]
  208. = { "__typekind", "__typeinfo", "__typename" };
  209. tree fields[3], ret;
  210. if (ubsan_type_descriptor_type)
  211. return ubsan_type_descriptor_type;
  212. tree itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
  213. tree flex_arr_type = build_array_type (char_type_node, itype);
  214. ret = make_node (RECORD_TYPE);
  215. for (int i = 0; i < 3; i++)
  216. {
  217. fields[i] = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
  218. get_identifier (field_names[i]),
  219. (i == 2) ? flex_arr_type
  220. : short_unsigned_type_node);
  221. DECL_CONTEXT (fields[i]) = ret;
  222. if (i)
  223. DECL_CHAIN (fields[i - 1]) = fields[i];
  224. }
  225. tree type_decl = build_decl (input_location, TYPE_DECL,
  226. get_identifier ("__ubsan_type_descriptor"),
  227. ret);
  228. DECL_IGNORED_P (type_decl) = 1;
  229. DECL_ARTIFICIAL (type_decl) = 1;
  230. TYPE_FIELDS (ret) = fields[0];
  231. TYPE_NAME (ret) = type_decl;
  232. TYPE_STUB_DECL (ret) = type_decl;
  233. layout_type (ret);
  234. ubsan_type_descriptor_type = ret;
  235. return ret;
  236. }
  237. /* Cached ubsan_get_source_location_type () return value. */
  238. static GTY(()) tree ubsan_source_location_type;
  239. /* Build
  240. struct __ubsan_source_location
  241. {
  242. const char *__filename;
  243. unsigned int __line;
  244. unsigned int __column;
  245. }
  246. type. */
  247. tree
  248. ubsan_get_source_location_type (void)
  249. {
  250. static const char *field_names[3]
  251. = { "__filename", "__line", "__column" };
  252. tree fields[3], ret;
  253. if (ubsan_source_location_type)
  254. return ubsan_source_location_type;
  255. tree const_char_type = build_qualified_type (char_type_node,
  256. TYPE_QUAL_CONST);
  257. ret = make_node (RECORD_TYPE);
  258. for (int i = 0; i < 3; i++)
  259. {
  260. fields[i] = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
  261. get_identifier (field_names[i]),
  262. (i == 0) ? build_pointer_type (const_char_type)
  263. : unsigned_type_node);
  264. DECL_CONTEXT (fields[i]) = ret;
  265. if (i)
  266. DECL_CHAIN (fields[i - 1]) = fields[i];
  267. }
  268. tree type_decl = build_decl (input_location, TYPE_DECL,
  269. get_identifier ("__ubsan_source_location"),
  270. ret);
  271. DECL_IGNORED_P (type_decl) = 1;
  272. DECL_ARTIFICIAL (type_decl) = 1;
  273. TYPE_FIELDS (ret) = fields[0];
  274. TYPE_NAME (ret) = type_decl;
  275. TYPE_STUB_DECL (ret) = type_decl;
  276. layout_type (ret);
  277. ubsan_source_location_type = ret;
  278. return ret;
  279. }
  280. /* Helper routine that returns a CONSTRUCTOR of __ubsan_source_location
  281. type with its fields filled from a location_t LOC. */
  282. static tree
  283. ubsan_source_location (location_t loc)
  284. {
  285. expanded_location xloc;
  286. tree type = ubsan_get_source_location_type ();
  287. xloc = expand_location (loc);
  288. tree str;
  289. if (xloc.file == NULL)
  290. {
  291. str = build_int_cst (ptr_type_node, 0);
  292. xloc.line = 0;
  293. xloc.column = 0;
  294. }
  295. else
  296. {
  297. /* Fill in the values from LOC. */
  298. size_t len = strlen (xloc.file) + 1;
  299. str = build_string (len, xloc.file);
  300. TREE_TYPE (str) = build_array_type_nelts (char_type_node, len);
  301. TREE_READONLY (str) = 1;
  302. TREE_STATIC (str) = 1;
  303. str = build_fold_addr_expr (str);
  304. }
  305. tree ctor = build_constructor_va (type, 3, NULL_TREE, str, NULL_TREE,
  306. build_int_cst (unsigned_type_node,
  307. xloc.line), NULL_TREE,
  308. build_int_cst (unsigned_type_node,
  309. xloc.column));
  310. TREE_CONSTANT (ctor) = 1;
  311. TREE_STATIC (ctor) = 1;
  312. return ctor;
  313. }
  314. /* This routine returns a magic number for TYPE. */
  315. static unsigned short
  316. get_ubsan_type_info_for_type (tree type)
  317. {
  318. gcc_assert (TYPE_SIZE (type) && tree_fits_uhwi_p (TYPE_SIZE (type)));
  319. if (TREE_CODE (type) == REAL_TYPE)
  320. return tree_to_uhwi (TYPE_SIZE (type));
  321. else if (INTEGRAL_TYPE_P (type))
  322. {
  323. int prec = exact_log2 (tree_to_uhwi (TYPE_SIZE (type)));
  324. gcc_assert (prec != -1);
  325. return (prec << 1) | !TYPE_UNSIGNED (type);
  326. }
  327. else
  328. return 0;
  329. }
  330. /* Helper routine that returns ADDR_EXPR of a VAR_DECL of a type
  331. descriptor. It first looks into the hash table; if not found,
  332. create the VAR_DECL, put it into the hash table and return the
  333. ADDR_EXPR of it. TYPE describes a particular type. PSTYLE is
  334. an enum controlling how we want to print the type. */
  335. tree
  336. ubsan_type_descriptor (tree type, enum ubsan_print_style pstyle)
  337. {
  338. /* See through any typedefs. */
  339. type = TYPE_MAIN_VARIANT (type);
  340. tree decl = decl_for_type_lookup (type);
  341. /* It is possible that some of the earlier created DECLs were found
  342. unused, in that case they weren't emitted and varpool_node::get
  343. returns NULL node on them. But now we really need them. Thus,
  344. renew them here. */
  345. if (decl != NULL_TREE && varpool_node::get (decl))
  346. return build_fold_addr_expr (decl);
  347. tree dtype = ubsan_get_type_descriptor_type ();
  348. tree type2 = type;
  349. const char *tname = NULL;
  350. pretty_printer pretty_name;
  351. unsigned char deref_depth = 0;
  352. unsigned short tkind, tinfo;
  353. /* Get the name of the type, or the name of the pointer type. */
  354. if (pstyle == UBSAN_PRINT_POINTER)
  355. {
  356. gcc_assert (POINTER_TYPE_P (type));
  357. type2 = TREE_TYPE (type);
  358. /* Remove any '*' operators from TYPE. */
  359. while (POINTER_TYPE_P (type2))
  360. deref_depth++, type2 = TREE_TYPE (type2);
  361. if (TREE_CODE (type2) == METHOD_TYPE)
  362. type2 = TYPE_METHOD_BASETYPE (type2);
  363. }
  364. /* If an array, get its type. */
  365. type2 = strip_array_types (type2);
  366. if (pstyle == UBSAN_PRINT_ARRAY)
  367. {
  368. while (POINTER_TYPE_P (type2))
  369. deref_depth++, type2 = TREE_TYPE (type2);
  370. }
  371. if (TYPE_NAME (type2) != NULL)
  372. {
  373. if (TREE_CODE (TYPE_NAME (type2)) == IDENTIFIER_NODE)
  374. tname = IDENTIFIER_POINTER (TYPE_NAME (type2));
  375. else if (DECL_NAME (TYPE_NAME (type2)) != NULL)
  376. tname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type2)));
  377. }
  378. if (tname == NULL)
  379. /* We weren't able to determine the type name. */
  380. tname = "<unknown>";
  381. if (pstyle == UBSAN_PRINT_POINTER)
  382. {
  383. pp_printf (&pretty_name, "'%s%s%s%s%s%s%s",
  384. TYPE_VOLATILE (type2) ? "volatile " : "",
  385. TYPE_READONLY (type2) ? "const " : "",
  386. TYPE_RESTRICT (type2) ? "restrict " : "",
  387. TYPE_ATOMIC (type2) ? "_Atomic " : "",
  388. TREE_CODE (type2) == RECORD_TYPE
  389. ? "struct "
  390. : TREE_CODE (type2) == UNION_TYPE
  391. ? "union " : "", tname,
  392. deref_depth == 0 ? "" : " ");
  393. while (deref_depth-- > 0)
  394. pp_star (&pretty_name);
  395. pp_quote (&pretty_name);
  396. }
  397. else if (pstyle == UBSAN_PRINT_ARRAY)
  398. {
  399. /* Pretty print the array dimensions. */
  400. gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
  401. tree t = type;
  402. pp_printf (&pretty_name, "'%s ", tname);
  403. while (deref_depth-- > 0)
  404. pp_star (&pretty_name);
  405. while (TREE_CODE (t) == ARRAY_TYPE)
  406. {
  407. pp_left_bracket (&pretty_name);
  408. tree dom = TYPE_DOMAIN (t);
  409. if (dom && TREE_CODE (TYPE_MAX_VALUE (dom)) == INTEGER_CST)
  410. {
  411. if (tree_fits_uhwi_p (TYPE_MAX_VALUE (dom))
  412. && tree_to_uhwi (TYPE_MAX_VALUE (dom)) + 1 != 0)
  413. pp_printf (&pretty_name, HOST_WIDE_INT_PRINT_DEC,
  414. tree_to_uhwi (TYPE_MAX_VALUE (dom)) + 1);
  415. else
  416. pp_wide_int (&pretty_name,
  417. wi::add (wi::to_widest (TYPE_MAX_VALUE (dom)), 1),
  418. TYPE_SIGN (TREE_TYPE (dom)));
  419. }
  420. else
  421. /* ??? We can't determine the variable name; print VLA unspec. */
  422. pp_star (&pretty_name);
  423. pp_right_bracket (&pretty_name);
  424. t = TREE_TYPE (t);
  425. }
  426. pp_quote (&pretty_name);
  427. /* Save the tree with stripped types. */
  428. type = t;
  429. }
  430. else
  431. pp_printf (&pretty_name, "'%s'", tname);
  432. switch (TREE_CODE (type))
  433. {
  434. case BOOLEAN_TYPE:
  435. case ENUMERAL_TYPE:
  436. case INTEGER_TYPE:
  437. tkind = 0x0000;
  438. break;
  439. case REAL_TYPE:
  440. /* FIXME: libubsan right now only supports float, double and
  441. long double type formats. */
  442. if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
  443. || TYPE_MODE (type) == TYPE_MODE (double_type_node)
  444. || TYPE_MODE (type) == TYPE_MODE (long_double_type_node))
  445. tkind = 0x0001;
  446. else
  447. tkind = 0xffff;
  448. break;
  449. default:
  450. tkind = 0xffff;
  451. break;
  452. }
  453. tinfo = get_ubsan_type_info_for_type (type);
  454. /* Create a new VAR_DECL of type descriptor. */
  455. const char *tmp = pp_formatted_text (&pretty_name);
  456. size_t len = strlen (tmp) + 1;
  457. tree str = build_string (len, tmp);
  458. TREE_TYPE (str) = build_array_type_nelts (char_type_node, len);
  459. TREE_READONLY (str) = 1;
  460. TREE_STATIC (str) = 1;
  461. char tmp_name[32];
  462. static unsigned int type_var_id_num;
  463. ASM_GENERATE_INTERNAL_LABEL (tmp_name, "Lubsan_type", type_var_id_num++);
  464. decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (tmp_name),
  465. dtype);
  466. TREE_STATIC (decl) = 1;
  467. TREE_PUBLIC (decl) = 0;
  468. DECL_ARTIFICIAL (decl) = 1;
  469. DECL_IGNORED_P (decl) = 1;
  470. DECL_EXTERNAL (decl) = 0;
  471. DECL_SIZE (decl)
  472. = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (TREE_TYPE (str)));
  473. DECL_SIZE_UNIT (decl)
  474. = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl),
  475. TYPE_SIZE_UNIT (TREE_TYPE (str)));
  476. tree ctor = build_constructor_va (dtype, 3, NULL_TREE,
  477. build_int_cst (short_unsigned_type_node,
  478. tkind), NULL_TREE,
  479. build_int_cst (short_unsigned_type_node,
  480. tinfo), NULL_TREE, str);
  481. TREE_CONSTANT (ctor) = 1;
  482. TREE_STATIC (ctor) = 1;
  483. DECL_INITIAL (decl) = ctor;
  484. varpool_node::finalize_decl (decl);
  485. /* Save the VAR_DECL into the hash table. */
  486. decl_for_type_insert (type, decl);
  487. return build_fold_addr_expr (decl);
  488. }
  489. /* Create a structure for the ubsan library. NAME is a name of the new
  490. structure. LOCCNT is number of locations, PLOC points to array of
  491. locations. The arguments in ... are of __ubsan_type_descriptor type
  492. and there are at most two of them, followed by NULL_TREE, followed
  493. by optional extra arguments and another NULL_TREE. */
  494. tree
  495. ubsan_create_data (const char *name, int loccnt, const location_t *ploc, ...)
  496. {
  497. va_list args;
  498. tree ret, t;
  499. tree fields[6];
  500. vec<tree, va_gc> *saved_args = NULL;
  501. size_t i = 0;
  502. int j;
  503. /* Firstly, create a pointer to type descriptor type. */
  504. tree td_type = ubsan_get_type_descriptor_type ();
  505. td_type = build_pointer_type (td_type);
  506. /* Create the structure type. */
  507. ret = make_node (RECORD_TYPE);
  508. for (j = 0; j < loccnt; j++)
  509. {
  510. gcc_checking_assert (i < 2);
  511. fields[i] = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
  512. ubsan_get_source_location_type ());
  513. DECL_CONTEXT (fields[i]) = ret;
  514. if (i)
  515. DECL_CHAIN (fields[i - 1]) = fields[i];
  516. i++;
  517. }
  518. va_start (args, ploc);
  519. for (t = va_arg (args, tree); t != NULL_TREE;
  520. i++, t = va_arg (args, tree))
  521. {
  522. gcc_checking_assert (i < 4);
  523. /* Save the tree arguments for later use. */
  524. vec_safe_push (saved_args, t);
  525. fields[i] = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
  526. td_type);
  527. DECL_CONTEXT (fields[i]) = ret;
  528. if (i)
  529. DECL_CHAIN (fields[i - 1]) = fields[i];
  530. }
  531. for (t = va_arg (args, tree); t != NULL_TREE;
  532. i++, t = va_arg (args, tree))
  533. {
  534. gcc_checking_assert (i < 6);
  535. /* Save the tree arguments for later use. */
  536. vec_safe_push (saved_args, t);
  537. fields[i] = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
  538. TREE_TYPE (t));
  539. DECL_CONTEXT (fields[i]) = ret;
  540. if (i)
  541. DECL_CHAIN (fields[i - 1]) = fields[i];
  542. }
  543. va_end (args);
  544. tree type_decl = build_decl (input_location, TYPE_DECL,
  545. get_identifier (name), ret);
  546. DECL_IGNORED_P (type_decl) = 1;
  547. DECL_ARTIFICIAL (type_decl) = 1;
  548. TYPE_FIELDS (ret) = fields[0];
  549. TYPE_NAME (ret) = type_decl;
  550. TYPE_STUB_DECL (ret) = type_decl;
  551. layout_type (ret);
  552. /* Now, fill in the type. */
  553. char tmp_name[32];
  554. static unsigned int ubsan_var_id_num;
  555. ASM_GENERATE_INTERNAL_LABEL (tmp_name, "Lubsan_data", ubsan_var_id_num++);
  556. tree var = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (tmp_name),
  557. ret);
  558. TREE_STATIC (var) = 1;
  559. TREE_PUBLIC (var) = 0;
  560. DECL_ARTIFICIAL (var) = 1;
  561. DECL_IGNORED_P (var) = 1;
  562. DECL_EXTERNAL (var) = 0;
  563. vec<constructor_elt, va_gc> *v;
  564. vec_alloc (v, i);
  565. tree ctor = build_constructor (ret, v);
  566. /* If desirable, set the __ubsan_source_location element. */
  567. for (j = 0; j < loccnt; j++)
  568. {
  569. location_t loc = LOCATION_LOCUS (ploc[j]);
  570. CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, ubsan_source_location (loc));
  571. }
  572. size_t nelts = vec_safe_length (saved_args);
  573. for (i = 0; i < nelts; i++)
  574. {
  575. t = (*saved_args)[i];
  576. CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, t);
  577. }
  578. TREE_CONSTANT (ctor) = 1;
  579. TREE_STATIC (ctor) = 1;
  580. DECL_INITIAL (var) = ctor;
  581. varpool_node::finalize_decl (var);
  582. return var;
  583. }
  584. /* Instrument the __builtin_unreachable call. We just call the libubsan
  585. routine instead. */
  586. bool
  587. ubsan_instrument_unreachable (gimple_stmt_iterator *gsi)
  588. {
  589. gimple g;
  590. location_t loc = gimple_location (gsi_stmt (*gsi));
  591. if (flag_sanitize_undefined_trap_on_error)
  592. g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
  593. else
  594. {
  595. tree data = ubsan_create_data ("__ubsan_unreachable_data", 1, &loc,
  596. NULL_TREE, NULL_TREE);
  597. data = build_fold_addr_expr_loc (loc, data);
  598. tree fn
  599. = builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE);
  600. g = gimple_build_call (fn, 1, data);
  601. }
  602. gimple_set_location (g, loc);
  603. gsi_replace (gsi, g, false);
  604. return false;
  605. }
  606. /* Return true if T is a call to a libubsan routine. */
  607. bool
  608. is_ubsan_builtin_p (tree t)
  609. {
  610. return TREE_CODE (t) == FUNCTION_DECL
  611. && DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
  612. && strncmp (IDENTIFIER_POINTER (DECL_NAME (t)),
  613. "__builtin___ubsan_", 18) == 0;
  614. }
  615. /* Create a callgraph edge for statement STMT. */
  616. static void
  617. ubsan_create_edge (gimple stmt)
  618. {
  619. gcall *call_stmt = dyn_cast <gcall *> (stmt);
  620. basic_block bb = gimple_bb (stmt);
  621. int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
  622. cgraph_node *node = cgraph_node::get (current_function_decl);
  623. tree decl = gimple_call_fndecl (call_stmt);
  624. if (decl)
  625. node->create_edge (cgraph_node::get_create (decl), call_stmt, bb->count,
  626. freq);
  627. }
  628. /* Expand the UBSAN_BOUNDS special builtin function. */
  629. bool
  630. ubsan_expand_bounds_ifn (gimple_stmt_iterator *gsi)
  631. {
  632. gimple stmt = gsi_stmt (*gsi);
  633. location_t loc = gimple_location (stmt);
  634. gcc_assert (gimple_call_num_args (stmt) == 3);
  635. /* Pick up the arguments of the UBSAN_BOUNDS call. */
  636. tree type = TREE_TYPE (TREE_TYPE (gimple_call_arg (stmt, 0)));
  637. tree index = gimple_call_arg (stmt, 1);
  638. tree orig_index_type = TREE_TYPE (index);
  639. tree bound = gimple_call_arg (stmt, 2);
  640. gimple_stmt_iterator gsi_orig = *gsi;
  641. /* Create condition "if (index > bound)". */
  642. basic_block then_bb, fallthru_bb;
  643. gimple_stmt_iterator cond_insert_point
  644. = create_cond_insert_point (gsi, false, false, true,
  645. &then_bb, &fallthru_bb);
  646. index = fold_convert (TREE_TYPE (bound), index);
  647. index = force_gimple_operand_gsi (&cond_insert_point, index,
  648. true, NULL_TREE,
  649. false, GSI_NEW_STMT);
  650. gimple g = gimple_build_cond (GT_EXPR, index, bound, NULL_TREE, NULL_TREE);
  651. gimple_set_location (g, loc);
  652. gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT);
  653. /* Generate __ubsan_handle_out_of_bounds call. */
  654. *gsi = gsi_after_labels (then_bb);
  655. if (flag_sanitize_undefined_trap_on_error)
  656. g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
  657. else
  658. {
  659. tree data
  660. = ubsan_create_data ("__ubsan_out_of_bounds_data", 1, &loc,
  661. ubsan_type_descriptor (type, UBSAN_PRINT_ARRAY),
  662. ubsan_type_descriptor (orig_index_type),
  663. NULL_TREE, NULL_TREE);
  664. data = build_fold_addr_expr_loc (loc, data);
  665. enum built_in_function bcode
  666. = (flag_sanitize_recover & SANITIZE_BOUNDS)
  667. ? BUILT_IN_UBSAN_HANDLE_OUT_OF_BOUNDS
  668. : BUILT_IN_UBSAN_HANDLE_OUT_OF_BOUNDS_ABORT;
  669. tree fn = builtin_decl_explicit (bcode);
  670. tree val = force_gimple_operand_gsi (gsi, ubsan_encode_value (index),
  671. true, NULL_TREE, true,
  672. GSI_SAME_STMT);
  673. g = gimple_build_call (fn, 2, data, val);
  674. }
  675. gimple_set_location (g, loc);
  676. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  677. /* Get rid of the UBSAN_BOUNDS call from the IR. */
  678. unlink_stmt_vdef (stmt);
  679. gsi_remove (&gsi_orig, true);
  680. /* Point GSI to next logical statement. */
  681. *gsi = gsi_start_bb (fallthru_bb);
  682. return true;
  683. }
  684. /* Expand UBSAN_NULL internal call. The type is kept on the ckind
  685. argument which is a constant, because the middle-end treats pointer
  686. conversions as useless and therefore the type of the first argument
  687. could be changed to any other pointer type. */
  688. bool
  689. ubsan_expand_null_ifn (gimple_stmt_iterator *gsip)
  690. {
  691. gimple_stmt_iterator gsi = *gsip;
  692. gimple stmt = gsi_stmt (gsi);
  693. location_t loc = gimple_location (stmt);
  694. gcc_assert (gimple_call_num_args (stmt) == 3);
  695. tree ptr = gimple_call_arg (stmt, 0);
  696. tree ckind = gimple_call_arg (stmt, 1);
  697. tree align = gimple_call_arg (stmt, 2);
  698. tree check_align = NULL_TREE;
  699. bool check_null;
  700. basic_block cur_bb = gsi_bb (gsi);
  701. gimple g;
  702. if (!integer_zerop (align))
  703. {
  704. unsigned int ptralign = get_pointer_alignment (ptr) / BITS_PER_UNIT;
  705. if (compare_tree_int (align, ptralign) == 1)
  706. {
  707. check_align = make_ssa_name (pointer_sized_int_node);
  708. g = gimple_build_assign (check_align, NOP_EXPR, ptr);
  709. gimple_set_location (g, loc);
  710. gsi_insert_before (&gsi, g, GSI_SAME_STMT);
  711. }
  712. }
  713. check_null = (flag_sanitize & SANITIZE_NULL) != 0;
  714. if (check_align == NULL_TREE && !check_null)
  715. {
  716. gsi_remove (gsip, true);
  717. /* Unlink the UBSAN_NULLs vops before replacing it. */
  718. unlink_stmt_vdef (stmt);
  719. return true;
  720. }
  721. /* Split the original block holding the pointer dereference. */
  722. edge e = split_block (cur_bb, stmt);
  723. /* Get a hold on the 'condition block', the 'then block' and the
  724. 'else block'. */
  725. basic_block cond_bb = e->src;
  726. basic_block fallthru_bb = e->dest;
  727. basic_block then_bb = create_empty_bb (cond_bb);
  728. add_bb_to_loop (then_bb, cond_bb->loop_father);
  729. loops_state_set (LOOPS_NEED_FIXUP);
  730. /* Make an edge coming from the 'cond block' into the 'then block';
  731. this edge is unlikely taken, so set up the probability accordingly. */
  732. e = make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
  733. e->probability = PROB_VERY_UNLIKELY;
  734. /* Connect 'then block' with the 'else block'. This is needed
  735. as the ubsan routines we call in the 'then block' are not noreturn.
  736. The 'then block' only has one outcoming edge. */
  737. make_single_succ_edge (then_bb, fallthru_bb, EDGE_FALLTHRU);
  738. /* Set up the fallthrough basic block. */
  739. e = find_edge (cond_bb, fallthru_bb);
  740. e->flags = EDGE_FALSE_VALUE;
  741. e->count = cond_bb->count;
  742. e->probability = REG_BR_PROB_BASE - PROB_VERY_UNLIKELY;
  743. /* Update dominance info for the newly created then_bb; note that
  744. fallthru_bb's dominance info has already been updated by
  745. split_block. */
  746. if (dom_info_available_p (CDI_DOMINATORS))
  747. set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
  748. /* Put the ubsan builtin call into the newly created BB. */
  749. if (flag_sanitize_undefined_trap_on_error)
  750. g = gimple_build_call (builtin_decl_implicit (BUILT_IN_TRAP), 0);
  751. else
  752. {
  753. enum built_in_function bcode
  754. = (flag_sanitize_recover & ((check_align ? SANITIZE_ALIGNMENT : 0)
  755. | (check_null ? SANITIZE_NULL : 0)))
  756. ? BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH
  757. : BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT;
  758. tree fn = builtin_decl_implicit (bcode);
  759. tree data
  760. = ubsan_create_data ("__ubsan_null_data", 1, &loc,
  761. ubsan_type_descriptor (TREE_TYPE (ckind),
  762. UBSAN_PRINT_POINTER),
  763. NULL_TREE,
  764. align,
  765. fold_convert (unsigned_char_type_node, ckind),
  766. NULL_TREE);
  767. data = build_fold_addr_expr_loc (loc, data);
  768. g = gimple_build_call (fn, 2, data,
  769. check_align ? check_align
  770. : build_zero_cst (pointer_sized_int_node));
  771. }
  772. gimple_stmt_iterator gsi2 = gsi_start_bb (then_bb);
  773. gimple_set_location (g, loc);
  774. gsi_insert_after (&gsi2, g, GSI_NEW_STMT);
  775. /* Unlink the UBSAN_NULLs vops before replacing it. */
  776. unlink_stmt_vdef (stmt);
  777. if (check_null)
  778. {
  779. g = gimple_build_cond (EQ_EXPR, ptr, build_int_cst (TREE_TYPE (ptr), 0),
  780. NULL_TREE, NULL_TREE);
  781. gimple_set_location (g, loc);
  782. /* Replace the UBSAN_NULL with a GIMPLE_COND stmt. */
  783. gsi_replace (&gsi, g, false);
  784. stmt = g;
  785. }
  786. if (check_align)
  787. {
  788. if (check_null)
  789. {
  790. /* Split the block with the condition again. */
  791. e = split_block (cond_bb, stmt);
  792. basic_block cond1_bb = e->src;
  793. basic_block cond2_bb = e->dest;
  794. /* Make an edge coming from the 'cond1 block' into the 'then block';
  795. this edge is unlikely taken, so set up the probability
  796. accordingly. */
  797. e = make_edge (cond1_bb, then_bb, EDGE_TRUE_VALUE);
  798. e->probability = PROB_VERY_UNLIKELY;
  799. /* Set up the fallthrough basic block. */
  800. e = find_edge (cond1_bb, cond2_bb);
  801. e->flags = EDGE_FALSE_VALUE;
  802. e->count = cond1_bb->count;
  803. e->probability = REG_BR_PROB_BASE - PROB_VERY_UNLIKELY;
  804. /* Update dominance info. */
  805. if (dom_info_available_p (CDI_DOMINATORS))
  806. {
  807. set_immediate_dominator (CDI_DOMINATORS, fallthru_bb, cond1_bb);
  808. set_immediate_dominator (CDI_DOMINATORS, then_bb, cond1_bb);
  809. }
  810. gsi2 = gsi_start_bb (cond2_bb);
  811. }
  812. tree mask = build_int_cst (pointer_sized_int_node,
  813. tree_to_uhwi (align) - 1);
  814. g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
  815. BIT_AND_EXPR, check_align, mask);
  816. gimple_set_location (g, loc);
  817. if (check_null)
  818. gsi_insert_after (&gsi2, g, GSI_NEW_STMT);
  819. else
  820. gsi_insert_before (&gsi, g, GSI_SAME_STMT);
  821. g = gimple_build_cond (NE_EXPR, gimple_assign_lhs (g),
  822. build_int_cst (pointer_sized_int_node, 0),
  823. NULL_TREE, NULL_TREE);
  824. gimple_set_location (g, loc);
  825. if (check_null)
  826. gsi_insert_after (&gsi2, g, GSI_NEW_STMT);
  827. else
  828. /* Replace the UBSAN_NULL with a GIMPLE_COND stmt. */
  829. gsi_replace (&gsi, g, false);
  830. }
  831. return false;
  832. }
  833. #define OBJSZ_MAX_OFFSET (1024 * 16)
  834. /* Expand UBSAN_OBJECT_SIZE internal call. */
  835. bool
  836. ubsan_expand_objsize_ifn (gimple_stmt_iterator *gsi)
  837. {
  838. gimple stmt = gsi_stmt (*gsi);
  839. location_t loc = gimple_location (stmt);
  840. gcc_assert (gimple_call_num_args (stmt) == 4);
  841. tree ptr = gimple_call_arg (stmt, 0);
  842. tree offset = gimple_call_arg (stmt, 1);
  843. tree size = gimple_call_arg (stmt, 2);
  844. tree ckind = gimple_call_arg (stmt, 3);
  845. gimple_stmt_iterator gsi_orig = *gsi;
  846. gimple g;
  847. /* See if we can discard the check. */
  848. if (TREE_CODE (size) != INTEGER_CST
  849. || integer_all_onesp (size))
  850. /* Yes, __builtin_object_size couldn't determine the
  851. object size. */;
  852. else if (TREE_CODE (offset) == INTEGER_CST
  853. && wi::ges_p (wi::to_widest (offset), -OBJSZ_MAX_OFFSET)
  854. && wi::les_p (wi::to_widest (offset), -1))
  855. /* The offset is in range [-16K, -1]. */;
  856. else
  857. {
  858. /* if (offset > objsize) */
  859. basic_block then_bb, fallthru_bb;
  860. gimple_stmt_iterator cond_insert_point
  861. = create_cond_insert_point (gsi, false, false, true,
  862. &then_bb, &fallthru_bb);
  863. g = gimple_build_cond (GT_EXPR, offset, size, NULL_TREE, NULL_TREE);
  864. gimple_set_location (g, loc);
  865. gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT);
  866. /* If the offset is small enough, we don't need the second
  867. run-time check. */
  868. if (TREE_CODE (offset) == INTEGER_CST
  869. && wi::ges_p (wi::to_widest (offset), 0)
  870. && wi::les_p (wi::to_widest (offset), OBJSZ_MAX_OFFSET))
  871. *gsi = gsi_after_labels (then_bb);
  872. else
  873. {
  874. /* Don't issue run-time error if (ptr > ptr + offset). That
  875. may happen when computing a POINTER_PLUS_EXPR. */
  876. basic_block then2_bb, fallthru2_bb;
  877. gimple_stmt_iterator gsi2 = gsi_after_labels (then_bb);
  878. cond_insert_point = create_cond_insert_point (&gsi2, false, false,
  879. true, &then2_bb,
  880. &fallthru2_bb);
  881. /* Convert the pointer to an integer type. */
  882. tree p = make_ssa_name (pointer_sized_int_node);
  883. g = gimple_build_assign (p, NOP_EXPR, ptr);
  884. gimple_set_location (g, loc);
  885. gsi_insert_before (&cond_insert_point, g, GSI_NEW_STMT);
  886. p = gimple_assign_lhs (g);
  887. /* Compute ptr + offset. */
  888. g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
  889. PLUS_EXPR, p, offset);
  890. gimple_set_location (g, loc);
  891. gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT);
  892. /* Now build the conditional and put it into the IR. */
  893. g = gimple_build_cond (LE_EXPR, p, gimple_assign_lhs (g),
  894. NULL_TREE, NULL_TREE);
  895. gimple_set_location (g, loc);
  896. gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT);
  897. *gsi = gsi_after_labels (then2_bb);
  898. }
  899. /* Generate __ubsan_handle_type_mismatch call. */
  900. if (flag_sanitize_undefined_trap_on_error)
  901. g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
  902. else
  903. {
  904. tree data
  905. = ubsan_create_data ("__ubsan_objsz_data", 1, &loc,
  906. ubsan_type_descriptor (TREE_TYPE (ptr),
  907. UBSAN_PRINT_POINTER),
  908. NULL_TREE,
  909. build_zero_cst (pointer_sized_int_node),
  910. ckind,
  911. NULL_TREE);
  912. data = build_fold_addr_expr_loc (loc, data);
  913. enum built_in_function bcode
  914. = (flag_sanitize_recover & SANITIZE_OBJECT_SIZE)
  915. ? BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH
  916. : BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT;
  917. tree p = make_ssa_name (pointer_sized_int_node);
  918. g = gimple_build_assign (p, NOP_EXPR, ptr);
  919. gimple_set_location (g, loc);
  920. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  921. g = gimple_build_call (builtin_decl_explicit (bcode), 2, data, p);
  922. }
  923. gimple_set_location (g, loc);
  924. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  925. /* Point GSI to next logical statement. */
  926. *gsi = gsi_start_bb (fallthru_bb);
  927. /* Get rid of the UBSAN_OBJECT_SIZE call from the IR. */
  928. unlink_stmt_vdef (stmt);
  929. gsi_remove (&gsi_orig, true);
  930. return true;
  931. }
  932. /* Get rid of the UBSAN_OBJECT_SIZE call from the IR. */
  933. unlink_stmt_vdef (stmt);
  934. gsi_remove (gsi, true);
  935. return true;
  936. }
  937. /* Cached __ubsan_vptr_type_cache decl. */
  938. static GTY(()) tree ubsan_vptr_type_cache_decl;
  939. /* Expand UBSAN_VPTR internal call. The type is kept on the ckind
  940. argument which is a constant, because the middle-end treats pointer
  941. conversions as useless and therefore the type of the first argument
  942. could be changed to any other pointer type. */
  943. bool
  944. ubsan_expand_vptr_ifn (gimple_stmt_iterator *gsip)
  945. {
  946. gimple_stmt_iterator gsi = *gsip;
  947. gimple stmt = gsi_stmt (gsi);
  948. location_t loc = gimple_location (stmt);
  949. gcc_assert (gimple_call_num_args (stmt) == 5);
  950. tree op = gimple_call_arg (stmt, 0);
  951. tree vptr = gimple_call_arg (stmt, 1);
  952. tree str_hash = gimple_call_arg (stmt, 2);
  953. tree ti_decl_addr = gimple_call_arg (stmt, 3);
  954. tree ckind_tree = gimple_call_arg (stmt, 4);
  955. ubsan_null_ckind ckind = (ubsan_null_ckind) tree_to_uhwi (ckind_tree);
  956. tree type = TREE_TYPE (TREE_TYPE (ckind_tree));
  957. gimple g;
  958. basic_block fallthru_bb = NULL;
  959. if (ckind == UBSAN_DOWNCAST_POINTER)
  960. {
  961. /* Guard everything with if (op != NULL) { ... }. */
  962. basic_block then_bb;
  963. gimple_stmt_iterator cond_insert_point
  964. = create_cond_insert_point (gsip, false, false, true,
  965. &then_bb, &fallthru_bb);
  966. g = gimple_build_cond (NE_EXPR, op, build_zero_cst (TREE_TYPE (op)),
  967. NULL_TREE, NULL_TREE);
  968. gimple_set_location (g, loc);
  969. gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT);
  970. *gsip = gsi_after_labels (then_bb);
  971. gsi_remove (&gsi, false);
  972. gsi_insert_before (gsip, stmt, GSI_NEW_STMT);
  973. gsi = *gsip;
  974. }
  975. tree htype = TREE_TYPE (str_hash);
  976. tree cst = wide_int_to_tree (htype,
  977. wi::uhwi (((uint64_t) 0x9ddfea08 << 32)
  978. | 0xeb382d69, 64));
  979. g = gimple_build_assign (make_ssa_name (htype), BIT_XOR_EXPR,
  980. vptr, str_hash);
  981. gimple_set_location (g, loc);
  982. gsi_insert_before (gsip, g, GSI_SAME_STMT);
  983. g = gimple_build_assign (make_ssa_name (htype), MULT_EXPR,
  984. gimple_assign_lhs (g), cst);
  985. gimple_set_location (g, loc);
  986. gsi_insert_before (gsip, g, GSI_SAME_STMT);
  987. tree t1 = gimple_assign_lhs (g);
  988. g = gimple_build_assign (make_ssa_name (htype), LSHIFT_EXPR,
  989. t1, build_int_cst (integer_type_node, 47));
  990. gimple_set_location (g, loc);
  991. tree t2 = gimple_assign_lhs (g);
  992. gsi_insert_before (gsip, g, GSI_SAME_STMT);
  993. g = gimple_build_assign (make_ssa_name (htype), BIT_XOR_EXPR,
  994. vptr, t1);
  995. gimple_set_location (g, loc);
  996. gsi_insert_before (gsip, g, GSI_SAME_STMT);
  997. g = gimple_build_assign (make_ssa_name (htype), BIT_XOR_EXPR,
  998. t2, gimple_assign_lhs (g));
  999. gimple_set_location (g, loc);
  1000. gsi_insert_before (gsip, g, GSI_SAME_STMT);
  1001. g = gimple_build_assign (make_ssa_name (htype), MULT_EXPR,
  1002. gimple_assign_lhs (g), cst);
  1003. gimple_set_location (g, loc);
  1004. gsi_insert_before (gsip, g, GSI_SAME_STMT);
  1005. tree t3 = gimple_assign_lhs (g);
  1006. g = gimple_build_assign (make_ssa_name (htype), LSHIFT_EXPR,
  1007. t3, build_int_cst (integer_type_node, 47));
  1008. gimple_set_location (g, loc);
  1009. gsi_insert_before (gsip, g, GSI_SAME_STMT);
  1010. g = gimple_build_assign (make_ssa_name (htype), BIT_XOR_EXPR,
  1011. t3, gimple_assign_lhs (g));
  1012. gimple_set_location (g, loc);
  1013. gsi_insert_before (gsip, g, GSI_SAME_STMT);
  1014. g = gimple_build_assign (make_ssa_name (htype), MULT_EXPR,
  1015. gimple_assign_lhs (g), cst);
  1016. gimple_set_location (g, loc);
  1017. gsi_insert_before (gsip, g, GSI_SAME_STMT);
  1018. if (!useless_type_conversion_p (pointer_sized_int_node, htype))
  1019. {
  1020. g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
  1021. NOP_EXPR, gimple_assign_lhs (g));
  1022. gimple_set_location (g, loc);
  1023. gsi_insert_before (gsip, g, GSI_SAME_STMT);
  1024. }
  1025. tree hash = gimple_assign_lhs (g);
  1026. if (ubsan_vptr_type_cache_decl == NULL_TREE)
  1027. {
  1028. tree atype = build_array_type_nelts (pointer_sized_int_node, 128);
  1029. tree array = build_decl (UNKNOWN_LOCATION, VAR_DECL,
  1030. get_identifier ("__ubsan_vptr_type_cache"),
  1031. atype);
  1032. DECL_ARTIFICIAL (array) = 1;
  1033. DECL_IGNORED_P (array) = 1;
  1034. TREE_PUBLIC (array) = 1;
  1035. TREE_STATIC (array) = 1;
  1036. DECL_EXTERNAL (array) = 1;
  1037. DECL_VISIBILITY (array) = VISIBILITY_DEFAULT;
  1038. DECL_VISIBILITY_SPECIFIED (array) = 1;
  1039. varpool_node::finalize_decl (array);
  1040. ubsan_vptr_type_cache_decl = array;
  1041. }
  1042. g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
  1043. BIT_AND_EXPR, hash,
  1044. build_int_cst (pointer_sized_int_node, 127));
  1045. gimple_set_location (g, loc);
  1046. gsi_insert_before (gsip, g, GSI_SAME_STMT);
  1047. tree c = build4_loc (loc, ARRAY_REF, pointer_sized_int_node,
  1048. ubsan_vptr_type_cache_decl, gimple_assign_lhs (g),
  1049. NULL_TREE, NULL_TREE);
  1050. g = gimple_build_assign (make_ssa_name (pointer_sized_int_node),
  1051. ARRAY_REF, c);
  1052. gimple_set_location (g, loc);
  1053. gsi_insert_before (gsip, g, GSI_SAME_STMT);
  1054. basic_block then_bb, fallthru2_bb;
  1055. gimple_stmt_iterator cond_insert_point
  1056. = create_cond_insert_point (gsip, false, false, true,
  1057. &then_bb, &fallthru2_bb);
  1058. g = gimple_build_cond (NE_EXPR, gimple_assign_lhs (g), hash,
  1059. NULL_TREE, NULL_TREE);
  1060. gimple_set_location (g, loc);
  1061. gsi_insert_after (&cond_insert_point, g, GSI_NEW_STMT);
  1062. *gsip = gsi_after_labels (then_bb);
  1063. if (fallthru_bb == NULL)
  1064. fallthru_bb = fallthru2_bb;
  1065. tree data
  1066. = ubsan_create_data ("__ubsan_vptr_data", 1, &loc,
  1067. ubsan_type_descriptor (type), NULL_TREE, ti_decl_addr,
  1068. build_int_cst (unsigned_char_type_node, ckind),
  1069. NULL_TREE);
  1070. data = build_fold_addr_expr_loc (loc, data);
  1071. enum built_in_function bcode
  1072. = (flag_sanitize_recover & SANITIZE_VPTR)
  1073. ? BUILT_IN_UBSAN_HANDLE_DYNAMIC_TYPE_CACHE_MISS
  1074. : BUILT_IN_UBSAN_HANDLE_DYNAMIC_TYPE_CACHE_MISS_ABORT;
  1075. g = gimple_build_call (builtin_decl_explicit (bcode), 3, data, op, hash);
  1076. gimple_set_location (g, loc);
  1077. gsi_insert_before (gsip, g, GSI_SAME_STMT);
  1078. /* Point GSI to next logical statement. */
  1079. *gsip = gsi_start_bb (fallthru_bb);
  1080. /* Get rid of the UBSAN_VPTR call from the IR. */
  1081. unlink_stmt_vdef (stmt);
  1082. gsi_remove (&gsi, true);
  1083. return true;
  1084. }
  1085. /* Instrument a memory reference. BASE is the base of MEM, IS_LHS says
  1086. whether the pointer is on the left hand side of the assignment. */
  1087. static void
  1088. instrument_mem_ref (tree mem, tree base, gimple_stmt_iterator *iter,
  1089. bool is_lhs)
  1090. {
  1091. enum ubsan_null_ckind ikind = is_lhs ? UBSAN_STORE_OF : UBSAN_LOAD_OF;
  1092. unsigned int align = 0;
  1093. if (flag_sanitize & SANITIZE_ALIGNMENT)
  1094. {
  1095. align = min_align_of_type (TREE_TYPE (base));
  1096. if (align <= 1)
  1097. align = 0;
  1098. }
  1099. if (align == 0 && (flag_sanitize & SANITIZE_NULL) == 0)
  1100. return;
  1101. tree t = TREE_OPERAND (base, 0);
  1102. if (!POINTER_TYPE_P (TREE_TYPE (t)))
  1103. return;
  1104. if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (base)) && mem != base)
  1105. ikind = UBSAN_MEMBER_ACCESS;
  1106. tree kind = build_int_cst (build_pointer_type (TREE_TYPE (base)), ikind);
  1107. tree alignt = build_int_cst (pointer_sized_int_node, align);
  1108. gcall *g = gimple_build_call_internal (IFN_UBSAN_NULL, 3, t, kind, alignt);
  1109. gimple_set_location (g, gimple_location (gsi_stmt (*iter)));
  1110. gsi_insert_before (iter, g, GSI_SAME_STMT);
  1111. }
  1112. /* Perform the pointer instrumentation. */
  1113. static void
  1114. instrument_null (gimple_stmt_iterator gsi, bool is_lhs)
  1115. {
  1116. gimple stmt = gsi_stmt (gsi);
  1117. tree t = is_lhs ? gimple_get_lhs (stmt) : gimple_assign_rhs1 (stmt);
  1118. tree base = get_base_address (t);
  1119. const enum tree_code code = TREE_CODE (base);
  1120. if (code == MEM_REF
  1121. && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
  1122. instrument_mem_ref (t, base, &gsi, is_lhs);
  1123. }
  1124. /* Build an ubsan builtin call for the signed-integer-overflow
  1125. sanitization. CODE says what kind of builtin are we building,
  1126. LOC is a location, LHSTYPE is the type of LHS, OP0 and OP1
  1127. are operands of the binary operation. */
  1128. tree
  1129. ubsan_build_overflow_builtin (tree_code code, location_t loc, tree lhstype,
  1130. tree op0, tree op1)
  1131. {
  1132. if (flag_sanitize_undefined_trap_on_error)
  1133. return build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
  1134. tree data = ubsan_create_data ("__ubsan_overflow_data", 1, &loc,
  1135. ubsan_type_descriptor (lhstype), NULL_TREE,
  1136. NULL_TREE);
  1137. enum built_in_function fn_code;
  1138. switch (code)
  1139. {
  1140. case PLUS_EXPR:
  1141. fn_code = (flag_sanitize_recover & SANITIZE_SI_OVERFLOW)
  1142. ? BUILT_IN_UBSAN_HANDLE_ADD_OVERFLOW
  1143. : BUILT_IN_UBSAN_HANDLE_ADD_OVERFLOW_ABORT;
  1144. break;
  1145. case MINUS_EXPR:
  1146. fn_code = (flag_sanitize_recover & SANITIZE_SI_OVERFLOW)
  1147. ? BUILT_IN_UBSAN_HANDLE_SUB_OVERFLOW
  1148. : BUILT_IN_UBSAN_HANDLE_SUB_OVERFLOW_ABORT;
  1149. break;
  1150. case MULT_EXPR:
  1151. fn_code = (flag_sanitize_recover & SANITIZE_SI_OVERFLOW)
  1152. ? BUILT_IN_UBSAN_HANDLE_MUL_OVERFLOW
  1153. : BUILT_IN_UBSAN_HANDLE_MUL_OVERFLOW_ABORT;
  1154. break;
  1155. case NEGATE_EXPR:
  1156. fn_code = (flag_sanitize_recover & SANITIZE_SI_OVERFLOW)
  1157. ? BUILT_IN_UBSAN_HANDLE_NEGATE_OVERFLOW
  1158. : BUILT_IN_UBSAN_HANDLE_NEGATE_OVERFLOW_ABORT;
  1159. break;
  1160. default:
  1161. gcc_unreachable ();
  1162. }
  1163. tree fn = builtin_decl_explicit (fn_code);
  1164. return build_call_expr_loc (loc, fn, 2 + (code != NEGATE_EXPR),
  1165. build_fold_addr_expr_loc (loc, data),
  1166. ubsan_encode_value (op0, true),
  1167. op1 ? ubsan_encode_value (op1, true)
  1168. : NULL_TREE);
  1169. }
  1170. /* Perform the signed integer instrumentation. GSI is the iterator
  1171. pointing at statement we are trying to instrument. */
  1172. static void
  1173. instrument_si_overflow (gimple_stmt_iterator gsi)
  1174. {
  1175. gimple stmt = gsi_stmt (gsi);
  1176. tree_code code = gimple_assign_rhs_code (stmt);
  1177. tree lhs = gimple_assign_lhs (stmt);
  1178. tree lhstype = TREE_TYPE (lhs);
  1179. tree a, b;
  1180. gimple g;
  1181. /* If this is not a signed operation, don't instrument anything here.
  1182. Also punt on bit-fields. */
  1183. if (!INTEGRAL_TYPE_P (lhstype)
  1184. || TYPE_OVERFLOW_WRAPS (lhstype)
  1185. || GET_MODE_BITSIZE (TYPE_MODE (lhstype)) != TYPE_PRECISION (lhstype))
  1186. return;
  1187. switch (code)
  1188. {
  1189. case MINUS_EXPR:
  1190. case PLUS_EXPR:
  1191. case MULT_EXPR:
  1192. /* Transform
  1193. i = u {+,-,*} 5;
  1194. into
  1195. i = UBSAN_CHECK_{ADD,SUB,MUL} (u, 5); */
  1196. a = gimple_assign_rhs1 (stmt);
  1197. b = gimple_assign_rhs2 (stmt);
  1198. g = gimple_build_call_internal (code == PLUS_EXPR
  1199. ? IFN_UBSAN_CHECK_ADD
  1200. : code == MINUS_EXPR
  1201. ? IFN_UBSAN_CHECK_SUB
  1202. : IFN_UBSAN_CHECK_MUL, 2, a, b);
  1203. gimple_call_set_lhs (g, lhs);
  1204. gsi_replace (&gsi, g, false);
  1205. break;
  1206. case NEGATE_EXPR:
  1207. /* Represent i = -u;
  1208. as
  1209. i = UBSAN_CHECK_SUB (0, u); */
  1210. a = build_int_cst (lhstype, 0);
  1211. b = gimple_assign_rhs1 (stmt);
  1212. g = gimple_build_call_internal (IFN_UBSAN_CHECK_SUB, 2, a, b);
  1213. gimple_call_set_lhs (g, lhs);
  1214. gsi_replace (&gsi, g, false);
  1215. break;
  1216. case ABS_EXPR:
  1217. /* Transform i = ABS_EXPR<u>;
  1218. into
  1219. _N = UBSAN_CHECK_SUB (0, u);
  1220. i = ABS_EXPR<_N>; */
  1221. a = build_int_cst (lhstype, 0);
  1222. b = gimple_assign_rhs1 (stmt);
  1223. g = gimple_build_call_internal (IFN_UBSAN_CHECK_SUB, 2, a, b);
  1224. a = make_ssa_name (lhstype);
  1225. gimple_call_set_lhs (g, a);
  1226. gimple_set_location (g, gimple_location (stmt));
  1227. gsi_insert_before (&gsi, g, GSI_SAME_STMT);
  1228. gimple_assign_set_rhs1 (stmt, a);
  1229. update_stmt (stmt);
  1230. break;
  1231. default:
  1232. break;
  1233. }
  1234. }
  1235. /* Instrument loads from (non-bitfield) bool and C++ enum values
  1236. to check if the memory value is outside of the range of the valid
  1237. type values. */
  1238. static void
  1239. instrument_bool_enum_load (gimple_stmt_iterator *gsi)
  1240. {
  1241. gimple stmt = gsi_stmt (*gsi);
  1242. tree rhs = gimple_assign_rhs1 (stmt);
  1243. tree type = TREE_TYPE (rhs);
  1244. tree minv = NULL_TREE, maxv = NULL_TREE;
  1245. if (TREE_CODE (type) == BOOLEAN_TYPE && (flag_sanitize & SANITIZE_BOOL))
  1246. {
  1247. minv = boolean_false_node;
  1248. maxv = boolean_true_node;
  1249. }
  1250. else if (TREE_CODE (type) == ENUMERAL_TYPE
  1251. && (flag_sanitize & SANITIZE_ENUM)
  1252. && TREE_TYPE (type) != NULL_TREE
  1253. && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
  1254. && (TYPE_PRECISION (TREE_TYPE (type))
  1255. < GET_MODE_PRECISION (TYPE_MODE (type))))
  1256. {
  1257. minv = TYPE_MIN_VALUE (TREE_TYPE (type));
  1258. maxv = TYPE_MAX_VALUE (TREE_TYPE (type));
  1259. }
  1260. else
  1261. return;
  1262. int modebitsize = GET_MODE_BITSIZE (TYPE_MODE (type));
  1263. HOST_WIDE_INT bitsize, bitpos;
  1264. tree offset;
  1265. machine_mode mode;
  1266. int volatilep = 0, unsignedp = 0;
  1267. tree base = get_inner_reference (rhs, &bitsize, &bitpos, &offset, &mode,
  1268. &unsignedp, &volatilep, false);
  1269. tree utype = build_nonstandard_integer_type (modebitsize, 1);
  1270. if ((TREE_CODE (base) == VAR_DECL && DECL_HARD_REGISTER (base))
  1271. || (bitpos % modebitsize) != 0
  1272. || bitsize != modebitsize
  1273. || GET_MODE_BITSIZE (TYPE_MODE (utype)) != modebitsize
  1274. || TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
  1275. return;
  1276. bool ends_bb = stmt_ends_bb_p (stmt);
  1277. location_t loc = gimple_location (stmt);
  1278. tree lhs = gimple_assign_lhs (stmt);
  1279. tree ptype = build_pointer_type (TREE_TYPE (rhs));
  1280. tree atype = reference_alias_ptr_type (rhs);
  1281. gimple g = gimple_build_assign (make_ssa_name (ptype),
  1282. build_fold_addr_expr (rhs));
  1283. gimple_set_location (g, loc);
  1284. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  1285. tree mem = build2 (MEM_REF, utype, gimple_assign_lhs (g),
  1286. build_int_cst (atype, 0));
  1287. tree urhs = make_ssa_name (utype);
  1288. if (ends_bb)
  1289. {
  1290. gimple_assign_set_lhs (stmt, urhs);
  1291. g = gimple_build_assign (lhs, NOP_EXPR, urhs);
  1292. gimple_set_location (g, loc);
  1293. edge e = find_fallthru_edge (gimple_bb (stmt)->succs);
  1294. gsi_insert_on_edge_immediate (e, g);
  1295. gimple_assign_set_rhs_from_tree (gsi, mem);
  1296. update_stmt (stmt);
  1297. *gsi = gsi_for_stmt (g);
  1298. g = stmt;
  1299. }
  1300. else
  1301. {
  1302. g = gimple_build_assign (urhs, mem);
  1303. gimple_set_location (g, loc);
  1304. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  1305. }
  1306. minv = fold_convert (utype, minv);
  1307. maxv = fold_convert (utype, maxv);
  1308. if (!integer_zerop (minv))
  1309. {
  1310. g = gimple_build_assign (make_ssa_name (utype), MINUS_EXPR, urhs, minv);
  1311. gimple_set_location (g, loc);
  1312. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  1313. }
  1314. gimple_stmt_iterator gsi2 = *gsi;
  1315. basic_block then_bb, fallthru_bb;
  1316. *gsi = create_cond_insert_point (gsi, true, false, true,
  1317. &then_bb, &fallthru_bb);
  1318. g = gimple_build_cond (GT_EXPR, gimple_assign_lhs (g),
  1319. int_const_binop (MINUS_EXPR, maxv, minv),
  1320. NULL_TREE, NULL_TREE);
  1321. gimple_set_location (g, loc);
  1322. gsi_insert_after (gsi, g, GSI_NEW_STMT);
  1323. if (!ends_bb)
  1324. {
  1325. gimple_assign_set_rhs_with_ops (&gsi2, NOP_EXPR, urhs);
  1326. update_stmt (stmt);
  1327. }
  1328. gsi2 = gsi_after_labels (then_bb);
  1329. if (flag_sanitize_undefined_trap_on_error)
  1330. g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
  1331. else
  1332. {
  1333. tree data = ubsan_create_data ("__ubsan_invalid_value_data", 1, &loc,
  1334. ubsan_type_descriptor (type), NULL_TREE,
  1335. NULL_TREE);
  1336. data = build_fold_addr_expr_loc (loc, data);
  1337. enum built_in_function bcode
  1338. = (flag_sanitize_recover & (TREE_CODE (type) == BOOLEAN_TYPE
  1339. ? SANITIZE_BOOL : SANITIZE_ENUM))
  1340. ? BUILT_IN_UBSAN_HANDLE_LOAD_INVALID_VALUE
  1341. : BUILT_IN_UBSAN_HANDLE_LOAD_INVALID_VALUE_ABORT;
  1342. tree fn = builtin_decl_explicit (bcode);
  1343. tree val = force_gimple_operand_gsi (&gsi2, ubsan_encode_value (urhs),
  1344. true, NULL_TREE, true,
  1345. GSI_SAME_STMT);
  1346. g = gimple_build_call (fn, 2, data, val);
  1347. }
  1348. gimple_set_location (g, loc);
  1349. gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
  1350. ubsan_create_edge (g);
  1351. *gsi = gsi_for_stmt (stmt);
  1352. }
  1353. /* Instrument float point-to-integer conversion. TYPE is an integer type of
  1354. destination, EXPR is floating-point expression. ARG is what to pass
  1355. the libubsan call as value, often EXPR itself. */
  1356. tree
  1357. ubsan_instrument_float_cast (location_t loc, tree type, tree expr, tree arg)
  1358. {
  1359. tree expr_type = TREE_TYPE (expr);
  1360. tree t, tt, fn, min, max;
  1361. machine_mode mode = TYPE_MODE (expr_type);
  1362. int prec = TYPE_PRECISION (type);
  1363. bool uns_p = TYPE_UNSIGNED (type);
  1364. /* Float to integer conversion first truncates toward zero, so
  1365. even signed char c = 127.875f; is not problematic.
  1366. Therefore, we should complain only if EXPR is unordered or smaller
  1367. or equal than TYPE_MIN_VALUE - 1.0 or greater or equal than
  1368. TYPE_MAX_VALUE + 1.0. */
  1369. if (REAL_MODE_FORMAT (mode)->b == 2)
  1370. {
  1371. /* For maximum, TYPE_MAX_VALUE might not be representable
  1372. in EXPR_TYPE, e.g. if TYPE is 64-bit long long and
  1373. EXPR_TYPE is IEEE single float, but TYPE_MAX_VALUE + 1.0 is
  1374. either representable or infinity. */
  1375. REAL_VALUE_TYPE maxval = dconst1;
  1376. SET_REAL_EXP (&maxval, REAL_EXP (&maxval) + prec - !uns_p);
  1377. real_convert (&maxval, mode, &maxval);
  1378. max = build_real (expr_type, maxval);
  1379. /* For unsigned, assume -1.0 is always representable. */
  1380. if (uns_p)
  1381. min = build_minus_one_cst (expr_type);
  1382. else
  1383. {
  1384. /* TYPE_MIN_VALUE is generally representable (or -inf),
  1385. but TYPE_MIN_VALUE - 1.0 might not be. */
  1386. REAL_VALUE_TYPE minval = dconstm1, minval2;
  1387. SET_REAL_EXP (&minval, REAL_EXP (&minval) + prec - 1);
  1388. real_convert (&minval, mode, &minval);
  1389. real_arithmetic (&minval2, MINUS_EXPR, &minval, &dconst1);
  1390. real_convert (&minval2, mode, &minval2);
  1391. if (real_compare (EQ_EXPR, &minval, &minval2)
  1392. && !real_isinf (&minval))
  1393. {
  1394. /* If TYPE_MIN_VALUE - 1.0 is not representable and
  1395. rounds to TYPE_MIN_VALUE, we need to subtract
  1396. more. As REAL_MODE_FORMAT (mode)->p is the number
  1397. of base digits, we want to subtract a number that
  1398. will be 1 << (REAL_MODE_FORMAT (mode)->p - 1)
  1399. times smaller than minval. */
  1400. minval2 = dconst1;
  1401. gcc_assert (prec > REAL_MODE_FORMAT (mode)->p);
  1402. SET_REAL_EXP (&minval2,
  1403. REAL_EXP (&minval2) + prec - 1
  1404. - REAL_MODE_FORMAT (mode)->p + 1);
  1405. real_arithmetic (&minval2, MINUS_EXPR, &minval, &minval2);
  1406. real_convert (&minval2, mode, &minval2);
  1407. }
  1408. min = build_real (expr_type, minval2);
  1409. }
  1410. }
  1411. else if (REAL_MODE_FORMAT (mode)->b == 10)
  1412. {
  1413. /* For _Decimal128 up to 34 decimal digits, - sign,
  1414. dot, e, exponent. */
  1415. char buf[64];
  1416. mpfr_t m;
  1417. int p = REAL_MODE_FORMAT (mode)->p;
  1418. REAL_VALUE_TYPE maxval, minval;
  1419. /* Use mpfr_snprintf rounding to compute the smallest
  1420. representable decimal number greater or equal than
  1421. 1 << (prec - !uns_p). */
  1422. mpfr_init2 (m, prec + 2);
  1423. mpfr_set_ui_2exp (m, 1, prec - !uns_p, GMP_RNDN);
  1424. mpfr_snprintf (buf, sizeof buf, "%.*RUe", p - 1, m);
  1425. decimal_real_from_string (&maxval, buf);
  1426. max = build_real (expr_type, maxval);
  1427. /* For unsigned, assume -1.0 is always representable. */
  1428. if (uns_p)
  1429. min = build_minus_one_cst (expr_type);
  1430. else
  1431. {
  1432. /* Use mpfr_snprintf rounding to compute the largest
  1433. representable decimal number less or equal than
  1434. (-1 << (prec - 1)) - 1. */
  1435. mpfr_set_si_2exp (m, -1, prec - 1, GMP_RNDN);
  1436. mpfr_sub_ui (m, m, 1, GMP_RNDN);
  1437. mpfr_snprintf (buf, sizeof buf, "%.*RDe", p - 1, m);
  1438. decimal_real_from_string (&minval, buf);
  1439. min = build_real (expr_type, minval);
  1440. }
  1441. mpfr_clear (m);
  1442. }
  1443. else
  1444. return NULL_TREE;
  1445. t = fold_build2 (UNLE_EXPR, boolean_type_node, expr, min);
  1446. tt = fold_build2 (UNGE_EXPR, boolean_type_node, expr, max);
  1447. t = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, t, tt);
  1448. if (integer_zerop (t))
  1449. return NULL_TREE;
  1450. if (flag_sanitize_undefined_trap_on_error)
  1451. fn = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0);
  1452. else
  1453. {
  1454. /* Create the __ubsan_handle_float_cast_overflow fn call. */
  1455. tree data = ubsan_create_data ("__ubsan_float_cast_overflow_data", 0,
  1456. NULL, ubsan_type_descriptor (expr_type),
  1457. ubsan_type_descriptor (type), NULL_TREE,
  1458. NULL_TREE);
  1459. enum built_in_function bcode
  1460. = (flag_sanitize_recover & SANITIZE_FLOAT_CAST)
  1461. ? BUILT_IN_UBSAN_HANDLE_FLOAT_CAST_OVERFLOW
  1462. : BUILT_IN_UBSAN_HANDLE_FLOAT_CAST_OVERFLOW_ABORT;
  1463. fn = builtin_decl_explicit (bcode);
  1464. fn = build_call_expr_loc (loc, fn, 2,
  1465. build_fold_addr_expr_loc (loc, data),
  1466. ubsan_encode_value (arg, false));
  1467. }
  1468. return fold_build3 (COND_EXPR, void_type_node, t, fn, integer_zero_node);
  1469. }
  1470. /* Instrument values passed to function arguments with nonnull attribute. */
  1471. static void
  1472. instrument_nonnull_arg (gimple_stmt_iterator *gsi)
  1473. {
  1474. gimple stmt = gsi_stmt (*gsi);
  1475. location_t loc[2];
  1476. /* infer_nonnull_range needs flag_delete_null_pointer_checks set,
  1477. while for nonnull sanitization it is clear. */
  1478. int save_flag_delete_null_pointer_checks = flag_delete_null_pointer_checks;
  1479. flag_delete_null_pointer_checks = 1;
  1480. loc[0] = gimple_location (stmt);
  1481. loc[1] = UNKNOWN_LOCATION;
  1482. for (unsigned int i = 0; i < gimple_call_num_args (stmt); i++)
  1483. {
  1484. tree arg = gimple_call_arg (stmt, i);
  1485. if (POINTER_TYPE_P (TREE_TYPE (arg))
  1486. && infer_nonnull_range (stmt, arg, false, true))
  1487. {
  1488. gimple g;
  1489. if (!is_gimple_val (arg))
  1490. {
  1491. g = gimple_build_assign (make_ssa_name (TREE_TYPE (arg)), arg);
  1492. gimple_set_location (g, loc[0]);
  1493. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  1494. arg = gimple_assign_lhs (g);
  1495. }
  1496. basic_block then_bb, fallthru_bb;
  1497. *gsi = create_cond_insert_point (gsi, true, false, true,
  1498. &then_bb, &fallthru_bb);
  1499. g = gimple_build_cond (EQ_EXPR, arg,
  1500. build_zero_cst (TREE_TYPE (arg)),
  1501. NULL_TREE, NULL_TREE);
  1502. gimple_set_location (g, loc[0]);
  1503. gsi_insert_after (gsi, g, GSI_NEW_STMT);
  1504. *gsi = gsi_after_labels (then_bb);
  1505. if (flag_sanitize_undefined_trap_on_error)
  1506. g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
  1507. else
  1508. {
  1509. tree data = ubsan_create_data ("__ubsan_nonnull_arg_data",
  1510. 2, loc, NULL_TREE,
  1511. build_int_cst (integer_type_node,
  1512. i + 1),
  1513. NULL_TREE);
  1514. data = build_fold_addr_expr_loc (loc[0], data);
  1515. enum built_in_function bcode
  1516. = (flag_sanitize_recover & SANITIZE_NONNULL_ATTRIBUTE)
  1517. ? BUILT_IN_UBSAN_HANDLE_NONNULL_ARG
  1518. : BUILT_IN_UBSAN_HANDLE_NONNULL_ARG_ABORT;
  1519. tree fn = builtin_decl_explicit (bcode);
  1520. g = gimple_build_call (fn, 1, data);
  1521. }
  1522. gimple_set_location (g, loc[0]);
  1523. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  1524. ubsan_create_edge (g);
  1525. }
  1526. *gsi = gsi_for_stmt (stmt);
  1527. }
  1528. flag_delete_null_pointer_checks = save_flag_delete_null_pointer_checks;
  1529. }
  1530. /* Instrument returns in functions with returns_nonnull attribute. */
  1531. static void
  1532. instrument_nonnull_return (gimple_stmt_iterator *gsi)
  1533. {
  1534. greturn *stmt = as_a <greturn *> (gsi_stmt (*gsi));
  1535. location_t loc[2];
  1536. tree arg = gimple_return_retval (stmt);
  1537. /* infer_nonnull_range needs flag_delete_null_pointer_checks set,
  1538. while for nonnull return sanitization it is clear. */
  1539. int save_flag_delete_null_pointer_checks = flag_delete_null_pointer_checks;
  1540. flag_delete_null_pointer_checks = 1;
  1541. loc[0] = gimple_location (stmt);
  1542. loc[1] = UNKNOWN_LOCATION;
  1543. if (arg
  1544. && POINTER_TYPE_P (TREE_TYPE (arg))
  1545. && is_gimple_val (arg)
  1546. && infer_nonnull_range (stmt, arg, false, true))
  1547. {
  1548. basic_block then_bb, fallthru_bb;
  1549. *gsi = create_cond_insert_point (gsi, true, false, true,
  1550. &then_bb, &fallthru_bb);
  1551. gimple g = gimple_build_cond (EQ_EXPR, arg,
  1552. build_zero_cst (TREE_TYPE (arg)),
  1553. NULL_TREE, NULL_TREE);
  1554. gimple_set_location (g, loc[0]);
  1555. gsi_insert_after (gsi, g, GSI_NEW_STMT);
  1556. *gsi = gsi_after_labels (then_bb);
  1557. if (flag_sanitize_undefined_trap_on_error)
  1558. g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TRAP), 0);
  1559. else
  1560. {
  1561. tree data = ubsan_create_data ("__ubsan_nonnull_return_data",
  1562. 2, loc, NULL_TREE, NULL_TREE);
  1563. data = build_fold_addr_expr_loc (loc[0], data);
  1564. enum built_in_function bcode
  1565. = (flag_sanitize_recover & SANITIZE_RETURNS_NONNULL_ATTRIBUTE)
  1566. ? BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN
  1567. : BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT;
  1568. tree fn = builtin_decl_explicit (bcode);
  1569. g = gimple_build_call (fn, 1, data);
  1570. }
  1571. gimple_set_location (g, loc[0]);
  1572. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  1573. ubsan_create_edge (g);
  1574. *gsi = gsi_for_stmt (stmt);
  1575. }
  1576. flag_delete_null_pointer_checks = save_flag_delete_null_pointer_checks;
  1577. }
  1578. /* Instrument memory references. Here we check whether the pointer
  1579. points to an out-of-bounds location. */
  1580. static void
  1581. instrument_object_size (gimple_stmt_iterator *gsi, bool is_lhs)
  1582. {
  1583. gimple stmt = gsi_stmt (*gsi);
  1584. location_t loc = gimple_location (stmt);
  1585. tree t = is_lhs ? gimple_get_lhs (stmt) : gimple_assign_rhs1 (stmt);
  1586. tree type;
  1587. tree index = NULL_TREE;
  1588. HOST_WIDE_INT size_in_bytes;
  1589. type = TREE_TYPE (t);
  1590. if (VOID_TYPE_P (type))
  1591. return;
  1592. switch (TREE_CODE (t))
  1593. {
  1594. case COMPONENT_REF:
  1595. if (TREE_CODE (t) == COMPONENT_REF
  1596. && DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1)) != NULL_TREE)
  1597. {
  1598. tree repr = DECL_BIT_FIELD_REPRESENTATIVE (TREE_OPERAND (t, 1));
  1599. t = build3 (COMPONENT_REF, TREE_TYPE (repr), TREE_OPERAND (t, 0),
  1600. repr, NULL_TREE);
  1601. }
  1602. break;
  1603. case ARRAY_REF:
  1604. index = TREE_OPERAND (t, 1);
  1605. break;
  1606. case INDIRECT_REF:
  1607. case MEM_REF:
  1608. case VAR_DECL:
  1609. case PARM_DECL:
  1610. case RESULT_DECL:
  1611. break;
  1612. default:
  1613. return;
  1614. }
  1615. size_in_bytes = int_size_in_bytes (type);
  1616. if (size_in_bytes <= 0)
  1617. return;
  1618. HOST_WIDE_INT bitsize, bitpos;
  1619. tree offset;
  1620. machine_mode mode;
  1621. int volatilep = 0, unsignedp = 0;
  1622. tree inner = get_inner_reference (t, &bitsize, &bitpos, &offset, &mode,
  1623. &unsignedp, &volatilep, false);
  1624. if (bitpos % BITS_PER_UNIT != 0
  1625. || bitsize != size_in_bytes * BITS_PER_UNIT)
  1626. return;
  1627. bool decl_p = DECL_P (inner);
  1628. tree base;
  1629. if (decl_p)
  1630. base = inner;
  1631. else if (TREE_CODE (inner) == MEM_REF)
  1632. base = TREE_OPERAND (inner, 0);
  1633. else
  1634. return;
  1635. tree ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t);
  1636. while (TREE_CODE (base) == SSA_NAME)
  1637. {
  1638. gimple def_stmt = SSA_NAME_DEF_STMT (base);
  1639. if (gimple_assign_ssa_name_copy_p (def_stmt)
  1640. || (gimple_assign_cast_p (def_stmt)
  1641. && POINTER_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
  1642. || (is_gimple_assign (def_stmt)
  1643. && gimple_assign_rhs_code (def_stmt) == POINTER_PLUS_EXPR))
  1644. {
  1645. tree rhs1 = gimple_assign_rhs1 (def_stmt);
  1646. if (TREE_CODE (rhs1) == SSA_NAME
  1647. && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1))
  1648. break;
  1649. else
  1650. base = rhs1;
  1651. }
  1652. else
  1653. break;
  1654. }
  1655. if (!POINTER_TYPE_P (TREE_TYPE (base)) && !DECL_P (base))
  1656. return;
  1657. tree sizet;
  1658. tree base_addr = base;
  1659. gimple bos_stmt = NULL;
  1660. if (decl_p)
  1661. base_addr = build1 (ADDR_EXPR,
  1662. build_pointer_type (TREE_TYPE (base)), base);
  1663. unsigned HOST_WIDE_INT size = compute_builtin_object_size (base_addr, 0);
  1664. if (size != (unsigned HOST_WIDE_INT) -1)
  1665. sizet = build_int_cst (sizetype, size);
  1666. else if (optimize)
  1667. {
  1668. if (LOCATION_LOCUS (loc) == UNKNOWN_LOCATION)
  1669. loc = input_location;
  1670. /* Generate __builtin_object_size call. */
  1671. sizet = builtin_decl_explicit (BUILT_IN_OBJECT_SIZE);
  1672. sizet = build_call_expr_loc (loc, sizet, 2, base_addr,
  1673. integer_zero_node);
  1674. sizet = force_gimple_operand_gsi (gsi, sizet, false, NULL_TREE, true,
  1675. GSI_SAME_STMT);
  1676. /* If the call above didn't end up being an integer constant, go one
  1677. statement back and get the __builtin_object_size stmt. Save it,
  1678. we might need it later. */
  1679. if (SSA_VAR_P (sizet))
  1680. {
  1681. gsi_prev (gsi);
  1682. bos_stmt = gsi_stmt (*gsi);
  1683. /* Move on to where we were. */
  1684. gsi_next (gsi);
  1685. }
  1686. }
  1687. else
  1688. return;
  1689. /* Generate UBSAN_OBJECT_SIZE (ptr, ptr+sizeof(*ptr)-base, objsize, ckind)
  1690. call. */
  1691. /* ptr + sizeof (*ptr) - base */
  1692. t = fold_build2 (MINUS_EXPR, sizetype,
  1693. fold_convert (pointer_sized_int_node, ptr),
  1694. fold_convert (pointer_sized_int_node, base_addr));
  1695. t = fold_build2 (PLUS_EXPR, sizetype, t, TYPE_SIZE_UNIT (type));
  1696. /* Perhaps we can omit the check. */
  1697. if (TREE_CODE (t) == INTEGER_CST
  1698. && TREE_CODE (sizet) == INTEGER_CST
  1699. && tree_int_cst_le (t, sizet))
  1700. return;
  1701. if (index != NULL_TREE
  1702. && TREE_CODE (index) == SSA_NAME
  1703. && TREE_CODE (sizet) == INTEGER_CST)
  1704. {
  1705. gimple def = SSA_NAME_DEF_STMT (index);
  1706. if (is_gimple_assign (def)
  1707. && gimple_assign_rhs_code (def) == BIT_AND_EXPR
  1708. && TREE_CODE (gimple_assign_rhs2 (def)) == INTEGER_CST)
  1709. {
  1710. tree cst = gimple_assign_rhs2 (def);
  1711. tree sz = fold_build2 (EXACT_DIV_EXPR, sizetype, sizet,
  1712. TYPE_SIZE_UNIT (type));
  1713. if (tree_int_cst_sgn (cst) >= 0
  1714. && tree_int_cst_lt (cst, sz))
  1715. return;
  1716. }
  1717. }
  1718. if (bos_stmt && gimple_call_builtin_p (bos_stmt, BUILT_IN_OBJECT_SIZE))
  1719. ubsan_create_edge (bos_stmt);
  1720. /* We have to emit the check. */
  1721. t = force_gimple_operand_gsi (gsi, t, true, NULL_TREE, true,
  1722. GSI_SAME_STMT);
  1723. ptr = force_gimple_operand_gsi (gsi, ptr, true, NULL_TREE, true,
  1724. GSI_SAME_STMT);
  1725. tree ckind = build_int_cst (unsigned_char_type_node,
  1726. is_lhs ? UBSAN_STORE_OF : UBSAN_LOAD_OF);
  1727. gimple g = gimple_build_call_internal (IFN_UBSAN_OBJECT_SIZE, 4,
  1728. ptr, t, sizet, ckind);
  1729. gimple_set_location (g, loc);
  1730. gsi_insert_before (gsi, g, GSI_SAME_STMT);
  1731. }
  1732. /* True if we want to play UBSan games in the current function. */
  1733. bool
  1734. do_ubsan_in_current_function ()
  1735. {
  1736. return (current_function_decl != NULL_TREE
  1737. && !lookup_attribute ("no_sanitize_undefined",
  1738. DECL_ATTRIBUTES (current_function_decl)));
  1739. }
  1740. namespace {
  1741. const pass_data pass_data_ubsan =
  1742. {
  1743. GIMPLE_PASS, /* type */
  1744. "ubsan", /* name */
  1745. OPTGROUP_NONE, /* optinfo_flags */
  1746. TV_TREE_UBSAN, /* tv_id */
  1747. ( PROP_cfg | PROP_ssa ), /* properties_required */
  1748. 0, /* properties_provided */
  1749. 0, /* properties_destroyed */
  1750. 0, /* todo_flags_start */
  1751. TODO_update_ssa, /* todo_flags_finish */
  1752. };
  1753. class pass_ubsan : public gimple_opt_pass
  1754. {
  1755. public:
  1756. pass_ubsan (gcc::context *ctxt)
  1757. : gimple_opt_pass (pass_data_ubsan, ctxt)
  1758. {}
  1759. /* opt_pass methods: */
  1760. virtual bool gate (function *)
  1761. {
  1762. return flag_sanitize & (SANITIZE_NULL | SANITIZE_SI_OVERFLOW
  1763. | SANITIZE_BOOL | SANITIZE_ENUM
  1764. | SANITIZE_ALIGNMENT
  1765. | SANITIZE_NONNULL_ATTRIBUTE
  1766. | SANITIZE_RETURNS_NONNULL_ATTRIBUTE
  1767. | SANITIZE_OBJECT_SIZE)
  1768. && do_ubsan_in_current_function ();
  1769. }
  1770. virtual unsigned int execute (function *);
  1771. }; // class pass_ubsan
  1772. unsigned int
  1773. pass_ubsan::execute (function *fun)
  1774. {
  1775. basic_block bb;
  1776. gimple_stmt_iterator gsi;
  1777. initialize_sanitizer_builtins ();
  1778. FOR_EACH_BB_FN (bb, fun)
  1779. {
  1780. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
  1781. {
  1782. gimple stmt = gsi_stmt (gsi);
  1783. if (is_gimple_debug (stmt) || gimple_clobber_p (stmt))
  1784. {
  1785. gsi_next (&gsi);
  1786. continue;
  1787. }
  1788. if ((flag_sanitize & SANITIZE_SI_OVERFLOW)
  1789. && is_gimple_assign (stmt))
  1790. instrument_si_overflow (gsi);
  1791. if (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))
  1792. {
  1793. if (gimple_store_p (stmt))
  1794. instrument_null (gsi, true);
  1795. if (gimple_assign_load_p (stmt))
  1796. instrument_null (gsi, false);
  1797. }
  1798. if (flag_sanitize & (SANITIZE_BOOL | SANITIZE_ENUM)
  1799. && gimple_assign_load_p (stmt))
  1800. {
  1801. instrument_bool_enum_load (&gsi);
  1802. bb = gimple_bb (stmt);
  1803. }
  1804. if ((flag_sanitize & SANITIZE_NONNULL_ATTRIBUTE)
  1805. && is_gimple_call (stmt)
  1806. && !gimple_call_internal_p (stmt))
  1807. {
  1808. instrument_nonnull_arg (&gsi);
  1809. bb = gimple_bb (stmt);
  1810. }
  1811. if ((flag_sanitize & SANITIZE_RETURNS_NONNULL_ATTRIBUTE)
  1812. && gimple_code (stmt) == GIMPLE_RETURN)
  1813. {
  1814. instrument_nonnull_return (&gsi);
  1815. bb = gimple_bb (stmt);
  1816. }
  1817. if (flag_sanitize & SANITIZE_OBJECT_SIZE)
  1818. {
  1819. if (gimple_store_p (stmt))
  1820. instrument_object_size (&gsi, true);
  1821. if (gimple_assign_load_p (stmt))
  1822. instrument_object_size (&gsi, false);
  1823. }
  1824. gsi_next (&gsi);
  1825. }
  1826. }
  1827. return 0;
  1828. }
  1829. } // anon namespace
  1830. gimple_opt_pass *
  1831. make_pass_ubsan (gcc::context *ctxt)
  1832. {
  1833. return new pass_ubsan (ctxt);
  1834. }
  1835. #include "gt-ubsan.h"