xref: /aosp_15_r20/external/compiler-rt/test/asan/TestCases/Darwin/address-range-limit.mm (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot// Regression test for https://code.google.com/p/address-sanitizer/issues/detail?id=368.
2*7c3d14c8STreehugger Robot
3*7c3d14c8STreehugger Robot// RUN: %clangxx_asan %s -Wno-deprecated-declarations -flat_namespace -bundle -undefined suppress -o %t.bundle
4*7c3d14c8STreehugger Robot// RUN: %clangxx_asan %s -Wno-deprecated-declarations -o %t -framework Foundation && not %run %t 2>&1 | FileCheck %s
5*7c3d14c8STreehugger Robot
6*7c3d14c8STreehugger Robot#import <Foundation/Foundation.h>
7*7c3d14c8STreehugger Robot#import <mach-o/dyld.h>
8*7c3d14c8STreehugger Robot
9*7c3d14c8STreehugger Robot#include <string>
10*7c3d14c8STreehugger Robot
11*7c3d14c8STreehugger Robotint main(int argc, char *argv[]) {
12*7c3d14c8STreehugger Robot  for (int i = 0; i < 10; i++) {
13*7c3d14c8STreehugger Robot    NSObjectFileImage im;
14*7c3d14c8STreehugger Robot
15*7c3d14c8STreehugger Robot	std::string path = std::string(argv[0]) + ".bundle";
16*7c3d14c8STreehugger Robot    NSObjectFileImageReturnCode rc =
17*7c3d14c8STreehugger Robot        NSCreateObjectFileImageFromFile(path.c_str(), &im);
18*7c3d14c8STreehugger Robot    if (rc != NSObjectFileImageSuccess) {
19*7c3d14c8STreehugger Robot      fprintf(stderr, "Could not load bundle.\n");
20*7c3d14c8STreehugger Robot      exit(-1);
21*7c3d14c8STreehugger Robot    }
22*7c3d14c8STreehugger Robot
23*7c3d14c8STreehugger Robot    NSModule handle = NSLinkModule(im, "a.bundle", 0);
24*7c3d14c8STreehugger Robot    if (handle == 0) {
25*7c3d14c8STreehugger Robot      fprintf(stderr, "Could not load bundle.\n");
26*7c3d14c8STreehugger Robot      exit(-1);
27*7c3d14c8STreehugger Robot    }
28*7c3d14c8STreehugger Robot    printf("h: %p\n", handle);
29*7c3d14c8STreehugger Robot  }
30*7c3d14c8STreehugger Robot
31*7c3d14c8STreehugger Robot  char *ptr = (char *)malloc(10);
32*7c3d14c8STreehugger Robot  ptr[10] = 'x';  // BOOM
33*7c3d14c8STreehugger Robot}
34*7c3d14c8STreehugger Robot
35*7c3d14c8STreehugger Robot// CHECK: AddressSanitizer: heap-buffer-overflow
36*7c3d14c8STreehugger Robot// CHECK: WRITE of size 1
37*7c3d14c8STreehugger Robot// CHECK: {{#0 .* in main}}
38*7c3d14c8STreehugger Robot// CHECK: is located 0 bytes to the right of 10-byte region
39