gpt4 book ai didi

python - Python内存安全吗?

转载 作者:行者123 更新时间:2023-12-03 23:47:10 24 4
gpt4 key购买 nike

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

Safe Rust is the true Rust programming language. If all you do is write Safe Rust, you will never have to worry about type-safety or memory-safety. You will never endure a dangling pointer, a use-after-free, or any other kind of Undefined Behavior.



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

从一开始, article on memory safety维基百科上的甚至没有提到 Python,关于 Python 的文章似乎只提到了内存管理。
我最接近找到答案的是 this one by Daniel :

The wikipedia article associates type-safe to memory-safe, meaning, that the same memory area cannot be accessed as e.g. integer and string. In this way Python is type-safe. You cannot change the type of a object implicitly.



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

最佳答案

维基百科 lists以下是内存安全问题的示例:

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.

python at least tries以防止这些。
    Race condition - concurrent reads/writes to shared memory

在具有可变数据结构的语言中,这实际上并不难。 (函数式编程和不可变数据结构的拥护者经常使用这个事实作为支持他们的论点)。
    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.

没有真正的方法可以阻止某人尝试访问空指针。在 C# 和 Java 中,这会导致 exception .在 C++ 中,这个 results in undefined behavior .
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.

C#、Java 和 Python 等语言中的内存泄漏与手动管理内存的 C 和 C++ 等语言中的内存泄漏含义不同。在 C 或 C++ 中,由于未能释放分配的内存而导致内存泄漏。在具有托管内存的语言中,您不必显式地取消分配内存,但仍然可以通过意外地在某处维护对它的引用来做一些非常相似的事情。

这实际上很容易用 event handlers in 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.

仍然很有可能 - 我很尴尬地承认我个人在 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.

不需要的别名实际上在 Python 中很容易做到。这是一个 example在 Java 中(完全披露:我写了接受的答案);你可以很容易地在 Python 中做一些非常相似的事情。其他的由 Python 解释器本身管理。

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

关于python - Python内存安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61974312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com