Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 4f79119

Browse files
Bug 1858670 - Replace volatile accesses with inline assembly to ensure MOZ_CRASH() crashing sequence is always emitted r=glandium
Differential Revision: https://phabricator.services.mozilla.com/D198023
1 parent a72c16f commit 4f79119

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

mfbt/Assertions.h

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,40 @@ MOZ_NoReturn(int aLine) {
233233

234234
#else
235235

236+
// This function causes the process to crash by writing the line number
237+
// specified in the `aLine` parameter to the address provide by `aAddress`.
238+
// The store is implemented as volatile assembly code to ensure it's always
239+
// included in the output and always executed. This does not apply to ASAN
240+
// builds where we use __builtin_trap() instead, as an illegal access would
241+
// trip ASAN's checks.
242+
# if !defined(MOZ_ASAN)
243+
static inline void MOZ_CrashSequence(void* aAddress, intptr_t aLine) {
244+
# if defined(__i386__) || defined(__x86_64__)
245+
asm volatile(
246+
"mov %1, (%0);\n" // Write the line number to the crashing address
247+
: // no output registers
248+
: "r"(aAddress), "r"(aLine));
249+
# elif defined(__arm__) || defined(__aarch64__)
250+
asm volatile(
251+
"str %1,[%0];\n" // Write the line number to the crashing address
252+
: // no output registers
253+
: "r"(aAddress), "r"(aLine));
254+
# elif defined(__riscv)
255+
asm volatile(
256+
"sw %1,0(%0);\n" // Write the line number to the crashing address
257+
: // no output registers
258+
: "r"(aAddress), "r"(aLine));
259+
# else
260+
# warning \
261+
"Unsupported architecture, replace the code below with assembly suitable to crash the process"
262+
asm volatile("" ::: "memory");
263+
*((volatile int*)MOZ_CRASH_WRITE_ADDR) = line; /* NOLINT */
264+
# endif
265+
}
266+
# else
267+
# define MOZ_CrashSequence(x, y) __builtin_trap()
268+
# endif
269+
236270
/*
237271
* MOZ_CRASH_WRITE_ADDR is the address to be used when performing a forced
238272
* crash. NULL is preferred however if for some reason NULL cannot be used
@@ -250,16 +284,16 @@ MOZ_NoReturn(int aLine) {
250284
# endif
251285

252286
# ifdef __cplusplus
253-
# define MOZ_REALLY_CRASH(line) \
254-
do { \
255-
*((volatile int*)MOZ_CRASH_WRITE_ADDR) = line; /* NOLINT */ \
256-
MOZ_NOMERGE ::abort(); \
287+
# define MOZ_REALLY_CRASH(line) \
288+
do { \
289+
MOZ_CrashSequence(MOZ_CRASH_WRITE_ADDR, line); \
290+
MOZ_NOMERGE ::abort(); \
257291
} while (false)
258292
# else
259-
# define MOZ_REALLY_CRASH(line) \
260-
do { \
261-
*((volatile int*)MOZ_CRASH_WRITE_ADDR) = line; /* NOLINT */ \
262-
MOZ_NOMERGE abort(); \
293+
# define MOZ_REALLY_CRASH(line) \
294+
do { \
295+
MOZ_CrashSequence(MOZ_CRASH_WRITE_ADDR, line); \
296+
MOZ_NOMERGE abort(); \
263297
} while (false)
264298
# endif
265299
#endif

0 commit comments

Comments
 (0)