Skip to content

Commit 9747a5c

Browse files
committed
Add hw3
1 parent 7c1ad09 commit 9747a5c

18 files changed

+1919
-0
lines changed

Diff for: hw3/Makefile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
CC = gcc
2+
ASM64 = yasm -f elf64 -DYASM -D__x86_64__ -DPIC
3+
4+
CFLAGS = -g -Wall -fno-stack-protector
5+
6+
PROGS = libmini.so
7+
8+
all: $(PROGS)
9+
10+
libmini.so: libmini64.asm libmini.c
11+
$(ASM64) $< -o libmini64.o
12+
$(CC) -c $(CFLAGS) -fPIC -nostdlib libmini.c
13+
ld -shared libmini64.o libmini.o -o $@
14+
15+
.PHONY: test clean
16+
17+
test: start.asm test.c
18+
$(ASM64) $< -o start.o
19+
$(CC) -c $(CFLAGS) -nostdlib -I. -I.. -DUSEMINI test.c
20+
ld -m elf_x86_64 --dynamic-linker /lib64/ld-linux-x86-64.so.2 -o test test.o start.o -L. -L.. -lmini
21+
LD_LIBRARY_PATH=. ./test
22+
23+
clean:
24+
rm -f a.out test *.o $(PROGS) peda-*
25+

Diff for: hw3/alarm1.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "libmini.h"
2+
int main() {
3+
alarm(3);
4+
pause();
5+
return 0;
6+
}
7+

Diff for: hw3/alarm2.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "libmini.h"
2+
3+
int main() {
4+
sigset_t s;
5+
sigemptyset(&s);
6+
sigaddset(&s, SIGALRM);
7+
sigprocmask(SIG_BLOCK, &s, NULL);
8+
alarm(3);
9+
sleep(5);
10+
if(sigpending(&s) < 0) perror("sigpending");
11+
if(sigismember(&s, SIGALRM)) {
12+
char m[] = "sigalrm is pending.\n";
13+
write(1, m, sizeof(m));
14+
} else {
15+
char m[] = "sigalrm is not pending.\n";
16+
write(1, m, sizeof(m));
17+
}
18+
return 0;
19+
}
20+

Diff for: hw3/alarm3.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "libmini.h"
2+
3+
void handler(int s) { /* do nothing */ }
4+
5+
int main() {
6+
sigset_t s;
7+
sigemptyset(&s);
8+
sigaddset(&s, SIGALRM);
9+
sigprocmask(SIG_BLOCK, &s, NULL);
10+
signal(SIGALRM, SIG_IGN);
11+
signal(SIGINT, handler);
12+
alarm(1);
13+
pause();
14+
if(sigpending(&s) < 0) perror("sigpending");
15+
if(sigismember(&s, SIGALRM)) {
16+
char m[] = "sigalrm is pending.\n";
17+
write(1, m, sizeof(m));
18+
} else {
19+
char m[] = "sigalrm is not pending.\n";
20+
write(1, m, sizeof(m));
21+
}
22+
return 0;
23+
}

Diff for: hw3/jmp1.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "libmini.h"
2+
3+
typedef void (*proc_t)();
4+
static jmp_buf jb;
5+
6+
#define FUNBODY(m, from) { write(1, m, strlen(m)); longjmp(jb, from); }
7+
8+
void a() FUNBODY("This is function a().\n", 1);
9+
void b() FUNBODY("This is function b().\n", 2);
10+
void c() FUNBODY("This is function c().\n", 3);
11+
void d() FUNBODY("This is function d().\n", 4);
12+
void e() FUNBODY("This is function e().\n", 5);
13+
void f() FUNBODY("This is function f().\n", 6);
14+
void g() FUNBODY("This is function g().\n", 7);
15+
void h() FUNBODY("This is function h().\n", 8);
16+
void i() FUNBODY("This is function i().\n", 9);
17+
void j() FUNBODY("This is function j().\n", 10);
18+
19+
proc_t funs[] = { a, b, c, d, e, f, g, h, i, j };
20+
21+
int main() {
22+
volatile int i = 0;
23+
if(setjmp(jb) != 0) {
24+
i++;
25+
}
26+
if(i < 10) funs[i]();
27+
return 0;
28+
}

0 commit comments

Comments
 (0)