Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using assertion in the Linux kernel

I have a question about assert() in Linux: can I use it in the kernel?

If no, what techniques do you usually use if, for example I don't want to enter NULL pointer?

like image 411
macindows Avatar asked Jun 15 '11 14:06

macindows


People also ask

What is the use of assert () command in Linux?

This macro can help programmers find bugs in their programs, or handle exceptional cases via a crash that will produce limited debugging output. If expression is false (i.e., compares equal to zero), assert() prints an error message to standard error and terminates the program by calling abort(3).

What does WARN_ on do?

BUG_ON and WARN_ON are to catch bugs in your own code or problems with the hardware.

What is BUG_ON?

BUG() and BUG_ON(condition) are used as a debugging help when something in the kernel goes terribly wrong. When a BUG_ON() assertion fails, or the code takes a branch with BUG() in it, the kernel will print out the contents of the registers and a stack trace. After that the current process will die.


1 Answers

The corresponding kernel macros are BUG_ON and WARN_ON. The former is for when you want to make the kernel panic and bring the system down (i.e., unrecoverable error). The latter is for when you want to log something to the kernel log (viewable via dmesg).

As @Michael says, in the kernel, you need to validate anything that comes from userspace and just handle it, whatever it is. BUG_ON and WARN_ON are to catch bugs in your own code or problems with the hardware.

like image 76
Nemo Avatar answered Sep 29 '22 12:09

Nemo