<div dir="ltr"><div>Добрый день!</div><div><br></div><div>для тестирования самодельного модуля, использовали такую методику</div><div><br></div><div>взяли <a href="https://github.com/nginx/nginx-tests">https://github.com/nginx/nginx-tests</a></div><div>собрали nginx с самодельным модулем и gcc asan (не суть, могли бы взять clang asan).</div><div>собрали кучу вариантов прохода по коду (это круто), нашли сколько-то дефектов в своем модуле.</div><div><br></div><div>интересные вещи возникают с asan, причем, воспроизводятся на оригинальном nginx.</div><div>это непонятно. можете прокомментировать ?</div><div><br></div><div>1. беру nginx из официальной репы. смотрю "nginx -V", добавляю -fsanitize=address к cc-opts</div><div>2. выставляю ASAN_OPTIONS="log_path=asan.log"</div><div>3. запукаю тесты, смотрю, что упало в asan.log (а там есть сработки, это немного неожиданно)</div><div><br></div><div>скрипт</div><div><br></div><div>#!/bin/bash<br><br>set -e<br><br>sysctl kernel.core_pattern=/tmp/core-%e-%s-%u-%g-%p-%t<br>ulimit -c unlimited<br>export version=1.19.2<br><br>mkdir t<br>cd t<br><br>wget <a href="http://nginx.org/download/nginx-${version}.tar.gz">http://nginx.org/download/nginx-${version}.tar.gz</a><br>tar xf nginx-${version}.tar.gz<br><br>cd nginx-${version}<br><br>./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O0 -fdebug-prefix-map=/data/builder/debuild/nginx-1.19.2/debian/debuild-base/nginx-1.19.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC -fsanitize=address' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie -fsanitize=address -lasan' --with-debug<br><br><br>make<br>export ASAN_OPTIONS="log_path=asan.log"<br>export TEST_NGINX_BINARY=`pwd`/objs/nginx<br>export TEST_NGINX_GLOBALS='user root; '<br>export TEST_NGINX_GLOBALS_HTTP='error_log /tmp/e.log;'<br>git clone <a href="https://github.com/nginx/nginx-tests">https://github.com/nginx/nginx-tests</a><br>prove -r nginx-tests</div><div><br></div><div><br></div><div>сработки</div><div><br></div><div># locate asan.log<br>/root/t/nginx-1.19.2/nginx-tests/asan.log.46464<br>/root/t/nginx-1.19.2/nginx-tests/asan.log.46728<br>/root/t/nginx-1.19.2/nginx-tests/asan.log.46866<br>/root/t/nginx-1.19.2/nginx-tests/asan.log.46876<br>/root/t/nginx-1.19.2/nginx-tests/asan.log.47784<br>/root/t/nginx-1.19.2/nginx-tests/asan.log.47931<br>/root/t/nginx-1.19.2/nginx-tests/asan.log.47957<br>/root/t/nginx-1.19.2/nginx-tests/asan.log.47983<br>/root/t/nginx-1.19.2/nginx-tests/asan.log.48005<br>/root/t/nginx-1.19.2/nginx-tests/asan.log.48760<br>/root/t/nginx-1.19.2/nginx-tests/asan.log.48770<br>/root/t/nginx-1.19.2/nginx-tests/asan.log.48818<br>/root/t/nginx-1.19.2/nginx-tests/asan.log.49079<br>/root/t/nginx-1.19.2/nginx-tests/asan.log.49329</div><div><br></div><div>пример первой</div><div><br></div><div># cat /root/t/nginx-1.19.2/nginx-tests/asan.log.46464<br><br>=================================================================<br>==46464==ERROR: LeakSanitizer: detected memory leaks<br><br>Indirect leak of 16384 byte(s) in 1 object(s) allocated from:<br>    #0 0x7f36e86dbaa5 in posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10eaa5)<br>    #1 0x5627781c130d in ngx_memalign src/os/unix/ngx_alloc.c:57<br>    #2 0x5627781231ef in ngx_palloc_block src/core/ngx_palloc.c:186<br>    #3 0x562778123166 in ngx_palloc_small src/core/ngx_palloc.c:173<br>    #4 0x562778122f91 in ngx_palloc src/core/ngx_palloc.c:127<br>    #5 0x5627782744f4 in ngx_http_add_variable src/http/ngx_http_variables.c:449<br>    #6 0x56277828046b in ngx_http_variables_add_core_vars src/http/ngx_http_variables.c:2622<br>    #7 0x56277822fd37 in ngx_http_core_preconfiguration src/http/ngx_http_core_module.c:3262<br>    #8 0x562778213c81 in ngx_http_block src/http/ngx_http.c:227<br>    #9 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #10 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #11 0x56277816b106 in ngx_init_cycle src/core/ngx_cycle.c:275<br>    #12 0x5627781188ae in main src/core/nginx.c:291<br>    #13 0x7f36e7fa40b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)<br><br>Indirect leak of 16384 byte(s) in 1 object(s) allocated from:<br>    #0 0x7f36e86dbaa5 in posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10eaa5)<br>    #1 0x5627781c130d in ngx_memalign src/os/unix/ngx_alloc.c:57<br>    #2 0x5627781231ef in ngx_palloc_block src/core/ngx_palloc.c:186<br>    #3 0x562778123166 in ngx_palloc_small src/core/ngx_palloc.c:173<br>    #4 0x562778122f91 in ngx_palloc src/core/ngx_palloc.c:127<br>    #5 0x562778123957 in ngx_pcalloc src/core/ngx_palloc.c:302<br>    #6 0x5627783a72da in ngx_http_proxy_create_loc_conf src/http/modules/ngx_http_proxy_module.c:2854<br>    #7 0x56277821386d in ngx_http_block src/http/ngx_http.c:209<br>    #8 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #9 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #10 0x56277816b106 in ngx_init_cycle src/core/ngx_cycle.c:275<br>    #11 0x5627781188ae in main src/core/nginx.c:291<br>    #12 0x7f36e7fa40b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)<br><br>Indirect leak of 16384 byte(s) in 1 object(s) allocated from:<br>    #0 0x7f36e86dbaa5 in posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10eaa5)<br>    #1 0x5627781c130d in ngx_memalign src/os/unix/ngx_alloc.c:57<br>    #2 0x5627781231ef in ngx_palloc_block src/core/ngx_palloc.c:186<br>    #3 0x562778123166 in ngx_palloc_small src/core/ngx_palloc.c:173<br>    #4 0x562778122f91 in ngx_palloc src/core/ngx_palloc.c:127<br>    #5 0x562778123957 in ngx_pcalloc src/core/ngx_palloc.c:302<br>    #6 0x562778126bba in ngx_hash_init src/core/ngx_hash.c:392<br>    #7 0x562778281545 in ngx_http_variables_init_vars src/http/ngx_http_variables.c:2730<br>    #8 0x56277821470b in ngx_http_block src/http/ngx_http.c:314<br>    #9 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #10 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #11 0x56277816b106 in ngx_init_cycle src/core/ngx_cycle.c:275<br>    #12 0x5627781188ae in main src/core/nginx.c:291<br>    #13 0x7f36e7fa40b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)<br><br>Indirect leak of 16384 byte(s) in 1 object(s) allocated from:<br>    #0 0x7f36e86dbaa5 in posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10eaa5)<br>    #1 0x5627781c130d in ngx_memalign src/os/unix/ngx_alloc.c:57<br>    #2 0x5627781231ef in ngx_palloc_block src/core/ngx_palloc.c:186<br>    #3 0x562778123166 in ngx_palloc_small src/core/ngx_palloc.c:173<br>    #4 0x562778122f91 in ngx_palloc src/core/ngx_palloc.c:127<br>    #5 0x562778123957 in ngx_pcalloc src/core/ngx_palloc.c:302<br>    #6 0x5627783dfe02 in ngx_http_scgi_create_loc_conf src/http/modules/ngx_http_scgi_module.c:1232<br>    #7 0x56277822dbd8 in ngx_http_core_location src/http/ngx_http_core_module.c:2968<br>    #8 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #9 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #10 0x56277822cfe6 in ngx_http_core_server src/http/ngx_http_core_module.c:2875<br>    #11 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #12 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #13 0x562778213db5 in ngx_http_block src/http/ngx_http.c:237<br>    #14 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #15 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #16 0x56277816b106 in ngx_init_cycle src/core/ngx_cycle.c:275<br>    #17 0x5627781188ae in main src/core/nginx.c:291<br>    #18 0x7f36e7fa40b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)<br><br>Indirect leak of 16384 byte(s) in 1 object(s) allocated from:<br>    #0 0x7f36e86dbaa5 in posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10eaa5)<br>    #1 0x5627781c130d in ngx_memalign src/os/unix/ngx_alloc.c:57<br>    #2 0x5627781231ef in ngx_palloc_block src/core/ngx_palloc.c:186<br>    #3 0x562778123166 in ngx_palloc_small src/core/ngx_palloc.c:173<br>    #4 0x562778122f91 in ngx_palloc src/core/ngx_palloc.c:127<br>    #5 0x562778123957 in ngx_pcalloc src/core/ngx_palloc.c:302<br>    #6 0x5627783d1c12 in ngx_http_uwsgi_create_loc_conf src/http/modules/ngx_http_uwsgi_module.c:1443<br>    #7 0x5627782b22d9 in ngx_http_upstream src/http/ngx_http_upstream.c:5805<br>    #8 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #9 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #10 0x562778213db5 in ngx_http_block src/http/ngx_http.c:237<br>    #11 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #12 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #13 0x56277816b106 in ngx_init_cycle src/core/ngx_cycle.c:275<br>    #14 0x5627781188ae in main src/core/nginx.c:291<br>    #15 0x7f36e7fa40b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)<br><br>Indirect leak of 16384 byte(s) in 1 object(s) allocated from:<br>    #0 0x7f36e86dbaa5 in posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10eaa5)<br>    #1 0x5627781c130d in ngx_memalign src/os/unix/ngx_alloc.c:57<br>    #2 0x5627781225ac in ngx_create_pool src/core/ngx_palloc.c:23<br>    #3 0x562778169b2d in ngx_init_cycle src/core/ngx_cycle.c:69<br>    #4 0x5627781188ae in main src/core/nginx.c:291<br>    #5 0x7f36e7fa40b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)<br><br>Indirect leak of 8192 byte(s) in 1 object(s) allocated from:<br>    #0 0x7f36e86dabc8 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)<br>    #1 0x5627781c10fb in ngx_alloc src/os/unix/ngx_alloc.c:22<br>    #2 0x562778123488 in ngx_palloc_large src/core/ngx_palloc.c:220<br>    #3 0x562778122fa6 in ngx_palloc src/core/ngx_palloc.c:131<br>    #4 0x56277812479e in ngx_array_push src/core/ngx_array.c:76<br>    #5 0x5627781293c2 in ngx_hash_add_key src/core/ngx_hash.c:840<br>    #6 0x5627782747a6 in ngx_http_add_variable src/http/ngx_http_variables.c:468<br>    #7 0x562778384f28 in ngx_http_map_block src/http/modules/ngx_http_map_module.c:230<br>    #8 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #9 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #10 0x562778213db5 in ngx_http_block src/http/ngx_http.c:237<br>    #11 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #12 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #13 0x56277816b106 in ngx_init_cycle src/core/ngx_cycle.c:275<br>    #14 0x5627781188ae in main src/core/nginx.c:291<br>    #15 0x7f36e7fa40b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)<br><br>Indirect leak of 6464 byte(s) in 1 object(s) allocated from:<br>    #0 0x7f36e86dabc8 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)<br>    #1 0x5627781c10fb in ngx_alloc src/os/unix/ngx_alloc.c:22<br>    #2 0x562778123488 in ngx_palloc_large src/core/ngx_palloc.c:220<br>    #3 0x562778122fa6 in ngx_palloc src/core/ngx_palloc.c:131<br>    #4 0x562778126c4a in ngx_hash_init src/core/ngx_hash.c:399<br>    #5 0x562778281545 in ngx_http_variables_init_vars src/http/ngx_http_variables.c:2730<br>    #6 0x56277821470b in ngx_http_block src/http/ngx_http.c:314<br>    #7 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #8 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #9 0x56277816b106 in ngx_init_cycle src/core/ngx_cycle.c:275<br>    #10 0x5627781188ae in main src/core/nginx.c:291<br>    #11 0x7f36e7fa40b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)<br><br>Indirect leak of 4280 byte(s) in 1 object(s) allocated from:<br>    #0 0x7f36e86dabc8 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)<br>    #1 0x5627781c10fb in ngx_alloc src/os/unix/ngx_alloc.c:22<br>    #2 0x562778123488 in ngx_palloc_large src/core/ngx_palloc.c:220<br>    #3 0x562778122fa6 in ngx_palloc src/core/ngx_palloc.c:131<br>    #4 0x562778123957 in ngx_pcalloc src/core/ngx_palloc.c:302<br>    #5 0x5627781288aa in ngx_hash_keys_array_init src/core/ngx_hash.c:723<br>    #6 0x5627782803af in ngx_http_variables_add_core_vars src/http/ngx_http_variables.c:2608<br>    #7 0x56277822fd37 in ngx_http_core_preconfiguration src/http/ngx_http_core_module.c:3262<br>    #8 0x562778213c81 in ngx_http_block src/http/ngx_http.c:227<br>    #9 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #10 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #11 0x56277816b106 in ngx_init_cycle src/core/ngx_cycle.c:275<br>    #12 0x5627781188ae in main src/core/nginx.c:291<br>    #13 0x7f36e7fa40b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)<br><br>Indirect leak of 4280 byte(s) in 1 object(s) allocated from:<br>    #0 0x7f36e86dabc8 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)<br>    #1 0x5627781c10fb in ngx_alloc src/os/unix/ngx_alloc.c:22<br>    #2 0x562778123488 in ngx_palloc_large src/core/ngx_palloc.c:220<br>    #3 0x562778122fa6 in ngx_palloc src/core/ngx_palloc.c:131<br>    #4 0x562778123957 in ngx_pcalloc src/core/ngx_palloc.c:302<br>    #5 0x5627781287f3 in ngx_hash_keys_array_init src/core/ngx_hash.c:717<br>    #6 0x5627782803af in ngx_http_variables_add_core_vars src/http/ngx_http_variables.c:2608<br>    #7 0x56277822fd37 in ngx_http_core_preconfiguration src/http/ngx_http_core_module.c:3262<br>    #8 0x562778213c81 in ngx_http_block src/http/ngx_http.c:227<br>    #9 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #10 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #11 0x56277816b106 in ngx_init_cycle src/core/ngx_cycle.c:275<br>    #12 0x5627781188ae in main src/core/nginx.c:291<br>    #13 0x7f36e7fa40b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)<br><br>Indirect leak of 4280 byte(s) in 1 object(s) allocated from:<br>    #0 0x7f36e86dabc8 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)<br>    #1 0x5627781c10fb in ngx_alloc src/os/unix/ngx_alloc.c:22<br>    #2 0x562778123488 in ngx_palloc_large src/core/ngx_palloc.c:220<br>    #3 0x562778122fa6 in ngx_palloc src/core/ngx_palloc.c:131<br>    #4 0x562778123957 in ngx_pcalloc src/core/ngx_palloc.c:302<br>    #5 0x56277812873c in ngx_hash_keys_array_init src/core/ngx_hash.c:712<br>    #6 0x5627782803af in ngx_http_variables_add_core_vars src/http/ngx_http_variables.c:2608<br>    #7 0x56277822fd37 in ngx_http_core_preconfiguration src/http/ngx_http_core_module.c:3262<br>    #8 0x562778213c81 in ngx_http_block src/http/ngx_http.c:227<br>    #9 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #10 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #11 0x56277816b106 in ngx_init_cycle src/core/ngx_cycle.c:275<br>    #12 0x5627781188ae in main src/core/nginx.c:291<br>    #13 0x7f36e7fa40b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)<br><br>Indirect leak of 4096 byte(s) in 1 object(s) allocated from:<br>    #0 0x7f36e86dabc8 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)<br>    #1 0x5627781c10fb in ngx_alloc src/os/unix/ngx_alloc.c:22<br>    #2 0x562778123488 in ngx_palloc_large src/core/ngx_palloc.c:220<br>    #3 0x562778122fa6 in ngx_palloc src/core/ngx_palloc.c:131<br>    #4 0x56277812479e in ngx_array_push src/core/ngx_array.c:76<br>    #5 0x5627781293c2 in ngx_hash_add_key src/core/ngx_hash.c:840<br>    #6 0x5627782747a6 in ngx_http_add_variable src/http/ngx_http_variables.c:468<br>    #7 0x56277828046b in ngx_http_variables_add_core_vars src/http/ngx_http_variables.c:2622<br>    #8 0x56277822fd37 in ngx_http_core_preconfiguration src/http/ngx_http_core_module.c:3262<br>    #9 0x562778213c81 in ngx_http_block src/http/ngx_http.c:227<br>    #10 0x56277817679d in ngx_conf_handler src/core/ngx_conf_file.c:463<br>    #11 0x5627781757c4 in ngx_conf_parse src/core/ngx_conf_file.c:319<br>    #12 0x56277816b106 in ngx_init_cycle src/core/ngx_cycle.c:275<br>    #13 0x5627781188ae in main src/core/nginx.c:291<br>    #14 0x7f36e7fa40b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)<br><br>SUMMARY: AddressSanitizer: 129896 byte(s) leaked in 12 allocation(s).</div><div><br></div><div><br></div><div><br></div><div>реально утечки )) ?<br></div></div>