-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.c
48 lines (42 loc) · 862 Bytes
/
example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#define FUNCTIONS_IMPLEMENTATION
#define EXCEPTION_IMPLEMENTATION
#include "functions.h"
#include "exception.h"
patchable void foo(void){
puts("in foo");
}
void bar(void){
puts("in bar");
}
closeable int close_me(int a){
static int b = 0;
return b += a;
}
[[noreturn]] void my_handler(int signum){
if(except_handler.frame)
siglongjmp(*except_handler.frame, signum);
exit(1);
}
int main(){
foo();
if(is_patchable(foo))
hotpatch(foo, bar);
foo();
original_function(foo)();
if(is_patched(foo))
hotpatch(foo, NULL);
foo();
int e;
signal(SIGTERM, my_handler);
try{
raise(SIGTERM);
}except(e){
printf("Caught a %d: %s\n", e, strsignal(e));
}
int (*closure)(void) = closure_create(close_me, 1, 5); //increment by 5 each call
for(int i = 0; i < 10; ++i)
printf("%d\n", closure());
return 5;
}