Warning
This page is located in archive.

The Fourth Homework - Code Analysis

The task assignment is provided to the students in the form of the ZIP archive on http://cmp.felk.cvut.cz/~pisa/apo/du4/ page.

Each students downloads his/her assignment according to the personal number. You can find the personal number in Studium Component of the KOS system. It is imprinted on you CTU identification card or combined ISIC card. It is available from https://udb.fel.cvut.cz/ after login.

The solved task is submitted in the form of electronic ZIP archive to the BRUTE system. The archive should contain exactly the same set of files (with updated content) as are provided in the assignment archive.

Information sources for homework 4. solution

Example of little more complex task sources and program analysis than are programs in students assignments is available in archive.

homework-4-example.zip

The files in this example are not in the format expected by test system. Code in generator uses special machinery to inline system calls. Tasks solutions can use usual call of read, write, etc. functions provided by C language standard library.

Each task includes same algorithm compiled for i386 Linux system, MIPS Linux system and RISC-V 32-bit Linux system.

i386 assembler syntax uses AT&T and GNU assembler/disassembler variant.

GNU Assembler Syntax

Description of the small subset of used system calls which are used in the code fragments can be found in the materials to the lecture

Parameters Passing to Subroutines and Operating System Implemented Virtual Instructions (System Calls)

This lecture includes enough information for i386 system calls. The RISC-V architecture uses same system calls set but system calls numeric codes are different. RISC-V architecture mapping of system calls to the numbers can be found for example in following article:

https://jborza.com/post/2021-05-11-riscv-linux-syscalls/

The assignment of system calls for MIPS architecture and ABI can be found directly in GLIBC or Linux kernel sources cross-reference

http://lxr.linux.no/#linux+v3.8.8/arch/mips/include/uapi/asm/unistd.h

or directly in the Linux kernel sources GIT repository. The base (__NR_Linux) of the syscalls numbers assignment for MIPS 032 ABI MIPS O32 is defined in the file

arch/mips/include/uapi/asm/unistd.h

and the numbers of individual syscalls services are defined in the file

arch/mips/kernel/syscalls/syscall_o32.tb

arch/mips/include/uapi/asm/unistd.h

The complete list of Linux kernel system calls codes for all supported architectures is available on next page

https://fedora.juszkiewicz.com.pl/syscalls.html

System calls use for the call return value which is 32 or 64-bit usually the same register(s) as are used for regular function calls return value. The additional information which has to be passed to the caller is information about call failure/error. Linux kernel uses special range of the return values. The system call failure is indicated by return value in the range of “small negative number”. The impact of this selection is that system calls cannot return negative values or pointer values near the top of the address range (addresses are taken as unsigned) as a valid results. The C library then check if syscall result is in this reserved range and setups thread specific global variable errno in such case and usually return minus one (for some syscalls NULL) as indication of syscall failure to the calling program according to the POSIX specification. List of the error codes is included in GLIBC documentation and can be found in Linux kernel sources include files as well

https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/include/uapi/asm-generic/errno.h

The actual return value of the syscall in the case of error is negated value of the listed error code (this is -ENOSYS for nonexistent system call for example). LIBC errno then hold positive value of the error code.

An complete description of the callin convention for MIPS architecture (variant used for GNU/Linux system) can be found in the document MIPSpro N32 ABI Handbook https://www.linux-mips.org/pub/linux/mips/doc/ABI/MIPS-N32-ABI-Handbook.pdf

You can learn and test systemcalls processing in QtRvSim and QtMips simulators which support all system calls used in the homework. The basic description and links to complete documentation are available in the file with simulator QtRvSim documentation (respectively for MIPS in QtMips documentation). The homework-4-example.zip with simplified initialization crt0local.S (C library provides complete initialization in crt0.S) and simplified replacement of the printf() function can be found in the file

linux-hw4-mini.zip

The syscalls emulation has to be enabled in the emulator. It is possible to select if emulator stops on each syscall. The directory where emulated program opens and creates files can be selected as well. If the filed to select directory is left empty then all input and output operations are mapped to terminal window.

Program expect decimal number on its input which specifies number of the characters which are written to the “data” file.

Suggestions

Do not attempt to solve task by series of trial/fail attempts. It takes too much time. Read the descriptions of system calls from manual pages

man 2 open
man 2 write

and when you reveal basic function (for example read digit from standard input and each increase by one before printing), rewrite function into C language divided into two functions. The compiled program is simplified as much as possible, no standard libraries are used, system calls are inlined, eror checking is minimalized.

The evaluation system checks only order and parameters of system calls in the second phase of evaluation. Your program need not and cannot match original code exactly 1:1 on the instruction level. The listing is generated for 32-bit RISC-V, MIPS and x86 architectures. But your code is compiled and tested in 64-bit build environment. The operating systems and compiler provides that big advantage, that source code is portable between architectures and systems with rapidly different computation power. If you follow the rules for portable code writing then then function of the code is preserved without dependency of bytes/word order of the system (big/little endian), registers widths, size of pointers etc. Attempts to send programs which do not compile even on your local system or remote access server (postel.felk.cvut.cz) is useless.

courses/b35apo/en/homeworks/04/start.txt · Last modified: 2024/02/02 18:41 (external edit)