From cf47f6bb92d0530e58a09e00d586fcf47f04b26b Mon Sep 17 00:00:00 2001 From: liujia178 Date: Fri, 8 Dec 2023 17:24:35 +0800 Subject: [PATCH] fix fuzz warning Signed-off-by: liujia178 --- .../refbase_fuzzer/refbase_fuzzer.cpp | 64 +++++++++---------- .../fuzztest/timer_fuzzer/timer_fuzzer.cpp | 10 +-- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/base/test/fuzztest/refbase_fuzzer/refbase_fuzzer.cpp b/base/test/fuzztest/refbase_fuzzer/refbase_fuzzer.cpp index ec2ce49..7967166 100644 --- a/base/test/fuzztest/refbase_fuzzer/refbase_fuzzer.cpp +++ b/base/test/fuzztest/refbase_fuzzer/refbase_fuzzer.cpp @@ -15,7 +15,7 @@ #include "refbase_fuzzer.h" -#include +#include #include #include "fuzz_log.h" @@ -30,12 +30,6 @@ const int MAX_THREADS = 10; const int MAX_OPS = 200; const int SLEEP_NANO_SECONDS = 10; -uint32_t GetThreadId() -{ - std::thread::id tid = this_thread::get_id(); - return *(uint32_t*)&tid; -} - struct TestRefBase : public RefBase { public: TestRefBase(bool* deleted, Utils::RWLock& rwLock) : deleted_(deleted), rwLock_(rwLock) @@ -72,10 +66,10 @@ void TestRefBase::OnFirstStrongRef(const void* objectId) } struct SingleThreadRefCounts { - size_t strongCount = 0; - size_t weakCount = 0; + int strongCount = 0; + int weakCount = 0; bool weakRefCounterExists = false; - size_t weakRefCount = 0; + int weakRefCount = 0; }; TestRefBase* g_ref; @@ -93,7 +87,7 @@ const std::vector> if (shouldLock) { g_strongLock.LockWrite(); } - FUZZ_LOGI("thread = %{public}u, DecStrongRef, refState->strongCount = %{public}d", GetThreadId(), + FUZZ_LOGI("thread = %{public}lu, DecStrongRef, refState->strongCount = %{public}d", pthread_self(), refState->strongCount); g_ref->DecStrongRef(nullptr); if (shouldLock) { @@ -111,8 +105,8 @@ const std::vector> if (shouldLock) { g_strongLock.LockWrite(); } - FUZZ_LOGI("thread = %{public}u, weakRef->DecWeakRefCount, refState->weakRefCount = %{public}d", - GetThreadId(), refState->weakRefCount); + FUZZ_LOGI("thread = %{public}lu, weakRef->DecWeakRefCount, refState->weakRefCount = %{public}d", + pthread_self(), refState->weakRefCount); weakRef->DecWeakRefCount(nullptr); if (shouldLock) { g_strongLock.UnLockWrite(); @@ -128,7 +122,7 @@ const std::vector> if (shouldLock) { g_strongLock.LockWrite(); } - FUZZ_LOGI("thread = %{public}u, DecWeakRef, refState->weakCount = %{public}d", GetThreadId(), + FUZZ_LOGI("thread = %{public}lu, DecWeakRef, refState->weakCount = %{public}d", pthread_self(), refState->weakCount); g_ref->DecWeakRef(nullptr); if (shouldLock) { @@ -141,12 +135,12 @@ const std::vector> const std::vector> readOrIncOperations = { [](SingleThreadRefCounts*, WeakRefCounter*&) { - FUZZ_LOGI("thread = %{public}u, GetSptrRefCount", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, GetSptrRefCount", pthread_self()); g_ref->GetSptrRefCount(); }, [](SingleThreadRefCounts*, WeakRefCounter*&) { - FUZZ_LOGI("thread = %{public}u, GetRefCounter", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, GetRefCounter", pthread_self()); RefCounter* refCounter = g_ref->GetRefCounter(); if (refCounter != nullptr) { refCounter->GetRefCount(); @@ -154,30 +148,30 @@ const std::vector> }, [](SingleThreadRefCounts*, WeakRefCounter*&) { - FUZZ_LOGI("thread = %{public}u, GetWptrRefCount", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, GetWptrRefCount", pthread_self()); g_ref->GetWptrRefCount(); }, [](SingleThreadRefCounts*, WeakRefCounter*&) { - FUZZ_LOGI("thread = %{public}u, IsAttemptAcquireSet", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, IsAttemptAcquireSet", pthread_self()); g_ref->IsAttemptAcquireSet(); }, [](SingleThreadRefCounts*, WeakRefCounter*&) { - FUZZ_LOGI("thread = %{public}u, IsExtendLifeTimeSet", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, IsExtendLifeTimeSet", pthread_self()); g_ref->IsExtendLifeTimeSet(); }, [](SingleThreadRefCounts* refState, WeakRefCounter*& weakRef) { if (refState->weakRefCounterExists) { - FUZZ_LOGI("thread = %{public}u, weakRef->GetWeakRefCount", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, weakRef->GetWeakRefCount", pthread_self()); weakRef->GetWeakRefCount(); } }, [](SingleThreadRefCounts* refState, WeakRefCounter*&) { g_attemptLock.LockRead(); - FUZZ_LOGI("thread = %{public}u, IncStrongRef", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, IncStrongRef", pthread_self()); g_ref->IncStrongRef(nullptr); refState->strongCount++; g_refModified = true; @@ -186,7 +180,7 @@ const std::vector> [](SingleThreadRefCounts* refState, WeakRefCounter*& weakRef) { if (!refState->weakRefCounterExists) { - FUZZ_LOGI("thread = %{public}u, CreateWeakRef", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, CreateWeakRef", pthread_self()); weakRef = g_ref->CreateWeakRef(nullptr); refState->weakRefCounterExists = true; // Only CreateWeakRef then release, will not delete RefBase, so g_refModified will not change. @@ -195,7 +189,7 @@ const std::vector> [](SingleThreadRefCounts* refState, WeakRefCounter*& weakRef) { if (refState->weakRefCounterExists) { - FUZZ_LOGI("thread = %{public}u, weakRef->IncWeakRefCount", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, weakRef->IncWeakRefCount", pthread_self()); weakRef->IncWeakRefCount(nullptr); refState->weakRefCount++; g_refModified = true; @@ -205,7 +199,7 @@ const std::vector> [](SingleThreadRefCounts* refState, WeakRefCounter*& weakRef) { if (refState->weakRefCounterExists) { g_attemptLock.LockWrite(); - FUZZ_LOGI("thread = %{public}u, weakRef->AttemptIncStrongRef", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, weakRef->AttemptIncStrongRef", pthread_self()); weakRef->AttemptIncStrongRef(nullptr); refState->strongCount++; g_refModified = true; @@ -214,7 +208,7 @@ const std::vector> }, [](SingleThreadRefCounts* refState, WeakRefCounter*&) { - FUZZ_LOGI("thread = %{public}u, IncWeakRef", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, IncWeakRef", pthread_self()); g_ref->IncWeakRef(nullptr); refState->weakCount++; g_refModified = true; @@ -222,7 +216,7 @@ const std::vector> [](SingleThreadRefCounts* refState, WeakRefCounter*&) { g_attemptLock.LockWrite(); - FUZZ_LOGI("thread = %{public}u, AttemptAcquire", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, AttemptAcquire", pthread_self()); g_ref->AttemptAcquire(nullptr); g_ref->IncStrongRef(nullptr); refState->strongCount++; @@ -232,7 +226,7 @@ const std::vector> [](SingleThreadRefCounts* refState, WeakRefCounter*&) { g_attemptLock.LockWrite(); - FUZZ_LOGI("thread = %{public}u, AttemptIncStrongRef", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, AttemptIncStrongRef", pthread_self()); g_ref->AttemptIncStrongRef(nullptr); refState->strongCount++; g_refModified = true; @@ -241,7 +235,7 @@ const std::vector> [](SingleThreadRefCounts* refState, WeakRefCounter*&) { g_attemptLock.LockWrite(); - FUZZ_LOGI("thread = %{public}u, AttemptIncStrong", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, AttemptIncStrong", pthread_self()); g_ref->AttemptIncStrong(nullptr); g_ref->IncStrongRef(nullptr); refState->strongCount++; @@ -267,10 +261,10 @@ void TestLoop(const std::vector& fuzzOps) g_strongLock.LockRead(); if (g_refDeleted) { if (state.weakRefCounterExists) { - FUZZ_LOGI("thread = %{public}u, delete newWeakRef", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, delete newWeakRef", pthread_self()); delete newWeakRef; } - FUZZ_LOGI("thread = %{public}u return", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu return", pthread_self()); g_strongLock.UnLockRead(); return; } @@ -284,7 +278,7 @@ void TestLoop(const std::vector& fuzzOps) // Clean up WeakRefCounter if (state.weakRefCounterExists) { - FUZZ_LOGI("thread = %{public}u, delete newWeakRef", GetThreadId()); + FUZZ_LOGI("thread = %{public}lu, delete newWeakRef", pthread_self()); delete newWeakRef; } else if (state.weakRefCount > 0) { for (; state.weakRefCount > 0; --state.weakRefCount) { @@ -292,8 +286,8 @@ void TestLoop(const std::vector& fuzzOps) if (shouldLock) { g_strongLock.LockWrite(); } - FUZZ_LOGI("thread = %{public}u, clean up DecWeakRefCount, refState->weakRefCount = %{public}d", - GetThreadId(), state.weakRefCount); + FUZZ_LOGI("thread = %{public}lu, clean up DecWeakRefCount, refState->weakRefCount = %{public}d", + pthread_self(), state.weakRefCount); newWeakRef->DecWeakRefCount(nullptr); if (shouldLock) { g_strongLock.UnLockWrite(); @@ -307,7 +301,7 @@ void TestLoop(const std::vector& fuzzOps) if (shouldLock) { g_strongLock.LockWrite(); } - FUZZ_LOGI("thread = %{public}u, clean up DecWeakRef, refState->weakCount = %{public}d", GetThreadId(), + FUZZ_LOGI("thread = %{public}lu, clean up DecWeakRef, refState->weakCount = %{public}d", pthread_self(), state.weakCount - 1); g_ref->DecWeakRef(nullptr); if (shouldLock) { @@ -321,7 +315,7 @@ void TestLoop(const std::vector& fuzzOps) if (shouldLock) { g_strongLock.LockWrite(); } - FUZZ_LOGI("thread = %{public}u, clean up DecStrongRef, refState->strongCount = %{public}d", GetThreadId(), + FUZZ_LOGI("thread = %{public}lu, clean up DecStrongRef, refState->strongCount = %{public}d", pthread_self(), state.strongCount - 1); g_ref->DecStrongRef(nullptr); if (shouldLock) { diff --git a/base/test/fuzztest/timer_fuzzer/timer_fuzzer.cpp b/base/test/fuzztest/timer_fuzzer/timer_fuzzer.cpp index 9e84774..d250cd5 100644 --- a/base/test/fuzztest/timer_fuzzer/timer_fuzzer.cpp +++ b/base/test/fuzztest/timer_fuzzer/timer_fuzzer.cpp @@ -41,11 +41,11 @@ const std::vectorConsumeBool(); uint32_t timerId = timer.Register(TimeOutCallback, interval, once); val.push_back(timerId); - FUZZ_LOGI("Register, interval = %{public}d, once = %{public}d, timerId = %{public}d", interval, once, timerId); + FUZZ_LOGI("Register, interval = %{public}u, once = %{public}d, timerId = %{public}u", interval, once, timerId); uint32_t sleepTime = dataProvider->ConsumeIntegralInRange(0, MAX_TIME_MS); std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime)); - FUZZ_LOGI("sleep_for, time = %{public}d, g_data = %{public}d", - sleepTime, g_data.load(std::memory_order_relaxed)); + FUZZ_LOGI( + "sleep_for, time = %{public}u, g_data = %{public}d", sleepTime, g_data.load(std::memory_order_relaxed)); }, [](FuzzedDataProvider* dataProvider, Utils::Timer& timer, vector& val) { @@ -54,7 +54,7 @@ const std::vectorConsumeIntegralInRange(0, val.size() - 1); timer.Unregister(val[id]); - FUZZ_LOGI("Unregister, timerId = %{public}d", val[id]); + FUZZ_LOGI("Unregister, timerId = %{public}u", val[id]); for (size_t i = id; i < val.size() - 1; i++) { swap(val[i], val[i + 1]); } @@ -68,7 +68,7 @@ void TimerTestFunc(const uint8_t* data, size_t size, FuzzedDataProvider* dataPro string teststr = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); uint32_t timeoutMs = dataProvider->ConsumeIntegralInRange(0, MAX_TIME_MS); Utils::Timer timer(teststr, timeoutMs); - FUZZ_LOGI("Timer, str = %{public}s, ms = %{public}d", teststr.c_str(), timeoutMs); + FUZZ_LOGI("Timer, str = %{public}s, ms = %{public}u", teststr.c_str(), timeoutMs); vector timerIds; g_data = 0; -- Gitee