swift - 如何创建缓冲区溢出来测试 Address Sanitizer?

标签 swift buffer-overflow address-sanitizer

问题

我正在尝试创建缓冲区溢出,以便了解有关 Address Sanitizer 的更多信息。我编写了以下代码,我认为会造成缓冲区溢出,但我一定是错误的,因为它没有抛出预期的“检测到堆缓冲区溢出”

尝试

    var ints : [UInt8] = [ 1, 2, 3, 4 ]

    let a = UnsafeMutableBufferPointer(start: &ints, count: ints.count)

    a[28] = 17 // array out of index 

我通过单击我的应用程序 > 编辑方案...,然后单击“启用地址 sanitizer ”,在 Xcode 中启用了地址 sanitizer 。然后我在运行之前重建了我的应用程序。

问题

如何在 Swift 2 中创建缓冲区溢出?

最佳答案

来自https://developer.apple.com/videos/play/wwdc2015-413/?time=947

Address Sanitizer is an LLVM tool for C-based languages.

https://developer.apple.com/videos/play/wwdc2015-413/?time=1422

In order to use Address Sanitizer, Xcode passes a special flag to clang.

Address Sanitizer 似乎仅适用于 clang 适用于 C、Objective-C 等,但不适用于 Swift 编译器 swiftc

触发缓冲区溢出的简单 C 程序是

#include <stdio.h>
#include <stdlib.h>

int main(int argc, const char * argv[]) {

    int *p = malloc(4 * sizeof(int));
    p[28] = 17;

    return 0;
}

关于swift - 如何创建缓冲区溢出来测试 Address Sanitizer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34485008/

相关文章:

swift - Swift 字典如何用于存储通用对象 [T : C<T>]?

c - 需要检测任何可能的缓冲区溢出

c - %(limit)[^\n] 在 scanf 中的行为是什么?溢出安全吗?

clang - 如何使用 sanitizer 编译 Clang

ios - 如何使 Sprite 节点的纹理变暗

ios - 比较数组中的对象并将相等的值放入单独的新对象中

python - 从 Python 传递 shellcode 作为命令行参数

c++ - -fsanitize 在 GCC-6.1 中不使用黄金链接器

c++ - 带 CMake 的 LLVM 地址 sanitizer

ios - 如何在 UITableViewController 中的 UITableView 底部留出空间