Browse Source

Corrected build system & step positionment in child

Yann Weber 2 years ago
parent
commit
5c6886788d
8 changed files with 89 additions and 27 deletions
  1. 8
    4
      Makefile.am
  2. 53
    4
      asm_env.c
  3. 1
    0
      asm_env.h
  4. 2
    10
      child.s
  5. 9
    2
      tests/regen.sh
  6. 3
    0
      tests/samples/Makefile.am
  7. 1
    1
      tests/samples/asm_env_stamps.s
  8. 12
    6
      tests/tests_asm_env.c

+ 8
- 4
Makefile.am View File

@@ -5,6 +5,14 @@ libcheck_asmsh_a_SOURCES = mmap_parse.c asm_env.c compile.c logger.c
5 5
 asmsh_SOURCES = asmsh.c $(libasmsh_a_SOURCES)
6 6
 child_SOURCES = child.s
7 7
 
8
+asmsh_LDFLAGS=-g -O2
9
+
10
+CCASFLAGS=-g -O0
11
+child_LDFLAGS=-nostdlib
12
+
13
+child$(EXEEXT): $(child_SOURCES:.s=.o)
14
+	ld -g -o $@ $^
15
+
8 16
 SUBDIRS = 
9 17
 if CHECK
10 18
 noinst_LIBRARIES = libcheck_asmsh.a
@@ -32,7 +40,3 @@ clean-local:
32 40
 
33 41
 endif
34 42
 
35
-child$(EXEEXT): $(child_SOURCES:.s=.o)
36
-	ld $(child_LDFLAGS) -o $@ $<
37
-
38
-

+ 53
- 4
asm_env.c View File

@@ -17,6 +17,11 @@ asmsh_env_t* asmsh_env(const char *childpath)
17 17
 	}
18 18
 	child_mmap_init(&(res->mmap));
19 19
 
20
+	if((res->childpath = strdup(childpath)) == NULL)
21
+	{
22
+		goto err_pathdup;
23
+	}
24
+
20 25
 	if(_asmsh_env_spawn(res, childpath) < 0)
21 26
 	{
22 27
 		err = errno;
@@ -78,6 +83,8 @@ asmsh_env_t* asmsh_env(const char *childpath)
78 83
 
79 84
 	return res;
80 85
 err:
86
+	free(res->childpath);
87
+err_pathdup:
81 88
 	free(res);
82 89
 	errno = err;
83 90
 	return NULL;
@@ -91,6 +98,7 @@ void asmsh_env_free(asmsh_env_t *asmenv)
91 98
 		free(asmenv->mmap.maps);
92 99
 	}
93 100
 	kill(asmenv->pid, SIGKILL);
101
+	free(asmenv->childpath);
94 102
 	free(asmenv);
95 103
 }
96 104
 
@@ -211,17 +219,58 @@ static int _asmsh_env_spawn(asmsh_env_t *env, const char *childpath)
211 219
 		}
212 220
 	}
213 221
 	// mmap done by child process
222
+	
223
+	// WARNING totally depends on child.s source
224
+	// right now there is only 4 instructions (the fourth is the jmp to the txt_map)
225
+	// before reaching the start of the code mmap
226
+	for(int i=0; i<4; i++)
227
+	{
228
+		/* // DEBUG to monitor placement in child exec
229
+		asmsh_env_update_regs(env);
230
+		dprintf(2, "%d) rax: %08X rip : %08X mmap_addr = %08X\n",
231
+				i, env->regs.rax, env->regs.rip, env->txt_map_ptr);
232
+		*/
233
+		if(ptrace(PTRACE_SINGLESTEP, env->pid, NULL, 0) < 0)
234
+		{
235
+			err = errno;
236
+			perror("Unable to ptrace singlestep");
237
+			dprintf(2, "ptrace syscall failed on %d time\n", i+1);
238
+			goto err;
239
+		}
240
+		if(waitpid(env->pid, &wstatus, 0) < 0)
241
+		{
242
+			err = errno;
243
+			perror("Unable to wait for child process to stop on step");
244
+			goto err;
245
+		}
246
+		if(wstatus != 1407)
247
+		{
248
+			goto err_wstatus;
249
+		}
250
+
251
+	}
214 252
 
215 253
 	return 0;
216 254
 
217 255
 err_wstatus:
218
-	if(WCOREDUMP(wstatus))
256
+	if(WIFEXITED(wstatus))
257
+	{
258
+		dprintf(2, "Child exited with status %d\n", WEXITSTATUS(wstatus));
259
+	}
260
+	else if(WIFSIGNALED(wstatus))
219 261
 	{
220
-		dprintf(2, "Child segfault\n");
262
+		if(WCOREDUMP(wstatus))
263
+		{
264
+			dprintf(2, "Child segfault\n");
265
+		}
266
+		else
267
+		{
268
+			dprintf(2, "Child killed by sig %d\n", WTERMSIG(wstatus));
269
+		}
221 270
 	}
222
-	else if(WIFEXITED(wstatus))
271
+	else if(WIFSTOPPED(wstatus))
223 272
 	{
224
-		dprintf(2, "Child exited with status %d\n", WEXITSTATUS(wstatus));
273
+		dprintf(2, "Child stopped by sig %d\n", WSTOPSIG(wstatus));
225 274
 	}
226 275
 	else
227 276
 	{

+ 1
- 0
asm_env.h View File

@@ -21,6 +21,7 @@ typedef struct asmsh_env_s asmsh_env_t;
21 21
 
22 22
 struct asmsh_env_s
23 23
 {
24
+	char *childpath;
24 25
 	///! Child process pid
25 26
 	pid_t pid;
26 27
 	///! Child process registers value

+ 2
- 10
child.s View File

@@ -14,19 +14,11 @@ _start:
14 14
 	xor %r9, %r9
15 15
 	syscall
16 16
 
17
+	push %rax
17 18
 	cmp $0, %rax
18 19
 	jle .errmap
19 20
 
20
-	push %rax
21
-	#xor %rax, %rax
22
-	#push %rax
23
-
24
-
25
-	#mov $-1, %r15
26
-	#mov $34, %rax # sys_pause
27
-	#syscall
28
-
29
-	jmp *(%rsp)
21
+	jmp *(%rsp) # STOP HERE
30 22
 
31 23
 	mov $60, %rax
32 24
 	xor %rdi, %rdi

+ 9
- 2
tests/regen.sh View File

@@ -9,18 +9,25 @@ TESTS = $progs
9 9
 check_PROGRAMS = $progs
10 10
 noinst_HEADERS = asmsh_check.h
11 11
 EXTRA_DIST = samples
12
-SUBDIRS  = samples
12
+SUBDIRS = samples
13 13
 
14 14
 TESTLIB_NAME=libcheck_asmsh.a
15 15
 TESTLIB=\$(top_builddir)/\$(TESTLIB_NAME)
16
+CHILD_NAME=child
17
+CHILD=\$(top_builddir)/\$(CHILD_NAME)
18
+
19
+BUILT_SOURCES = \$(CHILD)
16 20
 
17 21
 clean-local:
18 22
 	-rm -rf *.gcov *.gcda *.gcno
19 23
 
24
+\$(CHILD):
25
+	make -C \$(top_builddir) \$(CHILD_NAME)
26
+
20 27
 \$(TESTLIB):
21 28
 	make -C \$(top_builddir) \$(TESTLIB_NAME)
22 29
 
23
-.PHONY: \$(TESTLIB)
30
+.PHONY: \$(TESTLIB) \$(CHILD)
24 31
 __EOF__
25 32
 
26 33
 for p in $progs

+ 3
- 0
tests/samples/Makefile.am View File

@@ -1,6 +1,9 @@
1
+if CHECK
1 2
 bin_PROGRAMS = asm_env_stamps
2 3
 
3 4
 asm_env_stamps_SOURCES = asm_env_stamps.s
5
+asm_env_stamps_CCASFLAGS = -g -O0
4 6
 
5 7
 asm_env_stamps$(EXEEXT): $(asm_env_stamps_SOURCES:.s=.o)
6 8
 	ld  $(child_LDFLAGS) -o $@ $<
9
+endif

+ 1
- 1
tests/samples/asm_env_stamps.s View File

@@ -25,7 +25,7 @@ _start:
25 25
 	push %rax
26 26
 
27 27
 
28
-	mov $-1, %r15
28
+	#mov $-1, %r15
29 29
 	mov $34, %rax # sys_pause
30 30
 	syscall
31 31
 

+ 12
- 6
tests/tests_asm_env.c View File

@@ -9,14 +9,19 @@
9 9
 #include "asmsh_check.h"
10 10
 #include "asm_env.h"
11 11
 
12
+#define REAL_CHILD "../child"
13
+#define STAMP_CHILD "samples/asm_env_stamps"
14
+
12 15
 static const long int stamp1 = 0x4242; // %r15
13 16
 static const long int stamp2 = -1; // %r14
14 17
 
15 18
 START_TEST(test_env)
16 19
 {
17
-	asmsh_env_t *env = asmsh_env("../child");
20
+	const char childpath[] = REAL_CHILD;
21
+	asmsh_env_t *env = asmsh_env(childpath);
18 22
 	
19 23
 	ck_assert_ptr_nonnull(env);
24
+	ck_assert_str_eq(childpath, env->childpath);
20 25
 
21 26
 	asmsh_env_free(env);
22 27
 }
@@ -24,9 +29,11 @@ END_TEST
24 29
 
25 30
 START_TEST(test_asm_stamps)
26 31
 {
27
-	asmsh_env_t *env = asmsh_env("samples/asm_env_stamps");
32
+	const char childpath[] = STAMP_CHILD;
33
+	asmsh_env_t *env = asmsh_env(childpath);
28 34
 	
29 35
 	ck_assert_ptr_nonnull(env);
36
+	ck_assert_str_eq(env->childpath, childpath);
30 37
 
31 38
 	ck_assert_mem_eq(&stamp1, &(env->regs.r15), 8);
32 39
 	ck_assert_mem_eq(&stamp2, &(env->regs.r14), 8);
@@ -37,15 +44,14 @@ END_TEST
37 44
 
38 45
 START_TEST(test_env_update)
39 46
 {
40
-	asmsh_env_t *env = asmsh_env("samples/asm_env_stamps");
47
+	const char childpath[] = STAMP_CHILD;
48
+	asmsh_env_t *env = asmsh_env(childpath);
41 49
 
42 50
 	ck_assert_ptr_nonnull(env);
51
+	ck_assert_str_eq(env->childpath, childpath);
43 52
 
44 53
 	asmsh_env_update_maps(env);
45 54
 
46
-	ck_assert_mem_eq(&stamp1, &(env->regs.r15), 8);
47
-	ck_assert_mem_eq(&stamp2, &(env->regs.r14), 8);
48
-
49 55
 	asmsh_env_free(env);
50 56
 }
51 57
 

Loading…
Cancel
Save