Skip to content

Commit

Permalink
Fix possible integer overflow in multiplication
Browse files Browse the repository at this point in the history
Signed-off-by: Enrico Minack <github@enrico.minack.dev>
  • Loading branch information
EnricoMi committed Jan 14, 2022
1 parent 0b1a4a6 commit 3f91c86
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion horovod/common/controller.cc
Expand Up @@ -486,7 +486,7 @@ int64_t Controller::TensorFusionThresholdBytes() {
// Ensuring that fusion buffer can hold a number of elements divisible by
// FUSION_BUFFER_ATOMIC_UNIT for performance
int double_size = GetTypeSize(HOROVOD_FLOAT64);
int64_t div = local_size_ * double_size * FUSION_BUFFER_ATOMIC_UNIT;
int64_t div = (int64_t)local_size_ * (int64_t)double_size * FUSION_BUFFER_ATOMIC_UNIT;
return ((proposed_fusion_threshold + div - 1) / div) * div;
}
return proposed_fusion_threshold;
Expand Down
8 changes: 4 additions & 4 deletions horovod/common/ops/adasum/adasum.h
Expand Up @@ -298,8 +298,8 @@ template <typename Communicator_type> class Adasum {
nghrCountVec_index++;

this->PointToPointSendRecv(
(char*)(&grad_buffer[sendOffset]), nghrCount * per_element_size,
(char*)(&recv_buffer[recvOffset]), myCount * per_element_size,
(char*)(&grad_buffer[sendOffset]), (int64_t)nghrCount * (int64_t)per_element_size,
(char*)(&recv_buffer[recvOffset]), (int64_t)myCount * (int64_t)per_element_size,
horovod_datatype, neighbor_rank, tag, communicator, global_state);
if ((rank & level) != 0) {
grad_buffer = &grad_buffer[nghrCount];
Expand Down Expand Up @@ -329,8 +329,8 @@ template <typename Communicator_type> class Adasum {
} else {
recv_buffer = &grad_buffer[-nghrCount];
}
this->PointToPointSendRecv(grad_buffer, myCount * per_element_size,
recv_buffer, nghrCount * per_element_size,
this->PointToPointSendRecv(grad_buffer, (int64_t)myCount * (int64_t)per_element_size,
recv_buffer, (int64_t)nghrCount * (int64_t)per_element_size,
horovod_datatype, neighbor_rank, tag,
communicator, global_state);
if ((rank & level) != 0) {
Expand Down
2 changes: 1 addition & 1 deletion horovod/common/ops/collective_operations.cc
Expand Up @@ -243,7 +243,7 @@ void AllgatherOp::MemcpyInFusionBuffer(

auto& process_set =
global_state_->process_set_table.Get(first_entry.process_set_id);
int64_t offset = displcmnts[process_set.controller->GetRank()] * element_size;
int64_t offset = (int64_t)displcmnts[process_set.controller->GetRank()] * (int64_t)element_size;
for (auto& e : entries) {
void* buffer_data_at_offset = (uint8_t*)buffer_data + offset;
MemcpyEntryInFusionBuffer(entries, e, buffer_data_at_offset);
Expand Down

0 comments on commit 3f91c86

Please sign in to comment.