|
| 1 | +// *** malloc: all bytes are uninitialized |
| 2 | +// * malloc byte 0 |
| 3 | +// RUN: %clang_msan -fsanitize-memory-track-origins=1 %s -o %t && not %run %t 0 2>&1 \ |
| 4 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC |
| 5 | +// RUN: %clang_msan -fsanitize-memory-track-origins=2 %s -o %t && not %run %t 0 2>&1 \ |
| 6 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC |
| 7 | +// |
| 8 | +// * malloc byte 6 |
| 9 | +// RUN: %clang_msan -fsanitize-memory-track-origins=2 %s -o %t && not %run %t 6 2>&1 \ |
| 10 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC |
| 11 | +// RUN: %clang_msan -fsanitize-memory-track-origins=1 %s -o %t && not %run %t 6 2>&1 \ |
| 12 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC |
| 13 | +// |
| 14 | +// This test assumes the allocator allocates 16 bytes for malloc(7). Bytes |
| 15 | +// 7-15 are padding. |
| 16 | +// |
| 17 | +// * malloc byte 7 |
| 18 | +// Edge case: when the origin granularity spans both ALLOC and ALLOC_PADDING, |
| 19 | +// ALLOC always takes precedence. |
| 20 | +// RUN: %clang_msan -fsanitize-memory-track-origins=1 %s -o %t && not %run %t 7 2>&1 \ |
| 21 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC |
| 22 | +// RUN: %clang_msan -fsanitize-memory-track-origins=2 %s -o %t && not %run %t 7 2>&1 \ |
| 23 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC |
| 24 | +// |
| 25 | +// Bytes 8-15 are padding |
| 26 | +// For track-origins=1, ALLOC is used instead of ALLOC_PADDING. |
| 27 | +// |
| 28 | +// * malloc byte 8 |
| 29 | +// RUN: %clang_msan -fsanitize-memory-track-origins=1 %s -o %t && not %run %t 8 2>&1 \ |
| 30 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC |
| 31 | +// RUN: %clang_msan -fsanitize-memory-track-origins=2 %s -o %t && not %run %t 8 2>&1 \ |
| 32 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC-PADDING |
| 33 | +// |
| 34 | +// * malloc byte 15 |
| 35 | +// RUN: %clang_msan -fsanitize-memory-track-origins=1 %s -o %t && not %run %t 15 2>&1 \ |
| 36 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC |
| 37 | +// RUN: %clang_msan -fsanitize-memory-track-origins=2 %s -o %t && not %run %t 15 2>&1 \ |
| 38 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC-PADDING |
| 39 | + |
| 40 | +// *** calloc |
| 41 | +// Bytes 0-6 are fully initialized, so no MSan report should happen. |
| 42 | +// |
| 43 | +// * calloc byte 0 |
| 44 | +// RUN: %clang_msan -fsanitize-memory-track-origins=1 -DUSE_CALLOC %s -o %t && %run %t 0 2>&1 |
| 45 | +// RUN: %clang_msan -fsanitize-memory-track-origins=2 -DUSE_CALLOC %s -o %t && %run %t 0 2>&1 |
| 46 | +// |
| 47 | +// * calloc byte 6 |
| 48 | +// RUN: %clang_msan -fsanitize-memory-track-origins=1 -DUSE_CALLOC %s -o %t && %run %t 6 2>&1 |
| 49 | +// RUN: %clang_msan -fsanitize-memory-track-origins=2 -DUSE_CALLOC %s -o %t && %run %t 6 2>&1 |
| 50 | +// |
| 51 | +// * calloc byte 7 |
| 52 | +// Byte 7 is uninitialized. Unlike malloc, this is tagged as ALLOC_PADDING |
| 53 | +// (since the origin does not need to track bytes 4-6). |
| 54 | +// RUN: %clang_msan -fsanitize-memory-track-origins=1 -DUSE_CALLOC %s -o %t && not %run %t 7 2>&1 \ |
| 55 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC-PADDING |
| 56 | +// RUN: %clang_msan -fsanitize-memory-track-origins=2 -DUSE_CALLOC %s -o %t && not %run %t 7 2>&1 \ |
| 57 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC-PADDING |
| 58 | +// |
| 59 | +// * calloc byte 8 |
| 60 | +// RUN: %clang_msan -fsanitize-memory-track-origins=1 -DUSE_CALLOC %s -o %t && not %run %t 8 2>&1 \ |
| 61 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC-PADDING |
| 62 | +// RUN: %clang_msan -fsanitize-memory-track-origins=2 -DUSE_CALLOC %s -o %t && not %run %t 8 2>&1 \ |
| 63 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC-PADDING |
| 64 | +// |
| 65 | +// * calloc byte 15 |
| 66 | +// RUN: %clang_msan -fsanitize-memory-track-origins=1 -DUSE_CALLOC %s -o %t && not %run %t 15 2>&1 \ |
| 67 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC-PADDING |
| 68 | +// RUN: %clang_msan -fsanitize-memory-track-origins=2 -DUSE_CALLOC %s -o %t && not %run %t 15 2>&1 \ |
| 69 | +// RUN: | FileCheck %s --check-prefixes=CHECK,ORIGIN-ALLOC-PADDING |
| 70 | + |
| 71 | +#include <assert.h> |
| 72 | +#include <stdio.h> |
| 73 | +#include <stdlib.h> |
| 74 | + |
| 75 | +int main(int argc, char **argv) { |
| 76 | +#ifdef USE_CALLOC |
| 77 | + char *p = (char *)calloc(7, 1); |
| 78 | +#else |
| 79 | + char *p = (char *)malloc(7); |
| 80 | +#endif |
| 81 | + |
| 82 | + if (argc == 2) { |
| 83 | + int index = atoi(argv[1]); |
| 84 | + |
| 85 | + printf("p[%d] = %d\n", index, p[index]); |
| 86 | + // CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value |
| 87 | + // CHECK: {{#0 0x.* in main .*allocator_padding.cpp:}}[[@LINE-2]] |
| 88 | + // ORIGIN-ALLOC: Uninitialized value was created by a heap allocation |
| 89 | + // ORIGIN-ALLOC-PADDING: Uninitialized value is outside of heap allocation |
| 90 | + free(p); |
| 91 | + } |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
0 commit comments