1570af302Sopenharmony_ci#include <stdio.h>
2570af302Sopenharmony_ci#include <stdlib.h>
3570af302Sopenharmony_ci#include <assert.h>
4570af302Sopenharmony_ci#include <info/fatal_message.h>
5570af302Sopenharmony_ci#include <stddef.h>
6570af302Sopenharmony_ci#include <signal.h>
7570af302Sopenharmony_ci#include "musl_log.h"
8570af302Sopenharmony_ci#define ASSERT_FATAL_MESSAGE_SIZE 1024
9570af302Sopenharmony_ci
10570af302Sopenharmony_cistatic assert_call g_cb = NULL;
11570af302Sopenharmony_civoid set_assert_callback(assert_call cb)
12570af302Sopenharmony_ci{
13570af302Sopenharmony_ci    g_cb = cb;
14570af302Sopenharmony_ci}
15570af302Sopenharmony_ci
16570af302Sopenharmony_civoid __assert_fail(const char *expr, const char *file, int line, const char *func)
17570af302Sopenharmony_ci{
18570af302Sopenharmony_ci    // Concatenate information for assert failures and save the final result in assert_fatal_message
19570af302Sopenharmony_ci    char assert_fatal_message[ASSERT_FATAL_MESSAGE_SIZE];
20570af302Sopenharmony_ci
21570af302Sopenharmony_ci    snprintf(assert_fatal_message, sizeof(assert_fatal_message),
22570af302Sopenharmony_ci             "Assertion failed: %s (%s: %s: %d)", expr, file, func, line);
23570af302Sopenharmony_ci
24570af302Sopenharmony_ci    // call set_fatal_message to set assert_fatal_message
25570af302Sopenharmony_ci    set_fatal_message(assert_fatal_message);
26570af302Sopenharmony_ci    AssertFailureInfo assert_fail = {
27570af302Sopenharmony_ci        expr, file, func, line
28570af302Sopenharmony_ci    };
29570af302Sopenharmony_ci
30570af302Sopenharmony_ci    Assert_Status assert_status = ASSERT_ABORT;
31570af302Sopenharmony_ci    if (g_cb) {
32570af302Sopenharmony_ci        assert_status = g_cb(assert_fail);
33570af302Sopenharmony_ci    }
34570af302Sopenharmony_ci    if (assert_status == ASSERT_RETRY) {
35570af302Sopenharmony_ci        MUSL_LOGE("CallbackFunction return ASSERT_RETRY:\n");
36570af302Sopenharmony_ci        raise(SIGUSR1);
37570af302Sopenharmony_ci    } else if (assert_status == ASSERT_IGNORE) {
38570af302Sopenharmony_ci        MUSL_LOGE("CallbackFunction return ASSERT_IGNORE:\n");
39570af302Sopenharmony_ci        return;
40570af302Sopenharmony_ci    } else {
41570af302Sopenharmony_ci        MUSL_LOGE("CallbackFunction return ASSERT_ABORT:\n");
42570af302Sopenharmony_ci        fprintf(stderr, "%s\n", assert_fatal_message);
43570af302Sopenharmony_ci        abort();
44570af302Sopenharmony_ci    }
45570af302Sopenharmony_ci}
46