开发者

How to write a c program which has a large code section

I need to have a C program having a large code segment size (at least some MB) I want to check out some behavior.

Here is the scenario :

Run two processes:

Process A: One having a large code segment size resided in the system RAM

Process B: which just eats up the system RAM ( a simple malloc+memset ).

Notice whether the resident set size of the process A decreases or some of its pages are swiped out 开发者_运维百科or not before the exhaustion of the memory. When I write a simple program the code segment size remains within the boundary of 1 page (4KB).


If it can be assembly instead of C: (using gnu as)

.text
.space 0x7FFFFFFF

though i wouldn't recommend going quite so large


If you write in C++ - I can advise you using templates.

For example:

template <int i>
struct PlainOfCode
{
    template <int j>
    static int Worker(int x)
    {
        return j ^ (x + i) ^ Worker<j-1>(x+j);
    }

    template <>
    static int Worker<0>(int x) { return x; }

    static int Batch(int x)
    {
        return PlainOfCode<i-1>::Batch(x) ^ Worker<i*i>(x);
    }
};

template <>
struct PlainOfCode<0>
{
    static int Batch(int x)
    {
        return x;
    }
};

// main
int j = PlainOfCode<30>::Batch(GetTickCount());
if (25 != j)
    GetTickCount();

This generates enormous implicit function count (and hence - code size). Disable all the optimizations.


On Linux and compatible systems, you could probably use mmap() to allocate some code-space, and then fill it with whatever you feel like (maybe just 1 MB of NOPs), then call it to make sure it really gets added to your code space.


Inspired by the C++ meta programming, I figured, just generate a large C source code from a C program. Change the "max" variable to how many functions you want. When you compile and run this code, it will create a C program called largefile.c. Compile this program, with optimizations turned off. It might be a good idea to remove the generated printfs too, it depends on what you want to do I guess. Good luck!

#include <stdio.h>

void writefunc(FILE *fp, int i)
{
    fprintf(fp, "void f%d()\n{\n\tprintf(\"%d\\n\");\n\tf%d();\n}\n", i, i, i+1);
}

int main(int argc, char** argv)
{
    FILE *fp = fopen("largecode.c", "w");
    int i;
    int max = 100;

    fprintf(fp, "#include<stdio.h>\n");

    fprintf(fp, "\nint f%d()\n{\n}\n", max + 1);

    for (i = max; i > 0; i--) {
        writefunc(fp, i);
    }

    fprintf(fp, "\nint main(int argc, char** argv){\n\tf1();\n}\n");

    fclose(fp);

    return 0;
}


Why do you want to write a program with large code segment, you can just borrow source code of any relatively large open source software, it is bound to have decent code segment size.


It is possible to generate large sections of repetitive code using macros. For example, the following is a C program (can also be compiled with a c++ compiler) with a large code segment:

volatile int x; // "volatile" to forbid compiler optimization

#define DO_0 ++x;
#define DO_1 {DO_0; DO_0; DO_0; DO_0; DO_0; DO_0; DO_0; DO_0; DO_0; DO_0}
#define DO_2 {DO_1; DO_1; DO_1; DO_1; DO_1; DO_1; DO_1; DO_1; DO_1; DO_1}
#define DO_3 {DO_2; DO_2; DO_2; DO_2; DO_2; DO_2; DO_2; DO_2; DO_2; DO_2}
#define DO_4 {DO_3; DO_3; DO_3; DO_3; DO_3; DO_3; DO_3; DO_3; DO_3; DO_3}
#define DO_5 {DO_4; DO_4; DO_4; DO_4; DO_4; DO_4; DO_4; DO_4; DO_4; DO_4}
#define DO_6 {DO_5; DO_5; DO_5; DO_5; DO_5; DO_5; DO_5; DO_5; DO_5; DO_5}
#define DO_7 {DO_6; DO_6; DO_6; DO_6; DO_6; DO_6; DO_6; DO_6; DO_6; DO_6}
#define DO_8 {DO_7; DO_7; DO_7; DO_7; DO_7; DO_7; DO_7; DO_7; DO_7; DO_7}
#define DO_9 {DO_8; DO_8; DO_8; DO_8; DO_8; DO_8; DO_8; DO_8; DO_8; DO_8}

int main()
{
    DO_6; // do trivial stuff 1 million times
}

This gives a pretty precise control on the size of the code segment. For example, on my system the size of the main function is 15000006 bytes (each DO_0 generates 15 bytes).

P.S. Just realized the question was asked 3 years ago.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜