I'm making a Python program that limits the available memory when running the program.
When tested with a limit of 1, 2, 3, 4 and 5 MB of memory, the following error occurs if the memory is limited to 3 or 4 MB.
./Main: error while loading shared libraries: libc.so.6: failed to map segment from shared object
Command '['./Main']' returned non-zero exit status 127.
The libc.so.6 file is in this location.
/usr/lib64/libc.so.6
This is my code.
import resource
import subprocess
import os
MEMORY_LIMIT = 3 * 1024 * 1024
resource.setrlimit(resource.RLIMIT_AS, (MEMORY_LIMIT, MEMORY_LIMIT))
soft, hard = resource.getrlimit(resource.RLIMIT_AS)
rlimit = ("pid {pid} soft {soft} hard {hard}"
.format(pid=os.getpid(), soft=soft, hard=hard))
print(rlimit)
try:
r=subprocess.run(['./Main'], check=True)
except Exception as e:
print(e)
And it's Main.c code.
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", a+b);
return 0;
}
I compiled it using the "gcc -o Main -O2 -Wall Main.c" command.
My question is:
- It runs normally when the memory limit is 5MB, but why does this error occur when the limit is 3 or 4MB is limited?
- Unlike RLIMIT_AS, why does RLIMIT_DATA or RLIMIT_STACK work well regardless of memory limitations?
Please answer me what the problem is.