Counting system calls - c

I have this .c file that counts the system calls that linux calls. These are just the main functions. There were a couple of other things that I had to do, like create an array
unsigned long syscall_counts[345];
and then in another file with some assembly I incremented the array with the command:
incl syscall_counts(,%eax,4)
// This function is called each time the application calls read(). It starts the process of
// accumulating data to fill the application buffer. Return a pointer representing the current
// item. Return NULL if there are no more items.
//
static void *counter_seq_start(struct seq_file *s, loff_t *record_number)
{
if (*record_number > 347)
return NULL;
return (void*)s;
}
// This function is called to compute the next record in the sequence given a pointer to the
// current record (in bookmark). It returns a pointer to the new record (essentially, an updated
// bookmark) and updates *record_number appropriately. Return NULL if there are no more items.
//
static void *counter_seq_next(struct seq_file *s, void *bookmark, loff_t *record_number)
{
unsigned long *temp_b =(unsigned long*) bookmark;
(*temp_b)++;
if (*temp_b > 345)
return NULL;
return (void*)temp_b;
}
// This function is called whenever an application buffer is filled (or when start or next
// returns NULL. It can be used to undo any special preparations done in start (such as
// deallocating auxillary memory that was allocated in start. In simple cases, you often do not
// need to do anything in this function.
//
static void counter_seq_stop(struct seq_file *s, void *bookmark)
{
}
// This function is called after next to actually compute the output. It can use various seq_...
// printing functions (such as seq_printf) to format the output. It returns 0 if successful or a
// negative value if it fails.
//
static int counter_seq_show(struct seq_file *s, void *bookmark)
{
loff_t *bpos = (loff_t *) bookmark;
seq_printf(s, "value: %Ld\n", *bpos);
return 0;
}
// Define the only file handling function we need.
static int counter_open(struct inode *inode, struct file *file)
{
return seq_open(file, &counter_seq_ops);
}
my output is very strange:
Anyone have any idea where the issue is?

Don't you think :
static int counter_seq_show(struct seq_file *s, void *bookmark) {
unsigned long *bpos = (unsigned long *) bookmark;
seq_printf(s, "value: %Ld\n", *bpos);
return 0;
}
Or even
static int counter_seq_show(struct seq_file *s, void *bookmark) {
seq_printf(s, "value: %lu\n", *((unsigned long *)bpos));
return 0;
}
I haven't fully understood your program but I saw two different ways you cast 'bookmark'. In one function you cast it to be 'unsigned long *' and other you do 'loff_t *' (long int). Ideally they should be the same, but are you doing it this way for some reason ?
HTH

Related

How to configure expectations in cmock for a "read" function

Given a function under test that does something like:
void funcUnderTest()
{
char buf[32];
int bufSize=32;
someReadFunction(buf, size);
}
int someReadFunction(char* buf, int size)
{
int readlen;
//loads buf with data
//returns number of bytes copied
return readlen;
}
How can i write my unit test such that the mock function:
Loads buf with specific data
returns a specified retval
eg:
void test_funcUnderTest()
{
char* testBuf="Hello World";
someReadFunc_ReturnArrayThruPtr_buf(testBuf,12) // Copy "testBuf" into "buf"
//How do we control return value?
funcUnderTest();
}
You can configure the Mock object by combining multiple expectations. It should work like this:
void test_funcUnderTest()
{
char* testBuf="Hello World";
someReadFunc_ExpectAnyArgsAndReturn(retval) // Return "retval" on next call
someReadFunc_ReturnArrayThruPtr_buf(testBuf,12) // Copy "testBuf" into "buf" on the same call
funcUnderTest();
}
Be aware though, that the order of this calls matters. Usually you need to call the "Expect" function first before defining the behavior of specific arguments.

Share variables internally across functions

Suppose I have a function like this:
static int init_processing(char *buf, FILE *stream, enum operationMode mode) {
/* save index of `stream' in current operations */
/* start processing */
/* save some important variables for continue_processing */
off_t position;
enum operationMode _mode;
return num_processing_operations_left;
}
.. that I would be calling occasionally. And I have another function that does the actual processing I want:
static int continue_processing(FILE *stream) {
/* lookup the index of `stream' in current operations */
/* do some stuff */
/* save some static variables */
static off_t left = position;
static void *some_ptr;
return --num_processing_operations_left;
}
I also have a cleaning function to invoke when finishing up a certain operation:
static int end_processing(FILE *stream) {
/* check */
if (num_processing_operations_left)
return 1;
/* clean everything */
return 0;
}
As you can see, this related functions technique is very familiar that it is used by the standard library itself (e.g [malloc, free, realloc], [fdopen, fopen, fclose]).
What I want to achieve here, is how to share some variables across a bunch of functions ?
I thought of two solutions:
Put each set of functions in a file of their own, providing static variables valid only for the file itself.
Use only one function that takes an extra enum parameter as a mode and structure the function accordingly.
But these solutions aren't actually solutions, they're just workarounds to cope with the problem. So, is there any standard technique to share variables among functions ?
Create a context structure keeping all you need:
struct Context
{
FILE * stream;
off_t position;
unsigned num_processing_operations_left;
/* define some more important variables */
}
and pass it to all functions in question:
static int init_processing(struct Context * pctx, char *buf, enum operationMode mode);
static int continue_processing(struct Context * pctx);
static int end_processing(struct Context * pctx);
int main(void)
{
struct Context ctx = {0};
/* init ctx here */
int result = init_processing(&ctx, ...);
...
result = continue_processing(&ctx);
...
result = end_processing(&ctx);
...
}

Passing a list of values to kernel space

I'm working on a linux project. I need to pass a list of integer values to the kernel from a userspace program. I implemented a system call for this. In the userspace program, I had the following code. The value of num_values is obtained from command line arguments.
int* ptr=NULL;
ptr = (int*) malloc(sizeof(int)*num_values);
for(loop_index=0;loop_index<num_values;loop_index++)
{
*(ptr+loop_index)=atoi(argv[optind+loop_index]);
}
Then, I called my system call,
ret = set_wcet_val(ptr,&num_values);
The syscall 'set_wcet_val' implementation in the kernel is as follows:
asmlinkage long sys_set_wcet_val(int* wcet_val, int* num_values)
{
int retval=0, retval2=0, index, loop_index;
int *wcet_ptr=NULL;
retval2 = get_user(index,(int*)num_values);
wcet_ptr = kmalloc(((sizeof(int))*index), GFP_ATOMIC);
if(!wcet_ptr)
printk("kmalloc:Error in allocating space..\n");
if (copy_from_user(wcet_ptr, wcet_val, sizeof(wcet_ptr)))
{
printk("Syscall-wcet_val failed to copy data..\n");
}
for(loop_index=0;loop_index<index;loop_index++)
printk("wcet_ptr value is %d\n",*(wcet_ptr+loop_index));
kfree(wcet_ptr);
return retval;
}
The value 'num_values' is properly copied to 'index'.
The problem is only the first two data are printed when I checked dmesg.
If the num_values is 3, I'm getting a random positive value for the third data.
If the num_values is 4, a random positive value for third and a negative value for fourth data.
For num_values > 4, all values from third data are zero.
In all cases, only the first two values are copied correctly.
What is the reason for this weird behaviour ?
At least correct the copy_from_user call (to copy the entire index entries):
asmlinkage long sys_set_wcet_val(int* wcet_val, int* num_values)
{
int retval=0, retval2=0, index, loop_index;
int *wcet_ptr=NULL;
retval2 = get_user(index,(int*)num_values);
wcet_ptr = kmalloc(((sizeof(int))*index), GFP_ATOMIC);
if(!wcet_ptr)
printk("kmalloc:Error in allocating space..\n");
if (copy_from_user(wcet_ptr, wcet_val, (sizeof(int))*index))
{
printk("Syscall-wcet_val failed to copy data..\n");
}
for(loop_index=0;loop_index<index;loop_index++)
printk("wcet_ptr value is %d\n",*(wcet_ptr+loop_index));
kfree(wcet_ptr);
return retval;
}
It would be better to pass num_values directly, instead of a pointer to it (unless you had to modify it somehow):
asmlinkage long sys_set_wcet_val(int* wcet_val, int num_values)
{
int retval=0, loop_index;
int *wcet_ptr=NULL;
wcet_ptr = kmalloc(((sizeof(int))*num_values), GFP_ATOMIC);
if(!wcet_ptr)
printk("kmalloc:Error in allocating space..\n");
if (copy_from_user(wcet_ptr, wcet_val, (sizeof(int))*num_values))
{
printk("Syscall-wcet_val failed to copy data..\n");
}
for(loop_index=0;loop_index<num_values;loop_index++)
printk("wcet_ptr value is %d\n",*(wcet_ptr+loop_index));
kfree(wcet_ptr);
return retval;
}
Call it as ret = set_wcet_val(ptr, num_values);.
But I think the best you could do is to get rid of a syscall and use a kobject instead. Look for kobject_create_and_add, sysfs_create_group, struct attribute_group.

set different memory allocation function in lua

I'm doing the exercises from the third edition of "Programming in Lua" book by Roberto Ierusalimschy. I have a problem with a bug in my solution to exercise 32.1. The statement is provided as comment in the code.
/*
Exercise 32.1:
Write a library that allows a script to limit the total amount of memory
used by its Lua state. It may offer a single function, setlimit, to set that
limit.
The library should set its own allocation funciton. This function, before
calling the original allocator, checks the total memory in use and returns
NULL if the requested memory exeeds the limit.
(Hint: the library can use lua_gc to initialize its byte count when it
starts. It also can use the user data of the allocation function to keep its
state: the byte count, the current memory limit, etc.; remember to use the
original user data when calling the original allocation function.)
*/
#ifdef WIN32
#define LUA_EXPORT __declspec(dllexport)
#else
#define LUA_EXPORT
#endif
#include <lauxlib.h>
typedef struct MemLimitUData
{
size_t mem_limit;
size_t currently_used;
lua_Alloc original_alloc;
void *original_ud;
}
MemLimitUData;
static int l_setlimit(lua_State *L)
{
MemLimitUData *ud;
size_t mem_limit = luaL_checkinteger(L, 1);
lua_getallocf(L, &ud);
ud->mem_limit = mem_limit;
return 0;
}
static int l_getlimit(lua_State *L)
{
MemLimitUData *ud;
lua_getallocf(L, &ud);
lua_pushnumber(L, ud->mem_limit);
return 1;
}
static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
MemLimitUData *udata = (MemLimitUData*)ud;
if (udata->mem_limit != 0 &&
udata->mem_limit < udata->currently_used - osize + nsize)
{
return NULL;
}
udata->currently_used += nsize - osize;
return udata->original_alloc(udata->original_ud, ptr, osize, nsize);
}
static const luaL_Reg memlimit[] =
{
{ "setlimit", l_setlimit },
{ "getlimit", l_getlimit },
{ NULL, NULL }
};
int LUA_EXPORT luaopen_memlimit(lua_State *L)
{
MemLimitUData *ud =
(MemLimitUData*)lua_newuserdata(L, sizeof(MemLimitUData));
ud->mem_limit = 0;
ud->currently_used =
lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0);
ud->original_alloc = lua_getallocf(L, &ud->original_ud);
lua_setallocf(L, l_alloc, ud);
luaL_newlib(L, memlimit);
return 1;
}
When I build the source as memlimit.dll and use it from Lua script,
local memlimit = require"memlimit" the program crashes when the script ends. When I use debugger to look for the problem, the problematic statement seems to be in Lua internals. The file is lmem.c line 84:
newblock = (*g->frealloc)(g->ud, block, osize, nsize);
The used version of Lua is 5.2.3.
What wrong I do to break the Lua memory management ?
I haven't tried your code but here is what caught my attention when I read it:
The ud in luaopen_memlimit is created as userdata but is not anchored in Lua. Passing it to lua_getallocf does not count as anchoring. ud is probably being collected when the program ends via lua_close when it tries to free all data using your l_alloc. You should probably use plain malloc or the original allocf to create ud.

External Functions and Parameter Size Limitation (C)

I am very much stuck in the following issue. Any help is very much appreciated!
Basically I have a program wich contains an array of structs and I am getting a segmentation error when I call an external function. The error only happens when I have more than 170 items on the array being passed.
Nothing on the function is processed. The program stops exactly when accessing the function.
Is there a limit for the size of the parameters that are passed to external functions?
Main.c
struct ratingObj {
int uid;
int mid;
double rating;
};
void *FunctionLib; /* Handle to shared lib file */
void (*Function)(); /* Pointer to loaded routine */
const char *dlError; /* Pointer to error string */
int main( int argc, char * argv[]){
// ... some code ...
asprintf(&query, "select mid, rating "
"from %s "
"where uid=%d "
"order by rand()", itable, uid);
if (mysql_query(conn2, query)) {
fprintf(stderr, "%s\n", mysql_error(conn2));
exit(1);
}
res2 = mysql_store_result(conn2);
int movieCount = mysql_num_rows(res2);
// withhold is a variable that defines a percentage of the entries
// to be used for calculations (generally 20%)
int listSize = round((movieCount * ((double)withhold/100)));
struct ratingObj moviesToRate[listSize];
int mvCount = 0;
int count =0;
while ((row2 = mysql_fetch_row(res2)) != NULL){
if(count<(movieCount-listSize)){
// adds to another table
}else{
moviesToRate[mvCount].uid = uid;
moviesToRate[mvCount].mid = atoi(row2[0]);
moviesToRate[mvCount].rating = 0.0;
mvCount++;
}
count++;
}
// ... more code ...
FunctionLib = dlopen("library.so", RTLD_LAZY);
dlError = dlerror();
if( dlError ) exit(1);
Function = dlsym( FunctionLib, "getResults");
dlError = dlerror();
(*Function)( moviesToRate, listSize );
// .. more code
}
library.c
struct ratingObj {
int uid;
int mid;
double rating;
};
typedef struct ratingObj ratingObj;
void getResults(struct ratingObj *moviesToRate, int listSize);
void getResults(struct ratingObj *moviesToRate, int listSize){
// ... more code
}
You are likely blowing up the stack. Move the array to outside of the function, i.e. from auto to static land.
Another option is that the // ... more code - array gets populated... part is corrupting the stack.
Edit 0:
After you posted more code - you are using C99 variable sized array on the stack - Bad IdeaTM. Think what happens when your data set grows to thousands, or millions, of records. Switch to dynamic memory allocation, see malloc(3).
You don't show us what listsize is, but I suppose it is a variable and not a constant.
What you are using are variable length arrays, VLA. These are a bit dangerous if they are too large since they usually allocated on the stack.
To work around that you can allocate such a beast dynamically
struct ratingObj (*movies)[listSize] = malloc(sizeof(*movies));
// ...
free(movies);
You'd then have in mind though that movies then is a pointer to array, so you have to reference with one * more than before.
Another, more classical C version would be
struct ratingObj * movies = malloc(sizeof(*movies)*listsize);
// ...
free(movies);

Resources