Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3ce3af7
Add Makefile with NASM build system and 64KB size check
Gaotax2006 May 21, 2026
4ef3a78
Add Linux x86_64 syscall number constants
Gaotax2006 May 21, 2026
d4f0d8c
Add constants include file
Gaotax2006 May 21, 2026
341bf05
Add common NASM macros
Gaotax2006 May 21, 2026
3cc945a
Add errno constants
Gaotax2006 May 21, 2026
e6bed60
Add Linux syscall wrappers
Gaotax2006 May 21, 2026
4d7414a
Add string operation primitives
Gaotax2006 May 21, 2026
c20abff
Add ring buffer and memory management
Gaotax2006 May 21, 2026
a6ad093
Add program entry point with CLI argument parsing
Gaotax2006 May 21, 2026
7019437
Add CLI argument parser
Gaotax2006 May 21, 2026
58f77fe
Add database dump pipeline module
Gaotax2006 May 21, 2026
9d40ed2
Add SHA-256 cryptographic hash implementation
Gaotax2006 May 21, 2026
3f4500d
Add test runner script
Gaotax2006 May 21, 2026
edbd3b9
Add calling convention documentation
Gaotax2006 May 21, 2026
d6eae7e
Add AES-NI accelerated AES-256 encryption
Gaotax2006 May 21, 2026
2c54251
Add TCP socket operations
Gaotax2006 May 21, 2026
ce97b43
Add minimal HTTP/1.1 client
Gaotax2006 May 21, 2026
7904d8d
Add LZ4 block compression
Gaotax2006 May 21, 2026
862b358
Add HMAC-SHA256 implementation
Gaotax2006 May 21, 2026
3a10b4c
Add S3 multipart upload module
Gaotax2006 May 21, 2026
5e4eadc
Add Base64 encoding/decoding
Gaotax2006 May 21, 2026
4126d9b
Add ISO 8601 time formatting
Gaotax2006 May 21, 2026
ad25fb5
Add AES-GCM encryption pipeline
Gaotax2006 May 21, 2026
2d75f79
Add GHASH for AES-GCM (PCLMULQDQ)
Gaotax2006 May 21, 2026
5d5b971
Add stream chunker module
Gaotax2006 May 21, 2026
ef87a9f
Add DNS resolver
Gaotax2006 May 21, 2026
573639d
Add JSON emitter
Gaotax2006 May 21, 2026
51fdacc
Add structured logger
Gaotax2006 May 21, 2026
34c1d4a
Add backup verification module
Gaotax2006 May 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions coolify-backup-asm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
NASM = nasm
NASMFLAGS = -f elf64 -g -F dwarf
LD = ld
LDFLAGS = -nostdlib -static --no-dynamic-linker -z noexecstack

SRCS = $(wildcard src/*.asm src/**/*.asm)
OBJS = $(SRCS:.asm=.o)

coolify-backup-asm: $(OBJS)
$(LD) $(LDFLAGS) -o $@ $^
@size=$$(stat -c%s $@); \
if [ $$size -gt 65536 ]; then \
echo "ERROR: Binary size $$size exceeds 64KB limit"; \
rm $@; \
exit 1; \
fi
@echo "Binary size: $$(stat -c%s $@) bytes (limit: 65536)"

%.o: %.asm
$(NASM) $(NASMFLAGS) -o $@ $<

test: coolify-backup-asm
@bash tests/test_runner.sh

clean:
rm -f $(OBJS) coolify-backup-asm

.PHONY: test clean
37 changes: 37 additions & 0 deletions coolify-backup-asm/docs/CALLING_CONVENTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Calling Convention

coolify-backup-asm uses a custom register convention (NOT System V AMD64 ABI).

## Register Allocation

| Register | Purpose | Preserved? |
|----------|---------|------------|
| rax | Return value / syscall number | No |
| rbx | Ring buffer base pointer | Yes |
| rcx | Ring buffer write position | Yes |
| rdx | Ring buffer read position | Yes |
| rsi | Source pointer (input) | No |
| rdi | Destination pointer (output) | No |
| r8 | Byte count / length | No |
| r9 | Flags / options | No |
| r10 | Scratch register 1 | No |
| r11 | Scratch register 2 | No |
| r12 | Pipeline stage context ptr | Yes |
| r13 | Error code accumulator | Yes |
| r14 | File descriptor for I/O | Yes |
| r15 | Timestamp / counter | Yes |
| rbp | Frame pointer | Yes |
| rsp | Stack pointer (16-byte aligned) | Yes |

## Function Requirements

1. Every function must preserve rbx, r12-r15, rbp (callee-saved)
2. Return error code in r13 (0 = success, negative = errno)
3. Document register inputs/outputs in comment header block
4. Use ALIGN 16 before every function entry point

## Stack Frame

- rbp points to base of stack frame
- Allocate 32-byte shadow space at function entry
- Restore rbp and rsp on return
57 changes: 57 additions & 0 deletions coolify-backup-asm/include/constants.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
; Buffer sizes and magic constants
%define RING_BUFFER_SIZE 65536
%define CONTROL_SIZE 64
%define CHUNK_SIZE 8388608 ; 8MB
%define MAX_ARGS 64
%define MAX_ARG_LEN 4096
%define HASH_TABLE_SIZE 4096 ; LZ4 hash table entries
%define MAX_RETRIES 3
%define BACKOFF_1S 1000000000
%define BACKOFF_2S 2000000000
%define BACKOFF_4S 4000000000
%define TIMEOUT_DEFAULT 3600 ; 1 hour
%define S3_CHUNK_SIZE 8388608 ; 8MB S3 parts
%define MAX_STACK_SIZE 65536 ; 64KB stack

; Exit codes
%define EXIT_SUCCESS 0
%define EXIT_FAILURE 1
%define EXIT_USAGE 3
%define EXIT_SIGTERM 143

; Ring buffer control offsets
%define CTRL_WRITE_POS 0
%define CTRL_READ_POS 8
%define CTRL_WATERMARK 16
%define CTRL_FLAGS 24
%define CTRL_BYTES_TOTAL 32
%define CTRL_STAGE_ID 40

; Ring buffer flags
%define RB_EOF 1
%define RB_ERROR 2
%define RB_FLUSH 4

; File open flags
%define O_RDONLY 0
%define O_WRONLY 1
%define O_RDWR 2
%define O_CREAT 0100
%define O_TRUNC 01000
%define O_APPEND 02000
%define O_CLOEXEC 02000000

; Protection flags for mmap
%define PROT_READ 1
%define PROT_WRITE 2
%define PROT_EXEC 4
%define MAP_PRIVATE 2
%define MAP_ANONYMOUS 32

; Signal numbers
%define SIGCHLD 17
%define SIGTERM 15
%define SIGALRM 14
%define SIGPIPE 13
%define SIGUSR1 10
%define SIGKILL 9
134 changes: 134 additions & 0 deletions coolify-backup-asm/include/errno.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
; Linux errno constants
%define EPERM 1
%define ENOENT 2
%define ESRCH 3
%define EINTR 4
%define EIO 5
%define ENXIO 6
%define E2BIG 7
%define ENOEXEC 8
%define EBADF 9
%define ECHILD 10
%define EAGAIN 11
%define ENOMEM 12
%define EACCES 13
%define EFAULT 14
%define ENOTBLK 15
%define EBUSY 16
%define EEXIST 17
%define EXDEV 18
%define ENODEV 19
%define ENOTDIR 20
%define EISDIR 21
%define EINVAL 22
%define ENFILE 23
%define EMFILE 24
%define ENOTTY 25
%define ETXTBSY 26
%define EFBIG 27
%define ENOSPC 28
%define ESPIPE 29
%define EROFS 30
%define EMLINK 31
%define EPIPE 32
%define EDOM 33
%define ERANGE 34
%define EDEADLK 35
%define ENAMETOOLONG 36
%define ENOLCK 37
%define ENOSYS 38
%define ENOTEMPTY 39
%define ELOOP 40
%define EWOULDBLOCK EAGAIN
%define ENOMSG 42
%define EIDRM 43
%define ECHRNG 44
%define EL2NSYNC 45
%define EL3HLT 46
%define EL3RST 47
%define ELNRNG 48
%define EUNATCH 49
%define ENOCSI 50
%define EL2HLT 51
%define EBADE 52
%define EBADR 53
%define EXFULL 54
%define ENOANO 55
%define EBADRQC 56
%define EBADSLT 57
%define EDEADLOCK EDEADLK
%define EBFONT 59
%define ENOSTR 60
%define ENODATA 61
%define ETIME 62
%define ENOSR 63
%define ENONET 64
%define ENOPKG 65
%define EREMOTE 66
%define ENOLINK 67
%define EADV 68
%define ESRMNT 69
%define ECOMM 70
%define EPROTO 71
%define EMULTIHOP 72
%define EDOTDOT 73
%define EBADMSG 74
%define EOVERFLOW 75
%define ENOTUNIQ 76
%define EBADFD 77
%define EREMCHG 78
%define ELIBACC 79
%define ELIBBAD 80
%define ELIBSCN 81
%define ELIBMAX 82
%define ELIBEXEC 83
%define EILSEQ 84
%define ERESTART 85
%define ESTRPIPE 86
%define EUSERS 87
%define ENOTSOCK 88
%define EDESTADDRREQ 89
%define EMSGSIZE 90
%define EPROTOTYPE 91
%define ENOPROTOOPT 92
%define EPROTONOSUPPORT 93
%define ESOCKTNOSUPPORT 94
%define EOPNOTSUPP 95
%define EPFNOSUPPORT 96
%define EAFNOSUPPORT 97
%define EADDRINUSE 98
%define EADDRNOTAVAIL 99
%define ENETDOWN 100
%define ENETUNREACH 101
%define ENETRESET 102
%define ECONNABORTED 103
%define ECONNRESET 104
%define ENOBUFS 105
%define EISCONN 106
%define ENOTCONN 107
%define ESHUTDOWN 108
%define ETOOMANYREFS 109
%define ETIMEDOUT 110
%define ECONNREFUSED 111
%define EHOSTDOWN 112
%define EHOSTUNREACH 113
%define EALREADY 114
%define EINPROGRESS 115
%define ESTALE 116
%define EUCLEAN 117
%define ENOTNAM 118
%define ENAVAIL 119
%define EISNAM 120
%define EREMOTEIO 121
%define EDQUOT 122
%define ENOMEDIUM 123
%define EMEDIUMTYPE 124
%define ECANCELED 125
%define ENOKEY 126
%define EKEYEXPIRED 127
%define EKEYREVOKED 128
%define EKEYREJECTED 129
%define EOWNERDEAD 130
%define ENOTRECOVERABLE 131
%define ERFKILL 132
%define EHWPOISON 133
95 changes: 95 additions & 0 deletions coolify-backup-asm/include/macros.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
; Common macros for coolify-backup-asm

; ALIGN - align to 16-byte boundary
%macro ALIGN 1
times ((%1 - ($$ - $) % %1) % %1) nop
%endmacro

; SYSCALL - invoke Linux syscall with up to 6 arguments
; Usage: SYSCALL number, arg1, arg2, arg3, arg4, arg5, arg6
%macro SYSCALL 1-7
mov rax, %1
%if %0 > 1
mov rdi, %2
%endif
%if %0 > 2
mov rsi, %3
%endif
%if %0 > 3
mov rdx, %4
%endif
%if %0 > 4
mov r10, %5
%endif
%if %0 > 5
mov r8, %6
%endif
%if %0 > 6
mov r9, %7
%endif
syscall
%endmacro

; ASSERT - abort if condition false (debug build only)
%macro ASSERT 1
%ifdef DEBUG
test %1, %1
jnz %%ok
SYSCALL SYS_EXIT, 99
%%ok:
%endif
%endmacro

; LOG - write string to stderr
%macro LOG 1
jmp %%skip
%%str: db %1, 10
%%skip:
push rcx
push r11
SYSCALL SYS_WRITE, 2, %%str, %%skip - %%str
pop r11
pop rcx
%endmacro

; LOGF - write formatted log (register value in hex)
%macro LOGF 2
push rdi
push rsi
mov rdi, %2
call hex_to_str
lea rsi, [rbp - 32]
sub rsp, 16
mov qword [rsp], %1
mov qword [rsp + 8], 32
SYSCALL SYS_WRITE, 2, rsp, 16
add rsp, 16
pop rsi
pop rdi
%endmacro

; PUSH_REGS - save caller-saved registers
%macro PUSH_REGS 0
push rax
push rcx
push rdx
push rsi
push rdi
push r8
push r9
push r10
push r11
%endmacro

; POP_REGS - restore caller-saved registers
%macro POP_REGS 0
pop r11
pop r10
pop r9
pop r8
pop rdi
pop rsi
pop rdx
pop rcx
pop rax
%endmacro
Loading