HAL  v4.5.0-251-g305763a14
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 
119  bool interpretBackground(QObject* caller, const QString& input, bool multiple_expressions = false);
120 
127  void interpretScript(QObject* caller, const QString& input);
128 
134  void forwardStdout(const QString& output);
135 
141  void forwardError(const QString& output);
142 
147  void scheduleClear();
148 
152  void scheduleReset();
153 
159  void setConsole(PythonConsole* console);
160 
171  std::vector<std::tuple<std::string, std::string>> complete(const QString& text, bool use_console_context);
172 
178  int checkCompleteStatement(const QString& text);
179 
180  // void set_history_length();
181 
182  // void read_history_file(const QString& file);
183  // void write_history_file(const QString& file);
187  void initPython();
188 
192  void closePython();
193 
197  void updateNetlist();
198 
199  PythonThread* pythonThread() const;
200 
201  static void initializeContext(py::dict* context);
202  static void initializeScript(py::dict* context);
203 
204  void abortThread();
205  void abortThreadAndWait();
206  bool isThreadRunning() const { return mThread != nullptr; }
207 
208  private Q_SLOTS:
209  void handleThreadFinished();
210  void handleScriptOutput(const QString& txt);
211  void handleScriptError(const QString& txt);
212  void handleInputRequired(int type, const QString& prompt, const QVariant& defaultValue);
213  void handleConsoleInputReceived(const QString& input);
214 
215  private:
216 
217  void handleReset();
218  void handleClear();
219 
220  // these have to be pointers, otherwise they are destructed after py::finalize_interpreter and segfault
221  // only one object for global and local is needed, as for the console we run it always in global scope wher globals() == locals()
222  py::dict* mContext;
223 
224  PythonContextSubscriber* mSender;
225 
226  std::string mHistoryFile;
227 
228  PythonConsole* mConsole;
229  bool mTriggerReset;
230  bool mTriggerClear;
231  LayoutLocker* mLayoutLocker;
232  PythonThread* mThread;
233  bool mThreadAborted;
234  PyThreadState* mMainThreadState;
235  QObject* mInterpreterCaller;
236 
237  void startThread(const QString& input, bool singleStatement);
238  };
239 } // 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