执行 python 脚本时 Gem5 不工作

Gem5 not working when executing a python script


我正在尝试执行一个简单的 python 脚本并传递命令行参数以在 gem5 中简单地添加数字 命令:

sudo ./build/X86/gem5.opt configs/example/se.py --cmd /usr/bin/python3 --options "sum.py 3 4"

sum.py源代码:

import sys
x=int(sys.argv[1])
y=int(sys.argv[2])
sum=x+y
print("The addition is :",sum)

我得到的错误:

osboxes@osboxes:~/gem5$ sudo ./build/X86/gem5.opt configs/example/se.py --cmd /usr/bin/python3 --options "sum.py 3 4"
gem5 Simulator System.  http://gem5.org
gem5 is copyrighted software; use the --copyright option for details.

gem5 version 21.0.0.0
gem5 compiled Aug  5 2021 21:03:24
gem5 started Aug 11 2021 20:44:15
gem5 executing on osboxes, pid 4072
command line: ./build/X86/gem5.opt configs/example/se.py --cmd /usr/bin/python3 --options 'sum.py 3 4'

warn: membus.slave is deprecated. `slave` is now called `cpu_side_ports`
warn: membus.slave is deprecated. `slave` is now called `cpu_side_ports`
warn: membus.slave is deprecated. `slave` is now called `cpu_side_ports`
warn: membus.slave is deprecated. `slave` is now called `cpu_side_ports`
warn: membus.slave is deprecated. `slave` is now called `cpu_side_ports`
warn: membus.master is deprecated. `master` is now called `mem_side_ports`
warn: membus.master is deprecated. `master` is now called `mem_side_ports`
warn: membus.slave is deprecated. `slave` is now called `cpu_side_ports`
Global frequency set at 1000000000000 ticks per second
warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
warn: Not reserving swap space. May cause SIGSEGV on actual usage
0: system.remote_gdb: listening for remote gdb on port 7000
** REAL SIMULATION **
info: Entering event queue @ 0.  Starting simulation...
warn: ignoring syscall access(...)
warn: ignoring syscall access(...)
warn: ignoring syscall access(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall access(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall access(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall access(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall access(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall access(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall access(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall mprotect(...)
warn: ignoring syscall set_robust_list(...)
warn: ignoring syscall rt_sigaction(...)
      (further warnings will be suppressed)
warn: ignoring syscall rt_sigprocmask(...)
      (further warnings will be suppressed)
fatal: Syscall 318 out of range
Memory Usage: 705664 KBytes

任何人都可以让我知道我在执行 python 脚本时哪里出错了。任何帮助将不胜感激。

谢谢

系统调用 #318 是 __NR_getrandom x86-64 Linux(参见 asm/unistd_64.h)。请参阅系统调用的 getrandom(2) man page。 CPython 在可用时使用它而不是 open("/dev/urandom"),作为 os.getrandom 实现的一部分。 (仅通过更改 .py 你 运行 解释器来避免调用它可能不切实际,甚至不可能避免调用它。)

它很新,例如getrandom syscall in C not found 提到 2017 年的许多 GNU/Linux 发行版尚未更新为包含包装函数的 glibc(以便从 C 调用)。所以我对 gem5 的单进程模式(不是系统模式)没有它的处理程序并不感到惊讶。

https://bugs.python.org/issue27955 表示 Python 的 os.getrandom 将尝试使用它,只有在它不存在时才返回到 open("/dev/urandom")(即 returns -ENOSYS)。该问题是 NAS 内核 returning -EPERM,通过让 CPython 也将其视为不可用来解决。

你的情况是相似的:GEM5 没有为它不知道的系统调用 returning -ENOSYS(在这种情况下会触发优雅的回退),而是对待它是一个致命错误。 这意味着 CPython 没有办法保持安全,除非首先从不尝试使用该系统调用。 python3 的旧版本可能是安全的,或者注释掉乐观尝试的自定义构建。

如果有办法改变 GEM5 的行为,那也可以。例如找到打印该错误的代码,并将其更改为 return -ENOSYS 以表示不受支持的呼叫号码,就像真正的 Linux 内核一样。 (如果还没有 GEM5 选项可以做到这一点。)

TL:DR:您的选择之一是:

  • 找到一个 GEM5 选项来以不同方式处理未知系统调用(如果有)。
  • 或:将 GEM5 源代码更改为 return -ENOSYS 而不是因该致命错误而中止并从该源构建 GEM5。
  • 或者:在 C 中更改其 os.getrandom 实现后从源代码重建 CPython
  • 或者:在尝试使用 Linux getrandom(2).
  • 之前使用旧的 python3 二进制文件

CPython解释器不是一个简单的小程序;它是用 C 语言编写的,而且很大。

请记住,您正在使用 .py 分析 /usr/bin/python3 作为已编译二进制可执行文件的 输入数据 这就是您正在分析的内容;请记住,CPython 纯粹是一个解释器。

您的“简单加法”绝不会直接 ​​运行宁作为本机机器代码,除了解释器最终将分派给加法运算符的处理程序,该处理程序将检查输入是否正确是否“简单”(适合 Python 的扩展精度整数数据结构的一大块),如果是这样的话,那么做特殊情况可能涉及 C + 操作,可能编译为 x86 addlea 指令,但 很多 代码只是为了达到这一点。大量的解释器开销与编译的 C 程序。