Skip to content
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
37 changes: 33 additions & 4 deletions kernels/portable/cpu/op_constant_pad_nd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,17 @@ void apply_padding_to_dim(

size_t pad_before = 0;
size_t pad_after = 0;
if (pad_i >= 0 && pad_i < pad.size() / 2) {
pad_before = pad[2 * pad_i];
pad_after = pad[2 * pad_i + 1];
if (pad_i < pad.size() / 2) {
int64_t pb = pad[2 * pad_i];
int64_t pa = pad[2 * pad_i + 1];
ET_KERNEL_CHECK_MSG(
ctx,
pb >= 0 && pa >= 0,
InvalidArgument,
/* void */,
"Padding values must be non-negative.");
pad_before = static_cast<size_t>(pb);
pad_after = static_cast<size_t>(pa);
}

size_t out_step_len = out_strides[dim];
Expand All @@ -62,6 +70,12 @@ void apply_padding_to_dim(
// Do not copy padding beyond the out tensor bounds.
// Use division to avoid potential overflow in multiplication.
if (pad_before > 0) {
ET_KERNEL_CHECK_MSG(
ctx,
out_data <= out_data_end,
InvalidArgument,
/* void */,
"Out data pointer exceeds buffer bounds.");
size_t remaining = out_data_end - out_data;
ET_KERNEL_CHECK_MSG(
ctx,
Expand Down Expand Up @@ -92,7 +106,12 @@ void apply_padding_to_dim(
/* void */,
"Out tensor overlaps with the input tensor. This is not supported.");
// Bounds check before memcpy
// Use overflow-safe check for remaining >= copy_len
ET_KERNEL_CHECK_MSG(
ctx,
out_data <= out_data_end,
InvalidArgument,
/* void */,
"Out data pointer exceeds buffer bounds.");
size_t remaining = out_data_end - out_data;
ET_KERNEL_CHECK_MSG(
ctx,
Expand Down Expand Up @@ -123,6 +142,10 @@ void apply_padding_to_dim(
last_padded_dim,
dim + 1);

if (ctx.failure_state() != Error::Ok) {
return;
}

out_data += out_step_len;
self_data += in_step_len;
}
Expand All @@ -131,6 +154,12 @@ void apply_padding_to_dim(
// Do not copy padding beyond the out tensor bounds.
// Use division to avoid potential overflow in multiplication.
if (pad_after > 0) {
ET_KERNEL_CHECK_MSG(
ctx,
out_data <= out_data_end,
InvalidArgument,
/* void */,
"Out data pointer exceeds buffer bounds.");
size_t remaining = out_data_end - out_data;
ET_KERNEL_CHECK_MSG(
ctx,
Expand Down
8 changes: 8 additions & 0 deletions kernels/portable/cpu/util/kernel_ops_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,14 @@ bool check_constant_pad_args(
pad.size() / 2,
in.dim());

for (size_t i = 0; i < pad.size(); ++i) {
ET_CHECK_OR_RETURN_FALSE(
pad[i] >= 0,
"Padding values must be non-negative, but got pad[%zu] = %" PRId64,
i,
pad[i]);
}

return true;
}

Expand Down
Loading