VirtualBox

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

Last change on this file since 86011 was 85502, checked in by vboxsync, 4 years ago

gccplugin: gcc 10 fixes. bugref:9746

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 34.4 KB
Line 
1/* $Id: VBoxCompilerPlugInsGcc.cpp 85502 2020-07-29 09:52:10Z 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# if RT_GNUC_PREREQ(4, 6)
246 affects_type_identity : false,
247#endif
248 handler : AttributeHandler,
249# if RT_GNUC_PREREQ(10, 0)
250 exclude : NULL,
251# endif
252 },
253 {
254 name : "iprt_format_maybe_null",
255 min_length : 2,
256 max_length : 2,
257 decl_required : false,
258 type_required : true,
259 function_type_required : true,
260# if RT_GNUC_PREREQ(4, 6)
261 affects_type_identity : false,
262#endif
263 handler : AttributeHandler,
264# if RT_GNUC_PREREQ(10, 0)
265 exclude : NULL,
266# endif
267 }
268};
269
270
271#ifdef DEBUG
272
273/**
274 * Debug function for printing the scope of a decl.
275 * @param hDecl Declaration to print scope for.
276 */
277static void dprintScope(tree hDecl)
278{
279# if 0 /* later? */
280 tree hScope = CP_DECL_CONTEXT(hDecl);
281 if (hScope == global_namespace)
282 return;
283 if (TREE_CODE(hScope) == RECORD_TYPE)
284 hScope = TYPE_NAME(hScope);
285
286 /* recurse */
287 dprintScope(hScope);
288
289 /* name the scope. */
290 dprintf("::%s", DECL_NAME(hScope) ? IDENTIFIER_POINTER(DECL_NAME(hScope)) : "<noname>");
291# endif
292}
293
294
295/**
296 * Debug function for printing a declaration.
297 * @param hDecl The declaration to print.
298 */
299static void dprintDecl(tree hDecl)
300{
301 enum tree_code const enmDeclCode = TREE_CODE(hDecl);
302 tree const hType = TREE_TYPE(hDecl);
303 enum tree_code const enmTypeCode = hType ? TREE_CODE(hType) : (enum tree_code)-1;
304#if 0
305 if ( enmTypeCode == RECORD_TYPE
306 && enmDeclCode == TYPE_DECL
307 && DECL_ARTIFICIAL(hDecl))
308 dprint_class(hType);
309#endif
310
311 dprintf("%s ", get_tree_code_name(enmDeclCode));
312 dprintScope(hDecl);
313 dprintf("::%s", DECL_NAME(hDecl) ? IDENTIFIER_POINTER(DECL_NAME(hDecl)) : "<noname>");
314 if (hType)
315 dprintf(" type %s", get_tree_code_name(enmTypeCode));
316 dprintf(" @%s:%d", DECL_SOURCE_FILE(hDecl), DECL_SOURCE_LINE(hDecl));
317}
318
319#endif /* DEBUG */
320
321
322static location_t MyGetLocationPlusColumnOffset(location_t hLoc, unsigned int offColumn)
323{
324 /*
325 * Skip NOOPs, reserved locations and macro expansion.
326 */
327 if ( offColumn != 0
328 && hLoc >= RESERVED_LOCATION_COUNT
329 && !linemap_location_from_macro_expansion_p(line_table, hLoc))
330 {
331#if __GNUC__ >= 5 /** @todo figure this... */
332 /*
333 * There is an API for doing this, nice.
334 */
335 location_t hNewLoc = linemap_position_for_loc_and_offset(line_table, hLoc, offColumn);
336 if (hNewLoc && hNewLoc != hLoc)
337 {
338 dprintf("MyGetLocationPlusColumnOffset: hNewLoc=%#x hLoc=%#x offColumn=%u\n", hNewLoc, hLoc, offColumn);
339 return hNewLoc;
340 }
341
342#elif __GNUC_MINOR__ > 5
343 /*
344 * Have to do the job ourselves, it seems. This is a bit hairy...
345 */
346 line_map const *pMap = NULL;
347 location_t hLoc2 = linemap_resolve_location(line_table, hLoc, LRK_SPELLING_LOCATION, &pMap);
348 if (hLoc2)
349 hLoc = hLoc2;
350
351 /* Guard against wrap arounds and overlaps. */
352 if ( hLoc + offColumn > MAP_START_LOCATION(pMap) /** @todo Use MAX_SOURCE_LOCATION? */
353 && ( pMap == LINEMAPS_LAST_ORDINARY_MAP(line_table)
354 || hLoc + offColumn < MAP_START_LOCATION((pMap + 1))))
355 {
356 /* Calc new column and check that it's within the valid range. */
357 unsigned int uColumn = SOURCE_COLUMN(pMap, hLoc) + offColumn;
358 if (uColumn < RT_BIT_32(ORDINARY_MAP_NUMBER_OF_COLUMN_BITS(pMap)))
359 {
360 /* Try add the position. If we get a valid result, replace the location. */
361 source_location hNewLoc = linemap_position_for_line_and_column((line_map *)pMap, SOURCE_LINE(pMap, hLoc), uColumn);
362 if ( hNewLoc <= line_table->highest_location
363 && linemap_lookup(line_table, hNewLoc) != NULL)
364 {
365 dprintf("MyGetLocationPlusColumnOffset: hNewLoc=%#x hLoc=%#x offColumn=%u uColumn=%u\n",
366 hNewLoc, hLoc, offColumn, uColumn);
367 return hNewLoc;
368 }
369 }
370 }
371#endif
372 }
373 dprintf("MyGetLocationPlusColumnOffset: taking fallback\n");
374 return hLoc;
375}
376
377
378#if 0
379DECLINLINE(int) MyGetLineLength(const char *pszLine)
380{
381 if (pszLine)
382 {
383 const char *pszEol = strpbrk(pszLine, "\n\r");
384 if (!pszEol)
385 pszEol = strchr(pszLine, '\0');
386 return (int)(pszEol - pszLine);
387 }
388 return 0;
389}
390#endif
391
392static location_t MyGetFormatStringLocation(PVFMTCHKSTATE pState, const char *pszLoc)
393{
394 location_t hLoc = pState->hFmtLoc;
395#if RT_GNUC_PREREQ(4,6)
396 intptr_t offString = pszLoc - pState->pszFmt;
397 if ( offString >= 0
398 && !linemap_location_from_macro_expansion_p(line_table, hLoc))
399 {
400 unsigned uCol = 1 + offString;
401# if 0 /* apparently not needed */
402 expanded_location XLoc = expand_location_to_spelling_point(hLoc);
403# if RT_GNUC_PREREQ(10,0)
404 char_span Span = location_get_source_line(XLoc.file, XLoc.line);
405 const char *pszLine = Span.m_ptr; /** @todo if enabled */
406 int cchLine = (int)Span.m_n_elts;
407# elif RT_GNUC_PREREQ(6,0)
408 int cchLine = 0;
409 const char *pszLine = location_get_source_line(XLoc.file, XLoc.line, &cchLine);
410# elif RT_GNUC_PREREQ(5,0)
411 int cchLine = 0;
412 const char *pszLine = location_get_source_line(XLoc, &cchLine);
413# else
414 const char *pszLine = location_get_source_line(XLoc);
415 int cchLine = MyGetLineLength(pszLine);
416# endif
417 if (pszLine)
418 {
419 /** @todo Adjust the position by parsing the source. */
420 pszLine += XLoc.column - 1;
421 cchLine -= XLoc.column - 1;
422 }
423# endif
424
425 hLoc = MyGetLocationPlusColumnOffset(hLoc, uCol);
426 }
427#endif
428 return hLoc;
429}
430
431
432/**
433 * Non-recursive worker for MyCheckFormatRecursive.
434 *
435 * This will attempt to result @a hFmtArg into a string literal which it then
436 * passes on to MyCheckFormatString for the actual analyzis.
437 *
438 * @param pState The format string checking state.
439 * @param hFmtArg The format string node.
440 */
441DECL_NO_INLINE(static, void) MyCheckFormatNonRecursive(PVFMTCHKSTATE pState, tree hFmtArg)
442{
443 dprintf("checker: hFmtArg=%p %s\n", hFmtArg, get_tree_code_name(TREE_CODE(hFmtArg)));
444
445 /*
446 * Try resolve variables into constant strings.
447 */
448 if (VAR_P(hFmtArg))
449 {
450 hFmtArg = decl_constant_value(hFmtArg);
451 STRIP_NOPS(hFmtArg); /* Used as argument and assigned call result. */
452 dprintf("checker1: variable => hFmtArg=%p %s\n", hFmtArg, get_tree_code_name(TREE_CODE(hFmtArg)));
453 }
454
455 /*
456 * Fend off NULLs.
457 */
458 if (integer_zerop(hFmtArg))
459 {
460 if (pState->fMaybeNull)
461 VFmtChkVerifyEndOfArgs(pState, 0);
462 else
463 error_at(MY_LOC(hFmtArg, pState), "Format string should not be NULL");
464 }
465 /*
466 * Need address expression to get any further.
467 */
468 else if (TREE_CODE(hFmtArg) != ADDR_EXPR)
469 dprintf("checker1: Not address expression (%s)\n", get_tree_code_name(TREE_CODE(hFmtArg)));
470 else
471 {
472 pState->hFmtLoc = EXPR_LOC_OR_LOC(hFmtArg, pState->hFmtLoc);
473 hFmtArg = TREE_OPERAND(hFmtArg, 0);
474
475 /*
476 * Deal with fixed string indexing, if possible.
477 */
478 HOST_WIDE_INT off = 0;
479 if ( TREE_CODE(hFmtArg) == ARRAY_REF
480 && MY_DOUBLE_INT_FITS_SHWI(TREE_OPERAND(hFmtArg, 1))
481 && MY_DOUBLE_INT_FITS_SHWI(TREE_OPERAND(hFmtArg, 1)) )
482 {
483 off = MY_DOUBLE_INT_TO_SHWI(TREE_OPERAND(hFmtArg, 1));
484 if (off < 0)
485 {
486 dprintf("checker1: ARRAY_REF, off=%ld\n", off);
487 return;
488 }
489 hFmtArg = TREE_OPERAND(hFmtArg, 0);
490 dprintf("checker1: ARRAY_REF => hFmtArg=%p %s, off=%ld\n", hFmtArg, get_tree_code_name(TREE_CODE(hFmtArg)), off);
491 }
492
493 /*
494 * Deal with static const char g_szFmt[] = "qwerty"; Take care as
495 * the actual string constant may not necessarily include the terminator.
496 */
497 tree hArraySize = NULL_TREE;
498 if ( VAR_P(hFmtArg)
499 && TREE_CODE(TREE_TYPE(hFmtArg)) == ARRAY_TYPE)
500 {
501 tree hArrayInitializer = decl_constant_value(hFmtArg);
502 if ( hArrayInitializer != hFmtArg
503 && TREE_CODE(hArrayInitializer) == STRING_CST)
504 {
505 hArraySize = DECL_SIZE_UNIT(hFmtArg);
506 hFmtArg = hArrayInitializer;
507 }
508 }
509
510 /*
511 * Are we dealing with a string literal now?
512 */
513 if (TREE_CODE(hFmtArg) != STRING_CST)
514 dprintf("checker1: Not string literal (%s)\n", get_tree_code_name(TREE_CODE(hFmtArg)));
515 else if (TYPE_MAIN_VARIANT(TREE_TYPE(TREE_TYPE(hFmtArg))) != char_type_node)
516 warning_at(pState->hFmtLoc, 0, "expected 'char' type string literal");
517 else
518 {
519 /*
520 * Yes we are, so get the pointer to the string and its length.
521 */
522 const char *pszFmt = TREE_STRING_POINTER(hFmtArg);
523 int cchFmt = TREE_STRING_LENGTH(hFmtArg);
524
525 /* Adjust cchFmt to the initialized array size if appropriate. */
526 if (hArraySize != NULL_TREE)
527 {
528 if (TREE_CODE(hArraySize) != INTEGER_CST)
529 warning_at(pState->hFmtLoc, 0, "Expected integer array size (not %s)", get_tree_code_name(TREE_CODE(hArraySize)));
530 else if (!MY_DOUBLE_INT_FITS_SHWI(hArraySize))
531 warning_at(pState->hFmtLoc, 0, "Unexpected integer overflow in array size constant");
532 else
533 {
534 HOST_WIDE_INT cbArray = MY_DOUBLE_INT_TO_SHWI(hArraySize);
535 if ( cbArray <= 0
536 || cbArray != (int)cbArray)
537 warning_at(pState->hFmtLoc, 0, "Unexpected integer array size constant value: %ld", cbArray);
538 else if (cchFmt > cbArray)
539 {
540 dprintf("checker1: cchFmt=%d => cchFmt=%ld (=cbArray)\n", cchFmt, cbArray);
541 cchFmt = (int)cbArray;
542 }
543 }
544 }
545
546 /* Apply the offset, if given. */
547 if (off)
548 {
549 if (off >= cchFmt)
550 {
551 dprintf("checker1: off=%ld >= cchFmt=%d -> skipping\n", off, cchFmt);
552 return;
553 }
554 pszFmt += off;
555 cchFmt -= (int)off;
556 }
557
558 /*
559 * Check for unterminated strings.
560 */
561 if ( cchFmt < 1
562 || pszFmt[cchFmt - 1] != '\0')
563 warning_at(pState->hFmtLoc, 0, "Unterminated format string (cchFmt=%d)", cchFmt);
564 /*
565 * Call worker to check the actual string.
566 */
567 else
568 MyCheckFormatCString(pState, pszFmt);
569 }
570 }
571}
572
573
574/**
575 * Deal recursively with special format string constructs.
576 *
577 * This will call MyCheckFormatNonRecursive to validate each format string.
578 *
579 * @param pState The format string checking state.
580 * @param hFmtArg The format string node.
581 */
582static void MyCheckFormatRecursive(PVFMTCHKSTATE pState, tree hFmtArg)
583{
584 /*
585 * Catch wrong attribute use.
586 */
587 if (hFmtArg == NULL_TREE)
588 error_at(pState->hFmtLoc, "IPRT format attribute is probably used incorrectly (hFmtArg is NULL)");
589 /*
590 * NULL format strings may cause crashes.
591 */
592 else if (integer_zerop(hFmtArg))
593 {
594 if (pState->fMaybeNull)
595 VFmtChkVerifyEndOfArgs(pState, 0);
596 else
597 error_at(MY_LOC(hFmtArg, pState), "Format string should not be NULL");
598 }
599 /*
600 * Check both branches of a ternary operator.
601 */
602 else if (TREE_CODE(hFmtArg) == COND_EXPR)
603 {
604 MyCheckFormatRecursive(pState, TREE_OPERAND(hFmtArg, 1));
605 MyCheckFormatRecursive(pState, TREE_OPERAND(hFmtArg, 2));
606 }
607 /*
608 * Strip coercion.
609 */
610 else if ( CONVERT_EXPR_P(hFmtArg)
611 && TYPE_PRECISION(TREE_TYPE(hFmtArg)) == TYPE_PRECISION(TREE_TYPE(TREE_OPERAND(hFmtArg, 0))) )
612 MyCheckFormatRecursive(pState, TREE_OPERAND(hFmtArg, 0));
613 /*
614 * We're good, hand it to the non-recursive worker.
615 */
616 else
617 MyCheckFormatNonRecursive(pState, hFmtArg);
618}
619
620
621#if !RT_GNUC_PREREQ(4, 9)
622/**
623 * Execute my pass.
624 * @returns Flags indicates stuff todo, we return 0.
625 */
626static unsigned int MyPassExecuteCallback(void)
627{
628 return MyPassExecuteCallbackWithFunction(cfun);
629}
630#endif
631
632/**
633 * Execute my pass.
634 * @returns Flags indicates stuff todo, we return 0.
635 */
636static unsigned int MyPassExecuteCallbackWithFunction(struct function *pFun)
637{
638 dprintf("MyPassExecuteCallback:\n");
639
640 /*
641 * Enumerate the basic blocks.
642 */
643 basic_block hBasicBlock;
644 FOR_EACH_BB_FN(hBasicBlock, pFun)
645 {
646 dprintf(" hBasicBlock=%p\n", hBasicBlock);
647
648 /*
649 * Enumerate the statements in the current basic block.
650 * We're interested in calls to functions with the __iprt_format__ attribute.
651 */
652 for (gimple_stmt_iterator hStmtItr = gsi_start_bb(hBasicBlock); !gsi_end_p(hStmtItr); gsi_next(&hStmtItr))
653 {
654#if RT_GNUC_PREREQ(6, 0)
655 const gimple * const hStmt = gsi_stmt(hStmtItr);
656#else
657 gimple const hStmt = gsi_stmt(hStmtItr);
658#endif
659
660 enum gimple_code const enmCode = gimple_code(hStmt);
661#ifdef DEBUG
662 unsigned const cOps = gimple_num_ops(hStmt);
663 dprintf(" hStmt=%p %s (%d) ops=%d\n", hStmt, gimple_code_name[enmCode], enmCode, cOps);
664 for (unsigned iOp = 0; iOp < cOps; iOp++)
665 {
666 tree const hOp = gimple_op(hStmt, iOp);
667 if (hOp)
668 dprintf(" %02d: %p, code %s(%d)\n", iOp, hOp, get_tree_code_name(TREE_CODE(hOp)), TREE_CODE(hOp));
669 else
670 dprintf(" %02d: NULL_TREE\n", iOp);
671 }
672#endif
673 if (enmCode == GIMPLE_CALL)
674 {
675 /*
676 * Check if the function type has the __iprt_format__ attribute.
677 */
678 tree const hFn = gimple_call_fn(hStmt);
679 dprintf(" hFn =%p %s(%d); args=%d\n",
680 hFn, hFn ? get_tree_code_name(TREE_CODE(hFn)) : NULL, hFn ? TREE_CODE(hFn) : - 1,
681 gimple_call_num_args(hStmt));
682#ifdef DEBUG
683 if (hFn && DECL_P(hFn))
684 dprintf(" hFn is decl: %s %s:%d\n",
685 DECL_NAME(hFn) ? IDENTIFIER_POINTER(DECL_NAME(hFn)) : "<unamed>",
686 DECL_SOURCE_FILE(hFn), DECL_SOURCE_LINE(hFn));
687#endif
688 tree const hFnDecl = gimple_call_fndecl(hStmt);
689 if (hFnDecl)
690 dprintf(" hFnDecl=%p %s(%d) %s type=%p %s:%d\n",
691 hFnDecl, get_tree_code_name(TREE_CODE(hFnDecl)), TREE_CODE(hFnDecl),
692 DECL_NAME(hFnDecl) ? IDENTIFIER_POINTER(DECL_NAME(hFnDecl)) : "<unamed>",
693 TREE_TYPE(hFnDecl), DECL_SOURCE_FILE(hFnDecl), DECL_SOURCE_LINE(hFnDecl));
694 tree const hFnType = gimple_call_fntype(hStmt);
695 if (hFnType == NULL_TREE)
696 {
697 if ( hFnDecl == NULL_TREE
698 && gimple_call_internal_p(hStmt) /* va_arg() kludge */)
699 continue;
700 error_at(gimple_location(hStmt), "Failed to resolve function type [fn=%s fndecl=%s]\n",
701 hFn ? get_tree_code_name(TREE_CODE(hFn)) : "<null>",
702 hFnDecl ? get_tree_code_name(TREE_CODE(hFnDecl)) : "<null>");
703 }
704 else if (POINTER_TYPE_P(hFnType))
705 error_at(gimple_location(hStmt), "Got a POINTER_TYPE when expecting a function type [fn=%s]\n",
706 get_tree_code_name(TREE_CODE(hFn)));
707 if (hFnType)
708 dprintf(" hFnType=%p %s(%d) %s\n", hFnType, get_tree_code_name(TREE_CODE(hFnType)), TREE_CODE(hFnType),
709 TYPE_NAME(hFnType) && DECL_NAME(TYPE_NAME(hFnType))
710 ? IDENTIFIER_POINTER(DECL_NAME(TYPE_NAME(hFnType))) : "<unamed>");
711
712 tree const hAttr = hFnType ? lookup_attribute("iprt_format", TYPE_ATTRIBUTES(hFnType)) : NULL_TREE;
713 tree const hAttrMaybe0 = hFnType ? lookup_attribute("iprt_format_maybe_null", TYPE_ATTRIBUTES(hFnType)) : NULL_TREE;
714 if (hAttr || hAttrMaybe0)
715 {
716 /*
717 * Yeah, it has the attribute!
718 */
719 tree const hAttrArgs = hAttr ? TREE_VALUE(hAttr) : TREE_VALUE(hAttrMaybe0);
720 VFMTCHKSTATE State;
721 State.iFmt = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(hAttrArgs));
722 State.iArgs = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(TREE_CHAIN(hAttrArgs)));
723 State.pszFmt = NULL;
724 State.fMaybeNull = hAttr == NULL_TREE;
725 State.hStmt = hStmt;
726 State.hFmtLoc = gimple_location(hStmt);
727 dprintf(" %s() __iprt_format%s__(iFmt=%ld, iArgs=%ld)\n",
728 hFnDecl && DECL_NAME(hFnDecl) ? IDENTIFIER_POINTER(DECL_NAME(hFnDecl)) : "<unamed>",
729 State.fMaybeNull ? "_maybe_null" : "", State.iFmt, State.iArgs);
730
731 unsigned cCallArgs = gimple_call_num_args(hStmt);
732 if (cCallArgs >= State.iFmt)
733 MyCheckFormatRecursive(&State, gimple_call_arg(hStmt, State.iFmt - 1));
734 else
735 error_at(gimple_location(hStmt),
736 "Call has only %d arguments; %s() format string is argument #%lu (1-based), thus missing\n",
737 cCallArgs, DECL_NAME(hFnDecl) ? IDENTIFIER_POINTER(DECL_NAME(hFnDecl)) : "<unamed>", State.iFmt);
738 }
739 }
740 }
741 }
742 return 0;
743}
744
745
746/**
747 * Gate callback for my pass that indicates whether it should execute or not.
748 * @returns true to execute.
749 */
750static bool MyPassGateCallback(void)
751{
752 dprintf("MyPassGateCallback:\n");
753 return true;
754}
755
756
757/**
758 * Validate the use of an attribute.
759 *
760 * @returns ??
761 * @param phOnNode The node the attribute is being used on.
762 * @param hAttrName The attribute name.
763 * @param hAttrArgs The attribute arguments.
764 * @param fFlags Some kind of flags...
765 * @param pfDontAddAttrib Whether to add the attribute to this node or not.
766 */
767static tree AttributeHandler(tree *phOnNode, tree hAttrName, tree hAttrArgs, int fFlags, bool *pfDontAddAttrib)
768{
769 dprintf("AttributeHandler: name=%s fFlags=%#x", IDENTIFIER_POINTER(hAttrName), fFlags);
770 long iFmt = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(hAttrArgs));
771 long iArgs = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(TREE_CHAIN(hAttrArgs)));
772 dprintf(" iFmt=%ld iArgs=%ld", iFmt, iArgs);
773
774 tree hType = *phOnNode;
775 dprintf(" hType=%p %s(%d)\n", hType, get_tree_code_name(TREE_CODE(hType)), TREE_CODE(hType));
776
777 if (pfDontAddAttrib)
778 *pfDontAddAttrib = false;
779 return NULL_TREE;
780}
781
782
783/**
784 * Called when we can register attributes.
785 *
786 * @param pvEventData Ignored.
787 * @param pvUser Ignored.
788 */
789static void RegisterAttributesEvent(void *pvEventData, void *pvUser)
790{
791 NOREF(pvEventData); NOREF(pvUser);
792 dprintf("RegisterAttributesEvent: pvEventData=%p\n", pvEventData);
793
794 register_attribute(&g_AttribSpecs[0]);
795 register_attribute(&g_AttribSpecs[1]);
796}
797
798
799/**
800 * The plug-in entry point.
801 *
802 * @returns 0 to indicate success?
803 * @param pPlugInInfo Plugin info structure.
804 * @param pGccVer GCC Version.
805 */
806int plugin_init(plugin_name_args *pPlugInInfo, plugin_gcc_version *pGccVer)
807{
808 dprintf("plugin_init: %s\n", pPlugInInfo->full_name);
809 dprintf("gcc version: basever=%s datestamp=%s devphase=%s revision=%s\n",
810 pGccVer->basever, pGccVer->datestamp, pGccVer->devphase, pGccVer->revision);
811
812 /* Ask for callback in which we may register the attribute. */
813 register_callback(pPlugInInfo->base_name, PLUGIN_ATTRIBUTES, RegisterAttributesEvent, NULL /*pvUser*/);
814
815 /* Register our pass. */
816#if RT_GNUC_PREREQ(4, 9)
817 /** The registration info for my pass. */
818 struct register_pass_info MyPassInfo;
819 MyPassInfo.pass = new MyPass(g);
820 MyPassInfo.reference_pass_name = "ssa";
821 MyPassInfo.ref_pass_instance_number = 1;
822 MyPassInfo.pos_op = PASS_POS_INSERT_BEFORE;
823 register_callback(pPlugInInfo->base_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &MyPassInfo);
824#else
825 register_callback(pPlugInInfo->base_name, PLUGIN_PASS_MANAGER_SETUP, NULL, (void *)&g_MyPassInfo);
826#endif
827
828 /* Register plug-in info. */
829 register_callback(pPlugInInfo->base_name, PLUGIN_INFO, NULL, (void *)&g_PlugInInfo);
830
831 return 0;
832}
833
834
835
836
837/*
838 *
839 * Functions used by the common code.
840 * Functions used by the common code.
841 * Functions used by the common code.
842 *
843 */
844
845void VFmtChkWarnFmt(PVFMTCHKSTATE pState, const char *pszLoc, const char *pszFormat, ...)
846{
847 char szTmp[1024];
848 va_list va;
849 va_start(va, pszFormat);
850 vsnprintf(szTmp, sizeof(szTmp), pszFormat, va);
851 va_end(va);
852
853 /* display the warning. */
854 warning_at(MyGetFormatStringLocation(pState, pszLoc), 0, "%s", szTmp);
855}
856
857
858void VFmtChkErrFmt(PVFMTCHKSTATE pState, const char *pszLoc, const char *pszFormat, ...)
859{
860 char szTmp[1024];
861 va_list va;
862 va_start(va, pszFormat);
863 vsnprintf(szTmp, sizeof(szTmp), pszFormat, va);
864 va_end(va);
865
866 /* display the warning. */
867 error_at(MyGetFormatStringLocation(pState, pszLoc), "%s", szTmp);
868}
869
870
871
872void VFmtChkVerifyEndOfArgs(PVFMTCHKSTATE pState, unsigned iArg)
873{
874 dprintf("VFmtChkVerifyEndOfArgs: iArg=%u iArgs=%ld cArgs=%u\n", iArg, pState->iArgs, gimple_call_num_args(pState->hStmt));
875 if (pState->iArgs > 0)
876 {
877 iArg += pState->iArgs - 1;
878 unsigned cArgs = gimple_call_num_args(pState->hStmt);
879 if (iArg == cArgs)
880 { /* fine */ }
881 else if (iArg < cArgs)
882 {
883 tree hArg = gimple_call_arg(pState->hStmt, iArg);
884 if (cArgs - iArg > 1)
885 error_at(MY_LOC(hArg, pState), "%u extra arguments not consumed by format string", cArgs - iArg);
886 else if ( TREE_CODE(hArg) != INTEGER_CST
887 || !MY_DOUBLE_INT_FITS_SHWI(hArg)
888 || MY_DOUBLE_INT_TO_SHWI(hArg) != -99) /* ignore final dummy argument: ..., -99); */
889 error_at(MY_LOC(hArg, pState), "one extra argument not consumed by format string");
890 }
891 /* This should be handled elsewhere, but just in case. */
892 else if (iArg - 1 == cArgs)
893 error_at(pState->hFmtLoc, "one argument too few");
894 else
895 error_at(pState->hFmtLoc, "%u arguments too few", iArg - cArgs);
896 }
897}
898
899
900bool VFmtChkRequirePresentArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
901{
902 if (pState->iArgs > 0)
903 {
904 iArg += pState->iArgs - 1;
905 unsigned cArgs = gimple_call_num_args(pState->hStmt);
906 if (iArg >= cArgs)
907 {
908 VFmtChkErrFmt(pState, pszLoc, "Missing argument! %s", pszMessage);
909 return false;
910 }
911
912 tree hArg = gimple_call_arg(pState->hStmt, iArg);
913 tree hType = TREE_TYPE(hArg);
914 dprintf("arg%u: hArg=%p [%s] hType=%p [%s] cls=%s\n", iArg, hArg, get_tree_code_name(TREE_CODE(hArg)),
915 hType, get_tree_code_name(TREE_CODE(hType)), tree_code_class_strings[TREE_CODE_CLASS(TREE_CODE(hType))]);
916 dprintf(" nm=%p\n", TYPE_NAME(hType));
917 dprintf(" cb=%p %s value=%ld\n", TYPE_SIZE(hType), get_tree_code_name(TREE_CODE(TYPE_SIZE(hType))),
918 MY_DOUBLE_INT_TO_SHWI(TYPE_SIZE(hType)) );
919 dprintf(" unit=%p %s value=%ld\n", TYPE_SIZE_UNIT(hType), get_tree_code_name(TREE_CODE(TYPE_SIZE_UNIT(hType))),
920 MY_DOUBLE_INT_TO_SHWI(TYPE_SIZE_UNIT(hType)) );
921 tree hTypeNm = TYPE_NAME(hType);
922 if (hTypeNm)
923 dprintf(" typenm=%p %s '%s'\n", hTypeNm, get_tree_code_name(TREE_CODE(hTypeNm)),
924 IDENTIFIER_POINTER(DECL_NAME(hTypeNm)));
925 }
926 return true;
927}
928
929
930bool VFmtChkRequireIntArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
931{
932 if (VFmtChkRequirePresentArg(pState, pszLoc, iArg, pszMessage))
933 {
934 /** @todo type check. */
935 return true;
936 }
937 return false;
938}
939
940
941bool VFmtChkRequireStringArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
942{
943 if (VFmtChkRequirePresentArg(pState, pszLoc, iArg, pszMessage))
944 {
945 /** @todo type check. */
946 return true;
947 }
948 return false;
949}
950
951
952bool VFmtChkRequireVaListPtrArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
953{
954 if (VFmtChkRequirePresentArg(pState, pszLoc, iArg, pszMessage))
955 {
956 /** @todo type check. */
957 return true;
958 }
959 return false;
960}
961
962
963void VFmtChkHandleReplacementFormatString(PVFMTCHKSTATE pState, const char *pszPctM, unsigned iArg)
964{
965 if (pState->iArgs > 0)
966 {
967 pState->iFmt = pState->iArgs + iArg;
968 pState->iArgs = pState->iFmt + 1;
969 pState->fMaybeNull = false;
970 MyCheckFormatRecursive(pState, gimple_call_arg(pState->hStmt, pState->iFmt - 1));
971 }
972}
973
974
975const char *VFmtChkGetFmtLocFile(PVFMTCHKSTATE pState)
976{
977 return LOCATION_FILE(pState->hFmtLoc);
978}
979
980
981unsigned int VFmtChkGetFmtLocLine(PVFMTCHKSTATE pState)
982{
983 return LOCATION_LINE(pState->hFmtLoc);
984}
985
986
987unsigned int VFmtChkGetFmtLocColumn(PVFMTCHKSTATE pState)
988{
989#ifdef LOCATION_COLUMN
990 return LOCATION_COLUMN(pState->hFmtLoc);
991#else
992 return 1;
993#endif
994}
995
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