1*66d00febSPaula 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 
9b47c9f53SAlex Brachet #include "src/assert/assert.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 
27b47c9f53SAlex Brachet void LLVM_LIBC_ENTRYPOINT(__assert_fail)(const char *assertion, const char *file,
28b47c9f53SAlex Brachet                                          unsigned line, const char *function) {
29b47c9f53SAlex Brachet   writeToStderr(file);
30b47c9f53SAlex Brachet   writeToStderr(": Assertion failed: '");
31b47c9f53SAlex Brachet   writeToStderr(assertion);
32b47c9f53SAlex Brachet   writeToStderr("' in function: '");
33b47c9f53SAlex Brachet   writeToStderr(function);
34b47c9f53SAlex Brachet   writeToStderr("'\n");
35b47c9f53SAlex Brachet   __llvm_libc::abort();
36b47c9f53SAlex Brachet }
37b47c9f53SAlex Brachet 
38b47c9f53SAlex Brachet } // namespace __llvm_libc
39