From 570bc24fb98d72d1bce9261fdc29139224ec86ec Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 8 Jun 2014 17:03:34 +0900 Subject: Fix use-after-free on exit with multi-file torrent download + DHT DefaultPieceStorage may be referenced by one of DHT task (e.g., DHTPeerLookupTask), after RequestGroup was deleted, and even after RequestGroupMan was deleted. DefaultPieceStorage has a reference to MultiDiskAdaptor which calls RequestGroupMan object on destruction. So when DHT task is destroyed, DefaultPieceStorage is destroyed, which in turn destroys MultiDiskAdaptor. DHT task is destroyed after RequestGroupMan was destroyed, MultiDiskAdaptor will use now freed RequestGroupMan object, this is use-after-free. --- src/OpenedFileCounter.h | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/OpenedFileCounter.h (limited to 'src/OpenedFileCounter.h') diff --git a/src/OpenedFileCounter.h b/src/OpenedFileCounter.h new file mode 100644 index 00000000..960292b1 --- /dev/null +++ b/src/OpenedFileCounter.h @@ -0,0 +1,76 @@ +/* */ +#ifndef D_OPENED_FILE_COUNTER_H +#define D_OPENED_FILE_COUNTER_H + +#include "common.h" + +namespace aria2 { + +class RequestGroupMan; + +class OpenedFileCounter { +public: + OpenedFileCounter(RequestGroupMan* rgman, size_t maxOpenFiles); + + // Keeps the number of open files under the global limit specified + // in the option. The caller requests that |numNewFiles| files are + // going to be opened. This function requires that |numNewFiles| is + // less than or equal to the limit. + // + // Currently the only download using MultiDiskAdaptor is affected by + // the global limit. + void ensureMaxOpenFileLimit(size_t numNewFiles); + + // Reduces the number of open files managed by this object. + void reduceNumOfOpenedFile(size_t numCloseFiles); + + void setMaxOpenFiles(size_t maxOpenFiles) + { + maxOpenFiles_ = maxOpenFiles; + } + + // Deactivates this object. + void deactivate(); + +private: + RequestGroupMan* rgman_; + size_t maxOpenFiles_; + size_t numOpenFiles_; +}; + +} // namespace aria2 + +#endif // D_OPENED_FILE_COUNTER_H -- cgit