Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] Check the return value of posix_memalign before use the aligned buffer. #25

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ endif()
option(WITH_TESTS "Build the starcache test" OFF)
option(WITH_TOOLS "Build the starcache tools" OFF)
option(WITH_COVERAGE "Enable code coverage build" OFF)
option(FIND_DEFAULT_PATH "Whether to find the library in default system library path" OFF)

# set CMAKE_BUILD_TYPE
if (NOT CMAKE_BUILD_TYPE)
Expand Down Expand Up @@ -83,43 +84,57 @@ set(CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX}/include)

#### External Dependencies ####

FUNCTION(SEARCH_LIBRARY RESULT LIB_NAME LIB_ROOT)
FUNCTION(FIND_LIBRARY_IN_PATH RESULT LIB_FILE LIB_NAME LIB_PATH)
if(FIND_DEFAULT_PATH)
find_library(${RESULT} NAMES ${LIB_FILE} ${LIB_NAME} PATHS ${LIB_PATH}/lib)
else()
find_library(${RESULT} NAMES ${LIB_FILE} ${LIB_NAME} PATHS ${LIB_PATH}/lib NO_DEFAULT_PATH)
endif()
if (NOT ${RESULT})
if(FIND_DEFAULT_PATH)
find_library(${RESULT} NAMES ${LIB_FILE} ${LIB_NAME} PATHS ${LIB_PATH}/lib64)
else()
find_library(${RESULT} NAMES ${LIB_FILE} ${LIB_NAME} PATHS ${LIB_PATH}/lib64 NO_DEFAULT_PATH)
endif()
endif()
ENDFUNCTION()

FUNCTION(SEARCH_LIBRARY RESULT LIB_FILE LIB_NAME LIB_ROOT)
if ("${LIB_ROOT}" STREQUAL "")
set(SEARCH_PATH ${STARCACHE_THIRDPARTY_DIR})
else()
set(SEARCH_PATH ${LIB_ROOT})
endif()

find_library(${RESULT} ${LIB_NAME} ${SEARCH_PATH}/lib)
if (NOT ${RESULT})
find_library(${RESULT} ${LIB_NAME} ${SEARCH_PATH}/lib64)
endif()

FIND_LIBRARY_IN_PATH(${RESULT} ${LIB_FILE} ${LIB_NAME} ${SEARCH_PATH})
if (${RESULT})
message(STATUS "find library ${LIB_NAME}")
if ((NOT "${LIB_ROOT}" STREQUAL "") AND (EXISTS "${LIB_ROOT}/include"))
include_directories(${LIB_ROOT}/include)
endif()
else()
message(ERROR "cannot find library ${LIB_NAME}")
endif()
ENDFUNCTION()

## PROTOBUF
SEARCH_LIBRARY(PROTOBUF_LIBPROTOBUF protobuf "${PROTOBUF_ROOT}")
SEARCH_LIBRARY(PROTOBUF_LIBPROTOBUF libprotobuf.a protobuf "${PROTOBUF_ROOT}")

## GFLAGS
SEARCH_LIBRARY(GFLAGS_LIB gflags "${GFLAGS_ROOT}")
SEARCH_LIBRARY(GFLAGS_LIB libgflags.a gflags "${GFLAGS_ROOT}")

## GLOG
SEARCH_LIBRARY(GLOG_LIB glog "${GLOG_ROOT}")
SEARCH_LIBRARY(GLOG_LIB libglog.a glog "${GLOG_ROOT}")

## BRPC
SEARCH_LIBRARY(BRPC_LIB brpc "${BRPC_ROOT}")
SEARCH_LIBRARY(BRPC_LIB libbrpc.a brpc "${BRPC_ROOT}")

## SSL
SEARCH_LIBRARY(SSL_LIB ssl "${SSL_ROOT}")
SEARCH_LIBRARY(CRYPTO_LIB crypto "${SSL_ROOT}")
SEARCH_LIBRARY(SSL_LIB libssl.a ssl "${SSL_ROOT}")
SEARCH_LIBRARY(CRYPTO_LIB libcrypto.a crypto "${SSL_ROOT}")

## FMT
SEARCH_LIBRARY(FMT_LIB fmt "${FMT_ROOT}")
SEARCH_LIBRARY(FMT_LIB libfmt.a fmt "${FMT_ROOT}")

## BOOST
if ("${BOOST_ROOT}" STREQUAL "")
Expand Down Expand Up @@ -203,11 +218,11 @@ target_link_libraries(starcache
${PROTOBUF_LIBPROTOBUF}
${SSL_LIB}
${CRYPTO_LIB}
${BOOST_LIB}
${BOOST_LIB}
${GLOG_LIB}
${GFLAGS_LIB}
${BRPC_LIB}
${FMT_LIB}
${FMT_LIB}
-Wl,--end-group
)

Expand All @@ -230,7 +245,7 @@ if(WITH_TESTS)
endif()
unset(CXX_INCLUDES)

SEARCH_LIBRARY(GTEST_LIB gtest "${GTEST_ROOT}")
SEARCH_LIBRARY(GTEST_LIB libgtest.a gtest "${GTEST_ROOT}")

# TEST LIBRARY
set(STARCACHE_TEST_SRCS
Expand Down
1 change: 1 addition & 0 deletions build-scripts/cmake-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ $STARCACHE_CMAKE_CMD -B ${CMAKE_BUILD_DIR} -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
-DFMT_ROOT=${WITH_FMT_ROOT} \
-DGTEST_ROOT=${WITH_GTEST_ROOT} \
-DBOOST_ROOT=${WITH_BOOST_ROOT} \
-DFIND_DEFAULT_PATH=${FIND_DEFAULT_PATH} \
${STARCACHE_TEST_COVERAGE:+"-Dstarcache_BUILD_COVERAGE=$STARCACHE_TEST_COVERAGE"} \
.

Expand Down
4 changes: 3 additions & 1 deletion src/aux_funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ uint64_t cachekey2id(const std::string& key) {
size_t align_iobuf(const butil::IOBuf& buf, void** aligned_data) {
size_t aligned_unit = config::FLAGS_io_align_unit_size;
size_t aligned_size = round_up(buf.size(), aligned_unit);
posix_memalign(aligned_data, aligned_unit, aligned_size);
if (posix_memalign(aligned_data, aligned_unit, aligned_size) != 0) {
return 0;
}

butil::IOBuf tmp_buf(buf);
// IOBufCutter is a specialized utility to cut from IOBuf faster than using corresponding
Expand Down
19 changes: 13 additions & 6 deletions src/block_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Status BlockFile::close() {
}

Status BlockFile::write(off_t offset, const IOBuf& buf) {
ssize_t ret = 0;
ssize_t ret = -1;
void* aligned_data = nullptr;
size_t aligned_size = buf.size();

Expand All @@ -57,26 +57,33 @@ Status BlockFile::write(off_t offset, const IOBuf& buf) {
auto data = buf.backing_block(0).data();
if (mem_need_align(data, buf.size())) {
aligned_size = align_iobuf(buf, &aligned_data);
ret = ::pwrite(_fd, aligned_data, aligned_size, offset);
free(aligned_data);
if (aligned_size > 0) {
ret = ::pwrite(_fd, aligned_data, aligned_size, offset);
free(aligned_data);
}
} else {
ret = ::pwrite(_fd, data, aligned_size, offset);
}
} else if (!config::FLAGS_enable_os_page_cache) {
aligned_size = align_iobuf(buf, &aligned_data);
ret = ::pwrite(_fd, aligned_data, aligned_size, offset);
free(aligned_data);
if (aligned_size > 0) {
ret = ::pwrite(_fd, aligned_data, aligned_size, offset);
free(aligned_data);
}
} else {
struct iovec iov[block_num];
for (size_t i = 0; i < block_num; ++i) {
iov[i] = {(void*)buf.backing_block(i).data(), buf.backing_block(i).size()};
}
ret = ::pwritev(_fd, iov, block_num, offset);
}

if (ret < 0) {
if (aligned_size == 0) {
errno = ENOMEM;
}
return _report_io_error("fail to write block file");
}

STAR_VLOG << "write block file success, fd: " << _fd << ", path: " << _file_path << ", offset: " << offset
<< ", buf size: " << buf.size() << ", aligned_size: " << aligned_size << ", buf block num: " << block_num;
return Status::OK();
Expand Down
1 change: 1 addition & 0 deletions src/cache_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include <atomic>
#include <mutex>
#include <shared_mutex>

#include "block_item.h"
Expand Down
1 change: 1 addition & 0 deletions src/mem_space_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <butil/memory/singleton.h>

#include <atomic>
#include <mutex>
#include <shared_mutex>

namespace starrocks::starcache {
Expand Down
1 change: 1 addition & 0 deletions src/sharded_lock_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <butil/memory/singleton.h>

#include <mutex>
#include <shared_mutex>

#include "aux_funcs.h"
Expand Down
4 changes: 4 additions & 0 deletions src/star_cache_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ Status StarCacheImpl::_write_block(CacheItemPtr cache_item, const BlockKey& bloc
// We allocate an aligned buffer here to avoid repeatedlly copying data to a new aligned buffer
// when flush to disk file in `O_DIRECT` mode.
size = align_iobuf(buf, &data);
if (size == 0) {
LOG(ERROR) << "align io buffer failed when write block";
return Status(ENOMEM, "align io buffer failed");
}
} else {
data = malloc(buf.size());
buf.copy_to(data);
Expand Down
1 change: 1 addition & 0 deletions src/tools/starcache_tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <memory>
#include <numeric>
#include <thread>
#include <mutex>
#include <shared_mutex>

#include "star_cache.h"
Expand Down