Files
failnix/targets/lib.h
2026-04-18 19:46:45 +02:00

75 lines
2.2 KiB
C

#ifndef _include_fail_h
#define _include_fail_h
#include <stdint.h>
#define INLINE __attribute__((always_inline)) inline
#define NOINLINE __attribute__((noinline))
#define EXPORT(fnct) __attribute__((export_name(fnct)))
#define IMPORT(fnct) __attribute__((import_module("env"), import_name(fnct)))
#if !defined(TARGET_FAIL) && !defined(TARGET_LINUX_BAREMETAL) && \
!defined(TARGET_LINUX)
// Set to linux while editing to prevent lsp errors
#define TARGET_LINUX
#endif
#ifdef TARGET_FAIL
#define MAIN void os_main(void)
#define PRINT(fmt, ...)
#define PRINT_ERROR(fmt, ...)
#define PRINT_SUCCESS(fmt, ...)
#define HOST_PRINT(msg)
#define RET(val) return
#endif
#ifdef TARGET_LINUX_BAREMETAL
#define MAIN int main(int argc, char *argv[])
#define PRINT(fmt, ...)
#define PRINT_ERROR(fmt, ...)
#define PRINT_SUCCESS(fmt, ...)
#define HOST_PRINT(msg)
#define RET(val) return
#endif
#ifdef TARGET_LINUX
#define MAIN int main(int argc, char *argv[])
#define PRINT(fmt, ...) fprintf(stdout, fmt, ##__VA_ARGS__)
#define PRINT_ERROR(fmt, ...) \
fprintf(stderr, "[Error] "); \
fprintf(stderr, fmt, ##__VA_ARGS__)
#define PRINT_SUCCESS(fmt, ...) \
fprintf(stdout, "[Success] "); \
fprintf(stdout, fmt, ##__VA_ARGS__)
#define HOST_PRINT(msg) print(msg)
#define RET(val) return val;
#endif
typedef uint16_t enc_t;
typedef uint8_t plain_t;
typedef int8_t sign_t;
#define check(vc, A, B) (((vc - B) % A) == 0)
#define encode(v, A, B) ((((plain_t)v) * ((sign_t)A)) + ((sign_t)B))
#define decode(vc, A, B) ((vc - B) / A)
#define equals(vc1, vc2, B1, B2) ((vc1 - vc2) == (B1 - B2))
#ifdef __cplusplus
extern "C" {
#endif
// Those functions are defined in the host program
void NOINLINE fail_start_trace(void); // Mark start of injection
void NOINLINE fail_stop_trace(void); // Mark end of injection
void NOINLINE fail_marker_positive(void); // Everything ok
void NOINLINE fail_marker_detected(void); // Everything ok
void NOINLINE fail_marker_negative(void); // Invalid code
void NOINLINE print(const char *msg);
#ifdef __cplusplus
}
#endif
#endif