Skip to content

Commit

Permalink
fix critical bug in crates/elf_parser: user stack now aligned to 16 B…
Browse files Browse the repository at this point in the history
…ytes; rewrite build_img.sh
  • Loading branch information
scPointer committed Mar 19, 2024
1 parent 14f9c3b commit e4bb802
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 19 deletions.
61 changes: 42 additions & 19 deletions build_img.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,52 @@
#!/bin/sh

arch=$1
fs=$2

if [ "$arch" != "riscv64" ] && [ "$arch" != "aarch64" ]; then
arch=x86_64
FILE=testsuits-x86_64-linux-musl
if [ ! -e testcases/$FILE ]; then
wget https://github.com/oscomp/testsuits-for-oskernel/releases/download/final-x86_64/$FILE.tgz
tar zxvf $FILE.tgz
mv $FILE testcases/$FILE -f
rm $FILE.tgz
# default setting
arch=x86_64
fs=fat32
FILE=

if [ -n "$1" ]; then
if [ "$1" = "riscv64" ] || [ "$1" = "aarch64" ] || [ "$1" = "x86_64" ]; then # $1 is arch
arch=$1
if [ -n "$2" ]; then
if [ "$2" = "ext4" ] || [ "$2" = "fat32" ]; then # $2 is type of file system
fs=$2
if [ -n "$3" ]; then
FILE=$3
fi
else # $2 is folder in testcases
FILE=$2
fi
fi
elif [ "$1" = "ext4" ] || [ "$1" = "fat32" ]; then # $1 is type of file system
fs=$1
if [ -n "$2" ]; then
FILE=$2
fi
else # $1 is folder in testcases
FILE=$1
fi
else
if [ -n "$3" ]; then
FILE=$3
fi

if [ ! -n $FILE ] || [ "$FILE" = "" ]; then # use default testcases
if [ "$arch" = "riscv64" ]; then
FILE=sdcard
elif [ "$arch" = "x86_64" ]; then
FILE=testsuits-x86_64-linux-musl
elif [ "$arch" = "aarch64" ]; then
FILE=aarch64
else
if [ "$arch" = "riscv64" ]; then
FILE=sdcard
else
FILE=aarch64
fi
exit 1
fi
fi

if [ "$FILE" = "testsuits-x86_64-linux-musl" ] && [ ! -e testcases/$FILE ]; then # auto download
wget https://github.com/oscomp/testsuits-for-oskernel/releases/download/final-x86_64/$FILE.tgz
tar zxvf $FILE.tgz
mv $FILE testcases/$FILE -f
rm $FILE.tgz
fi

rm disk.img
dd if=/dev/zero of=disk.img bs=4M count=30

Expand Down
10 changes: 10 additions & 0 deletions crates/elf_parser/src/user_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ pub fn init_stack(
.collect();
// 加入envs和argv的地址
stack.push(&[null::<u8>(), null::<u8>()]);
let final_sp = stack.get_sp()
- (auxv.len() * 2 + envs_slice.len() + argv_slice.len()) * core::mem::size_of::<usize>()
- 8 // auxv 与 envs 之间的空位
- 8 // envs 与 args 之间的空位
- 8; // argc 占用空间
if final_sp % 16 != 0 {
// 按照 SIMD 要求,保证最终用户栈是 16 Bytes 对齐的
// 更高的对齐要求理应对其他环境也适用,因此这里没有特殊指定 feature("fp_simd")
stack.push(&[null::<u8>()]);
}
// 再加入auxv
// 注意若是atrandom,则要指向栈上的一个16字节长度的随机字符串
for (key, value) in auxv.iter() {
Expand Down
Binary file added testcases/ZLM/busybox
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e4bb802

Please sign in to comment.