Exemple #1
0
        private void TestDisableCoreDumpGlobally()
        {
            Skip.If(libc == null);

            Debug.WriteLine("LibcProtectedMemoryAllocatorTest.TestDisableCoreDumpGlobally");

            // Mac allocator has global core dumps disabled on init
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                Assert.False(libcProtectedMemoryAllocator.AreCoreDumpsGloballyDisabled());
                libc.getrlimit(libcProtectedMemoryAllocator.GetRlimitCoreResource(), out var rlim);

                // Initial values here system dependent, assumes docker container spun up w/ unlimited
                Assert.Equal(rlimit.UNLIMITED, rlim.rlim_max);
                Assert.Equal(rlimit.UNLIMITED, rlim.rlim_cur);
            }

            libcProtectedMemoryAllocator.DisableCoreDumpGlobally();
            Assert.True(libcProtectedMemoryAllocator.AreCoreDumpsGloballyDisabled());
            rlimit zeroRlimit = rlimit.Zero();

            libc.getrlimit(libcProtectedMemoryAllocator.GetRlimitCoreResource(), out var newRlimit);
            Assert.Equal(zeroRlimit.rlim_cur, newRlimit.rlim_cur);
            Assert.Equal(zeroRlimit.rlim_max, newRlimit.rlim_max);
        }
Exemple #2
0
        private void TestDisableCoreDumpGlobally()
        {
            // Don't run libc tests on platforms that don't match libc/posix behaviors
            if (libc == null)
            {
                return;
            }

            // Mac allocator has global core dumps disabled on init
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                Assert.False(libcProtectedMemoryAllocator.AreCoreDumpsGloballyDisabled());
                libc.getrlimit(libcProtectedMemoryAllocator.GetRlimitCoreResource(), out var rlim);

                // Initial values here system dependent, assumes docker container spun up w/ unlimited
                Assert.Equal(rlimit.UNLIMITED, rlim.rlim_max);
                Assert.Equal(rlimit.UNLIMITED, rlim.rlim_cur);
            }

            libcProtectedMemoryAllocator.DisableCoreDumpGlobally();
            Assert.True(libcProtectedMemoryAllocator.AreCoreDumpsGloballyDisabled());
            rlimit zeroRlimit = rlimit.Zero();

            libc.getrlimit(libcProtectedMemoryAllocator.GetRlimitCoreResource(), out var newRlimit);
            Assert.Equal(zeroRlimit.rlim_cur, newRlimit.rlim_cur);
            Assert.Equal(zeroRlimit.rlim_max, newRlimit.rlim_max);
        }
Exemple #3
0
        protected LibcProtectedMemoryAllocatorLP64(LibcLP64 libc)
        {
            this.libc = libc ?? throw new ArgumentNullException(nameof(libc));

            libc.getrlimit(GetMemLockLimit(), out var rlim);
            if (rlim.rlim_max == rlimit.UNLIMITED)
            {
                resourceLimit = 0;
            }
            else
            {
                resourceLimit = (long)rlim.rlim_max;
            }
        }
        // ************************************
        // alloc / free
        // ************************************
        public virtual IntPtr Alloc(ulong length)
        {
#pragma warning disable 162
            if (Debug.On)
            {
                Logger.LogDebug("attempting to alloc length {length}", length);
            }
#pragma warning restore 162

            libc.getrlimit(GetMemLockLimit(), out var rlim);
            if (rlim.rlim_max != rlimit.UNLIMITED && rlim.rlim_max < length)
            {
                throw new MemoryLimitException(
                          $"Requested MemLock length exceeds resource limit max of {rlim.rlim_max}");
            }

            // Some platforms may require fd to be -1 even if using anonymous
            IntPtr protectedMemory = libc.mmap(
                IntPtr.Zero, length, GetProtReadWrite(), GetPrivateAnonymousFlags(), -1, 0);

            CheckIntPtr(protectedMemory, "mmap");
            try
            {
                CheckZero(libc.mlock(protectedMemory, length), "mlock");

                try
                {
                    SetNoDump(protectedMemory, length);
                }
                catch (Exception e)
                {
                    CheckZero(libc.munlock(protectedMemory, length), "munlock", e);
                    throw;
                }
            }
            catch (Exception e)
            {
                CheckZero(libc.munmap(protectedMemory, length), "munmap", e);
                throw;
            }

            return(protectedMemory);
        }
Exemple #5
0
 internal virtual ulong GetMemlockResourceLimit()
 {
     libc.getrlimit(GetMemLockLimit(), out var rlim);
     return(rlim.rlim_max);
 }