-
Notifications
You must be signed in to change notification settings - Fork 111
/
lj-releng
executable file
·240 lines (194 loc) · 5.94 KB
/
lj-releng
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
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Std;
my (@luas, @tests);
my $hits = 0;
my $RED = "\033[1;31m";
my $NC = "\033[0m"; # No Color
my %opts;
getopts('Lsea:', \%opts)
or die "Usage: lj-releng [-L] [-s] [-e] [-a allow_list] [files]\n";
my $silent = $opts{s};
my $stop_on_error = $opts{e};
my $no_long_line_check = $opts{L};
my $allow_list = $opts{a};
my $check_lua_ver = "luajit -v | awk '{print\$2}'| grep 2.1";
my $output = `$check_lua_ver`;
if ($output eq '') {
die "ERROR: lj-releng ONLY supports LuaJIT 2.1!\n";
}
my %allow_rules;
if ($allow_list) {
open my $reader, $allow_list
or die "Can't open $allow_list for reading\n";
while (<$reader>) {
chomp($_);
$allow_rules{"$_"} = 1;
}
close $reader;
}
if ($#ARGV != -1) {
@luas = @ARGV;
} else {
@luas = split /\n/, `find . -name '*.lua'`;
if (-d 't') {
@tests = map glob, qw{ t/*.t t/*/*.t t/*/*/*.t };
}
}
for my $f (sort @luas) {
process_file($f);
}
for my $t (@tests) {
blank(qq{grep -H -n --color -E '\\--- ?(ONLY|LAST)' $t});
}
if ($hits) {
exit 1;
}
# p: prints a string to STDOUT appending \n
# w: prints a string to STDERR appending \n
# Both respect the $silent value
sub p { print "$_[0]\n" if (!$silent) }
sub w { warn "$_[0]\n" if (!$silent) }
# blank: runs a command and looks at the output. If the output is not
# blank it is printed (and the program dies if stop_on_error is 1)
sub blank {
my ($command) = @_;
if ($stop_on_error) {
my $output = `$command`;
if ($output ne '') {
die $output;
}
} else {
system($command);
}
}
my $version;
sub process_file {
my $file = shift;
# Check the sanity of each .lua file
open my $in, $file or
die "ERROR: Can't open $file for reading: $!\n";
my $found_ver;
while (<$in>) {
my ($ver, $skipping);
if (/(?x) (?:_VERSION|version) \s* = .*? ([\d\.]*\d+) (.*? SKIP)?/) {
my $orig_ver = $ver = $1;
$found_ver = 1;
$skipping = $2;
$ver =~ s{^(\d+)\.(\d{3})(\d{3})$}{join '.', int($1), int($2), int($3)}e;
w("$file: $orig_ver ($ver)");
last;
} elsif (/(?x) (?:_VERSION|version) \s* = \s* ([a-zA-Z_]\S*)/) {
w("$file: $1");
$found_ver = 1;
last;
}
if ($ver and $version and !$skipping) {
if ($version ne $ver) {
die "$file: $ver != $version\n";
}
} elsif ($ver and !$version) {
$version = $ver;
}
}
if (!$found_ver) {
w("WARNING: No \"_VERSION\" or \"version\" field found in `$file`.");
}
close $in;
#p("Checking use of Lua global variables in file $file...");
#p("op no. line opcode args ; code");
my $cmd = "luajit -bL $file";
open $in, "$cmd|"
or die "cannot open output pipe for \"$cmd\": $!";
my @sections;
my $sec;
while (<$in>) {
#warn "line: $_";
if (/^-- BYTECODE -- \S.*?:(\d+)-\d+$/) {
my $def_line = $1;
#warn "$file: $line";
if (defined $sec) {
push @sections, $sec;
}
$sec = {
def_line => $def_line,
gsets => [],
ggets => [],
};
next;
}
if (/^ \d+ \s+ \[ (?:[^\]]+:)?(\d+) \] \s+ (?: \W+ \s+ )? G([GS])ET \s+ .*? ; \s+ \"([^"]+)" $/x) {
my ($line, $op, $name) = ($1, $2, $3);
#warn "found: $line $op $name";
if ($op eq 'S') {
push @{ $sec->{gsets} }, [$line, $name];
} else {
push @{ $sec->{ggets} }, [$line, $name];
}
next;
}
if (/^ \d+ \s+ \[ \d+ \] \s+ (G[GS]ET) \s+ $/x) {
die "bad $1 instruction: $_";
}
}
close $in;
if (defined $sec) {
push @sections, $sec;
}
my $last_idx = $#sections;
my $i = 0;
for my $sec (@sections) {
my $def_line = $sec->{def_line};
my $gsets = $sec->{gsets};
my $ggets = $sec->{ggets};
for my $gset (@$gsets) {
my ($line, $name) = @$gset;
if (exists($allow_rules{$name})) {
next;
}
$hits++;
warn "${RED}ERROR${NC}: $file: line $line: setting the Lua global ",
"\"$name\"\n";
}
if ($i == $last_idx) {
# being the top-level chunk
for my $gget (@$ggets) {
my ($line, $name) = @$gget;
if ( ($name =~ /^ (?: require|type|tostring|error|ngx|ndk|jit
|setmetatable|getmetatable|string|table|io
|os|print|tonumber|math|pcall|xpcall|unpack
|pairs|ipairs|assert|module|package
|coroutine|[gs]etfenv|next|rawget|rawset
|loadstring|dofile|loadfile|collectgarbage
|rawlen|select|arg|bit|debug|ngx|ndk|newproxy)$/x)
or (exists($allow_rules{$name})) )
{
next;
}
$hits++;
warn "${RED}ERROR${NC}: $file: line $line: getting the Lua ",
"global \"$name\"\n";
}
next;
}
for my $gget (@$ggets) {
my ($line, $name) = @$gget;
if (exists($allow_rules{$name})) {
next;
}
$hits++;
warn "${RED}ERROR${NC}: $file: line $line: getting the Lua ",
"global \"$name\"\n";
}
} continue {
$i++;
}
if ($stop_on_error && $hits > 0) {
exit 1
}
unless ($no_long_line_check) {
p("Checking line length exceeding 80...");
blank("grep -H -n -E --color '.{81}' $file");
}
}