VirtualBox

source: vbox/trunk/src/bldprogs/VBoxCompilerPlugInsGcc.cpp@ 89863

Last change on this file since 89863 was 88324, checked in by vboxsync, 4 years ago

gccplugin: gcc 7 fixes (and adjust the previously gcc 10 and later ones to 8, because 8.3.0 has them)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 34.7 KB
Line 
1/* $Id: VBoxCompilerPlugInsGcc.cpp 88324 2021-03-30 16:57:23Z vboxsync $ */
2/** @file
3 * gccplugin - GCC plugin for checking IPRT format strings.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <stdio.h>
23#include <iprt/cdefs.h>
24#include <iprt/stdarg.h>
25
26#if RT_GNUC_PREREQ(5, 1)
27# include "gcc-plugin.h"
28# include "plugin-version.h"
29#endif
30#if __GNUC__ == 4 && __GNUC_MINOR__ == 5
31# include "gmp.h"
32extern "C" {
33#endif
34#if __GNUC__ == 4 && __GNUC_MINOR__ == 5
35# include "coretypes.h"
36#endif
37#include "plugin.h"
38#include "basic-block.h"
39#include "tree.h"
40#include "tree-pass.h"
41#if __GNUC__ == 5 && __GNUC_MINOR__ == 4
42# include "tree-ssa-alias.h"
43# include "gimple-expr.h"
44#endif
45#include "gimple.h"
46#if RT_GNUC_PREREQ(4, 9)
47# include "gimple-iterator.h"
48# include "context.h" /* for g */
49#endif
50#include "cp/cp-tree.h"
51#if RT_GNUC_PREREQ(10, 0)
52# include "stringpool.h"
53# include "attribs.h"
54#endif
55#if __GNUC__ == 4 && __GNUC_MINOR__ == 5
56}
57#endif
58
59#include "VBoxCompilerPlugIns.h"
60
61
62/*********************************************************************************************************************************
63* Global Variables *
64*********************************************************************************************************************************/
65/** License indicator. */
66int plugin_is_GPL_compatible;
67
68
69/*********************************************************************************************************************************
70* Defined Constants And Macros *
71*********************************************************************************************************************************/
72/** Convencience macro not present in earlier gcc versions. */
73#ifndef VAR_P
74# define VAR_P(a_hNode) (TREE_CODE(a_hNode) == VAR_DECL)
75#endif
76/** Replacement for the 4.9.0 get_tree_code_name function. */
77#if !RT_GNUC_PREREQ(4, 9)
78# define get_tree_code_name(a_enmTreeCode) (tree_code_name[a_enmTreeCode])
79#endif
80
81
82/** For use with messages.
83 * @todo needs some more work... Actually, seems we're a bit handicapped by
84 * working on gimplified stuff. */
85#define MY_LOC(a_hPreferred, a_pState) EXPR_LOC_OR_LOC(a_hPreferred, (a_pState)->hFmtLoc)
86
87/** @name Compatibility glue
88 * @{ */
89#if __GNUC__ == 4 && __GNUC_MINOR__ == 5
90# define linemap_location_from_macro_expansion_p(a, b) false
91#endif
92#if __GNUC__ == 4 && __GNUC_MINOR__ == 5
93static tree gimple_call_fntype(gimple hStmt)
94{
95 tree hDecl = gimple_call_fndecl(hStmt);
96 if (hDecl)
97 return TREE_TYPE(hDecl);
98 hDecl = gimple_call_fn(hStmt);
99 if (TREE_CODE(hDecl) == OBJ_TYPE_REF)
100 hDecl = OBJ_TYPE_REF_EXPR(hDecl);
101 if (DECL_P(hDecl))
102 {
103 tree hType = TREE_TYPE(hDecl);
104 if (POINTER_TYPE_P(hType))
105 hType = TREE_TYPE(hType);
106 return hType;
107 }
108 return NULL_TREE; /* caller bitches about this*/
109}
110#endif
111
112///* Integer to HOST_WIDE_INT conversion fun. */
113//#if RT_GNUC_PREREQ(4, 6)
114//# define MY_INT_FITS_SHWI(hNode) (hNode).fits_shwi()
115//# define MY_INT_TO_SHWI(hNode) (hNode).to_shwi()
116//#else
117//# define MY_INT_FITS_SHWI(hNode) double_int_fits_in_shwi_p(hNode)
118//# define MY_INT_TO_SHWI(hNode) double_int_to_shwi(hNode)
119//#endif
120
121/* Integer to HOST_WIDE_INT conversion fun. */
122#if RT_GNUC_PREREQ(5, 1)
123# define MY_DOUBLE_INT_FITS_SHWI(hNode) tree_fits_shwi_p(hNode)
124# define MY_DOUBLE_INT_TO_SHWI(hNode) tree_to_shwi(hNode)
125#elif RT_GNUC_PREREQ(4, 6)
126# define MY_DOUBLE_INT_FITS_SHWI(hNode) (TREE_INT_CST(hNode).fits_shwi())
127# define MY_DOUBLE_INT_TO_SHWI(hNode) (TREE_INT_CST(hNode).to_shwi())
128#else
129# define MY_DOUBLE_INT_FITS_SHWI(hNode) double_int_fits_in_shwi_p(TREE_INT_CST(hNode))
130# define MY_DOUBLE_INT_TO_SHWI(hNode) double_int_to_shwi(TREE_INT_CST(hNode))
131#endif
132
133#ifndef EXPR_LOC_OR_LOC
134# define EXPR_LOC_OR_LOC(a,b) (b)
135#endif
136/** @} */
137
138
139/*********************************************************************************************************************************
140* Internal Functions *
141*********************************************************************************************************************************/
142static bool MyPassGateCallback(void);
143static unsigned int MyPassExecuteCallback(void);
144static unsigned int MyPassExecuteCallbackWithFunction(struct function *pFun);
145static tree AttributeHandler(tree *, tree, tree, int, bool *);
146
147
148/*********************************************************************************************************************************
149* Global Variables *
150*********************************************************************************************************************************/
151/** Plug-in info. */
152static const struct plugin_info g_PlugInInfo =
153{
154 version: "0.0.0-ALPHA",
155 help : "Implements the __iprt_format__ attribute for checking format strings and arguments."
156};
157
158#if RT_GNUC_PREREQ(4, 9)
159
160/** My pass. */
161static const pass_data g_MyPassData =
162{
163 type : GIMPLE_PASS,
164 name : "*iprt-format-checks", /* asterisk = no dump */
165# if RT_GNUC_PREREQ(10, 0)
166 optinfo_flags : OPTGROUP_NONE,
167# else
168 optinfo_flags : 0,
169# endif
170 tv_id : TV_NONE,
171 properties_required : 0,
172 properties_provided : 0,
173 properties_destroyed : 0,
174 todo_flags_start : 0,
175 todo_flags_finish : 0,
176};
177
178class MyPass : public gimple_opt_pass
179{
180public:
181 MyPass(gcc::context *pCtx) : gimple_opt_pass(g_MyPassData, pCtx)
182 { }
183
184 virtual bool gate(function *pFun)
185 {
186 NOREF(pFun);
187 return MyPassGateCallback();
188 }
189
190 virtual unsigned int execute(function *pFun)
191 {
192 NOREF(pFun);
193 return MyPassExecuteCallbackWithFunction(pFun);
194 }
195};
196
197#else /* < 4.9.0 */
198
199/** My pass. */
200static struct gimple_opt_pass g_MyPass =
201{
202 pass:
203 {
204 type : GIMPLE_PASS,
205 name : "*iprt-format-checks", /* asterisk = no dump */
206# if RT_GNUC_PREREQ(4, 6)
207 optinfo_flags : 0,
208# endif
209 gate : MyPassGateCallback,
210 execute : MyPassExecuteCallback,
211 sub : NULL,
212 next : NULL,
213 static_pass_number : 0,
214 tv_id : TV_NONE,
215 properties_required : 0,
216 properties_provided : 0,
217 properties_destroyed : 0,
218 todo_flags_start : 0,
219 todo_flags_finish : 0,
220 }
221};
222
223/** The registration info for my pass. */
224static const struct register_pass_info g_MyPassInfo =
225{
226 pass : &g_MyPass.pass,
227 reference_pass_name : "ssa",
228 ref_pass_instance_number : 1,
229 pos_op : PASS_POS_INSERT_BEFORE,
230};
231
232#endif /* < 4.9.0 */
233
234
235/** Attribute specifications. */
236static const struct attribute_spec g_AttribSpecs[] =
237{
238 {
239 name : "iprt_format",
240 min_length : 2,
241 max_length : 2,
242 decl_required : false,
243 type_required : true,
244 function_type_required : true,
245// gcc 7.3 at least moves this field to after "handler", and with 8.3 it is back
246#if RT_GNUC_PREREQ(4, 6) && !(RT_GNUC_PREREQ(7, 0) && !RT_GNUC_PREREQ(8, 0))
247 affects_type_identity : false,
248#endif
249 handler : AttributeHandler,
250#if RT_GNUC_PREREQ(7, 0) && !RT_GNUC_PREREQ(8, 0)
251 affects_type_identity : false,
252#endif
253#if RT_GNUC_PREREQ(8, 0)
254 exclude : NULL,
255#endif
256 },
257 {
258 name : "iprt_format_maybe_null",
259 min_length : 2,
260 max_length : 2,
261 decl_required : false,
262 type_required : true,
263 function_type_required : true,
264#if RT_GNUC_PREREQ(4, 6) && !(RT_GNUC_PREREQ(7, 0) && !RT_GNUC_PREREQ(8, 0))
265 affects_type_identity : false,
266#endif
267 handler : AttributeHandler,
268#if RT_GNUC_PREREQ(7, 0) && !RT_GNUC_PREREQ(8, 0)
269 affects_type_identity : false,
270#endif
271#if RT_GNUC_PREREQ(8, 0)
272 exclude : NULL,
273#endif
274 }
275};
276
277
278#ifdef DEBUG
279
280/**
281 * Debug function for printing the scope of a decl.
282 * @param hDecl Declaration to print scope for.
283 */
284static void dprintScope(tree hDecl)
285{
286# if 0 /* later? */
287 tree hScope = CP_DECL_CONTEXT(hDecl);
288 if (hScope == global_namespace)
289 return;
290 if (TREE_CODE(hScope) == RECORD_TYPE)
291 hScope = TYPE_NAME(hScope);
292
293 /* recurse */
294 dprintScope(hScope);
295
296 /* name the scope. */
297 dprintf("::%s", DECL_NAME(hScope) ? IDENTIFIER_POINTER(DECL_NAME(hScope)) : "<noname>");
298# endif
299}
300
301
302/**
303 * Debug function for printing a declaration.
304 * @param hDecl The declaration to print.
305 */
306static void dprintDecl(tree hDecl)
307{
308 enum tree_code const enmDeclCode = TREE_CODE(hDecl);
309 tree const hType = TREE_TYPE(hDecl);
310 enum tree_code const enmTypeCode = hType ? TREE_CODE(hType) : (enum tree_code)-1;
311#if 0
312 if ( enmTypeCode == RECORD_TYPE
313 && enmDeclCode == TYPE_DECL
314 && DECL_ARTIFICIAL(hDecl))
315 dprint_class(hType);
316#endif
317
318 dprintf("%s ", get_tree_code_name(enmDeclCode));
319 dprintScope(hDecl);
320 dprintf("::%s", DECL_NAME(hDecl) ? IDENTIFIER_POINTER(DECL_NAME(hDecl)) : "<noname>");
321 if (hType)
322 dprintf(" type %s", get_tree_code_name(enmTypeCode));
323 dprintf(" @%s:%d", DECL_SOURCE_FILE(hDecl), DECL_SOURCE_LINE(hDecl));
324}
325
326#endif /* DEBUG */
327
328
329static location_t MyGetLocationPlusColumnOffset(location_t hLoc, unsigned int offColumn)
330{
331 /*
332 * Skip NOOPs, reserved locations and macro expansion.
333 */
334 if ( offColumn != 0
335 && hLoc >= RESERVED_LOCATION_COUNT
336 && !linemap_location_from_macro_expansion_p(line_table, hLoc))
337 {
338#if __GNUC__ >= 5 /** @todo figure this... */
339 /*
340 * There is an API for doing this, nice.
341 */
342 location_t hNewLoc = linemap_position_for_loc_and_offset(line_table, hLoc, offColumn);
343 if (hNewLoc && hNewLoc != hLoc)
344 {
345 dprintf("MyGetLocationPlusColumnOffset: hNewLoc=%#x hLoc=%#x offColumn=%u\n", hNewLoc, hLoc, offColumn);
346 return hNewLoc;
347 }
348
349#elif __GNUC_MINOR__ > 5
350 /*
351 * Have to do the job ourselves, it seems. This is a bit hairy...
352 */
353 line_map const *pMap = NULL;
354 location_t hLoc2 = linemap_resolve_location(line_table, hLoc, LRK_SPELLING_LOCATION, &pMap);
355 if (hLoc2)
356 hLoc = hLoc2;
357
358 /* Guard against wrap arounds and overlaps. */
359 if ( hLoc + offColumn > MAP_START_LOCATION(pMap) /** @todo Use MAX_SOURCE_LOCATION? */
360 && ( pMap == LINEMAPS_LAST_ORDINARY_MAP(line_table)
361 || hLoc + offColumn < MAP_START_LOCATION((pMap + 1))))
362 {
363 /* Calc new column and check that it's within the valid range. */
364 unsigned int uColumn = SOURCE_COLUMN(pMap, hLoc) + offColumn;
365 if (uColumn < RT_BIT_32(ORDINARY_MAP_NUMBER_OF_COLUMN_BITS(pMap)))
366 {
367 /* Try add the position. If we get a valid result, replace the location. */
368 source_location hNewLoc = linemap_position_for_line_and_column((line_map *)pMap, SOURCE_LINE(pMap, hLoc), uColumn);
369 if ( hNewLoc <= line_table->highest_location
370 && linemap_lookup(line_table, hNewLoc) != NULL)
371 {
372 dprintf("MyGetLocationPlusColumnOffset: hNewLoc=%#x hLoc=%#x offColumn=%u uColumn=%u\n",
373 hNewLoc, hLoc, offColumn, uColumn);
374 return hNewLoc;
375 }
376 }
377 }
378#endif
379 }
380 dprintf("MyGetLocationPlusColumnOffset: taking fallback\n");
381 return hLoc;
382}
383
384
385#if 0
386DECLINLINE(int) MyGetLineLength(const char *pszLine)
387{
388 if (pszLine)
389 {
390 const char *pszEol = strpbrk(pszLine, "\n\r");
391 if (!pszEol)
392 pszEol = strchr(pszLine, '\0');
393 return (int)(pszEol - pszLine);
394 }
395 return 0;
396}
397#endif
398
399static location_t MyGetFormatStringLocation(PVFMTCHKSTATE pState, const char *pszLoc)
400{
401 location_t hLoc = pState->hFmtLoc;
402#if RT_GNUC_PREREQ(4,6)
403 intptr_t offString = pszLoc - pState->pszFmt;
404 if ( offString >= 0
405 && !linemap_location_from_macro_expansion_p(line_table, hLoc))
406 {
407 unsigned uCol = 1 + offString;
408# if 0 /* apparently not needed */
409 expanded_location XLoc = expand_location_to_spelling_point(hLoc);
410# if RT_GNUC_PREREQ(10,0)
411 char_span Span = location_get_source_line(XLoc.file, XLoc.line);
412 const char *pszLine = Span.m_ptr; /** @todo if enabled */
413 int cchLine = (int)Span.m_n_elts;
414# elif RT_GNUC_PREREQ(6,0)
415 int cchLine = 0;
416 const char *pszLine = location_get_source_line(XLoc.file, XLoc.line, &cchLine);
417# elif RT_GNUC_PREREQ(5,0)
418 int cchLine = 0;
419 const char *pszLine = location_get_source_line(XLoc, &cchLine);
420# else
421 const char *pszLine = location_get_source_line(XLoc);
422 int cchLine = MyGetLineLength(pszLine);
423# endif
424 if (pszLine)
425 {
426 /** @todo Adjust the position by parsing the source. */
427 pszLine += XLoc.column - 1;
428 cchLine -= XLoc.column - 1;
429 }
430# endif
431
432 hLoc = MyGetLocationPlusColumnOffset(hLoc, uCol);
433 }
434#endif
435 return hLoc;
436}
437
438
439/**
440 * Non-recursive worker for MyCheckFormatRecursive.
441 *
442 * This will attempt to result @a hFmtArg into a string literal which it then
443 * passes on to MyCheckFormatString for the actual analyzis.
444 *
445 * @param pState The format string checking state.
446 * @param hFmtArg The format string node.
447 */
448DECL_NO_INLINE(static, void) MyCheckFormatNonRecursive(PVFMTCHKSTATE pState, tree hFmtArg)
449{
450 dprintf("checker: hFmtArg=%p %s\n", hFmtArg, get_tree_code_name(TREE_CODE(hFmtArg)));
451
452 /*
453 * Try resolve variables into constant strings.
454 */
455 if (VAR_P(hFmtArg))
456 {
457 hFmtArg = decl_constant_value(hFmtArg);
458 STRIP_NOPS(hFmtArg); /* Used as argument and assigned call result. */
459 dprintf("checker1: variable => hFmtArg=%p %s\n", hFmtArg, get_tree_code_name(TREE_CODE(hFmtArg)));
460 }
461
462 /*
463 * Fend off NULLs.
464 */
465 if (integer_zerop(hFmtArg))
466 {
467 if (pState->fMaybeNull)
468 VFmtChkVerifyEndOfArgs(pState, 0);
469 else
470 error_at(MY_LOC(hFmtArg, pState), "Format string should not be NULL");
471 }
472 /*
473 * Need address expression to get any further.
474 */
475 else if (TREE_CODE(hFmtArg) != ADDR_EXPR)
476 dprintf("checker1: Not address expression (%s)\n", get_tree_code_name(TREE_CODE(hFmtArg)));
477 else
478 {
479 pState->hFmtLoc = EXPR_LOC_OR_LOC(hFmtArg, pState->hFmtLoc);
480 hFmtArg = TREE_OPERAND(hFmtArg, 0);
481
482 /*
483 * Deal with fixed string indexing, if possible.
484 */
485 HOST_WIDE_INT off = 0;
486 if ( TREE_CODE(hFmtArg) == ARRAY_REF
487 && MY_DOUBLE_INT_FITS_SHWI(TREE_OPERAND(hFmtArg, 1))
488 && MY_DOUBLE_INT_FITS_SHWI(TREE_OPERAND(hFmtArg, 1)) )
489 {
490 off = MY_DOUBLE_INT_TO_SHWI(TREE_OPERAND(hFmtArg, 1));
491 if (off < 0)
492 {
493 dprintf("checker1: ARRAY_REF, off=%ld\n", off);
494 return;
495 }
496 hFmtArg = TREE_OPERAND(hFmtArg, 0);
497 dprintf("checker1: ARRAY_REF => hFmtArg=%p %s, off=%ld\n", hFmtArg, get_tree_code_name(TREE_CODE(hFmtArg)), off);
498 }
499
500 /*
501 * Deal with static const char g_szFmt[] = "qwerty"; Take care as
502 * the actual string constant may not necessarily include the terminator.
503 */
504 tree hArraySize = NULL_TREE;
505 if ( VAR_P(hFmtArg)
506 && TREE_CODE(TREE_TYPE(hFmtArg)) == ARRAY_TYPE)
507 {
508 tree hArrayInitializer = decl_constant_value(hFmtArg);
509 if ( hArrayInitializer != hFmtArg
510 && TREE_CODE(hArrayInitializer) == STRING_CST)
511 {
512 hArraySize = DECL_SIZE_UNIT(hFmtArg);
513 hFmtArg = hArrayInitializer;
514 }
515 }
516
517 /*
518 * Are we dealing with a string literal now?
519 */
520 if (TREE_CODE(hFmtArg) != STRING_CST)
521 dprintf("checker1: Not string literal (%s)\n", get_tree_code_name(TREE_CODE(hFmtArg)));
522 else if (TYPE_MAIN_VARIANT(TREE_TYPE(TREE_TYPE(hFmtArg))) != char_type_node)
523 warning_at(pState->hFmtLoc, 0, "expected 'char' type string literal");
524 else
525 {
526 /*
527 * Yes we are, so get the pointer to the string and its length.
528 */
529 const char *pszFmt = TREE_STRING_POINTER(hFmtArg);
530 int cchFmt = TREE_STRING_LENGTH(hFmtArg);
531
532 /* Adjust cchFmt to the initialized array size if appropriate. */
533 if (hArraySize != NULL_TREE)
534 {
535 if (TREE_CODE(hArraySize) != INTEGER_CST)
536 warning_at(pState->hFmtLoc, 0, "Expected integer array size (not %s)", get_tree_code_name(TREE_CODE(hArraySize)));
537 else if (!MY_DOUBLE_INT_FITS_SHWI(hArraySize))
538 warning_at(pState->hFmtLoc, 0, "Unexpected integer overflow in array size constant");
539 else
540 {
541 HOST_WIDE_INT cbArray = MY_DOUBLE_INT_TO_SHWI(hArraySize);
542 if ( cbArray <= 0
543 || cbArray != (int)cbArray)
544 warning_at(pState->hFmtLoc, 0, "Unexpected integer array size constant value: %ld", cbArray);
545 else if (cchFmt > cbArray)
546 {
547 dprintf("checker1: cchFmt=%d => cchFmt=%ld (=cbArray)\n", cchFmt, cbArray);
548 cchFmt = (int)cbArray;
549 }
550 }
551 }
552
553 /* Apply the offset, if given. */
554 if (off)
555 {
556 if (off >= cchFmt)
557 {
558 dprintf("checker1: off=%ld >= cchFmt=%d -> skipping\n", off, cchFmt);
559 return;
560 }
561 pszFmt += off;
562 cchFmt -= (int)off;
563 }
564
565 /*
566 * Check for unterminated strings.
567 */
568 if ( cchFmt < 1
569 || pszFmt[cchFmt - 1] != '\0')
570 warning_at(pState->hFmtLoc, 0, "Unterminated format string (cchFmt=%d)", cchFmt);
571 /*
572 * Call worker to check the actual string.
573 */
574 else
575 MyCheckFormatCString(pState, pszFmt);
576 }
577 }
578}
579
580
581/**
582 * Deal recursively with special format string constructs.
583 *
584 * This will call MyCheckFormatNonRecursive to validate each format string.
585 *
586 * @param pState The format string checking state.
587 * @param hFmtArg The format string node.
588 */
589static void MyCheckFormatRecursive(PVFMTCHKSTATE pState, tree hFmtArg)
590{
591 /*
592 * Catch wrong attribute use.
593 */
594 if (hFmtArg == NULL_TREE)
595 error_at(pState->hFmtLoc, "IPRT format attribute is probably used incorrectly (hFmtArg is NULL)");
596 /*
597 * NULL format strings may cause crashes.
598 */
599 else if (integer_zerop(hFmtArg))
600 {
601 if (pState->fMaybeNull)
602 VFmtChkVerifyEndOfArgs(pState, 0);
603 else
604 error_at(MY_LOC(hFmtArg, pState), "Format string should not be NULL");
605 }
606 /*
607 * Check both branches of a ternary operator.
608 */
609 else if (TREE_CODE(hFmtArg) == COND_EXPR)
610 {
611 MyCheckFormatRecursive(pState, TREE_OPERAND(hFmtArg, 1));
612 MyCheckFormatRecursive(pState, TREE_OPERAND(hFmtArg, 2));
613 }
614 /*
615 * Strip coercion.
616 */
617 else if ( CONVERT_EXPR_P(hFmtArg)
618 && TYPE_PRECISION(TREE_TYPE(hFmtArg)) == TYPE_PRECISION(TREE_TYPE(TREE_OPERAND(hFmtArg, 0))) )
619 MyCheckFormatRecursive(pState, TREE_OPERAND(hFmtArg, 0));
620 /*
621 * We're good, hand it to the non-recursive worker.
622 */
623 else
624 MyCheckFormatNonRecursive(pState, hFmtArg);
625}
626
627
628#if !RT_GNUC_PREREQ(4, 9)
629/**
630 * Execute my pass.
631 * @returns Flags indicates stuff todo, we return 0.
632 */
633static unsigned int MyPassExecuteCallback(void)
634{
635 return MyPassExecuteCallbackWithFunction(cfun);
636}
637#endif
638
639/**
640 * Execute my pass.
641 * @returns Flags indicates stuff todo, we return 0.
642 */
643static unsigned int MyPassExecuteCallbackWithFunction(struct function *pFun)
644{
645 dprintf("MyPassExecuteCallback:\n");
646
647 /*
648 * Enumerate the basic blocks.
649 */
650 basic_block hBasicBlock;
651 FOR_EACH_BB_FN(hBasicBlock, pFun)
652 {
653 dprintf(" hBasicBlock=%p\n", hBasicBlock);
654
655 /*
656 * Enumerate the statements in the current basic block.
657 * We're interested in calls to functions with the __iprt_format__ attribute.
658 */
659 for (gimple_stmt_iterator hStmtItr = gsi_start_bb(hBasicBlock); !gsi_end_p(hStmtItr); gsi_next(&hStmtItr))
660 {
661#if RT_GNUC_PREREQ(6, 0)
662 const gimple * const hStmt = gsi_stmt(hStmtItr);
663#else
664 gimple const hStmt = gsi_stmt(hStmtItr);
665#endif
666
667 enum gimple_code const enmCode = gimple_code(hStmt);
668#ifdef DEBUG
669 unsigned const cOps = gimple_num_ops(hStmt);
670 dprintf(" hStmt=%p %s (%d) ops=%d\n", hStmt, gimple_code_name[enmCode], enmCode, cOps);
671 for (unsigned iOp = 0; iOp < cOps; iOp++)
672 {
673 tree const hOp = gimple_op(hStmt, iOp);
674 if (hOp)
675 dprintf(" %02d: %p, code %s(%d)\n", iOp, hOp, get_tree_code_name(TREE_CODE(hOp)), TREE_CODE(hOp));
676 else
677 dprintf(" %02d: NULL_TREE\n", iOp);
678 }
679#endif
680 if (enmCode == GIMPLE_CALL)
681 {
682 /*
683 * Check if the function type has the __iprt_format__ attribute.
684 */
685 tree const hFn = gimple_call_fn(hStmt);
686 dprintf(" hFn =%p %s(%d); args=%d\n",
687 hFn, hFn ? get_tree_code_name(TREE_CODE(hFn)) : NULL, hFn ? TREE_CODE(hFn) : - 1,
688 gimple_call_num_args(hStmt));
689#ifdef DEBUG
690 if (hFn && DECL_P(hFn))
691 dprintf(" hFn is decl: %s %s:%d\n",
692 DECL_NAME(hFn) ? IDENTIFIER_POINTER(DECL_NAME(hFn)) : "<unamed>",
693 DECL_SOURCE_FILE(hFn), DECL_SOURCE_LINE(hFn));
694#endif
695 tree const hFnDecl = gimple_call_fndecl(hStmt);
696 if (hFnDecl)
697 dprintf(" hFnDecl=%p %s(%d) %s type=%p %s:%d\n",
698 hFnDecl, get_tree_code_name(TREE_CODE(hFnDecl)), TREE_CODE(hFnDecl),
699 DECL_NAME(hFnDecl) ? IDENTIFIER_POINTER(DECL_NAME(hFnDecl)) : "<unamed>",
700 TREE_TYPE(hFnDecl), DECL_SOURCE_FILE(hFnDecl), DECL_SOURCE_LINE(hFnDecl));
701 tree const hFnType = gimple_call_fntype(hStmt);
702 if (hFnType == NULL_TREE)
703 {
704 if ( hFnDecl == NULL_TREE
705 && gimple_call_internal_p(hStmt) /* va_arg() kludge */)
706 continue;
707 error_at(gimple_location(hStmt), "Failed to resolve function type [fn=%s fndecl=%s]\n",
708 hFn ? get_tree_code_name(TREE_CODE(hFn)) : "<null>",
709 hFnDecl ? get_tree_code_name(TREE_CODE(hFnDecl)) : "<null>");
710 }
711 else if (POINTER_TYPE_P(hFnType))
712 error_at(gimple_location(hStmt), "Got a POINTER_TYPE when expecting a function type [fn=%s]\n",
713 get_tree_code_name(TREE_CODE(hFn)));
714 if (hFnType)
715 dprintf(" hFnType=%p %s(%d) %s\n", hFnType, get_tree_code_name(TREE_CODE(hFnType)), TREE_CODE(hFnType),
716 TYPE_NAME(hFnType) && DECL_NAME(TYPE_NAME(hFnType))
717 ? IDENTIFIER_POINTER(DECL_NAME(TYPE_NAME(hFnType))) : "<unamed>");
718
719 tree const hAttr = hFnType ? lookup_attribute("iprt_format", TYPE_ATTRIBUTES(hFnType)) : NULL_TREE;
720 tree const hAttrMaybe0 = hFnType ? lookup_attribute("iprt_format_maybe_null", TYPE_ATTRIBUTES(hFnType)) : NULL_TREE;
721 if (hAttr || hAttrMaybe0)
722 {
723 /*
724 * Yeah, it has the attribute!
725 */
726 tree const hAttrArgs = hAttr ? TREE_VALUE(hAttr) : TREE_VALUE(hAttrMaybe0);
727 VFMTCHKSTATE State;
728 State.iFmt = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(hAttrArgs));
729 State.iArgs = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(TREE_CHAIN(hAttrArgs)));
730 State.pszFmt = NULL;
731 State.fMaybeNull = hAttr == NULL_TREE;
732 State.hStmt = hStmt;
733 State.hFmtLoc = gimple_location(hStmt);
734 dprintf(" %s() __iprt_format%s__(iFmt=%ld, iArgs=%ld)\n",
735 hFnDecl && DECL_NAME(hFnDecl) ? IDENTIFIER_POINTER(DECL_NAME(hFnDecl)) : "<unamed>",
736 State.fMaybeNull ? "_maybe_null" : "", State.iFmt, State.iArgs);
737
738 unsigned cCallArgs = gimple_call_num_args(hStmt);
739 if (cCallArgs >= State.iFmt)
740 MyCheckFormatRecursive(&State, gimple_call_arg(hStmt, State.iFmt - 1));
741 else
742 error_at(gimple_location(hStmt),
743 "Call has only %d arguments; %s() format string is argument #%lu (1-based), thus missing\n",
744 cCallArgs, DECL_NAME(hFnDecl) ? IDENTIFIER_POINTER(DECL_NAME(hFnDecl)) : "<unamed>", State.iFmt);
745 }
746 }
747 }
748 }
749 return 0;
750}
751
752
753/**
754 * Gate callback for my pass that indicates whether it should execute or not.
755 * @returns true to execute.
756 */
757static bool MyPassGateCallback(void)
758{
759 dprintf("MyPassGateCallback:\n");
760 return true;
761}
762
763
764/**
765 * Validate the use of an attribute.
766 *
767 * @returns ??
768 * @param phOnNode The node the attribute is being used on.
769 * @param hAttrName The attribute name.
770 * @param hAttrArgs The attribute arguments.
771 * @param fFlags Some kind of flags...
772 * @param pfDontAddAttrib Whether to add the attribute to this node or not.
773 */
774static tree AttributeHandler(tree *phOnNode, tree hAttrName, tree hAttrArgs, int fFlags, bool *pfDontAddAttrib)
775{
776 dprintf("AttributeHandler: name=%s fFlags=%#x", IDENTIFIER_POINTER(hAttrName), fFlags);
777 long iFmt = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(hAttrArgs));
778 long iArgs = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(TREE_CHAIN(hAttrArgs)));
779 dprintf(" iFmt=%ld iArgs=%ld", iFmt, iArgs);
780
781 tree hType = *phOnNode;
782 dprintf(" hType=%p %s(%d)\n", hType, get_tree_code_name(TREE_CODE(hType)), TREE_CODE(hType));
783
784 if (pfDontAddAttrib)
785 *pfDontAddAttrib = false;
786 return NULL_TREE;
787}
788
789
790/**
791 * Called when we can register attributes.
792 *
793 * @param pvEventData Ignored.
794 * @param pvUser Ignored.
795 */
796static void RegisterAttributesEvent(void *pvEventData, void *pvUser)
797{
798 NOREF(pvEventData); NOREF(pvUser);
799 dprintf("RegisterAttributesEvent: pvEventData=%p\n", pvEventData);
800
801 register_attribute(&g_AttribSpecs[0]);
802 register_attribute(&g_AttribSpecs[1]);
803}
804
805
806/**
807 * The plug-in entry point.
808 *
809 * @returns 0 to indicate success?
810 * @param pPlugInInfo Plugin info structure.
811 * @param pGccVer GCC Version.
812 */
813int plugin_init(plugin_name_args *pPlugInInfo, plugin_gcc_version *pGccVer)
814{
815 dprintf("plugin_init: %s\n", pPlugInInfo->full_name);
816 dprintf("gcc version: basever=%s datestamp=%s devphase=%s revision=%s\n",
817 pGccVer->basever, pGccVer->datestamp, pGccVer->devphase, pGccVer->revision);
818
819 /* Ask for callback in which we may register the attribute. */
820 register_callback(pPlugInInfo->base_name, PLUGIN_ATTRIBUTES, RegisterAttributesEvent, NULL /*pvUser*/);
821
822 /* Register our pass. */
823#if RT_GNUC_PREREQ(4, 9)
824 /** The registration info for my pass. */
825 struct register_pass_info MyPassInfo;
826 MyPassInfo.pass = new MyPass(g);
827 MyPassInfo.reference_pass_name = "ssa";
828 MyPassInfo.ref_pass_instance_number = 1;
829 MyPassInfo.pos_op = PASS_POS_INSERT_BEFORE;
830 register_callback(pPlugInInfo->base_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &MyPassInfo);
831#else
832 register_callback(pPlugInInfo->base_name, PLUGIN_PASS_MANAGER_SETUP, NULL, (void *)&g_MyPassInfo);
833#endif
834
835 /* Register plug-in info. */
836 register_callback(pPlugInInfo->base_name, PLUGIN_INFO, NULL, (void *)&g_PlugInInfo);
837
838 return 0;
839}
840
841
842
843
844/*
845 *
846 * Functions used by the common code.
847 * Functions used by the common code.
848 * Functions used by the common code.
849 *
850 */
851
852void VFmtChkWarnFmt(PVFMTCHKSTATE pState, const char *pszLoc, const char *pszFormat, ...)
853{
854 char szTmp[1024];
855 va_list va;
856 va_start(va, pszFormat);
857 vsnprintf(szTmp, sizeof(szTmp), pszFormat, va);
858 va_end(va);
859
860 /* display the warning. */
861 warning_at(MyGetFormatStringLocation(pState, pszLoc), 0, "%s", szTmp);
862}
863
864
865void VFmtChkErrFmt(PVFMTCHKSTATE pState, const char *pszLoc, const char *pszFormat, ...)
866{
867 char szTmp[1024];
868 va_list va;
869 va_start(va, pszFormat);
870 vsnprintf(szTmp, sizeof(szTmp), pszFormat, va);
871 va_end(va);
872
873 /* display the warning. */
874 error_at(MyGetFormatStringLocation(pState, pszLoc), "%s", szTmp);
875}
876
877
878
879void VFmtChkVerifyEndOfArgs(PVFMTCHKSTATE pState, unsigned iArg)
880{
881 dprintf("VFmtChkVerifyEndOfArgs: iArg=%u iArgs=%ld cArgs=%u\n", iArg, pState->iArgs, gimple_call_num_args(pState->hStmt));
882 if (pState->iArgs > 0)
883 {
884 iArg += pState->iArgs - 1;
885 unsigned cArgs = gimple_call_num_args(pState->hStmt);
886 if (iArg == cArgs)
887 { /* fine */ }
888 else if (iArg < cArgs)
889 {
890 tree hArg = gimple_call_arg(pState->hStmt, iArg);
891 if (cArgs - iArg > 1)
892 error_at(MY_LOC(hArg, pState), "%u extra arguments not consumed by format string", cArgs - iArg);
893 else if ( TREE_CODE(hArg) != INTEGER_CST
894 || !MY_DOUBLE_INT_FITS_SHWI(hArg)
895 || MY_DOUBLE_INT_TO_SHWI(hArg) != -99) /* ignore final dummy argument: ..., -99); */
896 error_at(MY_LOC(hArg, pState), "one extra argument not consumed by format string");
897 }
898 /* This should be handled elsewhere, but just in case. */
899 else if (iArg - 1 == cArgs)
900 error_at(pState->hFmtLoc, "one argument too few");
901 else
902 error_at(pState->hFmtLoc, "%u arguments too few", iArg - cArgs);
903 }
904}
905
906
907bool VFmtChkRequirePresentArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
908{
909 if (pState->iArgs > 0)
910 {
911 iArg += pState->iArgs - 1;
912 unsigned cArgs = gimple_call_num_args(pState->hStmt);
913 if (iArg >= cArgs)
914 {
915 VFmtChkErrFmt(pState, pszLoc, "Missing argument! %s", pszMessage);
916 return false;
917 }
918
919 tree hArg = gimple_call_arg(pState->hStmt, iArg);
920 tree hType = TREE_TYPE(hArg);
921 dprintf("arg%u: hArg=%p [%s] hType=%p [%s] cls=%s\n", iArg, hArg, get_tree_code_name(TREE_CODE(hArg)),
922 hType, get_tree_code_name(TREE_CODE(hType)), tree_code_class_strings[TREE_CODE_CLASS(TREE_CODE(hType))]);
923 dprintf(" nm=%p\n", TYPE_NAME(hType));
924 dprintf(" cb=%p %s value=%ld\n", TYPE_SIZE(hType), get_tree_code_name(TREE_CODE(TYPE_SIZE(hType))),
925 MY_DOUBLE_INT_TO_SHWI(TYPE_SIZE(hType)) );
926 dprintf(" unit=%p %s value=%ld\n", TYPE_SIZE_UNIT(hType), get_tree_code_name(TREE_CODE(TYPE_SIZE_UNIT(hType))),
927 MY_DOUBLE_INT_TO_SHWI(TYPE_SIZE_UNIT(hType)) );
928 tree hTypeNm = TYPE_NAME(hType);
929 if (hTypeNm)
930 dprintf(" typenm=%p %s '%s'\n", hTypeNm, get_tree_code_name(TREE_CODE(hTypeNm)),
931 IDENTIFIER_POINTER(DECL_NAME(hTypeNm)));
932 }
933 return true;
934}
935
936
937bool VFmtChkRequireIntArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
938{
939 if (VFmtChkRequirePresentArg(pState, pszLoc, iArg, pszMessage))
940 {
941 /** @todo type check. */
942 return true;
943 }
944 return false;
945}
946
947
948bool VFmtChkRequireStringArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
949{
950 if (VFmtChkRequirePresentArg(pState, pszLoc, iArg, pszMessage))
951 {
952 /** @todo type check. */
953 return true;
954 }
955 return false;
956}
957
958
959bool VFmtChkRequireVaListPtrArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
960{
961 if (VFmtChkRequirePresentArg(pState, pszLoc, iArg, pszMessage))
962 {
963 /** @todo type check. */
964 return true;
965 }
966 return false;
967}
968
969
970void VFmtChkHandleReplacementFormatString(PVFMTCHKSTATE pState, const char *pszPctM, unsigned iArg)
971{
972 if (pState->iArgs > 0)
973 {
974 pState->iFmt = pState->iArgs + iArg;
975 pState->iArgs = pState->iFmt + 1;
976 pState->fMaybeNull = false;
977 MyCheckFormatRecursive(pState, gimple_call_arg(pState->hStmt, pState->iFmt - 1));
978 }
979}
980
981
982const char *VFmtChkGetFmtLocFile(PVFMTCHKSTATE pState)
983{
984 return LOCATION_FILE(pState->hFmtLoc);
985}
986
987
988unsigned int VFmtChkGetFmtLocLine(PVFMTCHKSTATE pState)
989{
990 return LOCATION_LINE(pState->hFmtLoc);
991}
992
993
994unsigned int VFmtChkGetFmtLocColumn(PVFMTCHKSTATE pState)
995{
996#ifdef LOCATION_COLUMN
997 return LOCATION_COLUMN(pState->hFmtLoc);
998#else
999 return 1;
1000#endif
1001}
1002
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette