Skip to content

Instantly share code, notes, and snippets.

@MasterDuke17
Last active October 10, 2020 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasterDuke17/68ca861b7311704d1eccac23d7680713 to your computer and use it in GitHub Desktop.
Save MasterDuke17/68ca861b7311704d1eccac23d7680713 to your computer and use it in GitHub Desktop.
[dan@alexandria perl6]$ LD_PRELOAD=./MoarVM/3rdparty/gmp/libgmp.so gdb --args ./install/bin/raku -e 'say 2⁴⁵⁵³⁵³⁵³⁴⁵³⁶⁴⁵³⁵³⁴⁵'
GNU gdb (GDB) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./install/bin/raku...
(gdb) r
Starting program: /home/dan/Source/perl6/install/bin/raku -e say\ 2⁴⁵⁵³⁵³⁵³⁴⁵³⁶⁴⁵³⁵³⁴⁵
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
[New Thread 0x7ffff6de5640 (LWP 133923)]
gmp: overflow in mpz type
Thread 1 "raku" received signal SIGABRT, Aborted.
0x00007ffff7474615 in raise () from /usr/lib/libc.so.6
(gdb) bt
#0 0x00007ffff7474615 in raise () from /usr/lib/libc.so.6
#1 0x00007ffff745d862 in abort () from /usr/lib/libc.so.6
#2 0x00007ffff7f651d5 in __gmpz_realloc (m=m@entry=0x55555819ac30, new_alloc=71148989771320869) at realloc.c:57
#3 0x00007ffff7f61f7f in __gmpz_n_pow_ui (r=r@entry=0x55555819ac30, bp=0x55555819ac10, bsize=1, e=0, e@entry=4553535345364535345) at n_pow_ui.c:339
#4 0x00007ffff7f62821 in __gmpz_pow_ui (r=r@entry=0x55555819ac30, b=b@entry=0x55555555d2e0, e=e@entry=4553535345364535345) at pow_ui.c:50
#5 0x00007ffff793fee4 in MVM_bigint_pow (tc=0x55555555a1a0, a=<optimized out>, b=<optimized out>, num_type=<optimized out>, int_type=0x5555574ed5f8) at src/math/bigintops.c:409
#6 0x00007ffff784e904 in MVM_interp_run (tc=0x2, initial_invoke=0x7fffffffd930, invoke_data=0x7fffffffd930, outer_runloop=0x7ffff7474615 <raise+325>) at src/core/interp.c:3518
#7 0x00005555555557c3 in main (argc=<optimized out>, argv=0x7fffffffe378) at src/vm/moar/runner/main.c:408
(gdb)
diff --git src/core/threadcontext.c src/core/threadcontext.c
index a4faff5dd..c09aaaebe 100644
--- src/core/threadcontext.c
+++ src/core/threadcontext.c
@@ -1,6 +1,24 @@
#include "moar.h"
#include "platform/time.h"
+static __thread void *gmp_tc = NULL;
+
+void * MVM_gmp_malloc(size_t alloc_size) {
+ if (alloc_size > INT_MAX)
+ MVM_exception_throw_adhoc(gmp_tc, "Can't alloc a GMP bigint this big!");
+ else
+ return (void *)MVM_malloc(alloc_size);
+}
+void * MVM_gmp_realloc(void *ptr, size_t old_size, size_t new_size) {
+ if (new_size > INT_MAX)
+ MVM_exception_throw_adhoc(gmp_tc, "Can't realloc a GMP bigint this big!");
+ else
+ return (void *)MVM_realloc(ptr, new_size);
+}
+void MVM_gmp_free(void *ptr, size_t size) {
+ MVM_free(ptr);
+}
+
/* Initializes a new thread context. Note that this doesn't set up a
* thread itself, it just creates the data structure that exists in
* MoarVM per thread. */
@@ -37,6 +55,8 @@ MVMThreadContext * MVM_tc_create(MVMThreadContext *parent, MVMInstance *instance
/* Allocate an initial call stack region for the thread. */
MVM_callstack_region_init(tc);
+ gmp_tc = tc;
+ mp_set_memory_functions(&MVM_gmp_malloc, &MVM_gmp_realloc, &MVM_gmp_free);
/* Initialize random number generator state. */
MVM_proc_seed(tc, (MVM_platform_now() / 10000) * MVM_proc_getpid(tc));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment