tensorflow 在propagator_state.cc中使用after free,

nbysray5  于 3个月前  发布在  其他
关注(0)|答案(9)|浏览(44)

问题类型

Bug

你是否在TF nightly版本中复现了这个bug?

是的

问题来源

source

Tensorflow版本

tf2.12

自定义代码

OS平台和发行版

  • 无响应*

移动设备

  • 无响应*

Python版本

  • 无响应*

Bazel版本

  • 无响应*

GCC/编译器版本

  • 无响应*

CUDA/cuDNN版本

  • 无响应*

GPU型号和内存

  • 无响应*

当前行为?

Pointer `next_iter` from function `PropagatorState::FrameState::IncrementIteration` is passed 
as the 1st parameter into `ActivateLoopInvs` where it is passed as the 1st parameter into 
`AdjustOutstandingOpsLocked`, then inside this function it is passed into `CleanupIterations` 
where it is deleted.

Then in `PropagatorState::FrameState::IncrementIteration` this possibly freed pointer is used 
in `return`-statement.

This behavior was introduced by https://github.com/tensorflow/tensorflow/commit/ae2a0e5c473f2a575767262021c26852d22886f8. 
Before this commit, no `return` was performed on the possibly freed pointer.

重现问题的独立代码

Bug was found by Svace static analysis tool.

相关日志输出

  • 无响应*
rjjhvcjd

rjjhvcjd1#

这段代码是TensorFlow中的一个片段,主要涉及到迭代器状态的初始化、激活、调整和清理。具体来说,它包括以下几个部分:

  1. 初始化下一个迭代:创建一个新的IterationState对象,并将其设置为当前迭代的状态。
  2. 激活新迭代中延迟根节点的后继:调用ActivateNexts方法,激活新迭代中延迟根节点的后继。
  3. 激活新迭代中的循环不变式:调用ActivateLoopInvs方法,激活新迭代中的循环不变式。
  4. 调整未完成的操作数:在AdjustOutstandingOpsLocked方法中,根据delta值调整未完成的操作数。
  5. 清理迭代:在CleanupIterations方法中,当一个迭代完成时,检查是否有延迟的迭代,如果有则启动它。同时,删除已完成的迭代状态,并更新未完成迭代的数量。

这段代码主要用于在TensorFlow中实现分布式计算,通过迭代器状态来管理计算图中的节点和操作。

lmvvr0a8

lmvvr0a82#

为了解决propagator_state.cc中的"use after free"错误,你需要确保由next_iter指向的内存在被删除或访问后不会被删除。

实现这一点的一种方法是修改代码,将next_iter的所有权转移到另一个负责管理其生命周期的对象或函数。例如,你可以使用智能指针,如std::unique_ptr或std::shared_ptr来自动管理next_iter的内存。

另一种方法是修改代码以确保在next_iter被删除后可能访问或修改它的任何函数都被修改,以防止这种情况发生。这可能包括在访问指针之前添加检查以确保指针有效,或者修改程序的控制流以确保在指针被删除后不访问它。

在任何情况下,都应仔细审查和测试修改后的代码,以确保它是正确的,并且没有引入新的错误。此外,重要的是要理解错误的根源,并解决可能导致错误的根本设计或架构问题,以防止将来出现类似的问题。我可以处理这个问题吗?@tiruk007

pkmbmrz7

pkmbmrz74#

我该如何继续进行?我在我的仓库中对代码进行了一些更改,并提交了pull request。但是它在TensorFlow主仓库的pull request页面上不可见。审查者如何查看并接受我的pull request?bhagirath20@daa460d . @mihaimaruseac@tiruk007

6yoyoihd

6yoyoihd5#

通过OSS VRP报告。

8ftvxx2r

8ftvxx2r6#

感谢@PaDarochek。
@bhagirath20 您需要针对TF仓库发起PR,而不是针对您自己的仓库。

t5fffqht

t5fffqht7#

Btw, this PR does not fix use after free. The function calling CleanupIterations will still have an ordinary (non unique ptr) pointer that is freed. Also, now the pointer is always deleted that is worse than before. Moreover, the code does not compile. I composed a hello world example for this:

#include <iostream>
#include <memory>

void foo(std::unique_ptr<int>& val) {
    std::cout << "val = " << *val << std::endl;
    val.reset();
}

int main() {
    int *a = new int(10);
    auto u = std::unique_ptr<int>(a);
    foo(u);
    std::cout << *a << std::endl;
}
$ clang++ -fsanitize=address main.cpp
$ ./a.out
val = 10
=================================================================
==141988==ERROR: AddressSanitizer: heap-use-after-free on address 0x602000000010 at pc 0x5633173797e0 bp 0x7ffffd860df0 sp 0x7ffffd860de8
READ of size 4 at 0x602000000010 thread T0
    #0 0x5633173797df in main (/tmp/a.out+0xde7df) (BuildId: 6ea64e75951ea28d632e046622292c29f16dbef4)
    #1 0x7f302ec37d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #2 0x7f302ec37e3f in __libc_start_main csu/../csu/libc-start.c:392:3
    #3 0x5633172b93c4 in _start (/tmp/a.out+0x1e3c4) (BuildId: 6ea64e75951ea28d632e046622292c29f16dbef4)
mkshixfv

mkshixfv8#

我猜修复需要熟悉TensorFlow代码库的维护者进行一些架构上的更改。

eufgjt7s

eufgjt7s9#

顺便说一下,这段文字似乎是由ChatGPT生成的;)

相关问题