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, ...). 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 27 LLVM_LIBC_FUNCTION(void, __assert_fail, 28 (const char *assertion, const char *file, unsigned line, 29 const char *function)) { 30 writeToStderr(file); 31 writeToStderr(": Assertion failed: '"); 32 writeToStderr(assertion); 33 writeToStderr("' in function: '"); 34 writeToStderr(function); 35 writeToStderr("'\n"); 36 __llvm_libc::abort(); 37 } 38 39 } // namespace __llvm_libc 40