FFmpeg
x86_64.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2025, Niklas Haas
3  * Copyright © 2018, VideoLAN and dav1d authors
4  * Copyright © 2018, Two Orioles, LLC
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef CHECKASM_PLATFORM_X86_64_H
30 #define CHECKASM_PLATFORM_X86_64_H
31 
32 #include <stdint.h>
33 
34 #include "checkasm/attributes.h"
35 #include "checkasm/checkasm.h"
36 
37 CHECKASM_API void checkasm_checked_call(void *func, ...);
40 
41 /* The upper 32 bits of 32-bit data types are undefined when passed as function
42  * parameters. In practice those bits usually end up being zero which may hide
43  * certain bugs, such as using a register containing undefined bits as a pointer
44  * offset, so we want to intentionally clobber those bits with junk to expose
45  * any issues. The following set of macros automatically calculates a bitmask
46  * specifying which parameters should have their upper halves clobbered. */
47 #ifdef _WIN32
48  /* Integer and floating-point parameters share "register slots". */
49  #define IGNORED_FP_ARGS 0
50 #else
51  /* Up to 8 floating-point parameters are passed in XMM registers, which are
52  * handled orthogonally from integer parameters passed in GPR registers. */
53  #define IGNORED_FP_ARGS 8
54 #endif
55 
56 #if CHECKASM_HAVE_GENERIC
57  #define clobber_type(arg) \
58  _Generic((void (*)(void *, arg)) NULL, \
59  void (*)(void *, int32_t): clobber_mask |= 1 << mpos++, \
60  void (*)(void *, uint32_t): clobber_mask |= 1 << mpos++, \
61  void (*)(void *, float): mpos += (fp_args++ >= IGNORED_FP_ARGS), \
62  void (*)(void *, double): mpos += (fp_args++ >= IGNORED_FP_ARGS), \
63  default: mpos++)
64 
65  #define init_clobber_mask(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, ...) \
66  unsigned clobber_mask = 0; \
67  { \
68  int mpos = 0, fp_args = 0; \
69  clobber_type(a); \
70  clobber_type(b); \
71  clobber_type(c); \
72  clobber_type(d); \
73  clobber_type(e); \
74  clobber_type(f); \
75  clobber_type(g); \
76  clobber_type(h); \
77  clobber_type(i); \
78  clobber_type(j); \
79  clobber_type(k); \
80  clobber_type(l); \
81  clobber_type(m); \
82  clobber_type(n); \
83  clobber_type(o); \
84  clobber_type(p); \
85  }
86 #else
87  /* Skip parameter clobbering on compilers without support for _Generic() */
88  #define init_clobber_mask(...) unsigned clobber_mask = 0
89 #endif
90 
91 #define checkasm_declare_impl(ret, ...) \
92  ret (*checked_call)(__VA_ARGS__, int, int, int, int, int, int, int, int, int, int, \
93  int, int, int, int, int, int, void *, unsigned) \
94  = (ret (*)(__VA_ARGS__, int, int, int, int, int, int, int, int, int, int, int, \
95  int, int, int, int, int, void *, \
96  unsigned))(void *) checkasm_checked_call; \
97  int emms_needed = 0; \
98  (void) emms_needed; \
99  init_clobber_mask(__VA_ARGS__, void *, void *, void *, void *, void *, void *, \
100  void *, void *, void *, void *, void *, void *, void *, void *, \
101  void *)
102 
103 #define checkasm_call_checked(func, ...) \
104  (checkasm_set_signal_handler_state(1), \
105  checked_call(__VA_ARGS__, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, \
106  func, clobber_mask)); \
107  checkasm_set_signal_handler_state(0)
108 
109 #define checkasm_declare_emms(cpu_flags, ret, ...) \
110  checkasm_declare(ret, __VA_ARGS__); \
111  if (checkasm_get_cpu_flags() & (cpu_flags)) { \
112  checked_call = (ret (*)(__VA_ARGS__, int, int, int, int, int, int, int, int, \
113  int, int, int, int, int, int, int, int, void *, \
114  unsigned))(void *) checkasm_checked_call_emms; \
115  emms_needed = 1; \
116  }
117 
118 #if defined(__GNUC__) || defined(__clang__)
119  #define checkasm_emms() __asm__ volatile("emms" ::: "memory")
120 #else
121  #define checkasm_emms() checkasm_empty_mmx()
122 #endif
123 
124 #define checkasm_clear_cpu_state() \
125  do { \
126  if (emms_needed) \
127  checkasm_emms(); \
128  } while (0)
129 
130 /* x86-64 needs 32- and 64-byte alignment for AVX2 and AVX-512. */
131 #define CHECKASM_ALIGNMENT 64
132 
133 #endif /* CHECKASM_PLATFORM_X86_64_H */
func
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:66
checkasm_empty_mmx
CHECKASM_API void checkasm_empty_mmx(void)
attributes.h
Platform and compiler attribute macros.
checkasm_checked_call
CHECKASM_API void checkasm_checked_call(void *func,...)
CHECKASM_API
#define CHECKASM_API
Symbol visibility attribute for public API functions.
Definition: attributes.h:90
checkasm_checked_call_emms
CHECKASM_API void checkasm_checked_call_emms(void *func,...)