diff --git a/frameworks/native/include/seq_packet_socket_client.h b/frameworks/native/include/seq_packet_socket_client.h index 90fdf01b26f2598f83fc25ad7612ab3a77dcc6a9..4549131ff8eeb569f950e836dc869cae3fc1cf1d 100644 --- a/frameworks/native/include/seq_packet_socket_client.h +++ b/frameworks/native/include/seq_packet_socket_client.h @@ -30,7 +30,7 @@ class SeqPacketSocketClient : public SocketClient { public: SeqPacketSocketClient(std::string serverPath, int socketOption) : SocketClient(serverPath, SOCK_SEQPACKET) { - int socketType = SOCK_SEQPACKET | (socketOption & allowOption); + int socketType = SOCK_SEQPACKET | ((uint32_t)socketOption & (uint32_t)allowOption); SetType(socketType); } ~SeqPacketSocketClient() = default; diff --git a/services/hilogd/include/log_compress.h b/services/hilogd/include/log_compress.h index 0a32f76dda4eb4da92b6486a88af80b693b561f5..8dad94e7376ae51c8c615d36afbeeb1ce27aa567 100644 --- a/services/hilogd/include/log_compress.h +++ b/services/hilogd/include/log_compress.h @@ -38,6 +38,7 @@ public: LogCompress(); virtual ~LogCompress() = default; virtual int Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &compressBuffer) = 0; + void DeleteZData(); unsigned char *zdata = nullptr; char buffIn[CHUNK] = {0}; char buffOut[CHUNK] = {0}; diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index eda4ec9ff4a68d1d3957f49f3aacd5512b82ff49..695ee636a2a7409ba5883f45e9fac94dcf42b7f4 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -30,7 +30,7 @@ typedef struct { } PersistRecoveryInfo; const std::string ANXILLARY_FILE_NAME = "persisterInfo_"; -uLong GetInfoCRC32(const PersistRecoveryInfo &info); +uint64_t GetInfoHash(const PersistRecoveryInfo &info); class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index 6a997af8cb59011f0f2e976d469308f643dd0eee..c9402ace625f3fd30916cdcf26bee5afe2a3cd99 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -26,6 +26,12 @@ LogCompress::LogCompress() { } +void LogCompress::DeleteZData() +{ + delete zdata; + zdata = nullptr; +} + int NoneCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &compressBuffer) { if (memcpy_s(compressBuffer->content, compressBuffer->offset, buffer->content, buffer->offset) != 0) { @@ -51,8 +57,7 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com cStream.zfree = Z_NULL; cStream.opaque = Z_NULL; if (deflateInit2(&cStream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) { - delete zdata; - zdata = nullptr; + DeleteZData();; return -1; } do { @@ -60,7 +65,7 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com if (flag) { memset_s(buffIn, CHUNK, 0, CHUNK); if (memmove_s(buffIn, CHUNK, buffer->content + src_pos, read - src_pos) != 0) { - delete zdata; + DeleteZData(); return -1; } cStream.avail_in = read - src_pos; @@ -68,7 +73,7 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com } else { memset_s(buffIn, CHUNK, 0, CHUNK); if (memmove_s(buffIn, CHUNK, buffer->content + src_pos, toRead) != 0) { - delete zdata; + DeleteZData(); return -1; }; src_pos += toRead; @@ -82,12 +87,12 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com cStream.avail_out = CHUNK; cStream.next_out = (Bytef *)buffOut; if (deflate(&cStream, flush) == Z_STREAM_ERROR) { - delete zdata; + DeleteZData(); return -1; } unsigned have = CHUNK - cStream.avail_out; if (memmove_s(zdata + dst_pos, CHUNK, buffOut, have) != 0) { - delete zdata; + DeleteZData(); return -1; } dst_pos += have; @@ -97,11 +102,11 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com (void)deflateEnd(&cStream); if (memcpy_s(compressBuffer->content + compressBuffer->offset, MAX_PERSISTER_BUFFER_SIZE - compressBuffer->offset, zdata, dst_pos) != 0) { - delete zdata; + DeleteZData(); return -1; } compressBuffer->offset += dst_pos; - delete zdata; + DeleteZData(); return 0; } @@ -119,7 +124,7 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com cctx = ZSTD_createCCtx(); if (cctx == nullptr) { cout << "ZSTD_createCCtx() failed!" << endl; - delete zdata; + DeleteZData();; return -1; } ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compressionlevel); @@ -133,7 +138,7 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com if (flag) { memset_s(buffIn, CHUNK, 0, CHUNK); if (memmove_s(buffIn, CHUNK, buffer->content + src_pos, read - src_pos) != 0) { - delete zdata; + DeleteZData(); return -1; } input = {buffIn, read - src_pos, 0}; @@ -141,7 +146,7 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com } else { memset_s(buffIn, CHUNK, 0, CHUNK); if (memmove_s(buffIn, CHUNK, buffer->content + src_pos, toRead) != 0) { - delete zdata; + DeleteZData(); return -1; } input = {buffIn, toRead, 0}; @@ -153,7 +158,7 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com ZSTD_outBuffer output = {buffOut, CHUNK, 0}; size_t const remaining = ZSTD_compressStream2(cctx, &output, &input, mode); if (memmove_s(zdata + dst_pos, zdlen, (Bytef *)buffOut, output.pos) != 0) { - delete zdata; + DeleteZData(); return -1; } dst_pos += output.pos; @@ -163,11 +168,11 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com ZSTD_freeCCtx(cctx); if (memcpy_s(compressBuffer->content + compressBuffer->offset, MAX_PERSISTER_BUFFER_SIZE - compressBuffer->offset, zdata, dst_pos) != 0) { - delete zdata; + DeleteZData(); return -1; } compressBuffer->offset += dst_pos; - delete zdata; + DeleteZData(); #endif // #ifdef USING_ZSTD_COMPRESS return 0; } diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 4831bfac0d3ab96cdb16d4af309c154708b29cdc..93929f6c42943776845d7fc059ff0dfcecb9bcdd 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -59,6 +59,7 @@ LogPersister::LogPersister(uint32_t id, string path, uint32_t fileSize, uint16_t hilogBuffer = &_buffer; compressor = nullptr; buffer = nullptr; + compressBuffer = nullptr; plainLogSize = 0; } diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 407172fc14f84f108cbc6cba8b9b03454474690c..9207c75147672bfe889ed031050b60a1075c09c0 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -25,11 +25,19 @@ namespace OHOS { namespace HiviewDFX { using namespace std; -uLong GetInfoCRC32(const PersistRecoveryInfo &info) -{ - uLong crc = crc32(0L, Z_NULL, 0); - crc = crc32(crc, (Bytef*)(&info), sizeof(PersistRecoveryInfo)); - return crc; +constexpr uint64_t PRIME = 0x100000001B3ull; +constexpr uint64_t BASIS = 0xCBF29CE484222325ull; +uint64_t GetInfoHash(const PersistRecoveryInfo &info) +{ + uint64_t ret {BASIS}; + const char *p = (char *)&info; + unsigned long i = 0; + while (i < sizeof(PersistRecoveryInfo)) { + ret ^= *(p+1); + ret *= PRIME; + i++; + } + return ret; } LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_t fileNum, string suffix) @@ -37,6 +45,7 @@ LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_ { index = -1; needRotate = true; + memset_s(&info, sizeof(info), 0, sizeof(info)); } LogPersisterRotator::~LogPersisterRotator() @@ -165,10 +174,10 @@ int LogPersisterRotator::SaveInfo(const LogPersistStartMsg& pMsg, const QueryCon void LogPersisterRotator::WriteRecoveryInfo() { std::cout << "Save Info file!" << std::endl; - uLong crc = GetInfoCRC32(info); + uint64_t hash = GetInfoHash(info); fseek(fdinfo, 0, SEEK_SET); fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); - fwrite(&crc, sizeof(uLong), 1, fdinfo); + fwrite(&hash, sizeof(hash), 1, fdinfo); fflush(fdinfo); fsync(fileno(fdinfo)); } diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 4ad8f1c2c259d6f046a63978c0eb5a9ca7ae1917..4b35009910fa8a7ba65129600012381066b93b79 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -597,12 +597,12 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) } PersistRecoveryInfo info; fread(&info, sizeof(PersistRecoveryInfo), 1, infile); - uLong crcSum = 0L; - fread(&crcSum, sizeof(uLong), 1, infile); + uint64_t hashSum = 0L; + fread(&hashSum, sizeof(hashSum), 1, infile); fclose(infile); - uLong crc = GetInfoCRC32(info); - if (crc != crcSum) { - std::cout << "Info file CRC Checksum Failed!" << std::endl; + uint64_t hash = GetInfoHash(info); + if (hash != hashSum) { + std::cout << "Info file Checksum Failed!" << std::endl; continue; } JobLauncher(info.msg, _buffer, true, info.index + 1);