Fix(core): Resolve Segmentation Fault when Creating String Constants (Issue #23611) #29969
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes a segmentation fault encountered when creating string type constants in OpenVINO.
Problem:
A segmentation fault occurs when attempting to create a constant tensor with string data type using the following Python code:
Root Cause Analysis:
The issue lies within the Constant constructor in src/core/src/op/constant.cpp. Specifically, the constructor chain for string constants includes:
Constant::Constant(const element::Type& type, const Shape& shape, const std::vectorstd::string& values)
: Constant(false, type, shape) {
// ...processing continues...
}
The problem is the passing of false for the memset_allocation parameter in the call to the base constructor. This flag determines how memory is initialized in the allocate_buffer function:
Subsequently, the constructor that handles raw data uses std::uninitialized_copy_n:
std::uninitialized_copy_n expects the destination memory to be allocated but not initialized. When memset_allocation is false for string constants, the memory isn't properly prepared for string objects, leading to a segmentation fault when std::uninitialized_copy_n is called.
Solution:
This Pull Request modifies the Constant constructor for string types to ensure that memset_allocation is set to true. This will ensure that the memory allocated for string constants is properly initialized, resolving the segmentation fault.
Testing:
The fix has been tested by successfully creating string constants using the code snippet provided in the problem description. Further testing with various string lengths and multiple string elements has also been performed.
Issue Link:
This Pull Request addresses and resolves issue #23611.