()
| 522 | |
| 523 | |
| 524 | def set_initial_memory(): |
| 525 | user_specified_initial_heap = 'INITIAL_HEAP' in user_settings |
| 526 | |
| 527 | # INITIAL_HEAP cannot be used when the memory object is created in JS: we don't know |
| 528 | # the size of static data here and thus the total initial memory size. |
| 529 | if settings.IMPORTED_MEMORY: |
| 530 | if user_specified_initial_heap: |
| 531 | # Some of these could (and should) be implemented. |
| 532 | exit_with_error('INITIAL_HEAP is currently not compatible with IMPORTED_MEMORY') |
| 533 | # The default for imported memory is to fall back to INITIAL_MEMORY. |
| 534 | settings.INITIAL_HEAP = -1 |
| 535 | |
| 536 | if not user_specified_initial_heap: |
| 537 | # For backwards compatibility, we will only use INITIAL_HEAP by default when the user |
| 538 | # specified neither INITIAL_MEMORY nor MAXIMUM_MEMORY. Both place an upper bounds on |
| 539 | # the overall initial linear memory (stack + static data + heap), and we do not know |
| 540 | # the size of static data at this stage. Setting any non-zero initial heap value in |
| 541 | # this scenario would risk pushing users over the limit they have set. |
| 542 | user_specified_initial = settings.INITIAL_MEMORY != -1 |
| 543 | user_specified_maximum = 'MAXIMUM_MEMORY' in user_settings or 'WASM_MEM_MAX' in user_settings or 'BINARYEN_MEM_MAX' in user_settings |
| 544 | if user_specified_initial or user_specified_maximum: |
| 545 | settings.INITIAL_HEAP = -1 |
| 546 | |
| 547 | # Apply the default if we are going with INITIAL_MEMORY. |
| 548 | if settings.INITIAL_HEAP == -1 and settings.INITIAL_MEMORY == -1: |
| 549 | default_setting('INITIAL_MEMORY', 16 * 1024 * 1024) |
| 550 | |
| 551 | def check_memory_setting(setting): |
| 552 | if settings[setting] % webassembly.WASM_PAGE_SIZE != 0: |
| 553 | exit_with_error(f'{setting} must be a multiple of WebAssembly page size (64KiB), was {settings[setting]}') |
| 554 | if settings[setting] >= 2**53: |
| 555 | exit_with_error(f'{setting} must be smaller than 2^53 bytes due to JS Numbers (doubles) being used to hold pointer addresses in JS side') |
| 556 | |
| 557 | # Due to the aforementioned lack of knowledge about the static data size, we delegate |
| 558 | # checking the overall consistency of these settings to wasm-ld. |
| 559 | if settings.INITIAL_HEAP != -1: |
| 560 | check_memory_setting('INITIAL_HEAP') |
| 561 | |
| 562 | if settings.INITIAL_MEMORY != -1: |
| 563 | check_memory_setting('INITIAL_MEMORY') |
| 564 | if settings.INITIAL_MEMORY < settings.STACK_SIZE: |
| 565 | exit_with_error(f'INITIAL_MEMORY must be larger than STACK_SIZE, was {settings.INITIAL_MEMORY} (STACK_SIZE={settings.STACK_SIZE})') |
| 566 | |
| 567 | check_memory_setting('MAXIMUM_MEMORY') |
| 568 | if settings.MEMORY_GROWTH_LINEAR_STEP != -1: |
| 569 | check_memory_setting('MEMORY_GROWTH_LINEAR_STEP') |
| 570 | |
| 571 | |
| 572 | # Set an upper estimate of what MAXIMUM_MEMORY should be. Take note that this value |
no test coverage detected