Python内存安全吗?

the*_*fee 12 python memory-management python-3.x memory-safety

随着 Deno 成为 Node.js 的新竞争对手,许多新闻文章都提到了 Rust 的内存安全特性,其中一篇特别的文章指出 Rust 和 Go 因其内存安全特性而受益,Swift 和 Kotlin 也是如此但后两者并没有广泛用于系统编程。

Safe Rust 是真正的 Rust 编程语言。如果您所做的只是编写 Safe Rust,您将永远不必担心类型安全或内存安全。您永远不会忍受悬空指针、释放后使用或任何其他类型的未定义行为。

这激起了我的兴趣,想了解 Python 是否可以被视为内存安全,如果是或否,安全或不安全的程度如何?

从一开始,维基百科上关于内存安全的文章就没有提到Python,而关于Python的文章似乎只提到了内存管理。我最接近找到答案的是Daniel 的这个

维基百科文章将类型安全与内存安全相关联,这意味着同一内存区域不能作为整数和字符串进行访问。这样Python就是类型安全的。您不能隐式更改对象的类型。

但即便如此,这似乎也仅仅暗示了两个方面之间的联系(使用维基百科的关联,这又是有争议的),并且对于 Python 是否可以被视为内存安全没有明确的答案。

EJo*_*ica 8

维基百科列出了以下内存安全问题的示例:

Access errors: invalid read/write of a pointer
    Buffer overflow - out-of-bound writes can corrupt the content of adjacent objects, or internal data (like bookkeeping information for the heap) or return addresses.
    Buffer over-read - out-of-bound reads can reveal sensitive data or help attackers bypass address space layout randomization.
Run Code Online (Sandbox Code Playgroud)

Python至少尝试防范这些问题。

    Race condition - concurrent reads/writes to shared memory
Run Code Online (Sandbox Code Playgroud)

对于具有可变数据结构的语言来说,这实际上并不难。(函数式编程和不可变数据结构的拥护者经常使用这一事实作为对他们有利的论据)。

    Invalid page fault - accessing a pointer outside the virtual memory space. A null pointer dereference will often cause an exception or program termination in most environments, but can cause corruption in operating system kernels or systems without memory protection, or when use of the null pointer involves a large or negative offset.
    Use after free - dereferencing a dangling pointer storing the address of an object that has been deleted.
Uninitialized variables - a variable that has not been assigned a value is used. It may contain an undesired or, in some languages, a corrupt value.
    Null pointer dereference - dereferencing an invalid pointer or a pointer to memory that has not been allocated
    Wild pointers arise when a pointer is used prior to initialization to some known state. They show the same erratic behaviour as dangling pointers, though they are less likely to stay undetected.
Run Code Online (Sandbox Code Playgroud)

没有真正的方法可以阻止某人尝试访问空指针。在 C# 和 Java 中,这会导致异常。在 C++ 中,这会导致未定义的行为

Memory leak - when memory usage is not tracked or is tracked incorrectly
    Stack exhaustion - occurs when a program runs out of stack space, typically because of too deep recursion. A guard page typically halts the program, preventing memory corruption, but functions with large stack frames may bypass the page.
Run Code Online (Sandbox Code Playgroud)

C#、Java 和 Python 等语言中的内存泄漏与手动管理内存的 C 和 C++ 等语言中的内存泄漏含义不同。在 C 或 C++ 中,由于未能释放分配的内存而导致内存泄漏。在具有托管内存的语言中,您不必显式地取消分配内存,但即使在不再需要该对象之后,仍然可以通过意外地在某处维护对对象的引用来执行类似的操作。

实际上,使用 C# 中的事件处理程序和长期存在的集合类之类的东西可以很容易地做到这一点;尽管我们使用的是托管内存,但我实际上参与过存在内存泄漏的项目。从某种意义上说,使用托管内存的环境实际上会使这些问题变得更加危险,因为程序员可能会有一种错误的安全感。根据我的经验,即使是经验丰富的工程师也常常无法进行内存分析或编写测试用例来检查这一点(可能是因为环境给了他们一种错误的安全感)。

在 Python 中,堆栈耗尽也很容易实现(例如,使用无限递归)。

    Heap exhaustion - the program tries to allocate more memory than the amount available. In some languages, this condition must be checked for manually after each allocation.
Run Code Online (Sandbox Code Playgroud)

仍然很有可能 - 我很尴尬地承认我个人已经在 C# 中做到了这一点(尽管还没有在 Python 中)。

    Double free - repeated calls to free may prematurely free a new object at the same address. If the exact address has not been reused, other corruption may occur, especially in allocators that use free lists.
    Invalid free - passing an invalid address to free can corrupt the heap.
    Mismatched free - when multiple allocators are in use, attempting to free memory with a deallocation function of a different allocator[20]
    Unwanted aliasing - when the same memory location is allocated and modified twice for unrelated purposes.
Run Code Online (Sandbox Code Playgroud)

实际上,在 Python 中很容易出现不需要的别名。这是 Java 中的一个示例(完全公开:我写了接受的答案);你可以在 Python 中轻松地完成类似的事情。其他的由Python解释器本身管理。

因此,内存安全似乎是相对的。完全取决于您对“内存安全问题”的看法,它实际上很难完全预防。Java、C# 和 Python 等高级语言可以防止其中许多最严重的错误,但还有其他问题很难或不可能完全防止。

  • 愤世嫉俗的我想说 Rust 需要炒作,而 Python 不需要。语言通常需要一些引人注目的用例才能得到广泛采用。Python 已经拥有许多众所周知的用例(尤其是众所周知的 ML 和数据分析库,如 Pandas、Tensorflow 和 scikit-learn)。 (4认同)

归档时间:

查看次数:

5605 次

最近记录:

3 年,10 月 前