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

[GLUTEN-8471][VL] Fix usage of uninitialized variables #8470

Merged
merged 1 commit into from
Jan 11, 2025

Conversation

jkhaliqi
Copy link
Contributor

@jkhaliqi jkhaliqi commented Jan 8, 2025

Use of Uninitialized Variables

false positives(mainly since the file was most likely deleted and only contains 1480 lines now. Went over that file and tried to find any other Uninitialized Variables and change them accordingly. There is 9 FP below and also 9 changes in that file after taking a look at what could have been the line numbers):
cpp/velox/substrait/SubstraitToVeloxPlan.cc:1902
cpp/velox/substrait/SubstraitToVeloxPlan.cc:1762
cpp/velox/substrait/SubstraitToVeloxPlan.cc:1680
cpp/velox/substrait/SubstraitToVeloxPlan.cc:1653
cpp/velox/substrait/SubstraitToVeloxPlan.cc:1927
pp/velox/substrait/SubstraitToVeloxPlan.cc:2539
cpp/velox/substrait/SubstraitToVeloxPlan.cc:1960
cpp/velox/substrait/SubstraitToVeloxPlan.cc:1932
cpp/velox/substrait/SubstraitToVeloxPlan.cc:2441

(Fixes: #8471)

@github-actions github-actions bot added the VELOX label Jan 8, 2025
Copy link

github-actions bot commented Jan 8, 2025

Thanks for opening a pull request!

Could you open an issue for this pull request on Github Issues?

https://github.com/apache/incubator-gluten/issues

Then could you also rename commit message and pull request title in the following format?

[GLUTEN-${ISSUES_ID}][COMPONENT]feat/fix: ${detailed message}

See also:

@FelixYBW FelixYBW changed the title Fix C++ CVEs [VL] Fix C++ CVEs Jan 8, 2025
@FelixYBW FelixYBW changed the title [VL] Fix C++ CVEs [GLUTEN-8471][VL] Fix C++ CVEs Jan 8, 2025
Copy link

github-actions bot commented Jan 8, 2025

#8471

while (true) {
auto future = velox::ContinueFuture::makeEmpty();
if (task == nullptr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

task_?

we shouldn't check null in side of the loop.

Copy link
Collaborator

@majetideepak majetideepak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkhaliqi Let's limit changes to Use of Uninitialized Variables
Please update the title. "[VL] Fix usage of uninitialized variables"

while (true) {
auto future = velox::ContinueFuture::makeEmpty();
if (task == nullptr) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add this check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of Uninitialized Variable@cpp/velox/compute/WholeStageResultIterator.cc:212
Use of Uninitialized Variable@cpp/velox/compute/WholeStageResultIterator.cc:209
Assuming the task_ needed to be checked to make sure it was not nullptr in order to be used for task_->next(&future); and task_->taskId()

@@ -125,8 +125,8 @@ class ListenableArbitrator : public velox::memory::MemoryArbitrator {

uint64_t shrinkCapacity(uint64_t targetBytes, bool allowSpill, bool allowAbort) override {
velox::memory::ScopedMemoryArbitrationContext ctx{};
facebook::velox::exec::MemoryReclaimer::Stats status;
velox::memory::MemoryPool* pool;
facebook::velox::exec::MemoryReclaimer::Stats status{};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not required. facebook::velox::exec::MemoryReclaimer::Stats initializes its fields.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing

@@ -141,7 +141,7 @@ class ListenableArbitrator : public velox::memory::MemoryArbitrator {
}

Stats stats() const override {
Stats stats; // no-op
Stats stats{}; // no-op
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing

@@ -215,7 +215,11 @@ class ArbitratorFactoryRegister {
};

VeloxMemoryManager::VeloxMemoryManager(const std::string& kind, std::unique_ptr<AllocationListener> listener)
: MemoryManager(kind), listener_(std::move(listener)) {
: MemoryManager(kind) {
if (listener == nullptr) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add this check? Is it related to the CVE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah there was some CVE's for
VeloxMemoryManager.cc:243
Use of Uninitialized Variable@cpp/velox/memory/VeloxMemoryManager.cc:250
Use of Uninitialized Variable@cpp/velox/memory/VeloxMemoryManager.cc:253
which was around here so I figured it might be with the listener being passed in as nullptr so figured I would check that before the method goes in.

@@ -253,7 +257,10 @@ VeloxMemoryManager::VeloxMemoryManager(const std::string& kind, std::unique_ptr<

namespace {
MemoryUsageStats collectVeloxMemoryUsageStats(const velox::memory::MemoryPool* pool) {
MemoryUsageStats stats;
if (pool == nullptr) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No related to the CVE.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of Uninitialized Variable@cpp/velox/memory/VeloxMemoryManager.cc:255
Use of Uninitialized Variable@cpp/velox/memory/VeloxMemoryManager.cc:256
Use of Uninitialized Variable@cpp/velox/memory/VeloxMemoryManager.cc:257
55 being the pool in the paramter I figured I would check if that is nullPtr
then stats being 56 I just added {}
and then 57 was using pool->usedBytes so the assuming the nullptr check above should fix that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed as false positive.

@@ -41,7 +41,7 @@ class Substrait2VeloxPlanValidatorTest : public exec::test::HiveConnectorTestBas
bool validatePlan(std::string file) {
std::string subPlanPath = FilePathGenerator::getDataFilePath(file);

::substrait::Plan substraitPlan;
::substrait::Plan substraitPlan{};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not required.

Copy link
Contributor Author

@jkhaliqi jkhaliqi Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of Uninitialized Variable@cpp/velox/tests/Substrait2VeloxPlanValidatorTest.cc:45

if (!readRel.has_filter()) {
tableHandle = std::make_shared<connector::hive::HiveTableHandle>(
kHiveConnectorId, "hive_table", filterPushdownEnabled, connector::hive::SubfieldFilters{}, nullptr);
} else {
connector::hive::SubfieldFilters subfieldFilters;
connector::hive::SubfieldFilters subfieldFilters{};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not required.

@@ -866,7 +866,7 @@ const core::WindowNode::Frame SubstraitToVeloxPlanConverter::createWindowFrame(
const ::substrait::Expression_WindowFunction_Bound& upper_bound,
const ::substrait::WindowType& type,
const RowTypePtr& inputType) {
core::WindowNode::Frame frame;
core::WindowNode::Frame frame{};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not required.

@@ -312,7 +312,7 @@ std::shared_ptr<const core::ConstantTypedExpr> SubstraitVeloxExprConverter::lite
std::vector<variant> variants;
variants.reserve(literals.size());
VELOX_CHECK_GE(literals.size(), 0, "List should have at least one item.");
std::optional<TypePtr> literalType;
std::optional<TypePtr> literalType = std::nullopt;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not required. std::optional is initialized by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of Uninitialized Variable@cpp/velox/substrait/SubstraitToVeloxExpr.cc:318
if (!literalType.has_value()) {
figured i'd explicitly initialize as well

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required. The constructor of std::optional does this.

@@ -433,7 +433,7 @@ VectorPtr SubstraitVeloxExprConverter::literalsToVector(
// Handle EmptyList and List together since the children could be either case.
case ::substrait::Expression_Literal::LiteralTypeCase::kEmptyList:
case ::substrait::Expression_Literal::LiteralTypeCase::kList: {
ArrayVectorPtr elements;
ArrayVectorPtr elements = nullptr;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not required. ArrayVectorPtr is a std::shared_ptr which is initialized by default.
Same below for RowVectorPtr and MapVectorPtr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of Uninitialized Variable@cpp/velox/substrait/SubstraitToVeloxExpr.cc:448 (if (!elements)
454 (return elements;)
472 (!mapVector)
478 (return mapVector;)
485 (!rowVector)
491 (return rowVector;)
Im assuming it would initialize it by default but the errors happening at these lines all point to these variables so I figured to explicitly call it nullptr? Should it be resolved some other way, or we can call these false positives?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false positive.

@jkhaliqi jkhaliqi force-pushed the jk_cve_variables branch 2 times, most recently from c3b05d2 to bdab7c5 Compare January 8, 2025 22:29
@jkhaliqi jkhaliqi changed the title [GLUTEN-8471][VL] Fix C++ CVEs [GLUTEN-8471][VL] Fix usage of uninitialized variables Jan 8, 2025
@@ -192,10 +192,13 @@ std::shared_ptr<velox::core::QueryCtx> WholeStageResultIterator::createNewVeloxQ

std::shared_ptr<ColumnarBatch> WholeStageResultIterator::next() {
tryAddSplitsToTask();
if (task_ == nullptr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we throw exception instead of returning nullptr?

if (task_->isFinished()) {
return nullptr;
}
velox::RowVectorPtr vector;
velox::RowVectorPtr vector = nullptr;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant since RowVectorPtr is a shared pointer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::shared_ptr<connector::hive::HiveBucketProperty> bucketProperty = nullptr;
I see the above as well, but not sure if assignment should be removed. Will not add in nullptr for share pointers in this PR for Uninitialized variables though, should be updated as false positives

if (listener == nullptr) {
throw gluten::GlutenException("VeloxMemoryManager failed");
}
listener_(std::move(listener))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why move this here?
You can keep it as is and check for listener_ above

@@ -265,20 +272,25 @@ MemoryUsageStats collectVeloxMemoryUsageStats(const velox::memory::MemoryPool* p
}

MemoryUsageStats collectGlutenAllocatorMemoryUsageStats(const MemoryAllocator* allocator) {
MemoryUsageStats stats;
MemoryUsageStats stats{};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant.

@@ -312,7 +312,7 @@ std::shared_ptr<const core::ConstantTypedExpr> SubstraitVeloxExprConverter::lite
std::vector<variant> variants;
variants.reserve(literals.size());
VELOX_CHECK_GE(literals.size(), 0, "List should have at least one item.");
std::optional<TypePtr> literalType;
std::optional<TypePtr> literalType = std::nullopt;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required. The constructor of std::optional does this.

@@ -433,7 +433,7 @@ VectorPtr SubstraitVeloxExprConverter::literalsToVector(
// Handle EmptyList and List together since the children could be either case.
case ::substrait::Expression_Literal::LiteralTypeCase::kEmptyList:
case ::substrait::Expression_Literal::LiteralTypeCase::kList: {
ArrayVectorPtr elements;
ArrayVectorPtr elements = nullptr;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false positive.

@jkhaliqi jkhaliqi force-pushed the jk_cve_variables branch 3 times, most recently from 4750239 to 41f2e96 Compare January 10, 2025 21:13
Use of Uninitialized Variables
@FelixYBW FelixYBW merged commit a7301e1 into apache:main Jan 11, 2025
47 of 48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[VL] Security Voliations
4 participants