Intricacies of C Semantics
1 Intricacies of C Semantics
- What are "strings"?
char *p = "hello";versuschar q[] = "hello";. - Address relationships
int x; int y; - Word alignment
- Standard Library:
libc strlen strcpy strcat - Files in ./modret/
2 SysCall Redirect
These notes are triggered by our attempt at writing a new Linux kernel module.
- The C language
staticis likeprivateof C++; that symbol is not exported. - Do not leave the sys-call-table writeable until the module exits. Not good. As soon as the redirect/hijack is made, bring it to read-only.
- The following was found in a blog.
struct linux_dirent64 *cur = dirp; ... int reclen = cur->d_reclen; char *next_rec = (char *)cur + reclen; int len = (int)dirp + rtn - (int)next_rec;
- Bad C usage. Reckless int-long-ptr synonyms. The type
intis compiler specific.sizeof(int)is typically 4 or 8 bytes. On very old systems, and even modern embedded systems, it can be 2 bytes. The width of a ptr is architecture specific. A ptr value on a 64-bit system is 8 bytes wide. The GNU C compiler indeed generated a warning about(int) dirp. - C ptr arithmetic. The resulting value of
p + xis computed as the equivalentlong long intvalue of ptrp, plus (ordinary arith)x * sizeof(p) asmlinkage int (*ogetdents64) (unsigned int fd, struct linux_dirent64 *dirp, unsigned int count);should not haveasmlinkageAnd, this is declaring a ptr variable namedogetdents64. It also declares that (i) the ptr it holds is the address of a function, (ii) this function takes three arguments as declared.- I was expecting the GNU C compiler to produce a warning/error on
ogetdents64(...)versus(*ogetdents64)(...)But, it did not! TBD Further investigation is warranted. - What is the proper declaration of sys-call-table?
void * * sys_call_tableworks expediently, but not "correct". TBD Will post details on this later. sys_call_tableinitialization
#define SYSCALLTBLPM 0xffffffff81801680 /* pmateti 3.19.0-20-lowlatency */ #define SYSCALLTBLAS 0xffffffff81801400 /* asish 4.0.1 */ #define __NR_ni 7 /* NR of sys_ni_syscall */