Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for global SAI api used #1379

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ testdash_gtest_LDADD = -lgtest -lhiredis -lswsscommon -lpthread \
$(top_srcdir)/lib/libsairedis.la $(top_srcdir)/syncd/libSyncd.a \
-L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -lzmq $(CODE_COVERAGE_LIBS)

TESTS = aspellcheck.pl conflictnames.pl swsslogentercheck.sh checkwhitespace.sh tests BCM56850.pl MLNX2700.pl BCM56971B0.pl NVDAMBF2H536C.pl testdash_gtest
TESTS = checksaiapi.sh aspellcheck.pl conflictnames.pl swsslogentercheck.sh checkwhitespace.sh tests BCM56850.pl MLNX2700.pl BCM56971B0.pl NVDAMBF2H536C.pl testdash_gtest
45 changes: 45 additions & 0 deletions tests/checksaiapi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

# this script checks if only allowed files are using global sai apis

# in entire project we only allow VendorSai.cpp and saisdkdump.cpp to use global SAI API,

# PLEASE DO NOT ADD ANY MORE EXCEPTIONS

# we want to keep track of usage global apis to minimize scope of usage for
# possible future mistakes that we will be using dynmically loaded libsai.so
# and by mistake someone will be calling global api that was linked with syncd,
# this will be hard error to locate

set -e

cd ..

find -name "*.o" |
grep -v pyext |
grep -v tests |
while read all;
do
echo -n $all;
nm $all |
grep "U sai_" |
grep -vP "sai_metadata|sai_serialize|sai_deserialize" |
perl -npe 'chomp'
echo
done |
grep "U sai_" |
awk '{print $1}' |
perl -ne 'chomp; die "file $_ is using global sai_xxx API, please correct your code" if not /VendorSai.o|saisdkdump/'

REGEX=`cat SAI/meta/saimetadata.c|grep dlsym|grep handle|perl -ne 'print "$1|" if /(sai_\w+)/'|perl -pe 'chop'|perl -ne 'print "\\\\b($_)\\\\b"'`

set +e
find -name "*.cpp" -o -name "*.c" |
xargs grep -P "$REGEX" |
grep -vP "/unittest/|/tests/|/SAI/|/pyext/|tests.cpp|sai_vs_interfacequery|sai_redis_interfacequery|saisdkdump|SWSS_LOG|.cpp:\s+\*|.cpp:\s+//|sai_status_t\s+sai_|VendorSai.cpp:.+=\s*&sai_"

if [ $? == 0 ]; then
echo not allowed files are using global sai_xxx API, please correct your code, only VendorSai.cpp and saisdkdump are allowed to use global SAI apis
exit 1
fi

Loading