Skip to content

Commit d6fb764

Browse files
committed
Add abort () to jerry-libc
Added declaration and implementations of `void abort (void)` to jerry-libc. As the linux implementation relies on the `getpid` syscall - which has no arguments - `syscall_0` implementations have been added as well. `libc_fatal` has been adapted to use the new `abort` function instead of `exit`. This also made the definition of `LIBC_FATAL_ERROR_EXIT_CODE` unnecessary. Finally, the syscall fatal error message was fixed. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
1 parent 5c012a1 commit d6fb764

File tree

10 files changed

+92
-9
lines changed

10 files changed

+92
-9
lines changed

jerry-libc/include/stdlib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
#endif /* !__cplusplus */
2424

2525
extern EXTERN_C void __attribute__ ((noreturn)) exit (int);
26+
extern EXTERN_C void __attribute__ ((noreturn)) abort (void);
2627

2728
#endif /* !JERRY_LIBC_STDLIB_H */

jerry-libc/jerry-libc-defs.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@
2828
#define __attr_noreturn___ __attribute__((noreturn))
2929
#define __attr_noinline___ __attribute__((noinline))
3030

31-
/**
32-
* Constants
33-
*/
34-
#define LIBC_FATAL_ERROR_EXIT_CODE (2)
35-
3631
/**
3732
* Assertions
3833
*/

jerry-libc/jerry-libc-fatals.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ libc_fatal (const char *msg, /**< fatal error description */
4141
msg, function_name, file_name, line_number);
4242
}
4343

44-
exit (LIBC_FATAL_ERROR_EXIT_CODE);
44+
abort ();
4545
} /* libc_fatal */

jerry-libc/jerry-libc.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ FILE *stdin = (FILE*) 0;
3131
FILE *stdout = (FILE*) 1;
3232
FILE *stderr = (FILE*) 2;
3333

34-
LIBC_UNREACHABLE_STUB_FOR (void abort (void))
35-
3634
#ifdef __GNUC__
3735
/*
3836
* Making GCC not to replace:

jerry-libc/target/linux/asm_arm.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616
#ifndef ASM_ARM_H
1717
#define ASM_ARM_H
1818

19+
/*
20+
* mov syscall_no (%r0) -> %r7
21+
* svc #0
22+
*/
23+
#define SYSCALL_0 \
24+
push {r4-r12, lr}; \
25+
\
26+
mov r7, r0; \
27+
\
28+
svc #0; \
29+
\
30+
pop {r4-r12, pc};
31+
1932
/*
2033
* mov syscall_no (%r0) -> %r7
2134
* mov arg1 (%r1) -> %r0

jerry-libc/target/linux/asm_x64.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616
#ifndef ASM_X64_H
1717
#define ASM_X64_H
1818

19+
/*
20+
* mov syscall_no (%rdi) -> %rax
21+
* syscall
22+
*/
23+
#define SYSCALL_0 \
24+
mov %rdi, %rax; \
25+
syscall; \
26+
ret;
27+
1928
/*
2029
* mov syscall_no (%rdi) -> %rax
2130
* mov arg1 (%rsi) -> %rdi

jerry-libc/target/linux/asm_x86.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616
#ifndef ASM_X86_H
1717
#define ASM_X86_H
1818

19+
/*
20+
* mov syscall_no -> %eax
21+
* int $0x80
22+
* mov %eax -> ret
23+
*/
24+
#define SYSCALL_0 \
25+
push %edi; \
26+
push %esi; \
27+
push %ebx; \
28+
mov 0x10 (%esp), %eax; \
29+
int $0x80; \
30+
pop %ebx; \
31+
pop %esi; \
32+
pop %edi; \
33+
ret;
34+
1935
/*
2036
* mov syscall_no -> %eax
2137
* mov arg1 -> %ebx

jerry-libc/target/linux/jerry-asm.S

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ _start:
1414
_START
1515
.size _start, . - _start
1616

17+
.global syscall_0_asm
18+
.type syscall_0_asm, %function
19+
syscall_0_asm:
20+
SYSCALL_0
21+
.size syscall_0_asm, . - syscall_0_asm
22+
1723
.global syscall_1_asm
1824
.type syscall_1_asm, %function
1925
syscall_1_asm:

jerry-libc/target/linux/jerry-libc-target.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,34 @@ LIBC_UNREACHABLE_STUB_FOR (int raise (int sig_no __attr_unused___))
4848
#define LIBC_EXIT_ON_ERROR(syscall_ret_val) \
4949
if ((syscall_ret_val) < 0) \
5050
{ \
51-
libc_fatal ("Syscall successful", __FILE__, __FUNCTION__, __LINE__); \
51+
libc_fatal ("Syscall", __FILE__, __FUNCTION__, __LINE__); \
5252
}
5353

54+
static long int syscall_0 (long int syscall_no);
5455
static long int syscall_1 (long int syscall_no, long int arg1);
5556
static long int syscall_2 (long int syscall_no, long int arg1, long int arg2);
5657
static long int syscall_3 (long int syscall_no, long int arg1, long int arg2, long int arg3);
5758

59+
extern long int syscall_0_asm (long int syscall_no);
5860
extern long int syscall_1_asm (long int syscall_no, long int arg1);
5961
extern long int syscall_2_asm (long int syscall_no, long int arg1, long int arg2);
6062
extern long int syscall_3_asm (long int syscall_no, long int arg1, long int arg2, long int arg3);
6163

64+
/**
65+
* System call with no argument.
66+
*
67+
* @return syscall's return value
68+
*/
69+
static __attr_noinline___ long int
70+
syscall_0 (long int syscall_no) /**< syscall number */
71+
{
72+
long int ret = syscall_0_asm (syscall_no);
73+
74+
LIBC_EXIT_ON_ERROR (ret);
75+
76+
return ret;
77+
} /* syscall_0 */
78+
6279
/**
6380
* System call with one argument.
6481
*
@@ -153,6 +170,25 @@ exit (int status) /**< status code */
153170
}
154171
} /* exit */
155172

173+
/**
174+
* Abort current process, producing an abnormal program termination.
175+
* The function raises the SIGABRT signal.
176+
*/
177+
void __attr_noreturn___ __attr_used___
178+
abort (void)
179+
{
180+
syscall_1 (__NR_close, (long int)stdin);
181+
syscall_1 (__NR_close, (long int)stdout);
182+
syscall_1 (__NR_close, (long int)stderr);
183+
184+
syscall_2 (__NR_kill, syscall_0 (__NR_getpid), SIGABRT);
185+
186+
while (true)
187+
{
188+
/* unreachable */
189+
}
190+
} /* abort */
191+
156192
/**
157193
* fopen
158194
*

jerry-libc/target/mcu-stubs/jerry-libc-target.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ exit (int status __attr_unused___)
3838
}
3939
} /* exit */
4040

41+
/** abort - cause abnormal process termination */
42+
void __attr_noreturn___ __attr_used___
43+
abort (void)
44+
{
45+
while (true)
46+
{
47+
}
48+
} /* abort */
49+
4150
/**
4251
* fwrite
4352
*

0 commit comments

Comments
 (0)