From 0536e36935831121596fa25696ef40f71ee0905c Mon Sep 17 00:00:00 2001 From: Ashish Tiwari Date: Thu, 26 Dec 2024 01:44:35 +0530 Subject: [PATCH 1/3] fix: add support for ignoring "load" global variable --- .gitignore | 1 - utils/lj-releng | 228 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+), 1 deletion(-) create mode 100755 utils/lj-releng diff --git a/.gitignore b/.gitignore index 94669c34502a..549d9384a541 100644 --- a/.gitignore +++ b/.gitignore @@ -56,7 +56,6 @@ uwsgi_temp proxy_temp fastcgi_temp client_body_temp -utils/lj-releng utils/reindex *.etcd/ t/lib/dubbo*/**/target/ diff --git a/utils/lj-releng b/utils/lj-releng new file mode 100755 index 000000000000..cd46a3e0ea92 --- /dev/null +++ b/utils/lj-releng @@ -0,0 +1,228 @@ +#!/usr/bin/env perl +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +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('Lse', \%opts) or die "Usage: lj-releng [-L] [-s] [-e] [files]\n"; + +my $silent = $opts{s}; +my $stop_on_error = $opts{e}; +my $no_long_line_check = $opts{L}; + +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"; +} + +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; + print("$file: $orig_ver ($ver)\n"); + last; + + } elsif (/(?x) (?:_VERSION|version) \s* = \s* ([a-zA-Z_]\S*)/) { + print("$file: $1\n"); + $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) { + $hits++; + my ($line, $name) = @$gset; + 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|collectgarbage|load + |rawlen|select|arg|bit|debug|ngx|ndk|newproxy)$/x) + { + next; + } + + $hits++; + warn "${RED}ERROR${NC}: $file: line $line: getting the Lua ", + "global \"$name\"\n"; + } + + next; + } + + for my $gget (@$ggets) { + $hits++; + my ($line, $name) = @$gget; + 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 100..."); + blank("grep -H -n -E --color '.{101}' $file | awk '{ print \"ERROR:\" \$0 }'"); + } +} From e4ee0121e29dedc93277e7291b5d091f4ab38a99 Mon Sep 17 00:00:00 2001 From: Ashish Tiwari Date: Mon, 30 Dec 2024 11:23:33 +0530 Subject: [PATCH 2/3] change copyright --- utils/lj-releng | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/utils/lj-releng b/utils/lj-releng index cd46a3e0ea92..b2c5040d0f09 100755 --- a/utils/lj-releng +++ b/utils/lj-releng @@ -1,20 +1,13 @@ #!/usr/bin/env perl -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# +# Copyright (c) 2011-2017, Yichun "agentzh" Zhang (章亦春) agentzh@gmail.com, OpenResty Inc. + +# This module is licensed under the terms of the BSD license. + +# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +# Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +# Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use strict; use warnings; From 75999242ba936b60ddc25d5c607951ce5a58df18 Mon Sep 17 00:00:00 2001 From: Ashish Tiwari Date: Mon, 30 Dec 2024 11:26:42 +0530 Subject: [PATCH 3/3] ignore utils/lj-releng for license check --- .licenserc.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.licenserc.yaml b/.licenserc.yaml index edb970dc74b4..570155fbc249 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -55,5 +55,6 @@ header: - '.luacheckrc' # Exclude file contains certificate revocation information - 't/certs/ocsp/index.txt' + - 'utils/lj-releng' comment: on-failure