Skip to content

Commit b463010

Browse files
committed
Fix mmap logic on MAP_FIXED
1 parent 452f416 commit b463010

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

qiling/os/posix/syscall/mman.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ def syscall_mmap_impl(ql: Qiling, addr: int, mlen: int, prot: int, flags: int, f
8282

8383
mmap_base = addr
8484
need_mmap = True
85-
eff_mmap_size = ((mlen + 0x1000 - 1) // 0x1000) * 0x1000
86-
87-
# align eff_mmap_size to page boundary
88-
aligned_address = (addr >> 12) << 12
89-
eff_mmap_size -= mmap_base - aligned_address
85+
eff_mmap_size = mlen
9086

9187
# initial ql.loader.mmap_address
9288
if addr != 0 and ql.mem.is_mapped(addr, mlen):
@@ -98,21 +94,24 @@ def syscall_mmap_impl(ql: Qiling, addr: int, mlen: int, prot: int, flags: int, f
9894
ql.log.debug(e)
9995
raise QlMemoryMappedError("Error: change protection at: 0x%x - 0x%x" % (addr, addr + mlen - 1))
10096
need_mmap = False
97+
else:
98+
addr = 0
10199

102100
# initialized mapping
103101
if need_mmap:
104-
if (flags & MAP_FIXED) > 0:
105-
mmap_base = addr
106-
else:
102+
eff_mmap_size = ((mlen + 0x1000 - 1) // 0x1000) * 0x1000
103+
if addr == 0:
107104
mmap_base = ql.loader.mmap_address
108-
ql.loader.mmap_address = mmap_base + eff_mmap_size
109-
ql.log.debug("%s - mapping needed for 0x%x" % (api_name, addr))
105+
ql.loader.mmap_address = mmap_base + eff_mmap_size
106+
# align eff_mmap_size to page boundary
107+
aligned_address = (mmap_base >> 12) << 12
108+
eff_mmap_size -= mmap_base - aligned_address
109+
ql.log.debug("%s - mapping needed for 0x%x" % (api_name, mmap_base))
110110
try:
111111
ql.mem.map(mmap_base, eff_mmap_size, info=("[syscall_%s]" % api_name))
112112
except Exception as e:
113113
raise QlMemoryMappedError("Error: mapping needed but failed")
114-
115-
ql.log.debug("%s - addr range 0x%x - 0x%x: " % (api_name, mmap_base, mmap_base + eff_mmap_size - 1))
114+
ql.log.debug("%s - addr range 0x%x - 0x%x: " % (api_name, mmap_base, mmap_base + eff_mmap_size - 1))
116115

117116
# FIXME: MIPS32 Big Endian
118117
try:
@@ -192,4 +191,4 @@ def ql_syscall_shmat(ql: Qiling, shmid: int, shmaddr: int, shmflg: int):
192191
else:
193192
addr = ql.mem.map(shmaddr, size, info="[shm]")
194193

195-
return addr
194+
return addr

0 commit comments

Comments
 (0)