Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support configurable stack size #1082

Open
3 of 5 tasks
Backl1ght opened this issue May 26, 2022 · 4 comments
Open
3 of 5 tasks

Support configurable stack size #1082

Backl1ght opened this issue May 26, 2022 · 4 comments
Labels
enhancement New feature or request low_priority This label is for issues that have less prority for now.

Comments

@Backl1ght
Copy link

Backl1ght commented May 26, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Is this feature provided in the master branch?

  • I have checked the changelog

The problem you are facing

The code below will receive RE locally due to stack size limit.

#include <bits/stdc++.h>

struct Foo {
  int a[5000005];
};

int main(int argc, char* argv[]) {
  Foo foo;
  foo.a[0] = 0;
  std::cout << foo.a[0] << std::endl;
  return 0;
}

Though we can use other methods to solve it, like using setrlimit system call on Linux to acquire a bigger stack mannually, I think it will be better if cpeditor can support configuable stack size.

Describe the feature you'd like

Support configurable stack size.

Describe alternatives you've considered

No response

Are you willing to contribute to this?

  • I'm ready to contribute to this (and will open a PR soon)
  • I'd like to have a try (with the help of the maintainers)
  • No, thanks

Anything else?

No response

@Backl1ght Backl1ght added the enhancement New feature or request label May 26, 2022
@ouuan ouuan added the low_priority This label is for issues that have less prority for now. label May 26, 2022
@coder3101
Copy link
Member

coder3101 commented Jun 15, 2022

Generally stack size is configurable at time of compilation so can you not change the compile command to suit your need? It should work for MinGW in Windows.

@coder3101
Copy link
Member

coder3101 commented Jun 15, 2022

You can add Add ulimit -s unlimited to .bashrc for linux? I don't think there is a way to specifically increase limit for one program except using setrlimit syscall.

@StupidQu
Copy link

StupidQu commented Oct 2, 2023

Adding ulimit -s unlimited to bashrc didn't work for me, the stack limit did change when I open a terminal, but it didn't work in CP Editor.

@coder3101
Copy link
Member

coder3101 commented Oct 2, 2023

@StupidQu correct, we spawn your code as child process with QProcess.

There are two ways to increase stack size for your program:

  • We set the cpeditor stack size to unlimited using setrlimit and if limits are inherited by child processes, your code will run with unlimited stack size. No doubt this is bad.

  • In your code you can programatically set the unlimited stack size limit. This limit change can be controlled with conditional compilation so that it doesn't run on judge.

#include <iostream>
#include <sys/resource.h>

int main() {
    struct rlimit rl;
    rl.rlim_cur = RLIM_INFINITY;  // Set soft limit to unlimited
    rl.rlim_max = RLIM_INFINITY;  // Set hard limit to unlimited

    if (setrlimit(RLIMIT_STACK, &rl) != 0) {
        perror("setrlimit");
        return 1;
    }

    // Your program logic here

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request low_priority This label is for issues that have less prority for now.
Projects
None yet
Development

No branches or pull requests

4 participants