1673dc3d4SNico Weber // RUN: %clang_asan -O2 %s -o %t
2673dc3d4SNico Weber // RUN: %run %t 2>&1 | FileCheck --check-prefix=CHECK-NOSCRIBBLE %s
3673dc3d4SNico Weber // RUN: %env MallocScribble=1 MallocPreScribble=1 %run %t 2>&1 | FileCheck --check-prefix=CHECK-SCRIBBLE %s
4673dc3d4SNico Weber // RUN: %env_asan_opts=max_free_fill_size=4096 %run %t 2>&1 | FileCheck --check-prefix=CHECK-SCRIBBLE %s
5673dc3d4SNico Weber
6673dc3d4SNico Weber #include <stdint.h>
7673dc3d4SNico Weber #include <stdio.h>
8673dc3d4SNico Weber #include <stdlib.h>
9673dc3d4SNico Weber #include <string.h>
10673dc3d4SNico Weber
11673dc3d4SNico Weber struct Isa {
12673dc3d4SNico Weber const char *class_name;
13673dc3d4SNico Weber };
14673dc3d4SNico Weber
15673dc3d4SNico Weber struct MyClass {
16*71841146SDan Liew // User memory and `ChunkHeader` overlap. In particular the `free_context_id`
17*71841146SDan Liew // is stored at the beginning of user memory when it is freed. That part of
18*71841146SDan Liew // user memory is not scribbled and is changed when the memory is freed. This
19*71841146SDan Liew // test relies on `isa` being scribbled or unmodified after memory is freed.
20*71841146SDan Liew // In order for this to work the start of `isa` must come after whatever is in
21*71841146SDan Liew // `ChunkHeader` (currently the 64-bit `free_context_id`). The padding here is
22*71841146SDan Liew // to ensure this is the case.
23*71841146SDan Liew uint64_t padding;
24673dc3d4SNico Weber Isa *isa;
25673dc3d4SNico Weber long data;
26673dc3d4SNico Weber
27673dc3d4SNico Weber void print_my_class_name();
28673dc3d4SNico Weber };
29673dc3d4SNico Weber
30673dc3d4SNico Weber __attribute__((no_sanitize("address")))
print_my_class_name()31673dc3d4SNico Weber void MyClass::print_my_class_name() {
32673dc3d4SNico Weber fprintf(stderr, "this = %p\n", this);
33673dc3d4SNico Weber fprintf(stderr, "padding = 0x%lx\n", this->padding);
34673dc3d4SNico Weber fprintf(stderr, "isa = %p\n", this->isa);
35673dc3d4SNico Weber
36673dc3d4SNico Weber if ((uint32_t)(uintptr_t)this->isa != 0x55555555) {
37673dc3d4SNico Weber fprintf(stderr, "class name: %s\n", this->isa->class_name);
38673dc3d4SNico Weber }
39673dc3d4SNico Weber }
40673dc3d4SNico Weber
main()41673dc3d4SNico Weber int main() {
42673dc3d4SNico Weber Isa *my_class_isa = (Isa *)malloc(sizeof(Isa));
43673dc3d4SNico Weber memset(my_class_isa, 0x77, sizeof(Isa));
44673dc3d4SNico Weber my_class_isa->class_name = "MyClass";
45673dc3d4SNico Weber
46673dc3d4SNico Weber MyClass *my_object = (MyClass *)malloc(sizeof(MyClass));
47673dc3d4SNico Weber memset(my_object, 0x88, sizeof(MyClass));
48673dc3d4SNico Weber my_object->isa = my_class_isa;
49673dc3d4SNico Weber my_object->data = 42;
50673dc3d4SNico Weber
51673dc3d4SNico Weber my_object->print_my_class_name();
52673dc3d4SNico Weber // CHECK-SCRIBBLE: class name: MyClass
53673dc3d4SNico Weber // CHECK-NOSCRIBBLE: class name: MyClass
54673dc3d4SNico Weber
55673dc3d4SNico Weber free(my_object);
56673dc3d4SNico Weber
57673dc3d4SNico Weber my_object->print_my_class_name();
58673dc3d4SNico Weber // CHECK-NOSCRIBBLE: class name: MyClass
59673dc3d4SNico Weber // CHECK-SCRIBBLE: isa = {{(0x)?}}{{5555555555555555|55555555}}
60673dc3d4SNico Weber
61673dc3d4SNico Weber fprintf(stderr, "okthxbai!\n");
62673dc3d4SNico Weber // CHECK-SCRIBBLE: okthxbai!
63673dc3d4SNico Weber // CHECK-NOSCRIBBLE: okthxbai!
64673dc3d4SNico Weber free(my_class_isa);
65673dc3d4SNico Weber }
66