VirtualBox

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

Last change on this file since 74856 was 70558, checked in by vboxsync, 7 years ago

gccplugin: Added testcase for missing 's' bug - need to validate format types.

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