ReactOS 0.4.16-dev-1946-g52006dd
longjmp.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS vcruntime library
3 * LICENSE: MIT (https://spdx.org/licenses/MIT)
4 * PURPOSE: Implementation of longjmp for x86.
5 * COPYRIGHT: Copyright 2025 Timo Kreuzer <timo.kreuzer@reactos.org>
6 */
7
8#include <setjmp.h>
9#include <intrin.h>
10
11#define _JUMP_BUFFER_COOKIE 0x56433230
12
14
15typedef void (__stdcall *PUNWIND_ROUTINE)(const _JUMP_BUFFER *);
16
17void _stdcall _seh_longjmp_unwind(const _JUMP_BUFFER* _Buf);
18
19__declspec(noreturn)
20void __longjmp_nounwind(const _JUMP_BUFFER* _Buf, int _Value);
21
22__declspec(noreturn)
23void
25longjmp(
26 _In_reads_(_JBLEN) jmp_buf _Buf,
27 _In_ int _Value)
28{
29 const _JUMP_BUFFER* jumpBuffer = (_JUMP_BUFFER*)_Buf;
30
31 /* Ensure _Value is non-zero */
32 _Value = (_Value == 0) ? 1 : _Value;
33
34 /* Check if the exception registration is from a previous function */
35 if (jumpBuffer->Registration != __readfsdword(0))
36 {
37 /* Do a global unwind to the function that owns the target SEH frame */
38 _global_unwind2((void*)jumpBuffer->Registration);
39 }
40
41 /* Check if we have a registration */
42 if (jumpBuffer->Registration != 0)
43 {
44 /* Check if this is a _setjmp3 buffer */
45 if (jumpBuffer->Cookie == _JUMP_BUFFER_COOKIE)
46 {
47 /* Call the unwind function if it is set */
48 PUNWIND_ROUTINE unwindFunc = (PUNWIND_ROUTINE)jumpBuffer->UnwindFunc;
49 if (unwindFunc != NULL)
50 {
51 unwindFunc(jumpBuffer);
52 }
53 }
54 else if (jumpBuffer->Registration != 0xFFFFFFFF)
55 {
56 /* This is a legacy _setjmp buffer, use old style unwind */
57 _seh_longjmp_unwind(jumpBuffer);
58 }
59 }
60
61 __longjmp_nounwind(jumpBuffer, _Value);
62}
#define __cdecl
Definition: accygwin.h:79
_In_ _Value
#define NULL
Definition: types.h:112
_Must_inspect_result_ _In_ CONST FLT_REGISTRATION * Registration
Definition: fltkernel.h:991
void __declspec(noinline) __cdecl _free_base(void *const block)
Definition: free_base.cpp:98
void(__stdcall * PUNWIND_ROUTINE)(const _JUMP_BUFFER *)
Definition: longjmp.c:15
void __cdecl _global_unwind2(void *Registration)
void _stdcall _seh_longjmp_unwind(const _JUMP_BUFFER *_Buf)
#define _JUMP_BUFFER_COOKIE
Definition: longjmp.c:11
PPC_QUAL unsigned long __readfsdword(const unsigned long Offset)
Definition: intrin_ppc.h:382
if(dx< 0)
Definition: linetemp.h:194
#define _In_reads_(s)
Definition: no_sal2.h:168
#define _In_
Definition: no_sal2.h:158
#define __stdcall
Definition: typedefs.h:25
_JBTYPE jmp_buf[_JBLEN]
Definition: setjmp.h:188