From 41bdbbbe12f45668ece305ad02a6ee59315d4f07 Mon Sep 17 00:00:00 2001 From: Balhau Date: Fri, 28 Jun 2024 09:09:03 +0100 Subject: [PATCH] Stuff --- .gitignore | 6 ++ Makefile | 95 ++++++++++++++++++++ README.md | 26 ++++++ c/beforeafter.c | 11 +++ c/cards.c | 63 +++++++++++++ c/ex1.c | 200 +++++++++++++++++++++++++++++++++++++++++ c/ex2.c | 57 ++++++++++++ c/ex3.c | 18 ++++ c/ex4.c | 51 +++++++++++ c/ex5.c | 22 +++++ c/ex6.c | 28 ++++++ c/ex7.c | 46 ++++++++++ c/forkhandler.c | 22 +++++ c/pthreadattrdestroy.c | 10 +++ c/pthreadex1.c | 21 +++++ c/pthreadex2.c | 18 ++++ c/pthreadex3.c | 34 +++++++ c/pthreadex4.c | 29 ++++++ c/pthreadex5.c | 42 +++++++++ c/pthreadex6.c | 40 +++++++++ c/sha3.c | 176 ++++++++++++++++++++++++++++++++++++ c/socket_ex.c | 26 ++++++ cpp/ex1.cpp | 8 ++ cpp/ex2.cpp | 21 +++++ defs/makefile.def | 26 ++++++ golang/calls.go | 91 +++++++++++++++++++ golang/findme.go | 15 ++++ golang/hello.go | 8 ++ golang/structs.go | 52 +++++++++++ golangc/silly.c | 18 ++++ golangc/silly.go | 23 +++++ golangc/silly.h | 17 ++++ java/Test.java | 6 ++ so/hook.c | 30 +++++++ 34 files changed, 1356 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 c/beforeafter.c create mode 100644 c/cards.c create mode 100644 c/ex1.c create mode 100644 c/ex2.c create mode 100644 c/ex3.c create mode 100644 c/ex4.c create mode 100644 c/ex5.c create mode 100644 c/ex6.c create mode 100644 c/ex7.c create mode 100644 c/forkhandler.c create mode 100644 c/pthreadattrdestroy.c create mode 100644 c/pthreadex1.c create mode 100644 c/pthreadex2.c create mode 100644 c/pthreadex3.c create mode 100644 c/pthreadex4.c create mode 100644 c/pthreadex5.c create mode 100644 c/pthreadex6.c create mode 100644 c/sha3.c create mode 100644 c/socket_ex.c create mode 100644 cpp/ex1.cpp create mode 100644 cpp/ex2.cpp create mode 100644 defs/makefile.def create mode 100644 golang/calls.go create mode 100644 golang/findme.go create mode 100644 golang/hello.go create mode 100644 golang/structs.go create mode 100644 golangc/silly.c create mode 100644 golangc/silly.go create mode 100644 golangc/silly.h create mode 100644 java/Test.java create mode 100644 so/hook.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6b61ee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.vscode/ +CGuidra* +bin/ +data/ +*.readelf +*.class diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..240ddc0 --- /dev/null +++ b/Makefile @@ -0,0 +1,95 @@ +include defs/makefile.def + +make: c-x86-debug cpp-x86-debug c-x86-release cpp-x86-release go gosilly make-so + + +make-so:${SO_DIR}/*.c + @mkdir -p ${LIB_DIR} + @for file in $? ; do \ + path=$$(echo "$$file" | cut -f 2 -d '/'); \ + out=$$(echo "$$path" | cut -f 1 -d '.'); \ + ${CC} ${CFLAG_LIB} $$file -o ${LIB_DIR}/$$out.o;\ + ${CC} -shared -o ${LIB_DIR}/$$out.so ${LIB_DIR}/$$out.o -ldl; \ + done + +gosilly: libsilly.so silly.o + CGO_CFLAGS=${CGO_CFGLAGS_DYN} \ + CGO_LDFLAGS=${CGO_LDFLAGS_DYN} \ + ${GO} build -o ${BIN_DIR}/silly.go golangc/silly.go + +.PHONY: libsilly.so +libsilly.so: silly.o + @rm -f ${BIN_DIR}/libsilly.so + @mkdir -p ${LIB_DIR} + ${CC} -shared -o ${LIB_DIR}/libsilly.so ${BIN_DIR}/silly.o + @ln -s ../${LIB_DIR}/libsilly.so ${BIN_DIR}/libsilly.so + +.PHONY: silly.o +silly.o: golangc/silly.c + @mkdir -p ${BIN_DIR} + ${CC} ${CFLAG_LIB} $? -o ${BIN_DIR}/silly.o + +c-x86-debug:${C_LANG_DIR}/*.c + @mkdir -p ${BIN_DIR} + @for file in $? ; do \ + path=$$(echo "$$file" | cut -f 2 -d '/'); \ + out=$$(echo "$$path" | cut -f 1 -d '.'); \ + echo ${CC} ${CFLAG_DEBUG} -o ${BIN_DIR}/$$out.c.debug.${ARCH_X86} $$file; \ + ${CC} ${CFLAG_DEBUG} -o ${BIN_DIR}/$$out.c.debug.${ARCH_X86} $$file -lpthread;\ + done + +cpp-x86-debug:${CPP_LANG_DIR}/*.cpp + @mkdir -p ${BIN_DIR} + @for file in $? ; do \ + path=$$(echo "$$file" | cut -f 2 -d '/'); \ + out=$$(echo "$$path" | cut -f 1 -d '.'); \ + echo ${CPP} ${CFLAG_DEBUG} -o ${BIN_DIR}/$$out.cpp.debug.${ARCH_X86} $$file; \ + ${CPP} ${CFLAG_DEBUG} -o ${BIN_DIR}/$$out.cpp.debug.${ARCH_X86} $$file; \ + done + +c-x86-release:${C_LANG_DIR}/*.c + @mkdir -p ${BIN_DIR} + @for file in $? ; do \ + path=$$(echo "$$file" | cut -f 2 -d '/'); \ + out=$$(echo "$$path" | cut -f 1 -d '.'); \ + echo ${CC} ${CFLAG_RELEASE} -o ${BIN_DIR}/$$out.c.release.${ARCH_X86} $$file; \ + ${CC} ${CFLAG_RELEASE} -o ${BIN_DIR}/$$out.c.release.${ARCH_X86} $$file; \ + done + +cpp-x86-release:${CPP_LANG_DIR}/*.cpp + @mkdir -p ${BIN_DIR} + @for file in $? ; do \ + path=$$(echo "$$file" | cut -f 2 -d '/'); \ + out=$$(echo "$$path" | cut -f 1 -d '.'); \ + echo ${CPP} ${CFLAG_RELEASE} -o ${BIN_DIR}/$$out.cpp.release.${ARCH_X86} $$file; \ + ${CPP} ${CFLAG_RELEASE} -o ${BIN_DIR}/$$out.cpp.release.${ARCH_X86} $$file; \ + done + +go:${GO_LANG_DIR}/*.go + @mkdir -p ${BIN_DIR} + @for file in $? ; do \ + path=$$(echo "$$file" | cut -f 2 -d '/'); \ + echo ${GO} build ${GO_FLAGS} -o ${BIN_DIR}/$$path $$file; \ + ${GO} build ${GO_FLAGS} -o ${BIN_DIR}/$$path $$file; \ + done + +c-arm:${C_LANG_DIR}/*.c + @mkdir -p ${BIN_DIR} + @for file in $? ; do \ + path=$$(echo "$$file" | cut -f 2 -d '/'); \ + out=$$(echo "$$path" | cut -f 1 -d '.'); \ + echo ${CC} ${CFLAG} -o ${BIN_DIR}/$$out.c.${ARCH} $$file; \ + ${CC_ARM} ${ARM_CFLAG} -o ${BIN_DIR}/$$out.c.${ARCH_ARM} $$file; \ + done + +c-asm-x86: + @mkdir -p ${ASM_X86_OUT} + ${CC} ${ASM_C_FLAGS} ${C_LANG_DIR}/*.c + @mv *.s ${ASM_X86_OUT} +c-asm-arm: + @mkdir -p ${ASM_ARM_OUT} + ${CC_ARM} ${ASM_ARM_C_FLAGS} ${C_LANG_DIR}/*.c + @mv *.s ${ASM_ARM_OUT} + +clean: + @rm -rf ${BIN_DIR} ${ASM_ARM_OUT} ${ASM_X86_OUT} ${LIB_DIR} diff --git a/README.md b/README.md new file mode 100644 index 0000000..553f423 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# Additional Info + +Install arm toolchain on debian linux (assuming x86 environment). + +```shell + sudo apt update -y && sudo apt upgrade -y + sudo apt install qemu-user qemu-user-static gcc-aarch64-linux-gnu binutils-aarch64-linux-gnu binutils-aarch64-linux-gnu-dbg build-essential +``` + + +## Utility commands + +Convert hex to dec in shell + +```shell +echo $((16#07c6)) +1990 +``` + +## Resources + +* [Go and GDB](https://go.dev/doc/gdb) +* [Go morestack_noctxt](https://blog.leexun.tw/go-runtime-morestack_noctxt) +* [.rodata segment](https://guyonbits.com/from-rodata-to-rwdata-introduction-to-memory-mapping-and-ld-scripts/) +* [mprotect](https://man7.org/linux/man-pages/man2/mprotect.2.html) +* [Makefile Automatic Variables](https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html) \ No newline at end of file diff --git a/c/beforeafter.c b/c/beforeafter.c new file mode 100644 index 0000000..0ca8153 --- /dev/null +++ b/c/beforeafter.c @@ -0,0 +1,11 @@ +#include + +// Before attribute +#define BEFORE_ATR __attribute__((constructor)) +// After attribute +#define AFTER_ATR __attribute__((destructor)) + +void BEFORE_ATR beforeFunc() { printf("Executed before main\n"); } +void AFTER_ATR afterFunc() { printf("Executed after main\n"); } + +int main(void) { printf("Executed in main\n"); } diff --git a/c/cards.c b/c/cards.c new file mode 100644 index 0000000..a0de03d --- /dev/null +++ b/c/cards.c @@ -0,0 +1,63 @@ +#include +#include + +#define ToString(a) #a + +typedef enum Naipe { Ouro, Espada, Pau, Copa } Naipe; + +typedef enum Cara { + Duque, + Terno, + Quadra, + Quina, + Sexta, + Dama, + Valete, + Rei, + Visca, + As +} Cara; + +typedef struct Carta { + Naipe naipe; + Cara cara; + int valor; +} Carta; + +const Carta cartas[] = {{.naipe = Copa, .cara = Duque, .valor = 0}, + {.naipe = Copa, .cara = Terno, .valor = 0}, + {.naipe = Copa, .cara = Quadra, .valor = 0}, + {.naipe = Copa, .cara = Quina, .valor = 0}, + {.naipe = Copa, .cara = Sexta, .valor = 0}, + {.naipe = Copa, .cara = Dama, .valor = 2}, + {.naipe = Copa, .cara = Valete, .valor = 3}, + {.naipe = Copa, .cara = Rei, .valor = 4}, + {.naipe = Copa, .cara = Visca, .valor = 10}, + {.naipe = Copa, .cara = As, .valor = 11}}; + +void printCarta(Carta carta) { + printf("Carta: cara=%s,naipe=%s,valor=%d", ToString(carta.cara), + ToString(carta.naipe), carta.valor); +} + +#define __S(X) #X +#define _S(X) __S(X) + +struct Point { + int x; + int y; +}; + +#define CAGALHAO 1 + +int main(void) { + + struct Point pontos[] = {{1, 2}, {3, 4}}; + + printf("Hello cards\n"); + printCarta(cartas[0]); + printf("Hello bostinha: " _S(1) "\n"); + printf("Cagalhao: " __S(CAGALHAO) "\n"); + printf("Cagalhao: " _S(CAGALHAO) "\n"); + printf("Size array: %d", sizeof(pontos) / sizeof(pontos[0])); +} diff --git a/c/ex1.c b/c/ex1.c new file mode 100644 index 0000000..8f1e5c8 --- /dev/null +++ b/c/ex1.c @@ -0,0 +1,200 @@ +#include + + +/** + * This file aims to help understanding calling conventions made by gcc + * Arch: x86_64 + * + * The conclusions here are only valid for gcc compilers + */ + +/*** + * Calling convention intel x86 + * movl eax, 0 + * + * Calling convention arm arch64 + * empty preamble + */ +int f00() +{ + return 7; +} + +void fl(char* str){ + printf("%s",str); +} + +/*** + * Calling convention intel x86 + * movl eax, 0 + * + * Calling convention arm arch64 + * empty preamble + */ +char f01() +{ + return 0; +} + +/** + * Calling convention intel x86 + * movl edi, a -> f1(edi) + * + * Calling convention arm arch64 + * + * subtract 16 bytes in the stack for arguments + * fetch from sp -12 (12=-16+4) + * + */ + +int f1(int a) +{ + return a + 1; +} + + + +/** + * Calling convention intel x86 + * movl edi, a -> f1(edi) + * + * Calling convention arm arch64 + * + * subtract 16 bytes in the stack for arguments + * fetch from sp -8 (12=-16+8) + * + */ +long f1l(long a) +{ + return a+1; +} + +/** + * Calling convention + * movl esi, b + * movl edi, a + * f(edi,esi) + */ +int f2(int a, int b) +{ + return a + b; +} + +/** + * Calling convention + * movl edx, c + * movl esi, b + * movl edi, a + */ +int f3(int a, int b, int c) +{ + return a + b - c; +} + +/** + * Calling convention + * + * movl ecx, d + * movl edx, c + * movl esi, b + * movl edi, a + */ +int f4(int a, int b, int c, int d) +{ + return (a + b) - (c + d); +} + +/** + * Calling convention + * + * movl r8d, e + * movl ecx, d + * movl edx, c + * movl esi, b + * movl edi, a + */ +int f5(int a, int b, int c, int d, int e) +{ + return (a + b) - (c + d) + e; +} + +/** + * Calling convention + * + * movl r9d, f + * movl r8d, e + * movl ecx, d + * movl edx, c + * movl esi, b + * movl edi, a + */ +int f6(int a, int b, int c, int d, int e, int f) +{ + int sum1 = a + b + c; + int sum2 = d + e + f; + int mult = sum1 * sum2; + return mult; +} + +/** + * Calling convention + * + * push h + * movl r9d, f + * movl r8d, e + * movl ecx, d + * movl edx, c + * movl esi, b + * movl edi, a + * + * Note: After the call we will have + * addq rsp 8 --> Revert the push into stack operation of size long + */ +int f7(int a, int b, int c, int d, int e, int f, int h) +{ + return a + b - c + d - e + f - h; +} + +/** + * Calling convention + * + * push j + * push h + * movl r9d, f + * movl r8d, e + * movl ecx, d + * movl edx, c + * movl esi, b + * movl edi, a + * + * Note: After the call we have + * addq rsp 16 --> Revert the two push into stack operations (by size long) + */ +int f8(int a, int b, int c, int d, int e, int f, int h, int j) +{ + return a + b + c - d - e - f - h + j; +} + +long fl0(long int a) +{ + return a * a; +} + +int main() +{ + fl("Hello Master"); + f00(); + f01(); + f1(1); + f1l(1); + f2(1, 2); + f3(1, 2, 3); + f4(1, 2, 3, 4); + f5(1, 2, 3, 4, 5); + f6(1, 2, 3, 4, 5, 6); + f7(1, 2, 3, 4, 5, 6, 7); + f8(1, 2, 3, 4, 5, 6, 7, 8); + + fl0(10L); + return 0; +} diff --git a/c/ex2.c b/c/ex2.c new file mode 100644 index 0000000..a58e690 --- /dev/null +++ b/c/ex2.c @@ -0,0 +1,57 @@ +#include +#include + +/** + * Return into + * eax + */ +unsigned int f0(unsigned int a){ + return 2*a; +} + +/** + * Return into + * rax + */ +unsigned long f1(unsigned long a){ + return 2*a; +} + +unsigned short f2(unsigned short a){ + return (short)(2*a); +} + +void fp0(int *a){ + *a=2*(*a); +} + +void fp1(long *a){ + *a=2*(*a); +} + +int main(){ + printf("Hello f0: %d\n",f0(1)); + printf("Hello f1: %ld\n",f1(2)); + printf("Hello f2: %d\n",f2(2)); + + int* ip = malloc(sizeof(int)); + long* lp = malloc(sizeof(long)); + + *ip=12; + *lp=21; + + printf("IPointer:\t %p, %d\n",ip, *ip); + printf("LPointer:\t %p, %ld\n",lp, *lp); + + fp0(ip); + fp1(lp); + + printf("IPointer:\t %p, %d\n",ip, *ip); + printf("LPointer:\t %p, %ld\n",lp, *lp); + + fp0(ip); + fp1(lp); + + free(ip); + free(lp); +} diff --git a/c/ex3.c b/c/ex3.c new file mode 100644 index 0000000..70e9242 --- /dev/null +++ b/c/ex3.c @@ -0,0 +1,18 @@ +#include +#include + +int main(int argc,char **argv) +{ + printf("Arg Len: %d\n",argc); + int input = strtol(argv[1],NULL,16); + printf("Secret inserted: %d\n", input); + int secret = 0xcafebabe; + if (secret == input) + { + printf("Great success\n"); + } + else + { + printf("Oh boy...\n"); + } +} \ No newline at end of file diff --git a/c/ex4.c b/c/ex4.c new file mode 100644 index 0000000..3312949 --- /dev/null +++ b/c/ex4.c @@ -0,0 +1,51 @@ +#include + +typedef struct DateOfBirth +{ + int Day; + int Month; + int Year; +} DateOfBirth; + +typedef struct User +{ + char *Name; + char *Location; + DateOfBirth DateOfBirth; +} User; + +void printUser(User user) +{ + printf("Name: %s\n Location: %s, DateOfBirth: %d/%d/%d", + user.Name, + user.Location, + user.DateOfBirth.Day, + user.DateOfBirth.Month, + user.DateOfBirth.Year); +} + +void printUserByRef(User *user) +{ + printf("Name: %s\n Location: %s, DateOfBirth: %d/%d/%d", + user->Name, + user->Location, + user->DateOfBirth.Day, + user->DateOfBirth.Month, + user->DateOfBirth.Year); +} + +int main(int argc, char **argv) +{ + User user = { + .Name = "John Doe", + .Location = "Los Alamos", + .DateOfBirth = { + .Day = 1, + .Month = 12, + .Year = 1990}}; + + printUser(user); + printUserByRef(&user); + + return 0; +} \ No newline at end of file diff --git a/c/ex5.c b/c/ex5.c new file mode 100644 index 0000000..22ff6ff --- /dev/null +++ b/c/ex5.c @@ -0,0 +1,22 @@ +#include +#include +#include + +int main(void) +{ + char *data1 = "HELLO"; + char data2[] = "Hello Master"; + + if (mprotect(data1, strlen(data1) + 1, PROT_WRITE) == 0) + { + data1[0] = (char)'F'; + printf("Data1 is writable: %s, %p, %ld chars\n", data1, data1, strlen(data1)); + printf("Data2 is writable: %s, %p, %ld chars\n", data2, data2, strlen(data2)); + } + else + { + data2[0] = (char)'F'; + printf("Data1 is not writable: %s, %p, %ld chars\n", data1, data1, strlen(data1)); + printf("Data1 is not writable: %s, %p, %ld chars\n", data2, data2, strlen(data2)); + } +} \ No newline at end of file diff --git a/c/ex6.c b/c/ex6.c new file mode 100644 index 0000000..54927b0 --- /dev/null +++ b/c/ex6.c @@ -0,0 +1,28 @@ +#include +#include + +int sum(int *array, int len) +{ + int i = 0; + int *p = array; + int sum = 0; + for (i = 0; i < len; i++) + { + sum += *p; + p = p + 1; + } + return sum; +} + +#define ALPHA_SIZE 10 +char alpha[] = {'A','B','H','C','E','F','0','1','5','6'}; + +int main(int argc, char** argv) +{ + int array[] = {1, 2, 3, 4, 5}; + int s = sum(array, 5); + int mul = atoi(argv[1]); + int ch_p = (s*mul) % ALPHA_SIZE; + + printf("Sum: %d %c\n", s, (int)alpha[ch_p]); +} diff --git a/c/ex7.c b/c/ex7.c new file mode 100644 index 0000000..30293b4 --- /dev/null +++ b/c/ex7.c @@ -0,0 +1,46 @@ +#include +#include + +#define log_2d(pointer) \ + printf("Value: %p, %u, %u\n", pointer, pointer->x, pointer->y) + +#define log_3d(pointer) \ + printf("Value: %p, %u, %u, %u\n", pointer, pointer->x, pointer->y, \ + pointer->z) + +typedef struct Point2D { + int x, y; +} Point2D; + +typedef struct Point3D { + int x, y, z; +} Point3D; + +int main(void) { + printf("Allocation Thinkering: \n"); + Point2D *heap_point_2d = malloc(sizeof(Point2D)); + Point2D *heap_point_2d_2 = malloc(sizeof(Point2D)); + Point3D *heap_point_3d; + log_2d(heap_point_2d); + *heap_point_2d = (Point2D){1, 2}; + *heap_point_2d_2 = (Point2D){3, 4}; + heap_point_3d = (Point3D *)(heap_point_2d); + log_3d(heap_point_3d); + log_2d(heap_point_2d); + log_2d(heap_point_2d_2); + free(heap_point_2d); + log_2d(heap_point_2d); + log_2d(heap_point_2d_2); + log_3d(heap_point_3d); + heap_point_2d = malloc(sizeof(Point2D)); + *heap_point_2d = (Point2D){5, 6}; + log_2d(heap_point_2d); + log_2d(heap_point_2d_2); + log_3d(heap_point_3d); + free(heap_point_2d_2); + log_2d(heap_point_2d); + log_2d(heap_point_2d_2); + log_3d(heap_point_3d); + + exit(0); +} diff --git a/c/forkhandler.c b/c/forkhandler.c new file mode 100644 index 0000000..44c1931 --- /dev/null +++ b/c/forkhandler.c @@ -0,0 +1,22 @@ +#include +#include +#include + +void prepare() { printf("preparing for fork...\n"); } + +void parent() { printf("in parent after fork...\n"); } + +void child() { printf("in child after fork...\n"); } + +int main() { + pthread_atfork(prepare, parent, child); + pid_t pid = fork(); + if (pid == 0) { + // in child process + printf("I am the child!\n"); + } else { + // in parent process + printf("I am the parent!\n"); + } + return 0; +} diff --git a/c/pthreadattrdestroy.c b/c/pthreadattrdestroy.c new file mode 100644 index 0000000..ff854fa --- /dev/null +++ b/c/pthreadattrdestroy.c @@ -0,0 +1,10 @@ +#include +#include + +int main() { + pthread_attr_t attr; + pthread_attr_init(&attr); + // use the attribute object... + pthread_attr_destroy(&attr); + return 0; +} diff --git a/c/pthreadex1.c b/c/pthreadex1.c new file mode 100644 index 0000000..c73bdfc --- /dev/null +++ b/c/pthreadex1.c @@ -0,0 +1,21 @@ +#include +#include + +void prepare() { printf("preparing for fork...\n"); } + +void parent() { printf("in parent after fork...\n"); } + +void child() { printf("in child after fork...\n"); } + +int main() { + __register_atfork(prepare, parent, child); + pid_t pid = fork(); + if (pid == 0) { + // in child process + printf("I am the child!\n"); + } else { + // in parent process + printf("I am the parent!\n"); + } + return 0; +} diff --git a/c/pthreadex2.c b/c/pthreadex2.c new file mode 100644 index 0000000..e81bcd1 --- /dev/null +++ b/c/pthreadex2.c @@ -0,0 +1,18 @@ +#include +#include + +void *thread_func(void *data) { + printf("Hello from the new thread!\n"); + return NULL; +} + +int main() { + pthread_t thread; + int result = pthread_create(&thread, NULL, thread_func, NULL); + if (result != 0) { + printf("Error creating thread: %d\n", result); + return 1; + } + printf("Hello from the main thread!\n"); + return 0; +} diff --git a/c/pthreadex3.c b/c/pthreadex3.c new file mode 100644 index 0000000..05483d2 --- /dev/null +++ b/c/pthreadex3.c @@ -0,0 +1,34 @@ +#include +#include +#include + +typedef struct coco { + char *name; +} Coco; + +char *COCO_NAME = "COCO"; + +void *thread_func(void *data) { + printf("Hello from the new thread!\n"); + Coco *c = malloc(sizeof(Coco)); + c->name = COCO_NAME; + return c; +} + +int main() { + pthread_t thread; + void *ret; + int result = pthread_create(&thread, NULL, thread_func, NULL); + if (result != 0) { + printf("Error creating thread: %d\n", result); + return 1; + } + result = pthread_join(thread, &ret); + if (result != 0) { + printf("Error joining thread: %d\n", result); + return 1; + } + printf("Hello from the main thread!\n"); + printf("Return Value: %s", ((Coco *)(ret))->name); + return 0; +} diff --git a/c/pthreadex4.c b/c/pthreadex4.c new file mode 100644 index 0000000..7c17efe --- /dev/null +++ b/c/pthreadex4.c @@ -0,0 +1,29 @@ +#include +#include +#include + +void *thread_func(void *data) { + sleep(1); + printf("Hello from the new thread!\n"); + return NULL; +} + +int main() { + pthread_t thread; + int result = pthread_create(&thread, NULL, thread_func, NULL); + if (result != 0) { + printf("Error creating thread: %d\n", result); + return 1; + } + while (1) { + result = pthread_tryjoin_np(thread, NULL); + if (result == 0) { + // thread has finished + break; + } + printf("Waiting for thread to finish...\n"); + sleep(1); + } + printf("Hello from the main thread!\n"); + return 0; +} diff --git a/c/pthreadex5.c b/c/pthreadex5.c new file mode 100644 index 0000000..6dfd7eb --- /dev/null +++ b/c/pthreadex5.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include + +void *thread_func(void *arg) { + // Sleep for a random amount of time between 1 and 5 seconds + int sleep_time = 1 + rand() % 5; + sleep(sleep_time); + + return arg; +} + +int main() { + pthread_t thread; + int thread_arg = 100; + + // Create the thread + pthread_create(&thread, NULL, thread_func, &thread_arg); + + // Set up a timeout for the pthread_timedjoin_np function + struct timespec timeout; + clock_gettime(CLOCK_REALTIME, &timeout); + timeout.tv_sec += 3; // Wait for at most 3 seconds + + void *result; + int ret; + + // Wait for the thread to finish, with a timeout + ret = pthread_timedjoin_np(thread, &result, &timeout); + if (ret == 0) { + printf("Thread finished within the timeout\n"); + } else if (ret == ETIMEDOUT) { + printf("Timed out waiting for thread to finish\n"); + } else { + printf("Error waiting for thread to finish %d\n", ret); + } + + return 0; +} diff --git a/c/pthreadex6.c b/c/pthreadex6.c new file mode 100644 index 0000000..960419c --- /dev/null +++ b/c/pthreadex6.c @@ -0,0 +1,40 @@ +#include +#include + +void *thread_func(void *arg) { + pthread_t thread_id = pthread_self(); + printf("Inside thread_func, thread ID = %ld\n", thread_id); + + // Return the thread ID as the thread's return value + return (void *)thread_id; +} + +int main() { + pthread_t thread1, thread2; + int thread1_arg = 10, thread2_arg = 20; + + // Create two threads + pthread_create(&thread1, NULL, thread_func, &thread1_arg); + pthread_create(&thread2, NULL, thread_func, &thread2_arg); + + void *thread1_result, *thread2_result; + + // Wait for the threads to finish + pthread_join(thread1, &thread1_result); + pthread_join(thread2, &thread2_result); + + // Compare the thread IDs + if (pthread_equal(thread1, thread2)) { + printf("Thread IDs are equal\n"); + } else { + printf("Thread IDs are not equal\n"); + } + + // Print the thread IDs and return values + printf("Thread 1 ID = %ld, return value = %ld\n", thread1, + (long)thread1_result); + printf("Thread 2 ID = %ld, return value = %ld\n", thread2, + (long)thread2_result); + + return 0; +} diff --git a/c/sha3.c b/c/sha3.c new file mode 100644 index 0000000..3d81566 --- /dev/null +++ b/c/sha3.c @@ -0,0 +1,176 @@ +#include +#include +#include + +#define DIGEST 32 // 256-bit digest in bytes. + +#define ROUNDS 24 // Number of KECCAK rounds to perform for SHA3-256. +#define WIDTH 200 // 1600-bit width in bytes. +#define LANES 25 // The state is an unrolled 5x5 array of 64-bit lanes. +#define RATE 136 // 1600-bit width - 512-bit capacity in bytes. + +#define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y)))) + +typedef unsigned long long uint64_t; +typedef unsigned char uint8_t; + +union state { + uint64_t words[LANES]; + uint8_t bytes[WIDTH]; +}; + +static void theta(void *arg) { + union state *state = (union state*) arg; + uint64_t C[5] = { 0 }; + uint64_t D[5] = { 0 }; + for (int i = 0; i < 5; i++) { + C[i] = state->words[i]; + C[i] ^= state->words[i + 5]; + C[i] ^= state->words[i + 10]; + C[i] ^= state->words[i + 15]; + C[i] ^= state->words[i + 20]; + } + for (int i = 0; i < 5; i++) { + D[i] = C[(i + 4) % 5] ^ ROTL64(C[(i + 1) % 5], 1); + } + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 25; j += 5) { + state->words[i + j] ^= D[i]; + } + } +} + +static void rho(void *arg) { + union state *state = (union state*) arg; + const int rotations[25] = { + 0, 1, 62, 28, 27, + 36, 44, 6, 55, 20, + 3, 10, 43, 25, 39, + 41, 45, 15, 21, 8, + 18, 2, 61, 56, 14 + }; + for (int i = 0; i < 25; i++) { + state->words[i] = ROTL64(state->words[i], rotations[i]); + } +} + +static void pi(void *arg) { + union state *state = (union state*) arg; + const int switcheroo[25] = { + 0, 10, 20, 5, 15, + 16, 1, 11, 21, 6, + 7, 17, 2, 12, 22, + 23, 8, 18, 3, 13, + 14, 24, 9, 19, 4 + }; + uint64_t temp[25] = { 0 }; + for (int i = 0; i < 25; i++) { + temp[i] = state->words[i]; + } + for (int i = 0; i < 25; i++) { + state->words[switcheroo[i]] = temp[i]; + } +} + +static void chi(void *arg) { + union state *state = (union state*) arg; + uint64_t temp[5] = { 0 }; + for (int j = 0; j < 25; j += 5) { + for (int i = 0; i < 5; i++) { + temp[i] = state->words[i + j]; + } + for (int i = 0; i < 5; i++) { + state->words[i + j] ^= (~temp[(i + 1) % 5]) & temp[(i + 2) % 5]; + } + } +} + +static void iota(const uint8_t round, void *arg) { + union state *state = (union state*) arg; + const uint64_t constants[24] = { + 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, + 0x8000000080008000, 0x000000000000808b, 0x0000000080000001, + 0x8000000080008081, 0x8000000000008009, 0x000000000000008a, + 0x0000000000000088, 0x0000000080008009, 0x000000008000000a, + 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, + 0x8000000000008003, 0x8000000000008002, 0x8000000000000080, + 0x000000000000800a, 0x800000008000000a, 0x8000000080008081, + 0x8000000000008080, 0x0000000080000001, 0x8000000080008008 + }; + state->words[0] ^= constants[round]; +} + +static void keccak(void *arg) { + union state *state = (union state*) arg; + for (int round = 0; round < ROUNDS; round++) { + theta(state); rho(state); pi(state); chi(state); iota(round,state); + } +} + +static int absorb(const uint64_t len, const uint8_t data[len], int absorbed, void *arg) { + union state *state = (union state*) arg; + for (uint64_t i = 0; i < len; i++) { + state->bytes[absorbed++] ^= data[i]; + if (absorbed == RATE) { + keccak(state); + absorbed = 0; + } + } + return absorbed; +} + +static void squeeze(uint8_t digest[DIGEST], int padpoint, void *arg) { + union state *state = (union state*) arg; + state->bytes[padpoint] ^= 0x06; + state->bytes[RATE - 1] ^= 0x80; + keccak(state); + for (int i = 0; i < DIGEST; i++) { + digest[i] = state->bytes[i]; + } +} + +void sha3_hash(uint8_t digest[DIGEST], const uint64_t len, const uint8_t data[len]) { + union state *state = malloc(sizeof(union state)); + memset(state,'\0',sizeof(union state)); // absolutely necessary + int absorbed = 0; + int padpoint = absorb(len, data, absorbed, state); + squeeze(digest, padpoint, state); + free(state); state = NULL; +} + +void printDig(uint8_t* cPtr, uint64_t len){ + uint64_t i=0; + uint8_t *p=cPtr; + for (i=0;i +#include +#include +#include +#include + +#define SECRET "MySecretThought" + +int main(int argc, char **argv) { + + if (argc > 1) { + printf("Your secret: %s\n", argv[1]); + int r = strcmp(argv[1], SECRET); + + if (r != 0) { + printf("Secret is invalid\n"); + } else { + printf("Yay secret is valid\n"); + } + } else { + printf("You need to provide a secret as an argument\n"); + } + + int fd = socket(AF_INET, SOCK_DGRAM, 0); + return fd; +} diff --git a/cpp/ex1.cpp b/cpp/ex1.cpp new file mode 100644 index 0000000..9b6efa3 --- /dev/null +++ b/cpp/ex1.cpp @@ -0,0 +1,8 @@ +#include +#include + +int main(){ + std::cout << "Quem es tu miuda" << std::endl; + std::cout << "Hello world" << std::endl; + return 0; +} diff --git a/cpp/ex2.cpp b/cpp/ex2.cpp new file mode 100644 index 0000000..90aba6a --- /dev/null +++ b/cpp/ex2.cpp @@ -0,0 +1,21 @@ +#include + +namespace World{ + namespace Country{ + class Citizen { + private: + int number; + public: + Citizen(int _number): number(_number){}; + int getNumber(){ + return this->number; + }; + }; + } +} + +int main(void){ + World::Country::Citizen john = World::Country::Citizen(12); + std::cout << john.getNumber() << std::endl; + +} \ No newline at end of file diff --git a/defs/makefile.def b/defs/makefile.def new file mode 100644 index 0000000..6e22e91 --- /dev/null +++ b/defs/makefile.def @@ -0,0 +1,26 @@ +CFLAG_DEBUG_STATIC=-Wall -g -static +CFLAG_DEBUG=-Wall -g +CFLAG_RELEASE=-Wall -O3 -static +CFLAG_LIB=-c -Wall -Werror -fpic +CC=gcc +CPP=g++ +CC_ARM=aarch64-linux-gnu-gcc +ARM_CFLAG=-Wall -g -static +ASM_C_FLAGS=-S #-masm=intel +ASM_ARM_C_FLAGS=-S +GO=go +BIN_DIR=bin +LIB_DIR=lib +GO_FLAGS=-gcflags=all="-N -l" +ARCH_X86=x86 +ARCH_ARM=aarch64 +GO_LANG_DIR=golang +CPP_LANG_DIR=cpp +C_LANG_DIR=c +ASM_ARM_OUT=asm/arm/ +ASM_X86_OUT=asm/x86/ +SO_DIR=so + +CGO_CFGLAGS_DYN="-I./golang/" +CGO_LDFLAGS_DYN="-L./${LIB_DIR}" + diff --git a/golang/calls.go b/golang/calls.go new file mode 100644 index 0000000..a0c2cf3 --- /dev/null +++ b/golang/calls.go @@ -0,0 +1,91 @@ +package main + +import "fmt" + +func nothing() { + +} + +func voidS8Bit() int8 { + return 77 +} + +func voidS16Bit() int16 { + return 100 +} + +func voidS32Bit() int32 { + return 200 +} + +func voidS64Bit() int64 { + return 300 +} + +func voidU8Bit() uint8 { + return 77 +} + +func voidU16Bit() uint16 { + return 100 +} + +func voidU32Bit() uint32 { + return 200 +} + +func voidU64Bit() uint64 { + return 300 +} + +func voidS8Pair() (int8, int8) { + return -100, 100 +} + +func voidS16Pair() (int16, int16) { + return -1000, 1000 +} + +func neg8(a int8) int8 { + return -a +} + +func neg16(a int16) int16 { + return -a +} + +func sum16(a int16, b int16) int32 { + return (int32)(a + b) +} + +func main() { + nothing() + + // Signed integers + s8 := voidS8Bit() + s16 := voidS16Bit() + s32 := voidS32Bit() + s64 := voidS64Bit() + + fmt.Println(s8, s16, s32, s64) + + //Unsigned integers + voidU8Bit() + voidU16Bit() + voidU32Bit() + voidU64Bit() + + //One argument function + a8 := neg8(1) + b8 := neg8(-1) + + //Return pairs + a8, b8 = voidS8Pair() + neg8(a8) + neg8(b8) + + a16, b16 := voidS16Pair() + neg16(a16) + neg16(b16) + sum16(a16, b16) +} diff --git a/golang/findme.go b/golang/findme.go new file mode 100644 index 0000000..6e12bf2 --- /dev/null +++ b/golang/findme.go @@ -0,0 +1,15 @@ +package main + +import ( + "fmt" + "os" +) + +func main() { + if len(os.Args) < 2 { + fmt.Println("Invalid arguments number") + os.Exit(-1) + } + + fmt.Println("Your secret is: ", os.Args[1]) +} diff --git a/golang/hello.go b/golang/hello.go new file mode 100644 index 0000000..ad3c971 --- /dev/null +++ b/golang/hello.go @@ -0,0 +1,8 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello World") + fmt.Println("Holla!") +} diff --git a/golang/structs.go b/golang/structs.go new file mode 100644 index 0000000..9dee220 --- /dev/null +++ b/golang/structs.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" +) + +type DateOfBirth struct { + Year uint32 + Month uint8 + Day uint8 +} + +type Lord struct { + Name string + Sex string +} + +type God struct { + Name string + Birth DateOfBirth +} + +func main() { + + fmt.Println("Hey") + + name := "Jizzus" + + fmt.Println(name) + + dob := DateOfBirth{ + Year: 0, + Month: 12, + Day: 25, + } + + lord := Lord{ + Name: "Jizz", + Sex: "Lots of them", + } + + fmt.Println(dob) + + fmt.Println(lord) + + jesus := God{ + Name: name, + Birth: dob, + } + + fmt.Println(jesus) +} diff --git a/golangc/silly.c b/golangc/silly.c new file mode 100644 index 0000000..5e4a5f0 --- /dev/null +++ b/golangc/silly.c @@ -0,0 +1,18 @@ +#include "silly.h" + +const char* SILLY_AUTHOR="Silly Author "; + + +void add(PSilly silly_one,PSilly silly_two){ + silly_one->age+=silly_two->legs; + silly_one->legs+=silly_two->age; +} + +void sub(PSilly silly_one, PSilly silly_two){ + silly_one->age-=silly_two->legs; + silly_one->legs-=silly_two->age; +} + +void print(PSilly silly){ + printf("Silly:\t age:%d\t%d\n",silly->age,silly->legs); +} diff --git a/golangc/silly.go b/golangc/silly.go new file mode 100644 index 0000000..7062c01 --- /dev/null +++ b/golangc/silly.go @@ -0,0 +1,23 @@ +package main + +/* +#cgo LDFLAGS: -lsilly +#include +*/ +import "C" + +import "fmt" + +/** +* Use LD_LIBRARY_PATH=. to inject the shared object. + */ +func main() { + silly_one := C.struct_Silly{age: 12, legs: 21} + silly_two := C.struct_Silly{age: 2, legs: 4} + C.print(&silly_one) + C.print(&silly_two) + C.add(&silly_one, &silly_two) + C.print(&silly_one) + C.print(&silly_two) + fmt.Println("Silly version %d, author: %s", C.SILLY_VERSION, C.SILLY_AUTHOR) +} diff --git a/golangc/silly.h b/golangc/silly.h new file mode 100644 index 0000000..a73e81a --- /dev/null +++ b/golangc/silly.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +const unsigned int SILLY_VERSION=0x7; +extern const char* SILLY_AUTHOR; + +typedef struct Silly{ + int age; + int legs; +} Silly; + +typedef Silly* PSilly; + +void add(PSilly silly_one,PSilly silly_two); +void sub(PSilly silly_one,PSilly silly_two); +void print(PSilly silly); diff --git a/java/Test.java b/java/Test.java new file mode 100644 index 0000000..f07376f --- /dev/null +++ b/java/Test.java @@ -0,0 +1,6 @@ +class Test{ + + public static void main(String[] args){ + System.out.println("Hello world"); + } +} diff --git a/so/hook.c b/so/hook.c new file mode 100644 index 0000000..6184404 --- /dev/null +++ b/so/hook.c @@ -0,0 +1,30 @@ +#define _GNU_SOURCE +#include +#include +#include +#include + +int (*original_socket)(int, int, int) = NULL; +int (*original_strcmp)(const char *arg1, const char *arg2) = NULL; + +/** + * Hook into socket function, replace the original with this hook + */ +int socket(int domain, int type, int protocol) { + original_socket = dlsym(RTLD_NEXT, "socket"); + if (original_socket == NULL) { + printf("Could not hook into socket"); + return -1; + } + printf("Socket function hooked"); + original_socket(domain, type, protocol); + return 0; +} + +/** + * Hook into strcmp function + */ +int strcmp(const char *__s1, const char *__s2) { + printf("strcmp hooked\n"); + return 0; +}