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

How do I create a buffer overflow to test Address Sanitizer?

问题

我正在尝试创建缓冲区溢出以了解有关 Address Sanitizer 的更多信息。我写了下面的代码,我 认为 会造成缓冲区溢出,但我一定是弄错了,因为它没有抛出预期的 "Heap buffer overflow detected".

尝试

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

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

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

我在 Xcode 中启用了 Address Sanitizer,方法是单击我的应用程序 > 编辑方案...,然后单击 "Enable Address 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;
}