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