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

[BUG] pop.aligned_alloc can throw std::bad_alloc with no way to handle it #3874

Open
owenhilyard opened this issue Dec 12, 2024 · 0 comments
Assignees
Labels
bug Something isn't working mojo-repo Tag all issues with this label

Comments

@owenhilyard
Copy link
Contributor

Bug description

The mojo runtime does not provide an error handling mechanism for failed allocations. I expect to be able one of the following to happen:

  1. The returned pointer is null.
  2. memory.memory._malloc raises, meaning that all methods which can allocate also raise.

Steps to reproduce

Run the following program on system with less than 256 GB of memory (vm.overcommit_memory=0 or vm.overcommit_memory=2, vm.overcommit_memory=1 will produce a segfault while walking the array):

from memory import UnsafePointer, memset

fn main():
	alias len = 1024 * 1024 * 1024 * 256
	var foo = UnsafePointer[UInt8].alloc(len)
	if not foo:
		print("NULL")
	else:
		print("Not NULL")
		memset(foo, 1, len)
		var sum: UInt8 = 0
		for i in range(len):
			sum += foo[i]			
		print(sum)

If you use mojo run, you get LLVM ERROR: out of memory. If you use mojo build, you get:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)

Here's a C program that works as I expect:

#include <memory.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>

int main() {
        char* foo = malloc(((size_t)1024) * 1024 * 1024 * 256);
        if (foo == NULL) {
                printf("NULL\n");
        } else {
                printf("Not NULL\n");
        }
}

System information

OS: Ubuntu 22.04.5 LTS rootful podman container running on Ubuntu 24.04.1 LTS
CPU: AMD EPYC 7313P 16-Core
Memory: 128 GB
@owenhilyard owenhilyard added bug Something isn't working mojo-repo Tag all issues with this label labels Dec 12, 2024
@lsh lsh self-assigned this Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

2 participants