Skip to content

Commit f3689e6

Browse files
committedSep 21, 2024
add stack first version (without tests)
1 parent e757bf2 commit f3689e6

File tree

10 files changed

+247
-81
lines changed

10 files changed

+247
-81
lines changed
 

‎markov_strings/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CC=g++
22
INCLUDE_DIR = include
33
# CFLAGS = -lm -I $(INCLUDE_DIR)
4-
CFLAGS=-Wall -Wextra -Wpedantic -I $(INCLUDE_DIR) -fpermissive -DDEBUG
4+
CFLAGS=-Wall -Wextra -Wpedantic -I $(INCLUDE_DIR) -fpermissive -O2 -DDEBUG
55
#-fsanitize=address -Og -DDEBUG -ggdb -D_FORTIFY_SOURCE=2 -lgtest -fpermissive -lstdc++
66
TEST_CFLAGS=-Wall -Wextra -Wpedantic -lm -I $(INCLUDE_DIR)
77
LDFLAGS=$(CFLAGS)

‎markov_strings/src/sort.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@
1010

1111
const char* special_symbols = " \n[](){}!?.,:;/*&^%$_+-–—=<>@|~";
1212

13+
inline void pass_special_symbols(char **a, char**b, int step){
14+
while (strchr(special_symbols, **a) && **a) *a += step;
15+
while (strchr(special_symbols, **b) && **b) *b += step;
16+
}
17+
1318
int forward_string_comparator(const void* arg1, const void* arg2){
1419
const char* a = (*(string*)arg1).start;
1520
const char* b = (*(string*)arg2).start;
1621

17-
while (strchr(special_symbols, *a) && *a) a++;
18-
while (strchr(special_symbols, *b) && *b) b++;
22+
pass_special_symbols(&a, &b, +1);
1923

2024
while (*a && (*a++ == *b++)){
21-
while (strchr(special_symbols, *a) && *a) a++;
22-
while (strchr(special_symbols, *b) && *b) b++;
25+
pass_special_symbols(&a, &b, +1);
2326
}
2427

2528
return *(const unsigned char*)(--a) - *(const unsigned char*)(--b);
@@ -29,12 +32,10 @@ int back_string_comparator(const void* arg1, const void* arg2){
2932
const char* a = (*(string*)arg1).end;
3033
const char* b = (*(string*)arg2).end;
3134

32-
while (strchr(special_symbols, *a) && *a) a--;
33-
while (strchr(special_symbols, *b) && *b) b--;
35+
pass_special_symbols(&a, &b, -1);
3436

3537
while (*a && (*a-- == *b--)){
36-
while (strchr(special_symbols, *a) && *a) a--;
37-
while (strchr(special_symbols, *b) && *b) b--;
38+
pass_special_symbols(&a, &b, -1);
3839
}
3940

4041
return *(const unsigned char*)(++a) - *(const unsigned char*)(++b);

‎markov_strings/tests/output.txt

+72-72
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,92 @@
1-
Weaving thousands on bag LV
2-
Weaving thousands on bag LV
3-
Weaving thousands on bag LV
4-
Ha!
5-
Your boyfriend will wipe it off, he's a rag (Ha ha ha)
6-
See, a moth suits me (haha)
7-
Paw-paw, hit the carcass (ha)
8-
My guards have guards (La la la)
9-
(Let's go, la la la)
1+
Adlib is fresher than the whole music business (blee-blee)
2+
Ay, bitch, we got some guns (pr-r, paw)
3+
A year later, for the third year now, well, yeah (Yeah, yeah)
104
A young samurai with a transparent umbrella
11-
Hey, the chain is on me, a rash of lava
12-
Hey, the chain is on me, a rash of lava
13-
Hey, the chain is on me, a rash of lava
5+
Bitch, I'm an aristocratic daddy
6+
Bitch, I'm a young aristocrat (I'm young)
7+
Bitch, I'm a young aristocrat (I'm young)
8+
Bitch, I'm hanging like a young Pushkin (y)
149
Bitch, I'm wearing hundred-carat stones (And you're dumb)
1510
Bitch, I'm wearing hundred-carat stones (And you're dumb)
16-
To my sports car at the club
17-
This is a new Cadillac
18-
This is the new Cadillac
19-
This is the new Cadillac
20-
The lookbook spread
11+
Bitch, I am a young aristocrat (Aristocrat)
12+
Bitch, I am a young aristocrat (Aristocrat)
13+
Bitch, I have a hundred carat stones on me (Expensive, fucked up)
14+
Bitch, I have hundred carat stones on me (Expensive, fucked up)
15+
But what should I say to those fuckers? (Hmm)
2116
Chains are hanging on Dad (e)
22-
Adlib is fresher than the whole music business (blee-blee)
23-
I'm a legend, you can die
2417
Digging the cache with a shovel (e)
25-
Who dreamed of what, then we took it to the takeaway (fuck-me)
18+
Do not slurp, observe etiquette
19+
Ha!
20+
Hey, look at this
21+
Hey, the chain is on me, a rash of lava
22+
Hey, the chain is on me, a rash of lava
23+
Hey, the chain is on me, a rash of lava
24+
How are you? How are you?
25+
How are you? How are you?
26+
How are you? How are you?
27+
How is your salary?
28+
Hundreds of bitches want stones
29+
Hundreds of bitches want stones
30+
Hundreds of bitches want stones
2631
Hundreds of bitches want to come to me
2732
Hundreds of bitches want to come to me
2833
Hundreds of bitches want to come to me
29-
This is my yard, the fucking chill zone
30-
Rich, like a capper (e)
31-
So much money, like an Emirate (Emirate)
32-
So much money, like an Emirate (Emirate)
33-
Do not slurp, observe etiquette
34-
Threw ice on the plane, the HYPE will leave
35-
Your girlfriend is jerking off
36-
Bitch, I'm a young aristocrat (I'm young)
37-
Bitch, I'm a young aristocrat (I'm young)
38-
Your hoe is always waiting for me (Yeah, yeah)
39-
A year later, for the third year now, well, yeah (Yeah, yeah)
34+
I'm a legend, you can die
35+
I'm a legend, you can die (Hey, come on, huh)
36+
I'm a legend, you can die (Hey, come on)
37+
I'm a legend, you can die (Khm-khm-khm)
38+
I'm in my early twenties
39+
I'm in my early twenties
40+
I'm like that joke: from mouth to another mouth
4041
I'm wearing two pillows (oh yeah)
4142
I am busy, I don't give a fuck about the crisis, I grew up in it (wah-wah-wah)
42-
Two cartoons on my watch
43-
Two cartoons on my watch
44-
I'm like that joke: from mouth to another mouth
45-
I'm a legend, you can die (Hey, come on, huh)
46-
So much money, like an Emirate (I'm like Dubai)
47-
So much money, like an Emirate (I'm like Dubai)
4843
I only need a top, you're like a little mermaid (Suck)
49-
Look, look
50-
I'm a legend, you can die (Khm-khm-khm)
51-
But what should I say to those fuckers? (Hmm)
5244
I splattered her, but not in a salami (Cum)
53-
Under heavy currency rain
54-
I'm a legend, you can die (Hey, come on)
55-
This is my house, this is my jon
56-
Slava, what did you do?
57-
Slava, what did you do?
58-
They tell me: I did everything by deception (Fucked up)
59-
Bitch, I have a hundred carat stones on me (Expensive, fucked up)
60-
Bitch, I have hundred carat stones on me (Expensive, fucked up)
6145
(Kh-kh-kh) Fenimore Cooper
62-
They say: I will not become a legend (Never)
63-
I'm in my early twenties
64-
I'm in my early twenties
65-
Hundreds of bitches want stones
66-
Hundreds of bitches want stones
67-
Hundreds of bitches want stones
46+
(Let's go, la la la)
47+
Look, look
48+
Making money, fucking like that (okay, few)
6849
Making money, fucking like this
6950
Making money, fucking like this
70-
Hey, look at this
71-
Smeared her face with a magic wand (Oops)
72-
Three on the neck, seven under the ass
73-
Three on the neck, seven under the ass
74-
This fit will kill faster than ko-ro-na-wee-rus
75-
The baby is jumping, but not on a skipping rope (On what?)
76-
Bitch, I am a young aristocrat (Aristocrat)
77-
Bitch, I am a young aristocrat (Aristocrat)
78-
She's looking for a treasure on my card (Find it)
79-
How are you? How are you?
80-
How are you? How are you?
81-
How are you? How are you?
82-
Ay, bitch, we got some guns (pr-r, paw)
83-
Making money, fucking like that (okay, few)
84-
Bitch, I'm an aristocratic daddy
85-
Stop being so fuckin' Gredy
8651
Making money, making money
8752
Making money, making money
8853
Making money, making money
54+
My guards have guards (La la la)
8955
My lady, I beg you, if you suck on me, do it culturally
90-
Bitch, I'm hanging like a young Pushkin (y)
56+
Paw-paw, hit the carcass (ha)
57+
Rich, like a capper (e)
9158
Sayonara "Sick" Boy
92-
How is your salary?
59+
See, a moth suits me (haha)
60+
She's looking for a treasure on my card (Find it)
61+
Slava, what did you do?
62+
Slava, what did you do?
63+
Smeared her face with a magic wand (Oops)
64+
So much money, like an Emirate (Emirate)
65+
So much money, like an Emirate (Emirate)
66+
So much money, like an Emirate (I'm like Dubai)
67+
So much money, like an Emirate (I'm like Dubai)
68+
Stop being so fuckin' Gredy
69+
The baby is jumping, but not on a skipping rope (On what?)
70+
The lookbook spread
71+
They say: I will not become a legend (Never)
72+
They tell me: I did everything by deception (Fucked up)
73+
This fit will kill faster than ko-ro-na-wee-rus
74+
This is a new Cadillac
75+
This is my house, this is my jon
76+
This is my yard, the fucking chill zone
77+
This is the new Cadillac
78+
This is the new Cadillac
79+
Three on the neck, seven under the ass
80+
Three on the neck, seven under the ass
81+
Threw ice on the plane, the HYPE will leave
82+
To my sports car at the club
83+
Two cartoons on my watch
84+
Two cartoons on my watch
85+
Under heavy currency rain
86+
Weaving thousands on bag LV
87+
Weaving thousands on bag LV
88+
Weaving thousands on bag LV
89+
Who dreamed of what, then we took it to the takeaway (fuck-me)
90+
Your boyfriend will wipe it off, he's a rag (Ha ha ha)
91+
Your girlfriend is jerking off
92+
Your hoe is always waiting for me (Yeah, yeah)

‎stack_jopa/Makefile

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
CC=g++
2+
INCLUDE_DIR = include
3+
# CFLAGS = -lm -I $(INCLUDE_DIR)
4+
CFLAGS=-Wall -Wextra -Wpedantic -I $(INCLUDE_DIR) -fpermissive -O2 -DDEBUG
5+
#-fsanitize=address -Og -DDEBUG -ggdb -D_FORTIFY_SOURCE=2 -lgtest -fpermissive -lstdc++
6+
TEST_CFLAGS=-Wall -Wextra -Wpedantic -lm -I $(INCLUDE_DIR)
7+
LDFLAGS=$(CFLAGS)
8+
TEST_BUILD=build_tests
9+
SRC_DIR=src
10+
EXECUTABLE=main
11+
TEST_EXECUTABLE=testrunner
12+
BUILD=build
13+
TEST_DIR=tests
14+
15+
SOURCES :=$(wildcard $(SRC_DIR)/*.c) #main.c archive.c bitstream.c utils.c output.c
16+
SOURCES := $(filter-out $(SRC_DIR)/main.c, $(SOURCES))
17+
OBJECTS=$(SOURCES:$(SRC_DIR)/%.c=$(BUILD)/%.o)
18+
19+
# TESTS=$(TEST_DIR)/runner.cpp
20+
TESTS=$(wildcard $(TEST_DIR)/*.cpp)
21+
TEST_OBJECTS=$(TESTS:$(TEST_DIR)/%.cpp=$(BUILD)/%.o)
22+
23+
# run: $(BUILD)/main.o $(OBJECTS) $(EXECUTABLE)
24+
# $(CC) $(OBJECTS) $(BUILD)/main.o $(CFLAGS) -o $(BUILD)/$(EXECUTABLE)
25+
26+
# test: $(OBJECTS) $(TEST_OBJECTS) $(TEST_EXECUTABLE)
27+
# $(CC) $(TEST_OBJECTS) $(TEST_CFLAGS) -o $(TEST_BUILD)/$(TEST_EXECUTABLE)
28+
29+
$(EXECUTABLE): $(OBJECTS) $(BUILD)/main.o
30+
$(CC) $(OBJECTS) $(BUILD)/main.o $(LDFLAGS) -o $(BUILD)/$@
31+
32+
$(TEST_EXECUTABLE): $(OBJECTS) $(TEST_OBJECTS)
33+
$(CC) $(OBJECTS) $(TEST_OBJECTS) $(LDFLAGS) -o $(BUILD)/$@
34+
35+
# %.o: $(SRC_DIR)/%.c
36+
# $(CC) $(CFLAGS) -c $< -o $(BUILD)/$@
37+
38+
39+
$(BUILD)/%.o: $(SRC_DIR)/%.c
40+
$(CC) $(CFLAGS) -c $< -o $@
41+
42+
$(BUILD)/%.o: $(TEST_DIR)/%.cpp
43+
$(CC) $(CFLAGS) -c $< -o $@
44+
45+
# $(TEST_BUILD)/%.o:
46+
# $(CC) $(CFLAGS) -c $< -o $@
47+
48+
clean:
49+
rm $(BUILD)/* #$(TEST_BUILD)/*

‎stack_jopa/README.MD

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
- Канарейки
3+
- Фаззер
4+
- после пуша на стек делать nmap страницу константной

‎stack_jopa/compile_flags.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-Wall
2+
-Wextra
3+
-Wpedantic
4+
-Iinclude

‎stack_jopa/include/random.h

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef RANDOM_H
2+
#define RANDOM_H
3+
4+
5+
inline void generate_stack_canary(char* buf);
6+
7+
#endif

‎stack_jopa/include/stack.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef STACK_H
2+
#define STACK_H
3+
4+
#include <stddef.h>
5+
6+
#define STACK_CAPACITY 512
7+
8+
/* typedef size_t stack_element; */
9+
typedef union {
10+
size_t num;
11+
unsigned char bytes[sizeof(size_t)];
12+
} stack_element;
13+
14+
typedef struct {
15+
union {
16+
stack_element* elements;
17+
char* buffer;
18+
};
19+
unsigned int size;
20+
stack_element canary;
21+
size_t capacity;
22+
} Stack;
23+
24+
25+
26+
#endif

‎stack_jopa/src/random.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "stack.h"
2+
#include <asm/unistd.h>
3+
#include <sys/types.h>
4+
#include <sys/syscall.h>
5+
#include <stddef.h>
6+
#include "random.h"
7+
8+
#define GETRANDOM_SYSCALL_NUMBER 318
9+
10+
// 0) https://stackoverflow.com/questions/9506353/how-to-invoke-a-system-call-via-syscall-or-sysenter-in-inline-assembly
11+
// 1) https://wiki.osdev.org/Inline_Assembly
12+
// 2) https://gcc.gnu.org/onlinedocs/gcc/extensions-to-the-c-language-family/how-to-use-inline-assembly-language-in-c-code.html#id2
13+
inline ssize_t getrandom(char* buf, size_t count, unsigned int flags){
14+
ssize_t ret;
15+
asm volatile
16+
(
17+
"syscall"
18+
: "=a" (ret)
19+
: "0"(SYS_getrandom),
20+
"D"(buf), // D - RDI
21+
"S"(count), // S - RSI
22+
"d"(flags) // d - RDX
23+
: "rcx", "r11", "memory" // clobbers
24+
);
25+
return ret;
26+
}
27+
28+
inline void generate_stack_canary(char* buf){
29+
getrandom(buf, sizeof(stack_element), NULL);
30+
}

‎stack_jopa/src/stack.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "stack.h"
2+
#include "random.h"
3+
4+
void stack_new(Stack* stack){
5+
generate_stack_canary(&stack->canary);
6+
stack->elements = (stack_element *)malloc(sizeof(stack_element) * STACK_CAPACITY);
7+
stack->size = 0;
8+
stack->capacity = STACK_CAPACITY;
9+
}
10+
11+
void stack_delete(Stack* stack){
12+
assert(stack != NULL);
13+
assert(stack->elements != NULL, "Already free!");
14+
15+
free(stack->elements);
16+
17+
stack->elements = NULL;
18+
stack->size = NULL;
19+
stack->capacity = NULL;
20+
stack->canary.num = NULL;
21+
}
22+
23+
// пока канарейка будет после каждого пуша и попа
24+
void stack_push(Stack* stack, stack_element element){
25+
assert(stack != NULL);
26+
27+
if (stack->size >= stack->capacity-1){ // capacity-1, capacity
28+
stack->capacity <<= 1;
29+
stack->elements = realloc(stack->elements, stack->capacity * sizeof(stack_element));
30+
}
31+
32+
stack->elements[stack->size++] = element;
33+
stack->elements[stack->size++] = stack->canary;
34+
}
35+
36+
stack_element stack_pop(Stack* stack){
37+
assert(stack != NULL);
38+
assert(stack->size < stack->capacity - 1);
39+
40+
stack_element canary = stack->elements[stack->size--];
41+
stack_element element = stack->elements[stack->size--];
42+
43+
assert(canary.num == stack->canary.num, "Stack corruption detected"); // TODO: заменить ассерт на что-то другое
44+
return element;
45+
}

0 commit comments

Comments
 (0)
Please sign in to comment.