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