HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
python_context.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
32 
33 #include <QString>
34 #include <QObject>
35 
36 #pragma GCC diagnostic push
37 #pragma GCC diagnostic ignored "-Wshadow"
38 #include "pybind11/embed.h"
39 #include "pybind11/eval.h"
40 #include "pybind11/functional.h"
41 #include "pybind11/operators.h"
42 #include "pybind11/stl.h"
43 #include "pybind11/stl_bind.h"
44 #pragma GCC diagnostic pop
45 
46 namespace hal
47 {
48  class PythonContextSubscriber;
49  class PythonEditor;
50  class PythonThread;
51  class LayoutLocker;
52 
57  {
58  Q_OBJECT
59  PythonThread* mThread;
60  public Q_SLOTS:
61  void handleGatesPicked(const QSet<u32>& gats) override;
62  public:
64  : GateSelectReceiver(parent), mThread(thread) {;}
65  };
66 
71  {
72  Q_OBJECT
73  PythonThread* mThread;
74  public Q_SLOTS:
75  void handleModulesPicked(const QSet<u32>& gats) override;
76  public:
78  : ModuleSelectReceiver(parent), mThread(thread) {;}
79  };
80 
81  namespace py = pybind11;
89  class __attribute__((visibility("default"))) PythonContext : public QObject
90  {
91  Q_OBJECT
92  public:
96  PythonContext(QObject* parent=nullptr);
97 
101  ~PythonContext();
102 
108  void interpretForeground(const QString& input);
109 
118  void interpretBackground(QObject* caller, const QString& input, bool multiple_expressions = false);
119 
126  void interpretScript(QObject* caller, const QString& input);
127 
133  void forwardStdout(const QString& output);
134 
140  void forwardError(const QString& output);
141 
146  void scheduleClear();
147 
151  void scheduleReset();
152 
158  void setConsole(PythonConsole* console);
159 
170  std::vector<std::tuple<std::string, std::string>> complete(const QString& text, bool use_console_context);
171 
177  int checkCompleteStatement(const QString& text);
178 
179  // void set_history_length();
180 
181  // void read_history_file(const QString& file);
182  // void write_history_file(const QString& file);
186  void initPython();
187 
191  void closePython();
192 
196  void updateNetlist();
197 
198  PythonThread* pythonThread() const;
199 
200  static void initializeContext(py::dict* context);
201  static void initializeScript(py::dict* context);
202 
203  void abortThread();
204  void abortThreadAndWait();
205  bool isThreadRunning() const { return mThread != nullptr; }
206 
207  private Q_SLOTS:
208  void handleThreadFinished();
209  void handleScriptOutput(const QString& txt);
210  void handleScriptError(const QString& txt);
211  void handleInputRequired(int type, const QString& prompt, const QVariant& defaultValue);
212  void handleConsoleInputReceived(const QString& input);
213 
214  private:
215 
216  void handleReset();
217  void handleClear();
218 
219  // these have to be pointers, otherwise they are destructed after py::finalize_interpreter and segfault
220  // only one object for global and local is needed, as for the console we run it always in global scope wher globals() == locals()
221  py::dict* mContext;
222 
223  PythonContextSubscriber* mSender;
224 
225  std::string mHistoryFile;
226 
227  PythonConsole* mConsole;
228  bool mTriggerReset;
229  bool mTriggerClear;
230  LayoutLocker* mLayoutLocker;
231  PythonThread* mThread;
232  bool mThreadAborted;
233  PyThreadState* mMainThreadState;
234  QObject* mInterpreterCaller;
235 
236  void startThread(const QString& input, bool singleStatement);
237  };
238 } // namespace hal
An interactable python console.
Interface for pybind11.
bool isThreadRunning() const
Interface for handling python outputs.
PythonGateSelectionReceiver(PythonThread *thread, QObject *parent=nullptr)
void handleGatesPicked(const QSet< u32 > &gats) override
PythonModuleSelectionReceiver(PythonThread *thread, QObject *parent=nullptr)
void handleModulesPicked(const QSet< u32 > &gats) override
Definition: defines.h:45
PinType type
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
QObject * parent() const const
QThread * thread() const const