Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move some macros used in .asm files into header files. #7202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ if (X86 AND X64)
set(ARCH_SRCS ${ARCH_SRCS} arch/${ARCH_NAME}/x86_to_x64.c)
endif (X86 AND X64)

if (AARCHXX)
set(ARCH_SRCS ${ARCH_SRCS} arch/${ARCH_NAME}/offsets.c)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agument the name: either asm_offsets.{c,h} or struct_offsets.{c,h}.

endif (AARCHXX)

if (WIN32)
# i#894: Win8 WDK ntdll.lib does not list Ki routines so we make our own .lib.
# Because the Ki are stdcall we can't just use a .def file: we need
Expand Down
11 changes: 5 additions & 6 deletions core/arch/aarch64/aarch64.asm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* **********************************************************
* Copyright (c) 2019-2024 Google, Inc. All rights reserved.
* Copyright (c) 2016 ARM Limited. All rights reserved.
* Copyright (c) 2016-2025 ARM Limited. All rights reserved.
* **********************************************************/

/*
Expand Down Expand Up @@ -35,6 +35,7 @@
* AArch64-specific assembly and trampoline code
*/

#include "offsets.h"
#include "../asm_defines.asm"
START_FILE

Expand Down Expand Up @@ -68,10 +69,8 @@ START_FILE

/* offsetof(priv_mcontext_t, simd) */
#define simd_OFFSET (16 * ARG_SZ*2 + 32)
/* offsetof(dcontext_t, dstack) */
#define dstack_OFFSET 0x9f8
/* offsetof(dcontext_t, is_exiting) */
#define is_exiting_OFFSET (dstack_OFFSET+1*ARG_SZ)
#define is_exiting_OFFSET (dcontext_t_OFFSET_dstack+1*ARG_SZ)
/* offsetof(struct tlsdesc_t, arg) */
#define tlsdesc_arg_OFFSET 8

Expand Down Expand Up @@ -178,7 +177,7 @@ GLOBAL_LABEL(dr_call_on_clean_stack:)
stp x29, x30, [sp, #-16]! /* Save frame pointer and link register. */
mov x29, sp /* Save sp across the call. */
/* Swap stacks. */
ldr x30, [x0, #dstack_OFFSET]
ldr x30, [x0, #dcontext_t_OFFSET_dstack]
mov sp, x30
/* Set up args. */
mov x30, x1 /* void *(*func)(arg1...arg8) */
Expand Down Expand Up @@ -354,7 +353,7 @@ GLOBAL_LABEL(cleanup_and_terminate:)
mov x24, #0
b cat_done_saving_dstack
cat_save_dstack:
mov x2, #(dstack_OFFSET)
mov x2, #(dcontext_t_OFFSET_dstack)
ldr x24, [x19, x2]
cat_done_saving_dstack:
CALLC0(GLOBAL_REF(get_cleanup_and_terminate_global_do_syscall_entry))
Expand Down
44 changes: 44 additions & 0 deletions core/arch/aarch64/offsets.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* **********************************************************
* Copyright (c) 2025 ARM Limited. All rights reserved.
* **********************************************************/

/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of ARM Limited nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL ARM LIMITED OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

#include <stddef.h>

#include "../globals.h"
#include "offsets.h"

/* This is an alternative to using static_assert that will work with
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could use static_assert as for C++ we require C++11 (and already use it in our C++ code), for Visual Studio we require VS2019, and while our UNIX has gnu99 the GNU extensions provide _Static_assert. Could do sthg like #if defined(__cplusplus) && !defined(_Static_assert) #define _Static_assert static_assert #endif and then use _Static_assert everywhere: or define a new STATIC_ASSERT that takes in a message.

* any C compiler. If a macro is incorrectly defined there will be a
* compile-time error that clearly shows which macro is wrong.
*/
struct _unused {
int x1[dcontext_t_OFFSET_dstack == offsetof(dcontext_t, dstack) ? 1 : -1];
};
43 changes: 43 additions & 0 deletions core/arch/aarch64/offsets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* **********************************************************
* Copyright (c) 2025 ARM Limited. All rights reserved.
* **********************************************************/

/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of ARM Limited nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL ARM LIMITED OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

#ifndef _offsets_h
#define _offsets_h

/* The following macros are used in .asm files to refer to C structs.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general approach seems good: take advantage of our use of the preprocessor in our asm files to share a header with a checking file.

* The file offsets.c checks at compile time that they have the correct
* values.
*/

#define dcontext_t_OFFSET_dstack 0x9f8

#endif /* _offsets_h */
9 changes: 4 additions & 5 deletions core/arch/arm/arm.asm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* ARM-specific assembly and trampoline code
*/

#include "offsets.h"
#include "../asm_defines.asm"
START_FILE
#include "include/syscall.h"
Expand All @@ -54,10 +55,8 @@ DECL_EXTERN(initstack_mutex)
#define RESTORE_FROM_DCONTEXT_VIA_REG(reg,offs,dest) ldr dest, PTRSZ [reg, POUND (offs)]
#define SAVE_TO_DCONTEXT_VIA_REG(reg,offs,src) str src, PTRSZ [reg, POUND (offs)]

/* offsetof(dcontext_t, dstack) */
#define dstack_OFFSET 0x170
/* offsetof(dcontext_t, is_exiting) */
#define is_exiting_OFFSET (dstack_OFFSET+1*ARG_SZ)
#define is_exiting_OFFSET (dcontext_t_OFFSET_dstack+1*ARG_SZ)

/* The struct priv_mcontext_t is defined in mcxtx_api.h. */
#define PRIV_MCXT_SIZE 0x148 /* sizeof(priv_mcontext_t) */
Expand Down Expand Up @@ -139,7 +138,7 @@ GLOBAL_LABEL(dr_call_on_clean_stack:)
mov REG_R4, REG_SP /* save sp across the call */
mov REG_R5, ARG2 /* save function in non-param reg */
/* Swap stacks */
RESTORE_FROM_DCONTEXT_VIA_REG(REG_R0, dstack_OFFSET, REG_SP)
RESTORE_FROM_DCONTEXT_VIA_REG(REG_R0, dcontext_t_OFFSET_dstack, REG_SP)
/* Set up args */
sub REG_SP, #(4*ARG_SZ)
ldr REG_R0, [REG_R4, #(11*ARG_SZ)]
Expand Down Expand Up @@ -286,7 +285,7 @@ GLOBAL_LABEL(cleanup_and_terminate:)
mov REG_R4, #0 /* save 0 for dstack to avoid double-free */
b cat_done_saving_dstack
cat_save_dstack:
RESTORE_FROM_DCONTEXT_VIA_REG(REG_R4, dstack_OFFSET, REG_R4)
RESTORE_FROM_DCONTEXT_VIA_REG(REG_R4, dcontext_t_OFFSET_dstack, REG_R4)
cat_done_saving_dstack:
CALLC0(GLOBAL_REF(get_cleanup_and_terminate_global_do_syscall_entry))
mov REG_R5, REG_R0
Expand Down
44 changes: 44 additions & 0 deletions core/arch/arm/offsets.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* **********************************************************
* Copyright (c) 2025 ARM Limited. All rights reserved.
* **********************************************************/

/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of ARM Limited nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL ARM LIMITED OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

#include <stddef.h>

#include "../globals.h"
#include "offsets.h"

/* This is an alternative to using static_assert that will work with
* any C compiler. If a macro is incorrectly defined there will be a
* compile-time error that clearly shows which macro is wrong.
*/
struct _unused {
int x1[dcontext_t_OFFSET_dstack == offsetof(dcontext_t, dstack) ? 1 : -1];
};
43 changes: 43 additions & 0 deletions core/arch/arm/offsets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* **********************************************************
* Copyright (c) 2025 ARM Limited. All rights reserved.
* **********************************************************/

/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of ARM Limited nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL ARM LIMITED OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

#ifndef _offsets_h
#define _offsets_h

/* The following macros are used in .asm files to refer to C structs.
* The file offsets.c checks at compile time that they have the correct
* values.
*/

#define dcontext_t_OFFSET_dstack 0x170

#endif /* _offsets_h */
Loading