From 495c67e74ebd365d250a96485a2e990f313f80c7 Mon Sep 17 00:00:00 2001 From: Michal Mielewczyk Date: Tue, 28 Jan 2025 16:01:58 +0100 Subject: [PATCH 1/3] posix env: Implement missing atomic operations Signed-off-by: Michal Mielewczyk --- env/posix/ocf_env.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/env/posix/ocf_env.h b/env/posix/ocf_env.h index 9220d1e..91167cc 100644 --- a/env/posix/ocf_env.h +++ b/env/posix/ocf_env.h @@ -1,6 +1,6 @@ /* * Copyright(c) 2019-2022 Intel Corporation - * Copyright(c) 2023-2024 Huawei Technologies + * Copyright(c) 2023-2025 Huawei Technologies * SPDX-License-Identifier: BSD-3-Clause */ @@ -378,12 +378,12 @@ typedef struct { static inline int env_atomic_read(const env_atomic *a) { - return a->counter; /* TODO */ + return __atomic_load_n(&a->counter, __ATOMIC_SEQ_CST); } static inline void env_atomic_set(env_atomic *a, int i) { - a->counter = i; /* TODO */ + __atomic_store_n(&a->counter, i, __ATOMIC_SEQ_CST); } static inline void env_atomic_add(int i, env_atomic *a) @@ -453,12 +453,12 @@ static inline int env_atomic_add_unless(env_atomic *a, int i, int u) static inline long env_atomic64_read(const env_atomic64 *a) { - return a->counter; /* TODO */ + return __atomic_load_n(&a->counter, __ATOMIC_SEQ_CST); } static inline void env_atomic64_set(env_atomic64 *a, long i) { - a->counter = i; /* TODO */ + __atomic_store_n(&a->counter, i, __ATOMIC_SEQ_CST); } static inline void env_atomic64_add(long i, env_atomic64 *a) From 8dc462e08e11dc2851ad61f78626e0566d35ea8a Mon Sep 17 00:00:00 2001 From: Michal Mielewczyk Date: Tue, 28 Jan 2025 09:47:08 +0100 Subject: [PATCH 2/3] Fix using request after free Signed-off-by: Michal Mielewczyk --- src/metadata/metadata_raw_dynamic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/metadata/metadata_raw_dynamic.c b/src/metadata/metadata_raw_dynamic.c index 481fc01..85b8806 100644 --- a/src/metadata/metadata_raw_dynamic.c +++ b/src/metadata/metadata_raw_dynamic.c @@ -1,6 +1,6 @@ /* * Copyright(c) 2012-2022 Intel Corporation - * Copyright(c) 2024 Huawei Technologies + * Copyright(c) 2024-2025 Huawei Technologies * SPDX-License-Identifier: BSD-3-Clause */ @@ -366,10 +366,10 @@ static void raw_dynamic_load_all_complete( { context->cmpl(context->priv, error); - ocf_req_put(context->req); env_secure_free(context->page, PAGE_SIZE); env_free(context->zpage); ctx_data_free(context->cache->owner, context->req->data); + ocf_req_put(context->req); env_vfree(context); } From 23abad76a8257bbf2281652d269aa5e718b4e460 Mon Sep 17 00:00:00 2001 From: Krzysztof Majzerowicz-Jaszcz Date: Thu, 8 Sep 2022 17:30:57 +0200 Subject: [PATCH 3/3] Add script for running PyOCF with sanitization GCC/Clang sanitizer can be used together with PyOCF to catch some errors during testing. CC was purposely removed from the Makefile. It always points to GCC on Linux by default. This allows to change the compiler and its options during the run of the script Signed-off-by: Krzysztof Majzerowicz-Jaszcz Signed-off-by: Michal Mielewczyk --- tests/functional/Makefile | 7 +++-- tests/functional/run_with_sanitizers.sh | 37 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100755 tests/functional/run_with_sanitizers.sh diff --git a/tests/functional/Makefile b/tests/functional/Makefile index a10d65f..b7ad1e4 100644 --- a/tests/functional/Makefile +++ b/tests/functional/Makefile @@ -1,5 +1,6 @@ # # Copyright(c) 2019-2022 Intel Corporation +# Copyright(c) 2025 Huawei Technologies # SPDX-License-Identifier: BSD-3-Clause # @@ -11,9 +12,8 @@ INCDIR=$(ADAPTERDIR)/ocf/include WRAPDIR=$(ADAPTERDIR)/c/wrappers HELPDIR=$(ADAPTERDIR)/c/helpers -CC=gcc -CFLAGS=-g -Wall -I$(INCDIR) -I$(SRCDIR)/ocf/env -LDFLAGS=-pthread -lz +CFLAGS=-g -Wall -I$(INCDIR) -I$(SRCDIR)/ocf/env $(OPT_CFLAGS) +LDFLAGS=-pthread #-lz SRC=$(shell find $(SRCDIR) $(WRAPDIR) $(HELPDIR) -name \*.c) OBJS=$(patsubst %.c, %.o, $(SRC)) @@ -48,6 +48,7 @@ distclean: clean @rm -rf $(OCFLIB) $(OBJS) @rm -rf $(SRCDIR)/ocf @rm -rf $(INCDIR)/ocf + @find . -name *.gc* -delete @echo " DISTCLEAN " .PHONY: all clean sync config_random distclean diff --git a/tests/functional/run_with_sanitizers.sh b/tests/functional/run_with_sanitizers.sh new file mode 100755 index 0000000..3805c22 --- /dev/null +++ b/tests/functional/run_with_sanitizers.sh @@ -0,0 +1,37 @@ +# +# Copyright(c) 2019-2022 Intel Corporation +# Copyright(c) 2025 Huawei Technologies +# SPDX-License-Identifier: BSD-3-Clause +# + +#!/usr/bin/env bash + +LIB_DIR="/lib/x86_64-linux-gnu/" + +ASAN_LIB="$LIB_DIR/libasan.so.6" +TSAN_LIB="$LIB_DIR/libtsan.so.0" +UBSAN_LIB="$LIB_DIR/libubsan.so.1" + +# Path to test file/directory +PYOCF_TESTS_PATH="tests/" + +echo "Cleaning and building with Address Sanitizer" +make distclean +OPT_CFLAGS="-fsanitize=address" make -j >/dev/null +echo "Running tests, please wait..." +LD_PRELOAD=$ASAN_LIB ASAN_OPTIONS=log_output=asan_log.txt PYTHONMALLOC=malloc pytest $PYOCF_TESTS_PATH 2>&1 | tee asan_output.txt +echo "Done, check asan_log.txt" + +echo "Cleaning and building with Thread Sanitizer" +make distclean +OPT_CFLAGS="-fsanitize=thread -fno-omit-frame-pointer" make -j >/dev/null +echo "Running tests, please wait..." +LD_PRELOAD=$TSAN_LIB TSAN_OPTIONS=log_output=tsan_log.txt pytest -s $PYOCF_TESTS_PATH 2>&1 | tee tsan_output.txt +echo "Done, check tsan_log.txt" + +echo "Cleaning and building with Undefined Behaviour Sanitizer" +make distclean +OPT_CFLAGS="-fsanitize=undefined -fno-sanitize=alignment" make -j >/dev/null +echo "Running tests, please wait..." +LD_PRELOAD=$UBSAN_LIB UBSAN_OPTIONS=log_output=ubsan_log.txt pytest -s $PYOCF_TESTS_PATH 2>&1 | tee ubsan_output.txt +echo "Done, check ubsan_output.txt"