Skip to content

Commit

Permalink
Sanitizer: Support address sanitizer for x86_64 platform (ossrs#3212)
Browse files Browse the repository at this point in the history
* Sanitizer: Support address sanitizer for x86_64 platform
* Sanitizer: Not mac os need -static-libasan
* Sanitizer: Add script for docker test.
* Sanitizer: Refine build script.
* Santizer: Fix ossrs/srs:dev-gcc7-cache cannot find libasan bug
* Sanitizer: Support sanitizer when use ossrs/srs:dev-cache and ubuntuxx-cache
* Sanitizer: Add sanitizer-static config

Co-authored-by: winlin <[email protected]>
  • Loading branch information
2 people authored and johzzy committed Apr 18, 2023
1 parent 7ae6410 commit e964d06
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 7 deletions.
4 changes: 2 additions & 2 deletions trunk/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ ENV DEBIAN_FRONTEND noninteractive
SHELL ["/bin/bash", "-c"]

# Install depends tools.
RUN apt-get update && apt-get install -y gcc make g++ patch unzip perl git
RUN apt-get update && apt-get install -y gcc make g++ patch unzip perl git libasan5

# Copy source code to docker.
COPY . /srs
WORKDIR /srs/trunk

# Build and install SRS.
# Note that SRT is enabled by default, so we configure without --srt=on.
RUN ./configure --jobs=${JOBS} && make -j${JOBS} && make install
RUN ./configure --jobs=${JOBS} --sanitizer-static=on && make -j${JOBS} && make install

# All config files for SRS.
RUN cp -R conf /usr/local/srs/conf && \
Expand Down
4 changes: 2 additions & 2 deletions trunk/Dockerfile.builds
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ RUN cd /srs/trunk && ./configure --jobs=2 --srt=off --cxx11=off --cxx14=off --ff
########################################################
FROM ossrs/srs:dev6-cache AS centos6-baseline
COPY . /srs
RUN cd /srs/trunk && ./configure --jobs=2 --srt=off --cxx11=off --cxx14=off && make -j2
RUN cd /srs/trunk && ./configure --jobs=2 --srt=off --gb28181=off --cxx11=off --cxx14=off --sanitizer=off && make -j2

FROM ossrs/srs:dev6-cache AS centos6-srt
COPY . /srs
RUN cd /srs/trunk && ./configure --jobs=2 --srt=on --cxx11=off --cxx14=off && make -j2
RUN cd /srs/trunk && ./configure --jobs=2 --srt=on --gb28181=off --cxx11=off --cxx14=off --sanitizer=off && make -j2

########################################################
FROM ossrs/srs:ubuntu16-cache AS ubuntu16-baseline
Expand Down
2 changes: 1 addition & 1 deletion trunk/Dockerfile.cov
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ COPY . /srs
WORKDIR /srs/trunk

# Note that we must enable the gcc7 or link failed.
RUN scl enable devtoolset-7 -- ./configure --srt=on --utest=on --gcov=on --jobs=2
RUN scl enable devtoolset-7 -- ./configure --srt=on --gb28181=off --utest=on --gcov=on --sanitizer=off --jobs=2
RUN scl enable devtoolset-7 -- make -j2 utest
2 changes: 1 addition & 1 deletion trunk/Dockerfile.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ COPY . /srs
WORKDIR /srs/trunk

# Note that we must enable the gcc7 or link failed.
RUN scl enable devtoolset-7 -- ./configure --srt=on --utest=on --jobs=2
RUN scl enable devtoolset-7 -- ./configure --srt=on --gb28181=off --utest=on --sanitizer=off --jobs=2
RUN scl enable devtoolset-7 -- make -j2 utest

# Build benchmark tool.
Expand Down
3 changes: 2 additions & 1 deletion trunk/auto/depends.sh
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ OSX_prepare; ret=$?; if [[ 0 -ne $ret ]]; then echo "OSX prepare failed, ret=$re
OS_IS_LOONGARCH64=$(g++ -dM -E - </dev/null |grep '#define __loongarch64 1' -q && echo YES)
OS_IS_MIPS64=$(g++ -dM -E - </dev/null |grep '#define __mips64 1' -q && echo YES)
OS_IS_LOONGSON=$(uname -r |grep -q loongson && echo YES)
echo "OS_IS_LOONGARCH64:$OS_IS_LOONGARCH64, OS_IS_MIPS64:$OS_IS_MIPS64, OS_IS_LOONGSON:$OS_IS_LOONGSON"
OS_IS_X86_64=$(g++ -dM -E - </dev/null |grep -q '#define __x86_64 1' && echo YES)
echo "OS_IS_LOONGARCH64:$OS_IS_LOONGARCH64, OS_IS_MIPS64:$OS_IS_MIPS64, OS_IS_LOONGSON:$OS_IS_LOONGSON, OS_IS_X86_64:$OS_IS_X86_64"

#####################################################################################
# for Centos, auto install tools by yum
Expand Down
8 changes: 8 additions & 0 deletions trunk/auto/options.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ SRS_LOG_TRACE=YES
# Experts options.
SRS_USE_SYS_SSL=NO # Use system ssl(-lssl) if required.
SRS_VALGRIND=NO
SRS_SANITIZER=YES
SRS_SANITIZER_STATIC=NO
SRS_BUILD_TAG= # Set the object files tag name.
SRS_CLEAN=YES # Whether do "make clean" when configure.
SRS_SIMULATOR=NO # Whether enable RTC simulate API.
Expand Down Expand Up @@ -150,6 +152,8 @@ Performance: @see https://blog.csdn.net/win_lin/article/details/5
--gmp=on|off Whether build memory profile with gperf tools. Default: $(value2switch $SRS_GPERF_MP)
--gcp=on|off Whether build cpu profile with gperf tools. Default: $(value2switch $SRS_GPERF_CP)
--gprof=on|off Whether build SRS with gprof(GNU profile tool). Default: $(value2switch $SRS_GPROF)
--sanitizer=on|off Whether build SRS with address sanitizer. Default: $(value2switch $SRS_SANITIZER)
--sanitizer-static=on|off Whether build SRS with static libasan. Default: $(value2switch $SRS_SANITIZER_STATIC)
--nasm=on|off Whether build FFMPEG for RTC with nasm. Default: $(value2switch $SRS_NASM)
--srtp-nasm=on|off Whether build SRTP with ASM(openssl-asm), requires RTC and openssl-1.0.*. Default: $(value2switch $SRS_SRTP_ASM)
Expand Down Expand Up @@ -330,6 +334,9 @@ function parse_user_option() {
--without-gprof) SRS_GPROF=NO ;;
--gprof) SRS_GPROF=$(switch2value $value) ;;

--sanitizer) SRS_SANITIZER=$(switch2value $value) ;;
--sanitizer-static) SRS_SANITIZER_STATIC=$(switch2value $value) ;;

--use-sys-ssl) SRS_USE_SYS_SSL=YES ;;
--sys-ssl) SRS_USE_SYS_SSL=$(switch2value $value) ;;

Expand Down Expand Up @@ -550,6 +557,7 @@ function regenerate_options() {
SRS_AUTO_CONFIGURE="${SRS_AUTO_CONFIGURE} --debug=$(value2switch $SRS_DEBUG)"
SRS_AUTO_CONFIGURE="${SRS_AUTO_CONFIGURE} --debug-stats=$(value2switch $SRS_DEBUG_STATS)"
SRS_AUTO_CONFIGURE="${SRS_AUTO_CONFIGURE} --cross-build=$(value2switch $SRS_CROSS_BUILD)"
SRS_AUTO_CONFIGURE="${SRS_AUTO_CONFIGURE} --sanitizer=$(value2switch $SRS_SANITIZER)"
if [[ $SRS_CROSS_BUILD_ARCH != "" ]]; then SRS_AUTO_CONFIGURE="$SRS_AUTO_CONFIGURE --arch=$SRS_CROSS_BUILD_ARCH"; fi
if [[ $SRS_CROSS_BUILD_CPU != "" ]]; then SRS_AUTO_CONFIGURE="$SRS_AUTO_CONFIGURE --cpu=$SRS_CROSS_BUILD_CPU"; fi
if [[ $SRS_CROSS_BUILD_HOST != "" ]]; then SRS_AUTO_CONFIGURE="$SRS_AUTO_CONFIGURE --host=$SRS_CROSS_BUILD_HOST"; fi
Expand Down
19 changes: 19 additions & 0 deletions trunk/configure
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ fi
if [[ $SRS_EXTRA_FLAGS != '' ]]; then
CXXFLAGS="${CXXFLAGS} $SRS_EXTRA_FLAGS";
fi

# For Sanitizer
# @doc: https://github.com/google/sanitizers/wiki/AddressSanitizer
if [[ $SRS_SANITIZER == YES && $OS_IS_X86_64 == YES ]]; then
CXXFLAGS="${CXXFLAGS} -fsanitize=address -fno-omit-frame-pointer";
if [[ $SRS_SANITIZER_STATIC == YES ]]; then
CXXFLAGS="${CXXFLAGS} -static-libasan";
fi
fi

# Start to generate the Makefile.
cat << END >> ${SRS_OBJS}/${SRS_MAKEFILE}
GCC = ${SRS_TOOL_CC}
Expand Down Expand Up @@ -211,6 +221,15 @@ if [[ $SRS_BACKTRACE == YES ]]; then
SrsLinkOptions="${SrsLinkOptions} -rdynamic";
fi

# For address sanitizer
# @doc: https://github.com/google/sanitizers/wiki/AddressSanitizer
if [[ $SRS_SANITIZER == YES && $OS_IS_X86_64 == YES ]]; then
SrsLinkOptions="${SrsLinkOptions} -fsanitize=address -fno-omit-frame-pointer";
if [[ $SRS_SANITIZER_STATIC == YES ]]; then
SrsLinkOptions="${SrsLinkOptions} -static-libasan";
fi
fi

#####################################################################################
# Modules, compile each module, then link to binary
#
Expand Down
41 changes: 41 additions & 0 deletions trunk/scripts/docker_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

# cd ~/git/srs
work_dir=$(cd -P $(dirname $0) && cd ../.. && pwd) && cd $work_dir && echo "Run script in ${work_dir}"
if [[ ! -d trunk ]]; then echo "no ./trunk found"; exit 1; fi

echo "For trunk/Dockerfile.test"
docker build --tag srs:test -f trunk/Dockerfile.test . &&
docker build --tag srs:cov -f trunk/Dockerfile.cov .
if [[ $? -ne 0 ]]; then echo "Docker for test failed"; exit 1; fi

echo "For trunk/Dockerfile"
docker buildx build --platform linux/arm/v7 --output "type=image,push=false" --build-arg IMAGE=ossrs/srs:ubuntu20-cache -f trunk/Dockerfile . &&
docker buildx build --platform linux/arm64/v8 --output "type=image,push=false" --build-arg IMAGE=ossrs/srs:ubuntu20-cache -f trunk/Dockerfile . &&
docker buildx build --platform linux/amd64 --output "type=image,push=false" --build-arg IMAGE=ossrs/srs:ubuntu20-cache -f trunk/Dockerfile .
if [[ $? -ne 0 ]]; then echo "Docker for release failed"; exit 1; fi

echo "For trunk/Dockerfile.pkg"
docker build --tag srs:pkg --build-arg version=1.2.3 -f trunk/Dockerfile.pkg .
if [[ $? -ne 0 ]]; then echo "Docker for pkg failed"; exit 1; fi

echo "For trunk/Dockerfile.builds"
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target centos7-baseline . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target centos7-all . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target centos7-no-webrtc . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target centos7-no-asm . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target centos7-ansi-no-ffmpeg . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target centos6-baseline . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target centos6-all . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target ubuntu16-baseline . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target ubuntu16-all . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target ubuntu18-baseline . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target ubuntu18-all . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target ubuntu20-baseline . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target ubuntu20-all . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target ubuntu16-cross-armv7 . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target ubuntu20-cross-armv7 . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target ubuntu16-cross-aarch64 . &&
DOCKER_BUILDKIT=1 docker build -f trunk/Dockerfile.builds --target ubuntu20-cross-aarch64 .
if [[ $? -ne 0 ]]; then echo "Docker for build failed"; exit 1; fi

0 comments on commit e964d06

Please sign in to comment.