Skip to content

Commit

Permalink
[BugFix] Check the return value of posix_memalign before use the al…
Browse files Browse the repository at this point in the history
…igned buffer.

Signed-off-by: GavinMar <[email protected]>
  • Loading branch information
GavinMar committed Apr 13, 2023
1 parent 8c54ad8 commit c65acb6
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 7 deletions.
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

0 comments on commit c65acb6

Please sign in to comment.