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

Warning required when total size of indices is greater than INT_MAX #6758

Closed
yanang007 opened this issue Nov 28, 2022 · 4 comments · Fixed by #6846
Closed

Warning required when total size of indices is greater than INT_MAX #6758

yanang007 opened this issue Nov 28, 2022 · 4 comments · Fixed by #6846
Assignees
Labels
feature request Suggest an idea on this project

Comments

@yanang007
Copy link

Describe the bug
Accessing elements in ndarray with size greater than INT_MAX will randomly receive SIGSEGV. (Arch=x64)

To Reproduce

import taichi as ti
ti.init(arch=ti.cpu, debug=True)
x = ti.ndarray(ti.f64, (45000, 45000))
y = ti.ndarray(ti.f64, (48000, 48000))
print('Fine, which is', x[5578, 0], flush=True)  # size < INT_MAX, nothing happened
print('Should be fine, which is', y[5578, 0])  # size > INT_MAX, SIGSEGV received

where 5578 is a random number. Here is an example to find one.

import taichi as ti
ti.init(arch=ti.cpu, debug=True)
arr = ti.ndarray(ti.f64, (48000, 48000))
for i in range(48000):
    print(i, end='', flush=True)
    a = arr[i, 0]
    print(': passed', flush=True)

Log/Screenshots

$ python example.py
[Taichi] version 1.2.2, llvm 10.0.0, commit 608e4b57, win, python 3.9.13
[Taichi] Starting on arch=x64
Fine, which is 0.0
[E 11/29/22 02:00:43.692 117500] Received signal 11 (SIGSEGV)


***********************************
* Taichi Compiler Stack Traceback *
***********************************
0x7fffb02fa8cd: taichi::Time::get_cycles in taichi_python.cp39-win_amd64.pyd
0x7fffb02e9540: taichi::lang::GraphicsDevice::image_to_buffer in taichi_python.cp39-win_amd64.pyd
0x7fffb02e9523: taichi::lang::GraphicsDevice::image_to_buffer in taichi_python.cp39-win_amd64.pyd
0x7fffb02e94f7: taichi::lang::GraphicsDevice::image_to_buffer in taichi_python.cp39-win_amd64.pyd
0x7fffb011253a: taichi::Canvas::triangle in taichi_python.cp39-win_amd64.pyd
0x7fffb0705f1c: taichi::Logger::error in taichi_python.cp39-win_amd64.pyd
0x7fffb02e8eae: taichi::lang::GraphicsDevice::image_to_buffer in taichi_python.cp39-win_amd64.pyd
0x7ff832170ef5: seh_filter_exe in ucrtbase.dll
0x7ff6d7602408: Unknown Function in python.exe
0x7ff82621e390: _C_specific_handler in VCRUNTIME140.dll
0x7ff83499241f: _chkstk in ntdll.dll
0x7ff8349414a4: RtlRaiseException in ntdll.dll
0x7ff834990f4e: KiUserExceptionDispatcher in ntdll.dll
0x7ff8262113b8: memcpy in VCRUNTIME140.dll
0x7fffb05b0b66: PyInit_taichi_python in taichi_python.cp39-win_amd64.pyd
0x7fffb020b50f: taichi::lang::Ndarray::read_float in taichi_python.cp39-win_amd64.pyd
0x7fffb020b3fd: taichi::lang::Ndarray::read_float in taichi_python.cp39-win_amd64.pyd
0x7fffb03b5104: PyInit_taichi_python in taichi_python.cp39-win_amd64.pyd
0x7fffb03cefae: PyInit_taichi_python in taichi_python.cp39-win_amd64.pyd
0x7fffb04acdbb: PyInit_taichi_python in taichi_python.cp39-win_amd64.pyd
0x7fffb04b1905: PyInit_taichi_python in taichi_python.cp39-win_amd64.pyd
0x7fffb04b183f: PyInit_taichi_python in taichi_python.cp39-win_amd64.pyd
0x7fffb0393418: PyInit_taichi_python in taichi_python.cp39-win_amd64.pyd
0x7fffe480205e: PyArg_ParseTuple_SizeT in python39.dll
0x7fffe482f0e9: PyObject_MakeTpCall in python39.dll
0x7fffe4945bc0: PyErr_FormatFromCauseTstate in python39.dll
0x7fffe4839fa4: Py_NewReference in python39.dll
0x7fffe4842f1b: PyEval_EvalFrameDefault in python39.dll
0x7ff827647bf6: PyInit_pydevd_frame_evaluator_win32_39_64 in pydevd_frame_evaluator_win32_39_64.cp39-win_amd64.pyd
0x7fffe483c786: PyFunction_Vectorcall in python39.dll
0x7fffe483c07d: PyFunction_Vectorcall in python39.dll
0x7fffe4868c0c: PyVectorcall_Call in python39.dll
0x7fffe4868afe: PyObject_Call in python39.dll
0x7fffe4843d14: PyEval_EvalFrameDefault in python39.dll
0x7ff827647bf6: PyInit_pydevd_frame_evaluator_win32_39_64 in pydevd_frame_evaluator_win32_39_64.cp39-win_amd64.pyd
0x7fffe483bfb4: PyFunction_Vectorcall in python39.dll
0x7fffe4868c0c: PyVectorcall_Call in python39.dll
0x7fffe4868afe: PyObject_Call in python39.dll
0x7fffe4843d14: PyEval_EvalFrameDefault in python39.dll
0x7ff827647bf6: PyInit_pydevd_frame_evaluator_win32_39_64 in pydevd_frame_evaluator_win32_39_64.cp39-win_amd64.pyd
0x7fffe483c786: PyFunction_Vectorcall in python39.dll
0x7fffe483c07d: PyFunction_Vectorcall in python39.dll
0x7fffe47d53fa: PyObject_SetAttr in python39.dll
0x7fffe47d51d4: PyObject_SetAttr in python39.dll
0x7fffe480a7c2: PyObject_GetItem in python39.dll
0x7fffe4842ea2: PyEval_EvalFrameDefault in python39.dll
0x7ff827647bf6: PyInit_pydevd_frame_evaluator_win32_39_64 in pydevd_frame_evaluator_win32_39_64.cp39-win_amd64.pyd
0x7fffe483c786: PyFunction_Vectorcall in python39.dll
0x7fffe4891f05: PyEval_EvalCodeWithName in python39.dll
0x7fffe4891e47: PyEval_EvalCodeEx in python39.dll
0x7fffe4891da5: PyEval_EvalCode in python39.dll
0x7fffe48919a6: PyType_LookupId in python39.dll
0x7fffe4891926: PyType_LookupId in python39.dll
0x7fffe4867833: PyRun_StringFlags in python39.dll
0x7fffe4891c0a: PyType_LookupId in python39.dll
0x7fffe4891a5f: PyType_LookupId in python39.dll
0x7fffe483a530: Py_NewReference in python39.dll
0x7fffe4842af2: PyEval_EvalFrameDefault in python39.dll
0x7ff827647bf6: PyInit_pydevd_frame_evaluator_win32_39_64 in pydevd_frame_evaluator_win32_39_64.cp39-win_amd64.pyd
0x7fffe483c786: PyFunction_Vectorcall in python39.dll
0x7fffe483a60c: Py_NewReference in python39.dll
0x7fffe4842af2: PyEval_EvalFrameDefault in python39.dll
0x7ff827647bf6: PyInit_pydevd_frame_evaluator_win32_39_64 in pydevd_frame_evaluator_win32_39_64.cp39-win_amd64.pyd
0x7fffe483a226: Py_NewReference in python39.dll
0x7fffe4842f1b: PyEval_EvalFrameDefault in python39.dll
0x7ff827647bf6: PyInit_pydevd_frame_evaluator_win32_39_64 in pydevd_frame_evaluator_win32_39_64.cp39-win_amd64.pyd
0x7fffe483a226: Py_NewReference in python39.dll
0x7fffe4842cf3: PyEval_EvalFrameDefault in python39.dll
0x7ff827647bf6: PyInit_pydevd_frame_evaluator_win32_39_64 in pydevd_frame_evaluator_win32_39_64.cp39-win_amd64.pyd
0x7fffe483a226: Py_NewReference in python39.dll
0x7fffe4842cf3: PyEval_EvalFrameDefault in python39.dll
0x7ff827647bf6: PyInit_pydevd_frame_evaluator_win32_39_64 in pydevd_frame_evaluator_win32_39_64.cp39-win_amd64.pyd
0x7fffe483a226: Py_NewReference in python39.dll
0x7fffe4842cf3: PyEval_EvalFrameDefault in python39.dll
0x7ff827647bf6: PyInit_pydevd_frame_evaluator_win32_39_64 in pydevd_frame_evaluator_win32_39_64.cp39-win_amd64.pyd
0x7fffe483c786: PyFunction_Vectorcall in python39.dll
0x7fffe483c07d: PyFunction_Vectorcall in python39.dll
0x7fffe4838936: PyType_GenericAlloc in python39.dll
0x7fffe4868c0c: PyVectorcall_Call in python39.dll
0x7ff8173475c5: Unknown Function in pydevd_cython_win32_39_64.cp39-win_amd64.pyd
0x7ff817380e52: PyInit_pydevd_cython_win32_39_64 in pydevd_cython_win32_39_64.cp39-win_amd64.pyd
0x7ff817380fe0: PyInit_pydevd_cython_win32_39_64 in pydevd_cython_win32_39_64.cp39-win_amd64.pyd
0x7fffe480205e: PyArg_ParseTuple_SizeT in python39.dll
0x7ff8173475c5: Unknown Function in pydevd_cython_win32_39_64.cp39-win_amd64.pyd
0x7ff81736ca2e: PyInit_pydevd_cython_win32_39_64 in pydevd_cython_win32_39_64.cp39-win_amd64.pyd
0x7ff81735a9aa: PyInit_pydevd_cython_win32_39_64 in pydevd_cython_win32_39_64.cp39-win_amd64.pyd
0x7ff81735b9fb: PyInit_pydevd_cython_win32_39_64 in pydevd_cython_win32_39_64.cp39-win_amd64.pyd
0x7fffe482f0e9: PyObject_MakeTpCall in python39.dll
0x7fffe496c7a9: PyErr_FormatFromCauseTstate in python39.dll
0x7fffe49deaaf: PySys_WriteStdout in python39.dll
0x7fffe49df664: PySys_WriteStdout in python39.dll
0x7fffe49d1a25: Py_FatalError_TstateNULL in python39.dll
0x7fffe49d1ced: Py_FatalError_TstateNULL in python39.dll
0x7fffe494806f: PyErr_FormatFromCauseTstate in python39.dll
0x7ff827647b7d: PyInit_pydevd_frame_evaluator_win32_39_64 in pydevd_frame_evaluator_win32_39_64.cp39-win_amd64.pyd
0x7fffe483c786: PyFunction_Vectorcall in python39.dll
0x7fffe4891f05: PyEval_EvalCodeWithName in python39.dll
0x7fffe4891e47: PyEval_EvalCodeEx in python39.dll
0x7fffe4891da5: PyEval_EvalCode in python39.dll
0x7fffe4891b56: PyType_LookupId in python39.dll
0x7fffe4891a5f: PyType_LookupId in python39.dll
0x7fffe4838f97: PyType_GenericAlloc in python39.dll
0x7fffe48d6410: PyType_GenericNew in python39.dll
0x7fffe48d63cb: PyType_GenericNew in python39.dll
0x7fffe49d2176: Py_FatalError_TstateNULL in python39.dll
0x7fffe4946219: PyErr_FormatFromCauseTstate in python39.dll
0x7fffe4842af2: PyEval_EvalFrameDefault in python39.dll
0x7ff827647b7d: PyInit_pydevd_frame_evaluator_win32_39_64 in pydevd_frame_evaluator_win32_39_64.cp39-win_amd64.pyd
0x7fffe483c786: PyFunction_Vectorcall in python39.dll
0x7fffe483c07d: PyFunction_Vectorcall in python39.dll
0x7fffe48d6410: PyType_GenericNew in python39.dll
0x7fffe48d63cb: PyType_GenericNew in python39.dll
0x7fffe49d20be: Py_FatalError_TstateNULL in python39.dll
0x7fffe4946219: PyErr_FormatFromCauseTstate in python39.dll
0x7fffe4842f1b: PyEval_EvalFrameDefault in python39.dll
0x7ff827647b7d: PyInit_pydevd_frame_evaluator_win32_39_64 in pydevd_frame_evaluator_win32_39_64.cp39-win_amd64.pyd
0x7fffe483bfb4: PyFunction_Vectorcall in python39.dll
0x7fffe48d6410: PyType_GenericNew in python39.dll
0x7fffe48d63cb: PyType_GenericNew in python39.dll
0x7fffe49d20be: Py_FatalError_TstateNULL in python39.dll
0x7fffe4946219: PyErr_FormatFromCauseTstate in python39.dll
0x7fffe4842cf3: PyEval_EvalFrameDefault in python39.dll
0x7fffe483c786: PyFunction_Vectorcall in python39.dll
0x7fffe483a60c: Py_NewReference in python39.dll
0x7fffe4842cf3: PyEval_EvalFrameDefault in python39.dll
0x7fffe483a226: Py_NewReference in python39.dll
0x7fffe4842af2: PyEval_EvalFrameDefault in python39.dll
0x7fffe483c786: PyFunction_Vectorcall in python39.dll
0x7fffe4891f05: PyEval_EvalCodeWithName in python39.dll
0x7fffe4891e47: PyEval_EvalCodeEx in python39.dll
0x7fffe4891da5: PyEval_EvalCode in python39.dll
0x7fffe48919a6: PyType_LookupId in python39.dll
0x7fffe4891926: PyType_LookupId in python39.dll
0x7fffe484638b: PyDict_DelItemString in python39.dll
0x7fffe4846110: PyImport_GetMagicNumber in python39.dll
0x7fffe4846f67: PyRun_SimpleFileExFlags in python39.dll
0x7fffe4845c40: PyRun_AnyFileExFlags in python39.dll
0x7fffe48459ea: Py_MakePendingCalls in python39.dll
0x7fffe48a5a78: Py_RunMain in python39.dll
0x7fffe48a5901: Py_RunMain in python39.dll
0x7fffe48a5e9a: Py_RunMain in python39.dll
0x7fffe491d195: Py_Main in python39.dll
0x7ff6d7601268: Unknown Function in python.exe
0x7ff8332874b4: BaseThreadInitThunk in KERNEL32.DLL
0x7ff8349426a1: RtlUserThreadStart in ntdll.dll

Internal error occurred. Check out this page for possible solutions:
https://docs.taichi-lang.org/docs/install

Additional comments
Please let me know if diagnoses or any other information is needed.

It may be a potential int overflow in indexing, but I haven't seen any for now.

T Ndarray::read(const std::vector<int> &I) const {
prog_->synchronize();
size_t index = flatten_index(total_shape_, I);
size_t size_ = sizeof(T);
taichi::lang::Device::AllocParams alloc_params;
alloc_params.host_write = false;
alloc_params.host_read = true;
alloc_params.size = size_;
alloc_params.usage = taichi::lang::AllocUsage::Storage;
auto staging_buf_ =
this->ndarray_alloc_.device->allocate_memory_unique(alloc_params);
staging_buf_->device->memcpy_internal(
staging_buf_->get_ptr(),
this->ndarray_alloc_.get_ptr(/*offset=*/index * sizeof(T)), size_);
char *const device_arr_ptr =
reinterpret_cast<char *>(staging_buf_->device->map(*staging_buf_));
TI_ASSERT(device_arr_ptr);
T data;
std::memcpy(&data, device_arr_ptr, size_);
staging_buf_->device->unmap(*staging_buf_);
return data;
}

I noticed that taichi has been tested on even larger scales. If this is an intended behavior or a known limitation for CPU arch, it probably would be better to give a warning about it.

@yanang007 yanang007 added the potential bug Something that looks like a bug but not yet confirmed label Nov 28, 2022
@taichi-gardener taichi-gardener moved this to Untriaged in Taichi Lang Nov 28, 2022
@strongoier
Copy link
Contributor

Hi @yanang007. Thanks for reporting this! Taichi currently uses i32 for indexing so this behavior is known.

it probably would be better to give a warning about it.

Thanks for the suggestion! We should give a warning indeed.

@yanang007
Copy link
Author

Thanks for your quick reply! Then should I close this issue or change it into a feature request for the warning message?

Repository owner moved this from Untriaged to Done in Taichi Lang Dec 1, 2022
@yanang007
Copy link
Author

Sorry to repoen the issue. I did see a warning from SNode::create_node, but code like this will still break the program without any alarm.

ti.Matrix.field(3, 3, dtype=ti.i8, shape=(16000, 16000)).to_numpy()

A good idea may be to introduce a unified check for total size somewhere.

@yanang007 yanang007 reopened this Dec 1, 2022
@strongoier strongoier added feature request Suggest an idea on this project and removed potential bug Something that looks like a bug but not yet confirmed labels Dec 1, 2022
@strongoier
Copy link
Contributor

A good idea may be to introduce a unified check for total size somewhere.

Yep. Thanks for the suggestion. I've just marked this issue as a feature request.

@strongoier strongoier changed the title SIGSEGV triggered when accessing ndarray with size greater than INT_MAX (Arch=x64) Warning required when total size of indices is greater than INT_MAX Dec 1, 2022
@strongoier strongoier moved this from Done to Untriaged in Taichi Lang Dec 2, 2022
@strongoier strongoier moved this from Untriaged to Todo in Taichi Lang Dec 2, 2022
@strongoier strongoier self-assigned this Dec 2, 2022
@turbo0628 turbo0628 moved this from Todo to In Progress in Taichi Lang Dec 9, 2022
Repository owner moved this from In Progress to Done in Taichi Lang Dec 13, 2022
yanang007 added a commit to MetallyInduced/metalpy that referenced this issue Jan 1, 2023
yanang007 added a commit to MetallyInduced/metalpy that referenced this issue Jan 1, 2023
yanang007 added a commit to MetallyInduced/metalpy that referenced this issue Jan 2, 2023
quadpixels pushed a commit to quadpixels/taichi that referenced this issue May 13, 2023
…v#6846)

Issue: fix taichi-dev#6758

### Brief Summary

This PR adds a boundary check for ndarray similar to
https://github.com/taichi-dev/taichi/blob/a87b98fd40fc93dbe1bdf8c4e3932a6078379fa3/taichi/ir/snode.cpp#L89-L93

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
yanang007 added a commit to MetallyInduced/metalpy that referenced this issue Jun 10, 2023
yanang007 added a commit to MetallyInduced/metalpy that referenced this issue Jun 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Suggest an idea on this project
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants