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

refactor: Refactor string input checks #263

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Changes from 2 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
53 changes: 14 additions & 39 deletions src/onnxruntime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2477,6 +2477,7 @@ ModelInstanceState::SetStringInputBuffer(
std::vector<TRITONBACKEND_Response*>* responses, char* input_buffer,
std::vector<const char*>* string_ptrs)
{
std::vector<std::pair<const char*, const uint32_t>> str_list;
// offset for each response
size_t buffer_copy_offset = 0;
for (size_t idx = 0; idx < expected_byte_sizes.size(); idx++) {
Expand All @@ -2485,50 +2486,24 @@ ModelInstanceState::SetStringInputBuffer(

size_t element_cnt = 0;
if ((*responses)[idx] != nullptr) {
size_t remaining_bytes = expected_byte_size;
char* data_content = input_buffer + buffer_copy_offset;
// Continue if the remaining bytes may still contain size info
while (remaining_bytes >= sizeof(uint32_t)) {
if (element_cnt >= expected_element_cnt) {
RESPOND_AND_SET_NULL_IF_ERROR(
&((*responses)[idx]),
TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_INVALID_ARG,
(std::string("unexpected number of string elements ") +
std::to_string(element_cnt + 1) + " for inference input '" +
input_name + "', expecting " +
std::to_string(expected_element_cnt))
.c_str()));
break;
}

const uint32_t len = *(reinterpret_cast<const uint32_t*>(data_content));
remaining_bytes -= sizeof(uint32_t);
TRITONSERVER_Error* err = ValidateStringBuffer(
data_content, expected_byte_size, expected_element_cnt,
input_name.c_str(), &str_list);
Tabrizian marked this conversation as resolved.
Show resolved Hide resolved
// Set string values.
for (const auto& [addr, len] : str_list) {
// Make first byte of size info 0, so that if there is string data
// in front of it, the data becomes valid C string.
*data_content = 0;
data_content = data_content + sizeof(uint32_t);
if (len > remaining_bytes) {
RESPOND_AND_SET_NULL_IF_ERROR(
&((*responses)[idx]),
TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_INVALID_ARG,
(std::string("incomplete string data for inference input '") +
input_name + "', expecting string of length " +
std::to_string(len) + " but only " +
std::to_string(remaining_bytes) + " bytes available")
.c_str()));
break;
} else {
string_ptrs->push_back(data_content);
element_cnt++;
data_content = data_content + len;
remaining_bytes -= len;
}
*const_cast<char*>(addr - sizeof(uint32_t)) = 0;
string_ptrs->push_back(addr);
}
}
str_list.clear();

FillStringData(string_ptrs, expected_element_cnt - element_cnt);
if (err != nullptr) {
RESPOND_AND_SET_NULL_IF_ERROR(&((*responses)[idx]), err);
FillStringData(string_ptrs, expected_element_cnt - element_cnt);
}
}
buffer_copy_offset += expected_byte_size;
}
}
Expand Down
Loading