Skip to content

Commit

Permalink
Handle a special grappler case resulting in crash.
Browse files Browse the repository at this point in the history
It might happen that a malformed input could be used to trick Grappler into trying to optimize a node with no inputs. This, in turn, would produce a null pointer dereference and a segfault.

PiperOrigin-RevId: 369242852
Change-Id: I2e5cbe7aec243d34a6d60220ac8ac9b16f136f6b
  • Loading branch information
mihaimaruseac authored and tensorflower-gardener committed Apr 19, 2021
1 parent 799f835 commit e6340f0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tensorflow/core/grappler/optimizers/arithmetic_optimizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,12 @@ class ReorderCastLikeAndValuePreserving : public ArithmeticOptimizerStage {

Status TrySimplify(NodeDef* consumer, string* simplified_node_name) override {
NodeDef* producer;

if (consumer->input_size() < 1) {
return errors::FailedPrecondition("Node ", simplified_node_name,
" lacks inputs");
}

TF_RETURN_IF_ERROR(GetInputNode(consumer->input(0), &producer));
const bool producer_is_cast = IsCastLike(*producer);
const bool can_optimize =
Expand Down Expand Up @@ -2538,6 +2544,11 @@ class ReplaceMulWithSquare : public ArithmeticOptimizerStage {
~ReplaceMulWithSquare() override = default;

bool IsSupported(const NodeDef* node) const override {
if (!node || node->input_size() < 2) {
// Invalid node
return false;
}

return IsAnyMul(*node) && node->input(0) == node->input(1);
}

Expand Down
6 changes: 6 additions & 0 deletions tensorflow/core/grappler/optimizers/dependency_optimizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ bool DependencyOptimizer::SafeToRemoveIdentity(const NodeDef& node) const {
// The output values of this node may be needed.
return false;
}

if (node.input_size() < 1) {
// Node lacks input, is invalid
return false;
}

const NodeDef* input = node_map_->GetNode(NodeName(node.input(0)));
CHECK(input != nullptr) << "node = " << node.name()
<< " input = " << node.input(0);
Expand Down

0 comments on commit e6340f0

Please sign in to comment.