rjw | 6c1fd8f | 2022-11-30 14:33:01 +0800 | [diff] [blame] | 1 | #!/usr/bin/perl
|
| 2 | #
|
| 3 | # Copyright Statement:
|
| 4 | # --------------------
|
| 5 | # This software is protected by Copyright and the information contained
|
| 6 | # herein is confidential. The software may not be copied and the information
|
| 7 | # contained herein may not be used or disclosed except with the written
|
| 8 | # permission of MediaTek Inc. (C) 2006
|
| 9 | #
|
| 10 | # BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
|
| 11 | # THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
|
| 12 | # RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
|
| 13 | # AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
|
| 14 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
|
| 15 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
|
| 16 | # NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
|
| 17 | # SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
|
| 18 | # SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
|
| 19 | # THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
|
| 20 | # NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
|
| 21 | # SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
|
| 22 | #
|
| 23 | # BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
|
| 24 | # LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
|
| 25 | # AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
|
| 26 | # OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
|
| 27 | # MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
|
| 28 | #
|
| 29 | # THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
|
| 30 | # WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
|
| 31 | # LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
|
| 32 | # RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
|
| 33 | # THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
|
| 34 | #
|
| 35 | #*****************************************************************************
|
| 36 | #*
|
| 37 | #* Filename:
|
| 38 | #* ---------
|
| 39 | #* ck_L6220E.pl
|
| 40 | #*
|
| 41 | #* Project:
|
| 42 | #* --------
|
| 43 | #* MAUI
|
| 44 | #*
|
| 45 | #* Description:
|
| 46 | #* ------------
|
| 47 | #* This script will search unsafe C standard API usage from .lis generated by
|
| 48 | #* ARMLINK
|
| 49 | #*
|
| 50 | #* find all such referenc in .lis file
|
| 51 | #* date.obj(i.localtimeOffset) refers to armlibc_rvct.obj(SHOULD_NOT_USED_FUNCTION) for localtime
|
| 52 | #* report:
|
| 53 | #* object filename, function name, used symbol, possible file path
|
| 54 | #*
|
| 55 | #* perl ck_L6220E.pl {.lis file path}+
|
| 56 | #* this script will try to detect .lis file path if no argument
|
| 57 | #*
|
| 58 | #* Author:
|
| 59 | #* -------
|
| 60 | #* Shuguang Wen (mtk80458)
|
| 61 | #*
|
| 62 | #*============================================================================
|
| 63 | #* HISTORY
|
| 64 | #* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
|
| 65 | #*------------------------------------------------------------------------------
|
| 66 | #* $Revision$
|
| 67 | #* $Modtime$
|
| 68 | #* $Log$
|
| 69 | #*
|
| 70 | #* 08 16 2012 ying.xin
|
| 71 | #* [MOLY00002197] [gcc]newlib tailoring and performance optimization check in
|
| 72 | #* .
|
| 73 | #*
|
| 74 | #*
|
| 75 | #*------------------------------------------------------------------------------
|
| 76 | #* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
|
| 77 | #*============================================================================
|
| 78 | #****************************************************************************/
|
| 79 | #
|
| 80 |
|
| 81 | use strict;
|
| 82 | use warnings;
|
| 83 |
|
| 84 | my @unsafe_callers;
|
| 85 |
|
| 86 | sub trim {
|
| 87 | my ($s) = @_;
|
| 88 |
|
| 89 | $s =~ s/^\s+//;
|
| 90 | $s =~ s/\s+$//;
|
| 91 |
|
| 92 | return $s;
|
| 93 | }
|
| 94 |
|
| 95 | sub strcmp_nocase {
|
| 96 | my ($a, $b) = @_;
|
| 97 |
|
| 98 | return lc $a eq lc $b;
|
| 99 | }
|
| 100 |
|
| 101 | sub has_unsafe {
|
| 102 | my ($line) = @_;
|
| 103 | my ($myobj, $mysec, $symname, $myfunc);
|
| 104 |
|
| 105 | $line = trim $line;
|
| 106 |
|
| 107 | if ($line =~ /(.+)\((.+)\) refers to .+\(SHOULD_NOT_USED_FUNCTION\) for (.+)/
|
| 108 | or $line =~ /(.+)\((.+)\) refers \(Special\) to .+\(SHOULD_NOT_USED_FUNCTION\) for (.+)/
|
| 109 | or $line =~ /(.+)\((.+)\) refers \(Weak\) to .+\(SHOULD_NOT_USED_FUNCTION\) for (.+)/) {
|
| 110 | $myobj = $1;
|
| 111 | $mysec = $2;
|
| 112 | $symname = $3;
|
| 113 | }
|
| 114 | else {
|
| 115 | return 0;
|
| 116 | }
|
| 117 |
|
| 118 | if ($mysec =~ /i\.(.+)/) {
|
| 119 | $myfunc = $1;
|
| 120 | }
|
| 121 | else {
|
| 122 | $myfunc = "";
|
| 123 | }
|
| 124 |
|
| 125 | push @unsafe_callers, { objfile => $myobj, sectname => $mysec, function => $myfunc, used_symbol => $symname, path => []};
|
| 126 | 1;
|
| 127 | }
|
| 128 |
|
| 129 | sub add_path_nodup {
|
| 130 | my ($arr_ref, $path) = @_;
|
| 131 |
|
| 132 | foreach (@{$arr_ref}) {
|
| 133 | if (strcmp_nocase($_, $path)) {
|
| 134 | return 0;
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | push (@{$arr_ref}, $path);
|
| 139 | 1;
|
| 140 | }
|
| 141 |
|
| 142 | sub find_object_filename {
|
| 143 | my ($line) = @_;
|
| 144 | my ($path, $objfile);
|
| 145 |
|
| 146 | $line = trim $line;
|
| 147 |
|
| 148 | if ($line =~ /([^\s]+)\s+0x00000000\s+Number\s+0\s+([^\s]+)\s+ABSOLUTE/) {
|
| 149 | $path = $1;
|
| 150 | $objfile = $2;
|
| 151 | }
|
| 152 | else {
|
| 153 | return;
|
| 154 | }
|
| 155 |
|
| 156 | foreach my $c (@unsafe_callers) {
|
| 157 | if (strcmp_nocase($objfile, $c->{objfile})) {
|
| 158 | add_path_nodup($c->{path}, $path);
|
| 159 | }
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | sub print_result {
|
| 164 | if ($#unsafe_callers == -1) {
|
| 165 | print " ... NO\n";
|
| 166 | return;
|
| 167 | }
|
| 168 | print "\n";
|
| 169 |
|
| 170 | print sprintf("%-16s %-20s %-12s path(s)\n", "object", "function", "unsafe api");
|
| 171 | print "=" x 79, "\n";
|
| 172 | foreach my $c (@unsafe_callers) {
|
| 173 | print sprintf("%-16s %-20s %-12s %s\n", $c->{objfile}, $c->{function}, $c->{used_symbol},
|
| 174 | $#{$c->{path}} >= 0 ? @{$c->{path}}[0] : "");
|
| 175 | for (my $i = 1; $i <= $#{$c->{path}}; $i++) {
|
| 176 | print sprintf("%-16s %-20s %-12s |-%s\n", "", "", "", @{$c->{path}}[$i]);
|
| 177 | }
|
| 178 | }
|
| 179 | print "\n";
|
| 180 | }
|
| 181 |
|
| 182 | sub find_in_lis {
|
| 183 | foreach my $f (@_) {
|
| 184 | open F, "<", $f or next;
|
| 185 | print "[I] unsafe API callers found from $f";
|
| 186 | @unsafe_callers = ();
|
| 187 | while (<F>) {
|
| 188 | has_unsafe $_;
|
| 189 | find_object_filename $_;
|
| 190 | }
|
| 191 | &print_result($f);
|
| 192 | close F;
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | sub expand_lis {
|
| 197 | my @ss = ();
|
| 198 | my $show_err = shift;
|
| 199 |
|
| 200 | for my $name (@_) {
|
| 201 | if (-f $name && -r $name) {
|
| 202 | push @ss, $name;
|
| 203 | }
|
| 204 | elsif (-d $name && -x $name) {
|
| 205 | my @all_files = glob "$name/*.lis";
|
| 206 | for my $subname (@all_files) {
|
| 207 | push @ss, $subname if -f $subname && -r $subname;
|
| 208 | }
|
| 209 | }
|
| 210 | elsif ($show_err) {
|
| 211 | print "[E] ignore not readable file/dir: $name\n";
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | return @ss;
|
| 216 | }
|
| 217 |
|
| 218 | my $quiet_mode = 0;
|
| 219 | sub main {
|
| 220 | my @non_option_args = ();
|
| 221 | my @lis_list = ();
|
| 222 | my $maui_root = undef;
|
| 223 |
|
| 224 | foreach (@ARGV) {
|
| 225 | if ($_ eq "-q") {
|
| 226 | $quiet_mode = 1;
|
| 227 | }
|
| 228 | else {
|
| 229 | push @non_option_args, $_;
|
| 230 | }
|
| 231 | }
|
| 232 |
|
| 233 | if ($#non_option_args >= 0) {
|
| 234 | foreach (@non_option_args) {
|
| 235 | push @lis_list, expand_lis(1, $_);
|
| 236 | }
|
| 237 | }
|
| 238 | # detect in root directory
|
| 239 | elsif (-d "build" && -d "make" && -d "tools") {
|
| 240 | $maui_root = ".";
|
| 241 | }
|
| 242 | elsif (-d "../build" && -d "../make" && -d "../tools") {
|
| 243 | $maui_root = "..";
|
| 244 | }
|
| 245 |
|
| 246 | if ($maui_root) {
|
| 247 | print "[I] MAUI root directory mcu/ detected($maui_root), search lis inside build/\n";
|
| 248 | my @dirs_in_build = glob "$maui_root/build/*";
|
| 249 | foreach (@dirs_in_build) {
|
| 250 | push @lis_list, expand_lis(0, $_);
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | if ($#lis_list == -1) {
|
| 255 | print "[E] no lis file found, please run it inside mcu/ directory or pass lis file path(s)\n";
|
| 256 | }
|
| 257 |
|
| 258 | find_in_lis @lis_list;
|
| 259 |
|
| 260 | print "\n\nfor RD: please refer to http://teams.mediatek.inc/sites/WCP/WCPSW/SystemService/Lists/System_Service_nnouncement/DispForm.aspx?ID=75&Source=http%3A%2F%2Fteams%2Emediatek%2Einc%2Fsites%2FWCP%2FWCPSW%2FSystemService%2FLists%2FSystem%5FService%5FAnnouncement%2FAllItems%2Easpx\n\n";
|
| 261 | }
|
| 262 |
|
| 263 | $| = 1;
|
| 264 | &main;
|
| 265 | if (not $quiet_mode) {
|
| 266 | system("pause");
|
| 267 | }
|
| 268 |
|
| 269 |
|