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

Patch for TF 2.3.1 #43358

Merged
merged 20 commits into from Sep 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2369d14
[tflite] Don't check for buffers on every subgraph.
mihaimaruseac Jul 28, 2020
00c7ed7
[tflite] Validate segment ids for segment_sum.
mihaimaruseac Sep 18, 2020
42ed6ac
[tflite] Test for `kTfLiteOptionalTensor` in `GetInput`.
mihaimaruseac Sep 18, 2020
7e283f9
[tflite] Make `GetOptionalInputTensor` the same as `GetInput`.
mihaimaruseac Sep 19, 2020
094329d
[tflite] Ensure inputs and outputs don't overlap.
mihaimaruseac Sep 19, 2020
1a506ae
[tflite] Ensure input tensors don't have `nullptr` buffers.
mihaimaruseac Sep 20, 2020
cd671a9
[tflite] Ensure `MatchingDim` does not allow buffer overflow.
mihaimaruseac Sep 18, 2020
b392591
[tflite] Ensure `ResolveAxis` properly handles negative inputs.
mihaimaruseac Sep 18, 2020
9a64529
Validate `NodeDef`s from `FunctionDefLibrary` of a `GraphDef`.
mihaimaruseac Sep 20, 2020
b98674e
Fix bad import
mihaimaruseac Sep 20, 2020
0fde760
Remove merge conflict marker from failed merge resolution
mihaimaruseac Sep 20, 2020
892d5e5
Validate `data_splits` for `tf.StringNGrams`.
mihaimaruseac Sep 18, 2020
ce90127
Prevent segfault in `GetSessionHandle{,V2}`.
mihaimaruseac Sep 18, 2020
0315fa7
Prevent format string vulnerability in `tf.strings.as_string`.
mihaimaruseac Sep 18, 2020
cb1422c
Prevent `int64` to `int` truncation in `Shard` API usage.
mihaimaruseac Sep 19, 2020
42783a6
Prevent integer truncation from 64 to 32 bits.
mihaimaruseac Sep 19, 2020
b307563
Fix multiple vulnerabilities in `tf.raw_ops.*CountSparseOutput`.
mihaimaruseac Sep 19, 2020
156872d
Fix heap buffer overflow in `tf.raw_ops.SparseFillEmptyRowsGrad`.
mihaimaruseac Sep 19, 2020
d8c69c2
Fix multiple vulnerabilities in `tf.experimental.dlpack.to_dlpack`.
mihaimaruseac Sep 19, 2020
92d5b97
Fix undefined behavior in `tf.raw_ops.Switch` in eager mode.
mihaimaruseac Sep 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 22 additions & 6 deletions tensorflow/c/eager/dlpack.cc
Expand Up @@ -248,21 +248,36 @@ void TFE_CallDLManagedTensorDeleter(void* dlm_ptr) {
}

void* TFE_HandleToDLPack(TFE_TensorHandle* h, TF_Status* status) {
auto tf_dlm_context = GetDlContext(h, status);
if (!status->status.ok()) {
return nullptr;
}

auto* tf_dlm_data = TFE_TensorHandleDevicePointer(h, status);
if (!status->status.ok()) {
return nullptr;
}

const Tensor* tensor = GetTensorFromHandle(h, status);
TF_DataType data_type = static_cast<TF_DataType>(tensor->dtype());
TensorReference tensor_ref(*tensor); // This will call buf_->Ref()

auto tf_dlm_type = GetDlDataType(data_type, status);
if (!status->status.ok()) {
return nullptr;
}

TensorReference tensor_ref(*tensor); // This will call buf_->Ref()
auto* tf_dlm_tensor_ctx = new TfDlManagedTensorCtx(tensor_ref);
tf_dlm_tensor_ctx->reference = tensor_ref;

DLManagedTensor* dlm_tensor = &tf_dlm_tensor_ctx->tensor;
dlm_tensor->manager_ctx = tf_dlm_tensor_ctx;
dlm_tensor->deleter = &DLManagedTensorDeleter;
dlm_tensor->dl_tensor.ctx = GetDlContext(h, status);
dlm_tensor->dl_tensor.ctx = tf_dlm_context;
int ndim = tensor->dims();
dlm_tensor->dl_tensor.ndim = ndim;
dlm_tensor->dl_tensor.data = TFE_TensorHandleDevicePointer(h, status);
dlm_tensor->dl_tensor.dtype = GetDlDataType(data_type, status);
dlm_tensor->dl_tensor.data = tf_dlm_data;
dlm_tensor->dl_tensor.dtype = tf_dlm_type;

std::vector<int64_t>* shape_arr = &tf_dlm_tensor_ctx->shape;
std::vector<int64_t>* stride_arr = &tf_dlm_tensor_ctx->strides;
Expand All @@ -275,13 +290,14 @@ void* TFE_HandleToDLPack(TFE_TensorHandle* h, TF_Status* status) {
(*stride_arr)[i] = (*shape_arr)[i + 1] * (*stride_arr)[i + 1];
}

dlm_tensor->dl_tensor.shape = &(*shape_arr)[0];
dlm_tensor->dl_tensor.shape = shape_arr->data();
// There are two ways to represent compact row-major data
// 1) nullptr indicates tensor is compact and row-majored.
// 2) fill in the strides array as the real case for compact row-major data.
// Here we choose option 2, since some frameworks didn't handle the strides
// argument properly.
dlm_tensor->dl_tensor.strides = &(*stride_arr)[0];
dlm_tensor->dl_tensor.strides = stride_arr->data();

dlm_tensor->dl_tensor.byte_offset =
0; // TF doesn't handle the strides and byte_offsets here
return static_cast<void*>(dlm_tensor);
Expand Down
46 changes: 31 additions & 15 deletions tensorflow/cc/saved_model/loader.cc
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
#include "tensorflow/cc/saved_model/loader_util.h"
#include "tensorflow/cc/saved_model/reader.h"
#include "tensorflow/core/framework/attr_value.pb.h"
#include "tensorflow/core/framework/function.proto.h"
#include "tensorflow/core/framework/node_def.pb.h"
#include "tensorflow/core/framework/tensor.pb.h"
#include "tensorflow/core/lib/io/path.h"
Expand Down Expand Up @@ -72,26 +73,41 @@ uint64 GetLatencyMicroseconds(const uint64 start_microseconds) {
// Ensure that constant tensors loaded from the saved model have valid shape.
// Also ensure that constant nodes have a value assigned to them.
// TODO(b/154763635): this is temporary and will be replaced with a better audit
static Status ValidateNode(const NodeDef& node) {
const auto node_iterator = node.attr().find("value");
if (node_iterator != node.attr().end()) {
AttrValue node_value = node_iterator->second;
if (node_value.has_tensor()) {
const PartialTensorShape node_shape(node_value.tensor().tensor_shape());
if (node_shape.num_elements() < 0) {
return errors::FailedPrecondition(
"Saved model contains node \"", node.name(), "\" (op \"", node.op(),
"\") which initializes from a tensor with ",
node_shape.num_elements(), " elements");
}
}
} else if (node.op() == "Const") {
return errors::FailedPrecondition(
"Saved model contains node \"", node.name(),
"\" which is a constant tensor but no value has been provided");
}
return Status::OK();
}

static Status ValidateSavedTensors(const GraphDef& graph_def) {
for (const auto& node : graph_def.node()) {
const auto node_iterator = node.attr().find("value");
if (node_iterator != node.attr().end()) {
AttrValue node_value = node_iterator->second;
if (node_value.has_tensor()) {
const PartialTensorShape node_shape(node_value.tensor().tensor_shape());
if (node_shape.num_elements() < 0) {
return errors::FailedPrecondition(
"Saved model contains node \"", node.name(), "\" (op \"",
node.op(), "\") which initializes from a tensor with ",
node_shape.num_elements(), " elements");
}
TF_RETURN_IF_ERROR(ValidateNode(node));
}

if (graph_def.has_library()) {
const FunctionDefLibrary& library = graph_def.library();
for (const auto& function : library.function()) {
for (const auto& node : function.node_def()) {
TF_RETURN_IF_ERROR(ValidateNode(node));
}
} else if (node.op() == "Const") {
return errors::FailedPrecondition(
"Saved model contains node \"", node.name(),
"\" which is a constant tensor but no value has been provided");
}
}

return Status::OK();
}

Expand Down
7 changes: 6 additions & 1 deletion tensorflow/core/common_runtime/eager/kernel_and_device.cc
Expand Up @@ -307,7 +307,12 @@ Status KernelAndDeviceOp::Run(
if (outputs != nullptr) {
outputs->clear();
for (int i = 0; i < context.num_outputs(); ++i) {
outputs->push_back(Tensor(*context.mutable_output(i)));
const auto* output_tensor = context.mutable_output(i);
if (output_tensor != nullptr) {
outputs->push_back(Tensor(*output_tensor));
} else {
outputs->push_back(Tensor());
}
}
}
return Status::OK();
Expand Down
18 changes: 18 additions & 0 deletions tensorflow/core/kernels/BUILD
Expand Up @@ -6085,6 +6085,24 @@ tf_kernel_library(
deps = STRING_DEPS,
)

tf_cc_test(
name = "as_string_op_test",
size = "small",
srcs = ["as_string_op_test.cc"],
deps = [
":as_string_op",
":ops_testutil",
":ops_util",
"//tensorflow/core:core_cpu",
"//tensorflow/core:framework",
"//tensorflow/core:lib",
"//tensorflow/core:protos_all_cc",
"//tensorflow/core:test",
"//tensorflow/core:test_main",
"//tensorflow/core:testlib",
],
)

tf_kernel_library(
name = "unicode_ops",
prefix = "unicode_ops",
Expand Down
19 changes: 18 additions & 1 deletion tensorflow/core/kernels/as_string_op.cc
Expand Up @@ -65,9 +65,26 @@ class AsStringOp : public OpKernel {
OP_REQUIRES(ctx, !(scientific && shortest),
errors::InvalidArgument(
"Cannot select both scientific and shortest notation"));

format_ = "%";
if (!fill_string.empty()) {
switch (fill_string[0]) {
case ' ':
case '+':
case '-':
case '0':
case '#':
strings::Appendf(&format_, "%s", fill_string.c_str());
break;
default:
bool fill_not_supported = true;
OP_REQUIRES(ctx, !fill_not_supported,
errors::InvalidArgument("Fill argument not supported: \"",
fill_string, "\""));
}
}
if (width > -1) {
strings::Appendf(&format_, "%s%d", fill_string.c_str(), width);
strings::Appendf(&format_, "%d", width);
}
if (precision > -1) {
strings::Appendf(&format_, ".%d", precision);
Expand Down