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 "include/sys/syscall.h"  // For syscall numbers.
14 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
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, ...).
21 static void write_to_stderr(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 
27 LLVM_LIBC_FUNCTION(void, __assert_fail,
28                    (const char *assertion, const char *file, unsigned line,
29                     const char *function)) {
30   write_to_stderr(file);
31   write_to_stderr(": Assertion failed: '");
32   write_to_stderr(assertion);
33   write_to_stderr("' in function: '");
34   write_to_stderr(function);
35   write_to_stderr("'\n");
36   __llvm_libc::abort();
37 }
38 
39 } // namespace __llvm_libc
40