Skip to content

Commit

Permalink
[tests] Introduce entrypoint tests
Browse files Browse the repository at this point in the history
These tests verify the process initialization state as described
in section 3.4 of System V Application binary Interface for x86_64.
They verify stack alignment, stack arguments, and values of
rFLAGS, x87 Floating-Point Control Word, MXCSR Status Bits.

Signed-off-by: Mariusz Zaborski <[email protected]>
  • Loading branch information
oshogbo committed Apr 27, 2022
1 parent cd9f5ee commit 8df132f
Show file tree
Hide file tree
Showing 18 changed files with 325 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .ci/lib/stage-clean-check.jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ stage('clean-check') {

make -C LibOS/shim/test/regression clean
make -C LibOS/shim/test/fs clean

gramine-test -C LibOS/shim/test/abi/x86_64 clean
rm -rf LibOS/shim/test/abi/x86_64/.pytest_cache \
LibOS/shim/test/abi/x86_64/__pycache__ \
LibOS/shim/test/abi/x86_64/*.xml

make -C Pal/regression clean

make -C CI-Examples/helloworld clean
Expand Down
12 changes: 12 additions & 0 deletions .ci/lib/stage-test.jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ stage('test') {
}
}

timeout(time: 15, unit: 'MINUTES') {
try {
sh '''
cd LibOS/shim/test/abi/x86_64
gramine-test build -v
python3 -m pytest -v --junit-xml abi.xml
'''
} finally {
junit 'LibOS/shim/test/abi/x86_64/*.xml'
}
}

timeout(time: 15, unit: 'MINUTES') {
try {
sh '''
Expand Down
Empty file removed LibOS/shim/test/.gitignore
Empty file.
3 changes: 3 additions & 0 deletions LibOS/shim/test/abi/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if host_machine.cpu_family() == 'x86_64'
subdir(host_machine.cpu_family())
endif
5 changes: 5 additions & 0 deletions LibOS/shim/test/abi/x86_64/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*.manifest
/*.xml

/build.ninja
/.ninja_*
15 changes: 15 additions & 0 deletions LibOS/shim/test/abi/x86_64/atexit_func.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; SPDX-License-Identifier: LGPL-3.0-or-later
; Copyright (C) 2022 Intel Corporation
; Mariusz Zaborski <[email protected]>

; rdx contains a function pointer that the application should register with atexit.
; Gramine sets rdx to NULL - it does not use this feature at all.

extern test_exit
global _start

section .text

_start:
mov rdi, rdx
jmp test_exit
22 changes: 22 additions & 0 deletions LibOS/shim/test/abi/x86_64/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2022 Intel Corporation
* Mariusz Zaborski <[email protected]>
*/

/*
* We have to add a function declaration to avoid warnings.
* This function is used with NASM, so creating a header is pointless.
*/
int test_str_neq(const char* orig, const char* new);

int test_str_neq(const char* orig, const char* new) {
if (orig == new)
return 0;

while (*orig && *orig == *new) {
orig++;
new++;
}

return *orig != *new;
}
17 changes: 17 additions & 0 deletions LibOS/shim/test/abi/x86_64/exit.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
; SPDX-License-Identifier: LGPL-3.0-or-later
; Copyright (C) 2022 Intel Corporation
; Mariusz Zaborski <[email protected]>

global test_exit

%define __NR_exit 60

section .text

test_exit:
xor rax, rax
test rdi, rdi
setne al
mov rdi, rax
mov rax, __NR_exit
syscall
27 changes: 27 additions & 0 deletions LibOS/shim/test/abi/x86_64/fpu_control_word.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
; SPDX-License-Identifier: LGPL-3.0-or-later
; Copyright (C) 2022 Intel Corporation
; Mariusz Zaborski <[email protected]>

; The x87 FPU Control Word register:
; +--+--+--+--+--+--+--+--+--+--+--+--+--+
; | X| RC| PC| R| R|PM|UM|OM|ZM|DM|IM|
; +--+--+--+--+--+--+--+--+--+--+--+--+--+
; The x87 FPU Control Word should be set as follow:
; +--+--+--+--+--+--+--+--+--+--+--+--+--+
; | X| 0 0| 1 1| X| X| 1| 1| 1| 1| 1| 1|
; +--+--+--+--+--+--+--+--+--+--+--+--+--+

extern test_exit
global _start

section .text

_start:
fstcw [rsp]

mov ax, [rsp]
mov rdi, rax
and rdi, 0b0111100111111
xor rdi, 0b0001100111111

jmp test_exit
23 changes: 23 additions & 0 deletions LibOS/shim/test/abi/x86_64/manifest.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
loader.entrypoint = "file:{{ gramine.libos }}"
libos.entrypoint = "{{ entrypoint }}"

# We set the argv[0] to the name of the entrypoint.
# This is crucial for stack tests as otherwise argv[0] contains an absolute path
# to the binary, and tests become unreliable.
loader.argv0_override = "{{ entrypoint }}"
loader.insecure__use_cmdline_argv = true

fs.mounts = [
{ path = "/lib", uri = "file:{{ gramine.runtimedir() }}" },
{ path = "/{{ entrypoint }}", uri = "file:{{ binary_dir }}/{{ entrypoint }}" },
]

sgx.nonpie_binary = true
sgx.debug = true
sgx.thread_num = 4

sgx.trusted_files = [
"file:{{ gramine.libos }}",
"file:{{ binary_dir }}/{{ entrypoint }}",
"file:{{ gramine.runtimedir() }}/",
]
37 changes: 37 additions & 0 deletions LibOS/shim/test/abi/x86_64/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
common_lib = static_library('common_lib',
'common.c',
nasm_gen.process('exit.asm')
)

tests = {
'atexit_func': {},
'fpu_control_word': {},
'mxcsr': {},
'rflags': {},
'stack': {},
'stack_arg': {},
}

install_dir = join_paths(pkglibdir, 'tests', 'libos', 'entrypoint')

foreach name, params : tests
filename = ''
if (params.get('type', 'asm') == 'asm')
filename = nasm_gen.process('@[email protected]'.format(name))
else
filename = '@[email protected]'.format(name)
endif

exe = executable(name,
filename,

link_with: [
params.get('link_with', [common_lib]),
],

link_args: params.get('link_args', ['-nostdlib', '-static']),

install: true,
install_dir: install_dir,
)
endforeach
27 changes: 27 additions & 0 deletions LibOS/shim/test/abi/x86_64/mxcsr.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
; SPDX-License-Identifier: LGPL-3.0-or-later
; Copyright (C) 2022 Intel Corporation
; Mariusz Zaborski <[email protected]>

; The MXCSR register:
; +--+--+--+--+--+--+--+--+--+---+--+--+--+--+--+--+
; |FZ| RC|PM|UM|OM|ZM|DM|IM|DAZ|PE|UE|OE|ZE|DE|IE|
; +--+--+--+--+--+--+--+--+--+---+--+--+--+--+--+--+
; The MXCSR should be set as follow:
; +--+--+--+--+--+--+--+--+--+---+--+--+--+--+--+--+
; | 0| 0 0| 1| 1| 1| 1| 1| 1| 0| X| X| X| X| X| X|
; +--+--+--+--+--+--+--+--+--+---+--+--+--+--+--+--+

extern test_exit

global _start

section .text

_start:
stmxcsr [rsp]

mov edi, [rsp]
and rdi, 0b1111111111000000
xor rdi, 0b0001111110000000

jmp test_exit
28 changes: 28 additions & 0 deletions LibOS/shim/test/abi/x86_64/rflags.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
; SPDX-License-Identifier: LGPL-3.0-or-later
; Copyright (C) 2022 Intel Corporation
; Mariusz Zaborski <[email protected]>

; The rFlags should be cleared.
; Verify OF, DF, SF, ZF, AF, PF, CF
; The EFLAGS:
; +--+--+--+--+--+--+--+--+--+--+--+--+
; |OF|DF|IF|TF|SF|ZF| 0|AF| 0|PF| 1|CF|
; +--+--+--+--+--+--+--+--+--+--+--+--+
; Mask to get interesting bits:
; +--+--+--+--+--+--+--+--+--+--+--+--+
; | 1| 1| 0| 0| 1| 1| 0| 1| 0| 1| 0| 1|
; +--+--+--+--+--+--+--+--+--+--+--+--+

extern test_exit

global _start

section .text

_start:
pushfq
pop rdi

and rdi, 0b110011010101

jmp test_exit
20 changes: 20 additions & 0 deletions LibOS/shim/test/abi/x86_64/stack.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; SPDX-License-Identifier: LGPL-3.0-or-later
; Copyright (C) 2022 Intel Corporation
; Mariusz Zaborski <[email protected]>

; "The end of the input argument area shall be aligned on a 16..."

extern test_exit

global _start

section .text

_start:
xor rdi, rdi

mov rax, rsp
and rax, 0xF
setne dil

jmp test_exit
50 changes: 50 additions & 0 deletions LibOS/shim/test/abi/x86_64/stack_arg.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
; SPDX-License-Identifier: LGPL-3.0-or-later
; Copyright (C) 2022 Intel Corporation
; Mariusz Zaborski <[email protected]>

; Verify argv which should be in rsp + 8.

default rel

extern test_exit
extern test_str_neq

global _start

section .text

_start:
mov rdi, [rsp] ; Verify argc
cmp rdi, 3
mov rdi, 1
jne test_exit

lea rdi, [argv0] ; Verify argv[0]
mov rsi, [rsp + 8 * 1]
call test_str_neq
mov rdi, rax
cmp rdi, 1
je test_exit

lea rdi, [argv1] ; Verify argv[1]
mov rsi, [rsp + 8 * 2]
call test_str_neq
mov rdi, rax
cmp rdi, 1
je test_exit

lea rdi, [argv2] ; Verify argv[2]
mov rsi, [rsp + 8 * 3]
call test_str_neq
mov rdi, rax
cmp rdi, 1
je test_exit

mov rdi, [rsp + 8 * 4]
jmp test_exit

section .data

argv0 db "stack_arg", 0x00
argv1 db "foo", 0x00
argv2 db "bar", 0x00
22 changes: 22 additions & 0 deletions LibOS/shim/test/abi/x86_64/test_entrypoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python3

from graminelibos.regression import RegressionTestCase

class TC_00_Entrypoint(RegressionTestCase):
def test_000_atexit_func(self):
self.run_binary(['atexit_func'])

def test_000_fpu_control_word(self):
self.run_binary(['fpu_control_word'])

def test_000_rflags(self):
self.run_binary(['rflags'])

def test_000_mxcsr(self):
self.run_binary(['mxcsr'])

def test_000_stack(self):
self.run_binary(['stack'])

def test_000_arg(self):
self.run_binary(['stack_arg', 'foo', 'bar'])
10 changes: 10 additions & 0 deletions LibOS/shim/test/abi/x86_64/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
binary_dir = "@GRAMINE_PKGLIBDIR@/tests/libos/entrypoint"

manifests = [
"atexit_func",
"fpu_control_word",
"mxcsr",
"rflags",
"stack",
"stack_arg",
]
1 change: 1 addition & 0 deletions LibOS/shim/test/meson.build
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
subdir('abi')
subdir('regression')
subdir('fs')

0 comments on commit 8df132f

Please sign in to comment.