Skip to content

Commit

Permalink
Add a hook for the cinder interpreter loop in third-party/python/3.12
Browse files Browse the repository at this point in the history
Summary:
Add a hook for the cinder interpreter loop in third-party/python/3.12

* Add version guards to reflect the changed signature of `_PyFrameEvalFunction` between 3.10 and 3.12
* Link `cinderx/Interpreter:Interpreter3.12` into `cinderx:_cinderx-lib`
* Add `Include/cinder/hooks.h` in `third-party/python/3.12`
* Add a call to cinderx's `Ci_hook_EvalFrame` from `third-party/python/3.12`

NOTE: The hook is currently not initialised, so that we can check this code in without all the runtime tests passing. This seems to be the best way to break the large diff up into smaller pieces.

Reviewed By: jbower-fb

Differential Revision: D62222196

fbshipit-source-id: 87384e77d40a67795f596c160c09c11de686f8f7
  • Loading branch information
Martin DeMello authored and facebook-github-bot committed Oct 8, 2024
1 parent f290b9e commit 9b84a02
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
34 changes: 34 additions & 0 deletions Include/cinder/ci_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

/*
* Macros for marking components in core CPython we currently export for the
* CinderX module. This includes things added by Cinder and things which
* already existed but which weren't public.
*
* The intent is grepping for "CiAPI" reveals everything the CinderX module may
* depend on in the core CPython code. Eliminating all of these is one of the
* prerequisites for CinderX being compatible with non-Cinder Python.
*/

// These function the same as PyAPI_* - exporting symbols for use in .so's etc.
#define CiAPI_FUNC(RTYPE) __attribute__ ((visibility ("default"))) RTYPE
#ifdef __clang__
# ifdef __cplusplus
# define CiAPI_DATA(RTYPE) __attribute__ ((visibility ("default"))) extern "C" RTYPE
# else
# define CiAPI_DATA(RTYPE) __attribute__ ((visibility ("default"))) extern RTYPE
# endif
#else
# ifdef __cplusplus
# define CiAPI_DATA(RTYPE) extern "C" __attribute__ ((visibility ("default")))RTYPE
# else
# define CiAPI_DATA(RTYPE) extern __attribute__ ((visibility ("default"))) RTYPE
# endif
#endif

// Clang seems to (always?) make symbols for static inline functions.
#ifdef __clang__
# define CiAPI_STATIC_INLINE_FUNC(RTYPE) static inline __attribute__ ((visibility ("default"))) RTYPE
#else
# define CiAPI_STATIC_INLINE_FUNC(RTYPE) static inline RTYPE
#endif
18 changes: 18 additions & 0 deletions Include/cinder/hooks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include "cinder/ci_api.h"
#include "pystate.h" // PyThreadState

/* Hooks needed by CinderX that have not been added to upstream. */

CiAPI_DATA(_PyFrameEvalFunction) Ci_hook_EvalFrame;

#ifdef __cplusplus
} // extern "C"
#endif
10 changes: 9 additions & 1 deletion Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@
#include "setobject.h"
#include "structmember.h" // struct PyMemberDef, T_OFFSET_EX

#include "cinder/hooks.h"

#include <ctype.h>
#include <stdbool.h>


#ifdef Py_DEBUG
/* For debugging the interpreter: */
# define LLTRACE 1 /* Low-level trace feature */
Expand Down Expand Up @@ -629,7 +632,6 @@ static inline void _Py_LeaveRecursiveCallPy(PyThreadState *tstate) {
tstate->py_recursion_remaining++;
}


/* Disable unused label warnings. They are handy for debugging, even
if computed gotos aren't used. */

Expand All @@ -650,6 +652,12 @@ static inline void _Py_LeaveRecursiveCallPy(PyThreadState *tstate) {
PyObject* _Py_HOT_FUNCTION
_PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
{
// TODO(T170700384): We need to teach a few things to treat
// tstate->interp->eval_frame as a stack before we can de-hookify this.
if (Ci_hook_EvalFrame != NULL) {
return Ci_hook_EvalFrame(tstate, frame, throwflag);
}

_Py_EnsureTstateNotNULL(tstate);
CALL_STAT_INC(pyeval_calls);

Expand Down
7 changes: 7 additions & 0 deletions Python/cinderhooks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

#include "Python.h"
#include "cinder/hooks.h"

/* Interpreter */
_PyFrameEvalFunction Ci_hook_EvalFrame = NULL;
1 change: 1 addition & 0 deletions capi-functions
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
_Py*;
/* Additional Meta (Cinder) C-API functions to export */
_Ci*;
Ci_*;
/* Export strobe symbols needed for pyperf */
__strobe_*;
/* CinderX's test runner uses this to redirect ASAN output */
Expand Down

0 comments on commit 9b84a02

Please sign in to comment.