Skip to content

Commit 4aadf74

Browse files
committed
probing intermittent sys_brk failures in wsl
1 parent caa3fa9 commit 4aadf74

File tree

10 files changed

+82
-49
lines changed

10 files changed

+82
-49
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ a.out
2323
/readwin
2424
/windynamic_linux
2525
*.dll
26+
*.log

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "cppdbg",
77
"request": "launch",
88
"program": "${workspaceFolder}/loader",
9-
"args": ["./tinyfetch"],
9+
"args": ["./string"],
1010
"stopAtEntry": false,
1111
"cwd": "${workspaceFolder}",
1212
"environment": [{ "name": "CFLAGS", "value": "-DVERBOSE" }],

Makefile

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ ifeq ($(CC),cc)
33
endif
44

55
OBJDUMP ?= objdump
6+
67
WARNINGS = \
78
-std=gnu2x \
89
-Wall -Wextra -Wpedantic -Wno-varargs \
910
-Wno-gnu-zero-variadic-macro-arguments \
10-
-Wno-gnu-statement-expression-from-macro-expansion \
1111
-Wconversion \
12-
-Werror=return-type \
13-
-Werror=incompatible-pointer-types \
12+
-Werror=return-type \
13+
-Werror=incompatible-pointer-types \
14+
-Wno-gnu-empty-initializer \
1415
-Wvla
1516

1617
all: \

loader_test.sh

+25-25
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ assert() {
88
fi
99
}
1010

11-
# Unit Test
12-
$PRELOADER ./unit_test
13-
assert [ $? == 0 ]
11+
# # Unit Test
12+
# $PRELOADER ./unit_test
13+
# assert [ $? == 0 ]
1414

15-
# Env Test
16-
assert [ "`$PRELOADER ./loader ./env | wc -c`" == "8" ]
17-
assert [ "`$PRELOADER ./loader ./env x`" == $'2\n./env\nx' ]
15+
# # Env Test
16+
# assert [ "`$PRELOADER ./loader ./env | wc -c`" == "8" ]
17+
# assert [ "`$PRELOADER ./loader ./env x`" == $'2\n./env\nx' ]
1818

1919
# String Test
2020
expected=`cat <<EOF
@@ -36,24 +36,24 @@ EOF
3636
`
3737
assert [ "`$PRELOADER ./loader string silent`" == "$expected" ]
3838

39-
# Dynamic Test
40-
expected=`cat <<EOF
41-
1st call
42-
2nd call
43-
16 + 16 = 32
44-
dynamic_var: 0
45-
dynamic_var: 42
46-
2nd shared lib length of 'how now brown cow': 17
47-
lib test_number_data: 12345
48-
lib test_number_data: 54321
49-
lib test_number_bss: 0
50-
lib test_number_bss: 54321
51-
lib get_test_number_data_internal_ref: 12345
52-
lib get_test_number_data_internal_ref: 54321
53-
malloc: ok
54-
add_many_result: 36
55-
EOF
56-
`
57-
assert [ "`$PRELOADER ./loader ./dynamic`" == "$expected" ]
39+
# # Dynamic Test
40+
# expected=`cat <<EOF
41+
# 1st call
42+
# 2nd call
43+
# 16 + 16 = 32
44+
# dynamic_var: 0
45+
# dynamic_var: 42
46+
# 2nd shared lib length of 'how now brown cow': 17
47+
# lib test_number_data: 12345
48+
# lib test_number_data: 54321
49+
# lib test_number_bss: 0
50+
# lib test_number_bss: 54321
51+
# lib get_test_number_data_internal_ref: 12345
52+
# lib get_test_number_data_internal_ref: 54321
53+
# malloc: ok
54+
# add_many_result: 36
55+
# EOF
56+
# `
57+
# assert [ "`$PRELOADER ./loader ./dynamic`" == "$expected" ]
5858

5959
echo All tests passed

src/dlls/msvcrt.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "msvcrt.h"
22
#include <stdarg.h>
3+
#include <stdbool.h>
34
#include <stddef.h>
45
#include <stdint.h>
56

src/loader/loader_lib.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ bool find_got_entry(
131131
bool read_to_string(const char *path, char **content, size_t size) {
132132
char *buffer = loader_malloc_arena(size);
133133
if (buffer == NULL) {
134-
BAIL("malloc failed\n");
134+
BAIL("loader_malloc_arena failed\n");
135135
}
136136

137137
int32_t fd = tiny_c_open(path, O_RDONLY);
@@ -196,7 +196,7 @@ bool get_runtime_symbols(
196196
.size = curr_symbol->size,
197197
};
198198
if (!RuntimeSymbolList_add(runtime_symbols, runtime_symbol)) {
199-
BAIL("malloc failed\n");
199+
BAIL("RuntimeSymbolList_add failed\n");
200200
}
201201
}
202202

src/loader/loader_main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ static bool initialize_dynamic_data(
278278
sizeof(struct RuntimeObject) * inferior_dyn_data->shared_libraries_len
279279
);
280280
if (shared_libraries == NULL) {
281-
BAIL("malloc failed\n");
281+
BAIL("loader_malloc_arena failed\n");
282282
}
283283

284284
size_t dynamic_lib_offset = LOADER_SHARED_LIB_START;
@@ -397,7 +397,7 @@ static bool initialize_dynamic_data(
397397
sizeof(struct RuntimeRelocation) * runtime_var_relocations_len
398398
);
399399
if (runtime_var_relocations == NULL) {
400-
BAIL("malloc failed\n");
400+
BAIL("loader_malloc_arena failed\n");
401401
}
402402

403403
for (size_t i = 0; i < inferior_dyn_data->var_relocations_len; i++) {

src/loader/pe_tools.c

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "elf_tools.h"
55
#include "loader_lib.h"
66
#include "memory_map.h"
7+
#include <assert.h>
78
#include <stdatomic.h>
89
#include <stddef.h>
910
#include <stdint.h>

src/programs/linux/string/string_main.c

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ extern int test_number_data;
1919

2020
int get_test_number_data();
2121

22+
// @todo:
23+
extern size_t tinyc_heap_start;
24+
extern size_t tinyc_heap_end;
25+
extern size_t tinyc_heap_index;
26+
2227
int main(void) {
2328
tiny_c_printf("inline string\n");
2429

@@ -32,7 +37,14 @@ int main(void) {
3237
tiny_c_printf("bss_int %d\n", bss_int);
3338
tiny_c_printf("bss_string %s\n", bss_string);
3439

40+
tiny_c_fprintf(STDERR, "tinyc_heap_start %x\n", tinyc_heap_start);
41+
tiny_c_fprintf(STDERR, "tinyc_heap_end %x\n", tinyc_heap_end);
42+
tiny_c_fprintf(STDERR, "tinyc_heap_index %x\n", tinyc_heap_index);
43+
tiny_c_fprintf(STDERR, "mallocing...\n");
3544
char *malloc_string = tinyc_malloc_arena(0x1000);
45+
tiny_c_fprintf(STDERR, "tinyc_heap_start %x\n", tinyc_heap_start);
46+
tiny_c_fprintf(STDERR, "tinyc_heap_end %x\n", tinyc_heap_end);
47+
tiny_c_fprintf(STDERR, "tinyc_heap_index %x\n", tinyc_heap_index);
3648
if (malloc_string == NULL) {
3749
tiny_c_fprintf(STDERR, "malloc failed\n");
3850
tiny_c_exit(-1);

src/tiny_c/tiny_c.c

+33-16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <stdbool.h>
77
#include <stddef.h>
88
#include <stdint.h>
9+
#include <string.h>
910
#include <sys/mman.h>
1011
#include <sys/syscall.h>
1112
#include <sys/types.h>
@@ -57,7 +58,7 @@ static void tiny_c_print(int32_t file_handle, const char *data) {
5758
static void tiny_c_newline(int32_t file_handle) {
5859
struct SysArgs args = (struct SysArgs){
5960
.param_one = (size_t)file_handle,
60-
.param_two = (size_t)"\n",
61+
.param_two = (size_t) "\n",
6162
.param_three = 1,
6263
};
6364
tiny_c_syscall(SYS_write, &args);
@@ -410,23 +411,39 @@ const char *tinyc_strerror(int32_t err_number) {
410411

411412
// @todo: alignment
412413
void *tinyc_malloc_arena(size_t n) {
413-
const size_t PAGE_SIZE = 0x1000;
414-
415-
if (tinyc_heap_start == 0) {
416-
tinyc_heap_start = tinyc_sys_brk(0);
417-
tinyc_heap_end = tinyc_heap_start;
418-
tinyc_heap_index = tinyc_heap_start;
419-
}
420-
if (tinyc_heap_index + n > tinyc_heap_end) {
421-
size_t extend_size = PAGE_SIZE * (n / PAGE_SIZE) + PAGE_SIZE;
422-
tinyc_heap_end = tinyc_sys_brk(tinyc_heap_end + extend_size);
423-
if (tinyc_heap_end <= tinyc_heap_start) {
424-
return NULL;
425-
}
414+
// const size_t PAGE_SIZE = 0x1000;
415+
416+
// if (tinyc_heap_start == 0) {
417+
size_t start = tinyc_sys_brk(0);
418+
size_t end = start;
419+
size_t index = start;
420+
// tinyc_heap_start = tinyc_sys_brk(0);
421+
// tinyc_heap_end = tinyc_heap_start;
422+
// tinyc_heap_index = tinyc_heap_start;
423+
// }
424+
// if (tinyc_heap_index + n > tinyc_heap_end) {
425+
// size_t extend_size = PAGE_SIZE * (n / PAGE_SIZE) + PAGE_SIZE;
426+
// tinyc_heap_end = tinyc_sys_brk(tinyc_heap_end + extend_size);
427+
// tinyc_heap_start = tinyc_sys_brk(0);
428+
// tinyc_heap_end = tinyc_sys_brk(tinyc_heap_end + 0x2000);
429+
// size_t temp_start = tinyc_sys_brk(0);
430+
// size_t temp_end = tinyc_sys_brk(temp_start + 0x2000);
431+
// if (temp_end <= temp_start) {
432+
// return NULL;
433+
// }
434+
end = tinyc_sys_brk(start + 0x2000);
435+
436+
tinyc_heap_start = start;
437+
tinyc_heap_end = end;
438+
tinyc_heap_index = index;
439+
440+
if (end <= start) {
441+
return NULL;
426442
}
443+
// }
427444

428-
void *address = (void *)tinyc_heap_index;
429-
tinyc_heap_index += n;
445+
void *address = (void *)index;
446+
// tinyc_heap_index += n;
430447

431448
return address;
432449
}

0 commit comments

Comments
 (0)