166d00febSPaula Toth //===-- Implementation of __assert_fail -----------------------------------===//
2b47c9f53SAlex Brachet //
3b47c9f53SAlex Brachet // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b47c9f53SAlex Brachet // See https://llvm.org/LICENSE.txt for license information.
5b47c9f53SAlex Brachet // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b47c9f53SAlex Brachet //
7b47c9f53SAlex Brachet //===----------------------------------------------------------------------===//
8b47c9f53SAlex Brachet 
95080840dSMichael Jones #include "src/assert/__assert_fail.h"
10b47c9f53SAlex Brachet #include "src/stdlib/abort.h"
11b47c9f53SAlex Brachet 
12b47c9f53SAlex Brachet // These includes are temporary.
13b47c9f53SAlex Brachet #include "config/linux/syscall.h" // For internal syscall function.
14b47c9f53SAlex Brachet #include "include/sys/syscall.h"  // For syscall numbers.
15b47c9f53SAlex Brachet 
16b47c9f53SAlex Brachet namespace __llvm_libc {
17b47c9f53SAlex Brachet 
18b47c9f53SAlex Brachet // This is just a temporary solution to make assert available to internal
19b47c9f53SAlex Brachet // llvm libc code. In the future writeToStderr will not exist and __assert_fail
20b47c9f53SAlex Brachet // will call fprintf(stderr, ...).
21b47c9f53SAlex Brachet static void writeToStderr(const char *s) {
22b47c9f53SAlex Brachet   size_t length = 0;
23b47c9f53SAlex Brachet   for (const char *curr = s; *curr; ++curr, ++length);
24b47c9f53SAlex Brachet   __llvm_libc::syscall(SYS_write, 2, s, length);
25b47c9f53SAlex Brachet }
26b47c9f53SAlex Brachet 
27*a0b65a7bSMichael Jones LLVM_LIBC_FUNCTION(void, __assert_fail,
28*a0b65a7bSMichael Jones                    (const char *assertion, const char *file, unsigned line,
29*a0b65a7bSMichael Jones                     const char *function)) {
30b47c9f53SAlex Brachet   writeToStderr(file);
31b47c9f53SAlex Brachet   writeToStderr(": Assertion failed: '");
32b47c9f53SAlex Brachet   writeToStderr(assertion);
33b47c9f53SAlex Brachet   writeToStderr("' in function: '");
34b47c9f53SAlex Brachet   writeToStderr(function);
35b47c9f53SAlex Brachet   writeToStderr("'\n");
36b47c9f53SAlex Brachet   __llvm_libc::abort();
37b47c9f53SAlex Brachet }
38b47c9f53SAlex Brachet 
39b47c9f53SAlex Brachet } // namespace __llvm_libc
40