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