File: load_and_store_n.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (64 lines) | stat: -rw-r--r-- 2,254 bytes parent folder | download | duplicates (19)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// RUN: %clangxx_asan -O2 -fsanitize-address-outline-instrumentation %s -o %t
// RUN: not %run %t A 2>&1 | FileCheck %s --check-prefix=CHECK_0_BYTES
// RUN: not %run %t B 2>&1 | FileCheck %s --check-prefix=CHECK_0_BYTES
// RUN: not %run %t C 2>&1 | FileCheck %s --check-prefix=CHECK_1_BYTES
// RUN: not %run %t D 2>&1 | FileCheck %s --check-prefix=CHECK_1_BYTES

// RUN: %clangxx_asan -O2 -fsanitize-address-outline-instrumentation %s -o %t \
// RUN:   -mllvm -asan-recover=1
// RUN: not %run %t A 2>&1 | FileCheck %s --check-prefix=CHECK_0_BYTES
// RUN: not %run %t B 2>&1 | FileCheck %s --check-prefix=CHECK_0_BYTES
// RUN: not %run %t C 2>&1 | FileCheck %s --check-prefix=CHECK_1_BYTES
// RUN: not %run %t D 2>&1 | FileCheck %s --check-prefix=CHECK_1_BYTES

// RUN: %clangxx_asan -O2 -fsanitize-address-outline-instrumentation %s -o %t \
// RUN:   -mllvm -asan-force-experiment=42
// RUN: not %run %t A 2>&1 | FileCheck %s --check-prefix=CHECK_0_BYTES
// RUN: not %run %t B 2>&1 | FileCheck %s --check-prefix=CHECK_0_BYTES
// RUN: not %run %t C 2>&1 | FileCheck %s --check-prefix=CHECK_1_BYTES
// RUN: not %run %t D 2>&1 | FileCheck %s --check-prefix=CHECK_1_BYTES

// CHECK_0_BYTES: ERROR: AddressSanitizer: global-buffer-overflow on address [[ADDR:.*]] at
// CHECK_0_BYTES: [[ADDR]] is located 0 bytes after

// CHECK_1_BYTES: ERROR: AddressSanitizer: global-buffer-overflow on address [[ADDR:.*]] at
// CHECK_1_BYTES: [[ADDR]] is located 1 bytes after

#include <sanitizer/asan_interface.h>

#include <stdlib.h>
#include <string.h>

static int64_t mem = -1;
static int64_t *volatile G = &mem;

inline uint16_t UNALIGNED_LOAD(const void *p) {
  uint16_t data;
  memcpy(&data, p, sizeof data);
  return data;
}

inline void UNALIGNED_STORE(uint16_t data, void *p) {
  memcpy(p, &data, sizeof data);
}

int main(int argc, char **argv) {
  if (argc != 2)
    return 1;
  int res = 1;
  switch (argv[1][0]) {
  case 'A':
    res = UNALIGNED_LOAD(reinterpret_cast<char *>(G) + 7);
    break;
  case 'B':
    UNALIGNED_STORE(0, reinterpret_cast<char *>(G) + 7);
    break;
  case 'C':
    res = UNALIGNED_LOAD(reinterpret_cast<char *>(G) + 9);
    break;
  case 'D':
    UNALIGNED_STORE(0, reinterpret_cast<char *>(G) + 9);
    break;
  }
  return res;
}