GithubHelp home page GithubHelp logo

nju-projectn / nemu Goto Github PK

View Code? Open in Web Editor NEW
817.0 817.0 174.0 380 KB

NJU EMUlator, a full system x86/mips32/riscv32/riscv64 emulator for teaching

License: Other

Makefile 3.97% C 90.10% C++ 0.77% Assembly 0.16% Lex 1.86% Yacc 3.14%

nemu's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nemu's Issues

[sdb.c:48] help command

Help command's description should be display information ...... .
Because information is an uncountable noun.

PA is a wonderful course. Thanks a lot.

an error

I met a error that the lecture does not mention,i retry to download all the files ,but i met the error again .And i spend a lot of time searching for the answer but i still have no idea how to fix it.Maybe it is my computer's problem? I hope you can give me some suggestions.Thank you.

problem description

this error happened when i compile the project by make ,then the error occured. I can roughly understand the problem , but i can solve it .
my environment is the virtual machine(VMware) and the ubuntu 20.04 system

src/utils/disasm.cc:55:8: error: ‘class llvm::MCInstPrinter’ has no member named ‘setPrintBranchImmAsAddress’
   55 |   gIP->setPrintBranchImmAsAddress(true);
      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~
make: *** [/home/tom/ics2021/nemu/scripts/build.mk:40: /home/tom/ics2021/nemu/build/obj-riscv32-nemu-interpreter/src/utils/disasm.o] Error 1

fedora 下 make 失败

make menuconfig 是成功的,但是接下来 make 编译的时候失败了,根据提示在 script/build.mk 第 54 行添加了 -fPIE。但是,依然报了同样的错误,我对 gcc 不是很熟悉,关于这个错误,我该怎么解决它呢?

OS: fedora36
llvm: 14.0.0
gcc: 12.1.1
g++: 12.1.1

make[1]: Entering directory '/home/yuan/Documents/ysyx-workbench'
make[1]: Leaving directory '/home/yuan/Documents/ysyx-workbench'
+ LD /home/yuan/Documents/ysyx-workbench/nemu/build/riscv32-nemu-interpreter
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/device/io/map.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/device/io/mmio.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/device/io/port-io.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/engine/interpreter/hostcall.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/cpu/cpu-exec.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/cpu/difftest/ref.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/monitor.o: relocation R_X86_64_32S against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/sdb/expr.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/sdb/sdb.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/sdb/watchpoint.o: relocation R_X86_64_32 against `.bss' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/utils/log.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/memory/paddr.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/isa/riscv32/inst.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
collect2: error: ld returned 1 exit status
make: *** [/home/yuan/Documents/ysyx-workbench/nemu/scripts/build.mk:54: /home/yuan/Documents/ysyx-workbench/nemu/build/riscv32-nemu-interpreter] Error 1

建议将PA GitBook中的部分图表修改

如题,在GitBook中,有一些使用字符表示的图标,例如PA2.4中“现在我们就可以厘清符号表和字符串表之间的关系了:”的后面。由于PA本身显示代码块的样式使得行间距很大,加之符号本身也并不是很清晰,导致看起来不够明确。建议调整行间距,或者改成相应的图片显示。
举例,在行间距较小的github中,行间距小,是这样的:

   LOAD       +-- 0x001000  0x03000000  0x03000000 +0x1d600  0x27240  RWE  0x1000
                               |                       |       |     
                               |   +-------------------+       |     
                               |   |                           |     
                               |   |     |           |         |       
                               |   |     |           |         |      
                               |   |     +-----------+ ---     |     
                               |   |     |00000000000|  ^      |   
                               |   | --- |00000000000|  |      |    
                               |   |  ^  |...........|  |      |  
                               |   |  |  |...........|  +------+
                               |   +--+  |...........|  |      
                               |      |  |...........|  |     
                               |      v  |...........|  v    
                               +-------> +-----------+ ---  
                                         |           |     
                                         |           |    
                                            Memory

但在GitBook网页中,行间距大,看起来却是:
image

fatal error: llvm/Support/TargetRegistry.h: No such file or directory

I have put forward a issues afternoon,but later i found that my ubuntu version is 20.04 and the requested is 21.04 . so i think the problem
is the version of llvm in my machine is too low . so i update it to llvm 14 -- the latest version .Howerver,i met another problem following

src/utils/disasm.cc:5:10: fatal error: llvm/Support/TargetRegistry.h: No such file or directory
    5 | #include "llvm/Support/TargetRegistry.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

i think the reason of this maybe i need to update other things ,but i reinstall all the things ,the error still exist.And i try to search for the answer but i haven't find it .And because i have used the ubuntu 20.04 for a long peroid of time ,so i haven't decide to change it to ubuntu 21.04.I hope you can give me some tips on how to solve this .Thank you.

question about the difftest code

I have a question about the difftest
in the file src/isa/riscv64/local-include/reg.h
static inline const char* reg_name(int idx, int width) { extern const char* regs[]; return regs[check_reg_idx(idx)]; }
why there is a parameter 'width' not used

无法在riscv32-pc–linux–gnu的配置下完成编译

您好,我在centos平台上编译nemu,发现出错的位置在init_disasm函数中,我尝试了去除MUXDEF,直接采用i686-pc–linux–gnu或者mipsel-pc–linux–gnu作为该函数的参数,发现编译成功。在init_disasm函数中,我发现它调用四个llvm的初始化函数,它们引入了一些后缀名为def的文件,我打开这些文件发现没有riscv,从而我尝试在.def文件中加入RISCV,但并没有解决该问题。我怀疑问题出在llvm上(12.0版),但是找不到解决方法,期待您的回复。

根据您的讲义,我重新安装了llvm,版本为11.0,目前已经能够成功编译NEMU。

A quetion about difftest's implement in `src/cpu/difftest/dut.c`

Hi, I'm not a student from NJU, but learning NJU's ICS accroding to ICS offcial website && open-source code right now. It's a very fantasitic experience to me! I have already learnt a lot from this course despite I'm just on the way to finish lab2.4! I want to say thank you for your guys hard works in this course and generosity at first!

So here is my question.

checkregs(&ref_r, pc);

checkregs need a PC parameter, I think that means we should check pc's value in it's definition. But after ref_difftest_exec, the PC value in ref's CPU_STATE should be next_pc. So I think we should pass next_pc or npc when calling checkregs just like the code below
checkregs(&ref_r, npc);

But ICS course gives student freedom to exploring by themselves. So I'm doubting whether it's a mistake, or it's on purpose for training student, or I'm just totally wrong like the code should be works fine(or works in some ISAs).

At last, very thanks for your guys again!

请问哪里可以找到 x86 的 isa?

  • clone PA2022 实验册,在 nemu/src/isa 这个文件夹只有 riscv32 以及 riscv64 这两个 ISA 指令集架构。
  • make menuconfig 选择 x86 的 ISA 之后,再进行 make 编译,发现它无法找到 x86isa-def.h 文件。

而这里的仓库也没有 x86 的 ISA,只有 riscv32riscv64mips32以及longarch32 的 ISA。
我在 GitHub 找了好久也没有发现,所以请问哪里可以找见?

error: array subscript ‘struct gdb_conn[0]’ is partly outside array bounds of ‘unsigned char[8]’

This error is raised after i upgraded my system from ubuntu 20.04 to 22.04.
The whole error log is as follows:

  • CC src/protocol.c
    src/protocol.c: In function ‘gdb_begin_inet’:
    src/protocol.c:72:13: error: array subscript ‘struct gdb_conn[0]’ is partly outside array bounds of ‘unsigned char[8]’ [-Werror=array-bounds]
    72 | conn->ack = true;
    | ^
    src/protocol.c:68:27: note: referencing an object of size 8 allocated by ‘calloc’
    68 | struct gdb_conn *conn = calloc(1, sizeof(struct gdb_conn *));
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    src/protocol.c:80:12: error: array subscript ‘struct gdb_conn[0]’ is partly outside array bounds of ‘unsigned char[8]’ [-Werror=array-bounds]
    80 | conn->in = fdopen(fd, "rb");
    | ~~~~~~~~~^~~~~~~~~~~~~~~~~~
    src/protocol.c:68:27: note: referencing an object of size 8 allocated by ‘calloc’
    68 | struct gdb_conn *conn = calloc(1, sizeof(struct gdb_conn *));
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    src/protocol.c:85:13: error: array subscript ‘struct gdb_conn[0]’ is partly outside array bounds of ‘unsigned char[8]’ [-Werror=array-bounds]
    85 | conn->out = fdopen(fd2, "wb");
    | ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
    src/protocol.c:68:27: note: referencing an object of size 8 allocated by ‘calloc’
    68 | struct gdb_conn *conn = calloc(1, sizeof(struct gdb_conn *));
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    src/protocol.c:91:3: error: array subscript ‘struct gdb_conn[0]’ is partly outside array bounds of ‘unsigned char[8]’ [-Werror=array-bounds]
    91 | fflush(conn->out);
    | ^~~~~~~~~~~~~~~~~
    src/protocol.c:68:27: note: referencing an object of size 8 allocated by ‘calloc’
    68 | struct gdb_conn *conn = calloc(1, sizeof(struct gdb_conn *));
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    cc1: all warnings being treated as errors
    make[1]: *** [Makefile:33: build/obj-riscv32/protocol.o] Error 1

i try to fix this issue by disabling the "Werror=array-bounds" check by following codes added in the makefile:
CFLAGS += -Wno-array-bounds

Then i successfully "make ISA=riscv run", and start nemu.

So could you help to check if these codes have some bug with ubuntu 22.04, or there may have some other ways to fix it.
Thanks

在PA2.3,安装Spike之后再次运行nemu遇到了问题(disasm.cc链接错误)

试着根据该区域的讲义中进行调试,但无论如何均没有效果。
STFW,但没有查询到有关信息。
第一次编译出现了绿框的“可以忽略如下结果”的提示,也可以进入nemu,但再次make run便出错。
已安装device-tree-compiler,反复查看了讲义。
重装了一次llvm,确定了版本是llvm11;也查看了PA0的末尾的设置和NEMU的issue以修改,均无用。
报错信息如图:
捕获
捕获2
捕获3

编译【spike-diff】时终端报【error: 'optional' in namespace 'std' does not name a template type】错误

开发环境
系统版本:WSL2 Ubuntu 20.04.5 LTS
GCC版本:9.4.0 (Ubuntu 9.4.0-1ubuntu~20.04.1)
指令架构:riscv64

问题描述
在开启CONFIG_DIFFTEST宏之后,在nemu目录下执行make run命令,终端会报很多类似下图中的错误。
image

原因分析
结合报错信息上网查找相关资料,得知optional是C++17标准中引入的新功能,而9.4.0版本的GCC默认使用的是C++11标准,所以需要在编译时通过参数指定C++标准版本。于是接下来我排查了动态生成的spike-diff/repo/build/Makefile文件,发现C++编译选项中已经添加了-std=c++17参数,即已经开启了对C++17标准的支持。但是为什么还会出现这种问题?optional是在GCC 7.2中首次从实验标准转为正式标准,GCC 9中肯定也存在。

后来终于发现在nemu/tools/spike-diff/Makefile中添加-std=c++17能编译成功,可是编译出错的位置是在spike仓库内部,并不在spike-diff中,说明在编译生成spike的静态链接库时编译依赖会向上传递?总之,该问题应该与GCC版本有关,某些版本的编译器需要手动添加参数。

问题发现者
@Jasonyanyusong
@myyerrol

解决方案
如下图所示,在nemu/tools/spike-diff/Makefile文件中的编译选项中添加-std=c++17即可。
image

Suggestion: PA2的顺序修改

在PA2第一部分"RTFSC(2)"的末尾,要求学生通过stringhello-str以外的所有测试样例,但difftest到PA2最后一部分才提出,这让习惯一步一步做的学生可能花费大量低效的时间来调试实现错误的指令。建议将difftest移到第一部分最后面。

Reconsider the usage of global temporary RTL registers

My suggestion is that instead of defining global at t0 t1 t2 and praying for no conflicts, we should encourge local definitions, which means any needed intermediate "registers" should be put into the corresponding function scope. e.g.

/// rtlreg_t at; 
// ...
void interpret_rtl_addi(const rtlreg_t* dest, const rtlreg_t* src, int imm){
   /// rtl_li(&at, imm);
   /// rtl_add(dest, src, &at); 
   rtlreg_t imm_at; 
   rtl_li(&imm_at, imm)
   rtl_add(dest, src, &imm_at); 
}

I expect some arguments like "this is simulation of hardware behavior". However, I'm somewhat familiar with Verilog, and I don't think there's a counterpart of RTL temporary registers like at.

Maybe it is a concept borrowed from MIPS, but AFAIK they are not sharing lots of similarities.

As we know, a graph of IC is a combination of synchronized circuits and combinatorial circuits, with the latter stateless and the former stateful.

Obviously, due to C language semantics, a globally defined at rtl register is more like a part of global state. However, the usage of temporary RTL registers(e.g. the "at" in rtl_li(&at, imm)), are basically something like wire in verilog, which is combinatorial, thus stateless.

What's more, when we refer to intel manual, the defnition of temporary registers is quite arbitrary, instead of sticking to some fixed set of temporary registers. Take near relative CALL in 64bit as an example:

tempDEST ← SignExtend(DEST); (* DEST is rel32 *)
tempRIP ← RIP + tempDEST;
IF stack not large enough for a 8-byte return address
THEN #SS(0); FI;
Push(RIP);
RIP ← tempRIP;

Here tempXXXs are local and stateless. Though we can use global registers like at t0 t1 to simulate this behavior, it is not intuisive and invites unexpected bugs.

In conclusion, we should encourge local definitions in function scopes, especially RTL ones. It will benifit code readability and robustness dramatically.

一个已经解决的问题(但还是想报告给老师,大概是Ubuntu22.04下的bug)

在/abstract-machine/am/src/native/platform.h中,__am__cpu_t结构体中有一个变量:uint8_t sigstack[SIGSTKSZ]
在用native测试时会显示‘variably modified array at file scope’,STFW后发现是不能把常量作为数组的大小。
https://stackoverflow.com/questions/13645936/variably-modified-array-at-file-scope-in-c)
暂时的解决办法是,输出SIGSTKSZ的值(在我的Ubuntu22,.04下是8192),换为uint8_t sigstack[8192]中即可解决这个问题。
另一个问题是,用native会提示缺少"SDL2/SDL.h",似乎Ubuntu22.04不自带这个库。我在解决的这个问题中遇到了一些麻烦,但最后还是解决了。
我知道下个学期上课后我会重做PA,应该也会更新用Ubuntu22.04来进行(我目前用Ubuntu22.04来做ICS2021,我自知是不合适的),于是我想报告一下这个问题(因为讲义上这两个问题都没见到),不知道这是否是需要被更新的问题,还是只是我个人的问题。

make menuconfig failed on Gentoo Linux

Running make menuconfig on Gentoo Linux can produce following error message even if nucrses being installed:
/usr/lib/gcc/x86_64-pc-linux-gnu/12/../../../../x86_64-pc-linux-gnu/bin/ld: /home/pzy/Documents/Sync/ics2022/nemu/tools/kconfig/build/obj-mconf/lxdialog/util.o: undefined reference to symbol 'nodelay' /usr/lib/gcc/x86_64-pc-linux-gnu/12/../../../../x86_64-pc-linux-gnu/bin/ld: /usr/lib64/libtinfo.so.6: error adding symbols: DSO missing from command line
It turns out to be the problem of Gentoo's "tinfo" USE flag, this use flag forces some symbols from libncurses.so to be moved to libtinfo.so.
This can be easily fixed bu adding "-ltinfo" after LIBS += -lncurses of /nemu/tools/kconfig/Makefile

However, I'm wondering if there is need to add this fix to source code or if pkg-config can be intruduce to source code to generate needed libraries to compeletely avoid this problem?

指令实现部分宏层数太多了

宏一层层套 看起来非常费劲 严重牺牲了可读性 本来指令很简单的 我想学生在做作业的时候一定也花了大量时间去理清楚这些宏

执行make时编译失败,提示未找到头文件isa-def.h

图片

您好,在我正常执行了make menuconfig (并未修改任何甚至)并退出后,执行make出现如上错误,请问可能是由什么原因导致的呢
系统是ubuntu 22.04

执行步骤:

  • 安装依赖
  • 克隆至本地 ~/Lab/nemu ~/Lab/abstract-machine
  • 执行 export AM_HOME=~/Lab/abstract-machine
  • export NEMU_HOME=~/Lab/nemu
  • make menuconfig
  • make
  • 出现错误

make_token()中的no match报错输出对长到折行的表达式不友好

nemu/src/monitor/debug/expr.c: 91行在无法匹配时输出错误位置的代码:
printf("no match at position %d\n%s\n%*.s^\n", position, e, position, "");
没有考虑e过长导致折行的情况,这样可能导致'^'指向错误的位置,希望能在guidebook中增加相关提示。
例:
tim 20181207181906
这个例子里我检测十六进制表达的正则实现有误,但是由于表达式过长造成折行,导致‘^‘指向了折行后的'-'。

启用difftest后出现段错误

我使用GDB调试后定位出错的位置如下图标记
image
已经尝试上网寻找但无果,希望老师看到能解答一下

关于声卡的适配

老师您好,我在PA2中实现了声卡并通过了Audio_test(有声音)。
但带音效的红白机版本我无法正常运行,会卡在Initialize audio处不再继续,开始会有一小段电流声。经检测,将声音改为HQ_SOUND_NONE可以正常使用。请问这个版本的PA是否可以使用声卡?

src/utils/disasm.cc:21:10: fatal error: llvm/MC/MCAsmInfo.h: No such file or directory

/ics2023/nemu$ make
make: llvm-config: No such file or directory
+ CXX src/utils/disasm.cc
src/utils/disasm.cc:21:10: fatal error: llvm/MC/MCAsmInfo.h: No such file or directory
   21 | #include "llvm/MC/MCAsmInfo.h"
      |          ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [/home/ubuntu/Home/Multipass_Files/ics2023/nemu/scripts/build.mk:40: /home/ubuntu/Home/Multipass_Files/ics2023/nemu/build/obj-riscv32-nemu-interpreter/src/utils/disasm.o] Error 1

Ubuntu 22.04.3 LTS

考虑添加license声明

似乎这个项目没有license,我在源码中也没找到相关声明。

觉得做为开源项目很有必要添加一个license。主要下游打包和有些用户可能需要明确的license,比如gentoo每个包都要license。其次是永远无法预测谁会用何种方式滥用这个项目,之前有过些经典案例。

It may not work well in WSL2

I've tried to follow the procedure in tutorial, and an error occur when I try to compile it.

+ CXX src/utils/disasm.cc
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
+ LD $NEMU_HOME$/build/riscv32-nemu-interpreter

Native Ubuntu is supported systemd, but not in WSL. So I try to replace the "systemd command" with "sysvinit command", but find no way to start. So is there any suggerstion for me to start?

issue about building nemu in WSL of ubuntu20.04 instance

try to setup NEMU in WSL of ubuntu20.04 instance

~/ics2021/nemu$ make

prompts for building information are below
J_MW_}~GS(FVPQT7K4 HWTQ

System has not been booted with systemd as init system (PID 1).
Can't operate.Failed to create bus connection: Host is down

which is a quite typical problem that WSL doesn't use Systemd and systemctl command instead of Sysvinit command. Is there any way to fix this issue without installing other third-party tools for systemctl in WSL?

Mac OS 编译时缺少相关库

make menuconfig 生成配置文件时出现:

ld: library not found for -ltinfo

请问缺少的库如何安装?是否必须在 Linux 下编译?

Memory access out of bound when printing ISA Logo

In src/isa/$ISA/logo.c, isa_logodoesn't have a termination character to indicate the end of a string.

unsigned char isa_logo[] = {
  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x5f, 0x20, 0x20, 0x20, 0x20,
  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x5f, 0x5f, 0x20,
  0x20, 0x5f, 0x5f, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  0x20, 0x20, 0x20, 0x20, 0x5f, 0x20, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
  0x20, 0x28, 0x5f, 0x29, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  0x20, 0x20, 0x20, 0x7c, 0x20, 0x20, 0x5c, 0x2f, 0x20, 0x20, 0x7c, 0x20,
  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7c, 0x20,
  0x7c, 0x0a, 0x20, 0x20, 0x5f, 0x20, 0x5f, 0x5f, 0x20, 0x5f, 0x20, 0x5f,
  0x5f, 0x5f, 0x20, 0x20, 0x5f, 0x5f, 0x5f, 0x20, 0x5f, 0x5f, 0x5f, 0x5f,
  0x5f, 0x5f, 0x5f, 0x5f, 0x20, 0x20, 0x20, 0x5f, 0x5f, 0x20, 0x7c, 0x20,
  0x5c, 0x20, 0x20, 0x2f, 0x20, 0x7c, 0x20, 0x5f, 0x5f, 0x20, 0x5f, 0x20,
  0x5f, 0x20, 0x5f, 0x5f, 0x20, 0x20, 0x5f, 0x20, 0x20, 0x20, 0x5f, 0x20,
  0x20, 0x5f, 0x5f, 0x20, 0x5f, 0x7c, 0x20, 0x7c, 0x0a, 0x20, 0x7c, 0x20,
  0x27, 0x5f, 0x5f, 0x7c, 0x20, 0x2f, 0x20, 0x5f, 0x5f, 0x7c, 0x2f, 0x20,
  0x5f, 0x5f, 0x7c, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5c, 0x20, 0x5c,
  0x20, 0x2f, 0x20, 0x2f, 0x20, 0x7c, 0x20, 0x7c, 0x5c, 0x2f, 0x7c, 0x20,
  0x7c, 0x2f, 0x20, 0x5f, 0x60, 0x20, 0x7c, 0x20, 0x27, 0x5f, 0x20, 0x5c,
  0x7c, 0x20, 0x7c, 0x20, 0x7c, 0x20, 0x7c, 0x2f, 0x20, 0x5f, 0x60, 0x20,
  0x7c, 0x20, 0x7c, 0x0a, 0x20, 0x7c, 0x20, 0x7c, 0x20, 0x20, 0x7c, 0x20,
  0x5c, 0x5f, 0x5f, 0x20, 0x5c, 0x20, 0x28, 0x5f, 0x5f, 0x20, 0x20, 0x20,
  0x20, 0x20, 0x20, 0x20, 0x20, 0x5c, 0x20, 0x56, 0x20, 0x2f, 0x20, 0x20,
  0x7c, 0x20, 0x7c, 0x20, 0x20, 0x7c, 0x20, 0x7c, 0x20, 0x28, 0x5f, 0x7c,
  0x20, 0x7c, 0x20, 0x7c, 0x20, 0x7c, 0x20, 0x7c, 0x20, 0x7c, 0x5f, 0x7c,
  0x20, 0x7c, 0x20, 0x28, 0x5f, 0x7c, 0x20, 0x7c, 0x20, 0x7c, 0x0a, 0x20,
  0x7c, 0x5f, 0x7c, 0x20, 0x20, 0x7c, 0x5f, 0x7c, 0x5f, 0x5f, 0x5f, 0x2f,
  0x5c, 0x5f, 0x5f, 0x5f, 0x7c, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  0x20, 0x5c, 0x5f, 0x2f, 0x20, 0x20, 0x20, 0x7c, 0x5f, 0x7c, 0x20, 0x20,
  0x7c, 0x5f, 0x7c, 0x5c, 0x5f, 0x5f, 0x2c, 0x5f, 0x7c, 0x5f, 0x7c, 0x20,
  0x7c, 0x5f, 0x7c, 0x5c, 0x5f, 0x5f, 0x2c, 0x5f, 0x7c, 0x5c, 0x5f, 0x5f,
  0x2c, 0x5f, 0x7c, 0x5f, 0x7c, 0x0a, /* '\0' should be placed here */
};

Therefore, in src/engine/interpreter/hostcall.c, there is a statement which access isa_logo as a string:

printf(ASNI_FMT("If it is the first case, see\n%s\nfor more details.\n\n"
        "If it is the second case, remember:\n"
        "* The machine is always right!\n"
        "* Every line of untested code is always wrong!\n\n", ASNI_FG_RED), isa_logo);

In the absence of '\0' at the end of isa_logo, memory access is not assured to be within legal bound. Hence, the program is under the risk of global heap overflow issue.

.config does not exists

$ make menuconfig

ics2021/nemu/scripts/config.mk:5: Warning: .config does not exists!
ics2021/nemu/scripts/config.mk:6: To build the project, first run 'make menuconfig'.

  • YACC build/parser.tab.h
    make[1]: bison: No such file or directory
    make[1]: *** [Makefile:27: build/parser.tab.h] Error 127
    make: *** [ics2021/nemu/scripts/config.mk:24: ics2021/nemu/tools/kconfig/build/mconf] Error 2

=========================
Dir: ~/ics2021/nemu
Distro: Ubuntu 22.04 LTS
Kernel: Linux 5.15.0-27-generic
GCC: 11.2.0
LLVM: 14.0.0

[Feature request] 建议 PA 讲义提及 glibc readline 库的内存泄漏漏洞

PA2 提供 -fsanitize=address 的编译开关,开启后可以触发 readline 库的一个内存泄漏漏洞。

性质:完全不影响PA流程。

可能有用的补充性质:我推测,绝大部分学生不会遇到、部分学生探究无果、部分学生STFW找到描述这个漏洞的追踪器、少部分学生完全理解并解释了这个现象。

为什么有必要提及:这个漏洞令人困惑,因为问题不出在学生的代码;它可能偶然被触发然后无法复现 ;即使能稳定触发,由于readline 库本身不含追踪信息,LeakSanitizer只能打印2个调用栈,很难追踪调试。我建议:在讲义中提及这个漏洞,不要给发现这个问题的学生留下遗憾。

漏洞描述:直接原因是调用了 add_history(),根本原因是 _rl_start_using_history() 没有 free 当前 data.
复现:只要在对当前行有修改的情况下用历史功能就会触发。

具体而言,
$ cd $NEMU_HOME; make memuconfig
开启 fsanitize address
$ make run;
> (nemu) 
敲击 a<CR>b<UP><CR>q<CR> 按键序列(<CR>、<UP>是Vi风格的按键代码转义)
得到
> ==<pid>==ERROR: LeakSanitizer: detected memory leaks
>
> Direct leak of 32 byte(s) in 1 object(s) allocated from:
>     #0 0x7f55100b4887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
>     #1 0x7f5510b4fbac in xmalloc (/lib/x86_64-linux-gnu/libreadline.so.8+0x39bac)
>
> SUMMARY: AddressSanitizer: 32 byte(s) leaked in 1 allocation(s).
> make: *** [path-to-ics/ics2023/nemu/scripts/native.mk:38: run] Error 1
表明成功触发.

解释 readline v8.2 修复了这个漏洞, diff一下 readline-/misc.c 就能看出来.
版本 readline version <= 8.1.2
临时修复 从源码编译 readline 8.2

diff --git a/nemu/src/filelist.mk b/nemu/src/filelist.mk
--- a/nemu/src/filelist.mk
+++ b/nemu/src/filelist.mk
@@ -19,7 +19,7 @@ 
-LIBS += $(if $(CONFIG_TARGET_NATIVE_ELF),-lreadline -ldl -pie,)
+LIBS += $(if $(CONFIG_TARGET_NATIVE_ELF),-ldl -pie,) path-to-directory/readline-8.2/libreadline.a -lncurses

FAQ 为什么不升级 readline? 要更换PPA。弄个NJU特供PPA?
为什么不安装新版 readline? 可能是我的问题,但是v8.2 ./configure && sudo make install 会由于兼容性不可用(找不到符号,会使一些CLI、Terminal如 gnone-terminal 崩溃)。
什么时候的漏洞? 不知道。给三个时间点:readline copyright 从 1987年开始,这个漏洞至少2006年(v5.2)就存在,第一个可考的漏洞反馈在2022年,同年被首次修复。

怎样在讲义中提及

  1. 更新框架代码,要求从源码编译/其他解决方案。
  2. 画个框框带过 (就像我不理解Ubuntu Software Updater 有什么问题一样,我可能完全不知道会发生什么、原因是什么)
  3. 讲一种修复方法。
  4. 告知这是readline 漏洞,作为选做题引导学生排查这个问题。

SDL庫的一個問題:PAL在進行打怪場景時,出現條色板顏色爲找到的情況

SDL庫的代碼

在8位像素模式的情況下,PAL出現調色板顏色缺失的情況
2023-03-16_21-48

PAL運行

2023-03-16_21-49

嘗試

通過STFW,在調色板缺失的情況下需要通過SDL_RGBMap函數來獲得缺失顏色的近似值。但Navy的miniSDL代碼的SDL_MapRGBA函數只支持32爲像素模式。我不知道這種情況屬於是我要自己實現解決,還是說這是miniSDL框架的缺陷。
2023-03-16_21-56

建议将 -fsanitize=address 和 -g 选项联合使用

如果只开启 -fsanitize=address,那么检测器只会告诉程序员代码泄露发生在哪个函数。而开启-g后,Asan会提示发生在哪一行中。

CFLAGS_BUILD += $(if $(CONFIG_CC_ASAN),-fsanitize=address -g,) # 增加 -g

Suggest: check if CONFIG_CC is empty before assign

现在编译menuconfig时,会从CONFIG_CC的值中提取CC。但是在初始执行make menuconfig时,CONFIG_CC为空,导致CC的值也为空。根据GNU make的文档:

Except by explicit request, make exports a variable only if it is either defined in the environment initially, or if set on the command line and its name consists only of letters, numbers, and underscores.

如果在调用make时,环境变量中有CC,就会导致这个空的CC被export。而fixdep目录下的Makefile没有指定CC,就会导致因为找不到CC而failed。

Reproduce
root@a55311a49b3b:/home/xin/ics-pa/nemu# CC=gcc make menuconfig
/home/xin/ics-pa/nemu/scripts/config.mk:20: Warning: .config does not exists!
/home/xin/ics-pa/nemu/scripts/config.mk:21: To build the project, first run 'make menuconfig'.
+ CC confdata.c
+ CC expr.c
+ CC preprocess.c
+ CC symbol.c
+ CC util.c
+ YACC build/parser.tab.h
+ LEX build/lexer.lex.c
+ CC build/lexer.lex.c
+ CC build/parser.tab.c
+ CC mconf.c
+ CC lxdialog/util.c
+ CC lxdialog/checklist.c
+ CC lxdialog/yesno.c
+ CC lxdialog/inputbox.c
+ CC lxdialog/textbox.c
+ CC lxdialog/menubox.c
+ LD /home/xin/ics-pa/nemu/tools/kconfig/build/mconf
+ CC confdata.c
+ CC expr.c
+ CC preprocess.c
+ CC symbol.c
+ CC util.c
+ CC build/lexer.lex.c
+ CC build/parser.tab.c
+ CC conf.c
+ LD /home/xin/ics-pa/nemu/tools/kconfig/build/conf
+ CC fixdep.c
make[1]: O2: No such file or directory
+ LD /home/xin/ics-pa/nemu/tools/fixdep/build/fixdep
/usr/bin/ld: cannot find /home/xin/ics-pa/nemu/tools/fixdep/build/obj-fixdep/fixdep.o: No such file or directory
collect2: error: ld returned 1 exit status
make[1]: *** [/home/xin/ics-pa/nemu/scripts/build.mk:54: /home/xin/ics-pa/nemu/tools/fixdep/build/fixdep] Error 1
make: *** [/home/xin/ics-pa/nemu/scripts/config.mk:42: /home/xin/ics-pa/nemu/tools/fixdep/build/fixdep] Error 2

如果在顶层Makefile判断CONFIG_CC非空后再赋值,即可解决这个问题。

Header File Ordering Problem in 'abstract-machine/am/src/native/ioe/audio.c'

I use clang format in vscode. However, I've encountered an issue where header files are automatically sorted, causing unexpected changes as illustrated below:

--- a/abstract-machine/am/src/native/ioe/audio.c
+++ b/abstract-machine/am/src/native/ioe/audio.c
@@ -1,8 +1,8 @@
#define _GNU_SOURCE
+#include <SDL2/SDL.h>
#include <fcntl.h>
-#include <unistd.h>
#include <klib.h>
-#include <SDL2/SDL.h>
+#include <unistd.h>

This leads to compilation error:

abstract-machine/am/src/native/ioe/audio.c: In function ‘__am_audio_init’:
abstract-machine/am/src/native/ioe/audio.c:13:15: error: implicit declaration of function‘pipe2’; did you mean ‘pipe’? [-Werror=implicit-function-declaration]
13 |     int ret = pipe2(fds, O_NONBLOCK);
|               ^~~~~
|               pipe
cc1: all warnings being treated as errors

Would it be possible to consider adding a comment or notice in the codebase to emphasize this behavior? It would be helpful to prevent such sorting of header files and subsequent compilation errors.

make失败后尝试修改build.mk文件,被锁定make

我尝试在slackware15_64环境下
kernel版本 5.15.19
gcc版本11.2.0
编译riscv32-nemu-interpreter,出现了了
image
这样的问题,我推测并尝试修改filelist.mk的编译参数后出现这样的提示
image
我并不清楚该实验项目的整体文件结构,以及如何找到实现该功能的脚本,请问我该在何处找到它?

Runing difftest may cause crash

I had a crash when I was doing PA2-2-Differential Testing. I collapsed in getopt_long.
The following is a screenshot of the crash.
图片

After a few traceing and debuging with nemu and glibc, I think maybe the flag RTLD_DEEPBIND cause this crash (Of course I am not seting CC_ASAN in Kconfig). When I just open the dynamic library with flag RTLD_LAZY , the program won't crash.

handle = dlopen(ref_so_file, RTLD_LAZY | MUXNDEF(CONFIG_CC_ASAN, RTLD_DEEPBIND, 0));

I am not sure about the specific reason, but I hope this information is helpful.

make ARCH=native run mainargs=mario don't work

When I first using make ARCH=native run mainargs=mario to test rom game

# Building fceux-run [native]
+ CXX src/emufile.cpp
# Building am-archive [native]
# Building klib-archive [native]
# Creating image [native]
+ LD -> build/fceux-native
/home/arutoria/ics2022/fceux-am/build/fceux-native
ROM is mario
Starting FCEUX 2.2.3-interim git...
Found ROM 'mario'
Loading ...

 PRG ROM:    2 x 16KiB
 CHR ROM:    1 x  8KiB
 ROM MD5:  0x8e3630186e35d477231bf8fd50e54cdd
 Mapper #:  0
 Mapper name: NROM
 Mirroring: Vertical
 Battery-backed: No
 Trained: No

Power on
Initializing video...
XDG_RUNTIME_DIR (/usr/lib/) is not owned by us (uid 1000), but by uid 0! (This could e.g. happen if you try to connect to a non-root PulseAudio as a root user, over the native protocol. Don't do that.)
XDG_RUNTIME_DIR (/usr/lib/) is not owned by us (uid 1000), but by uid 0! (This could e.g. happen if you try to connect to a non-root PulseAudio as a root user, over the native protocol. Don't do that.)

then I add export XDG_RUNTIME_DIR=/home/arutoria/ics2022/fceux-am in ~/.bashrc (Frankly, I don't know what this is gonna do). Run again

..........
Mapper #:  0
 Mapper name: NROM
 Mirroring: Vertical
 Battery-backed: No
 Trained: No

Power on
Initializing video...
ALSA lib confmisc.c:855:(parse_card) cannot find card '0'
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_card_inum returned error: No such file or directory
ALSA lib confmisc.c:422:(snd_func_concat) error evaluating strings
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_conca

I don't know what's problem is it, and steps is following the 2022 pa. Hoping to get some help.

Besides, testing the key program also with a problem: show ````Try to press any key (uart or keyboard)... ``` then rather pop up a window than appear

# Building amtest-run [native]
+ CC src/tests/hello.c
+ CC src/tests/devscan.c
+ AS src/tests/audio/audio-data.S
+ CC src/tests/audio.c
+ CC src/tests/keyboard.c
+ CC src/tests/intr.c
+ CC src/tests/mp.c
+ CC src/tests/vm.c
+ CC src/tests/video.c
+ CC src/tests/rtc.c
+ CC src/main.c
# Building am-archive [native]
# Building klib-archive [native]
# Creating image [native]
+ LD -> build/amtest-native
/home/arutoria/ics2022/am-kernels/tests/am-tests/build/amtest-native
Try to press any key (uart or keyboard)...
Authorization is required, but no Authorization protocol specified

Maybe a bug

ics2021/nemu/src/cup/cpu-exec.c line 47:#define NUMBERIC_FMT MUXDEF(CONFIG_TARGET_AM, "%ld", "%'ld") may be need to be changed as #define NUMBERIC_FMT MUXDEF(CONFIG_TARGET_AM, "%ld", "%lld")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.