Skip to content

Commit 1b9ebac

Browse files
committed
Merge remote-tracking branch 'origin/main' into 415-executable-tests-fix
2 parents 79cf1eb + dfd7be6 commit 1b9ebac

File tree

9 files changed

+128
-162
lines changed

9 files changed

+128
-162
lines changed

skip_test_cases.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ memory_tests/deterministic/mmap.c
3333
memory_tests/deterministic/mmap_complicated.c
3434
memory_tests/deterministic/mmap_file.c
3535
memory_tests/deterministic/mmaptest.c
36-
memory_tests/deterministic/shmtest.c
3736
memory_tests/deterministic/vtable.c
3837
memory_tests/non-deterministic/segfault.c
39-
memory_tests/non-deterministic/shm.c
4038
networking_tests/deterministic/pipepong.c
4139
networking_tests/deterministic/uds-getsockname.c
4240
networking_tests/deterministic/uds-nb-select.c

src/cage/src/cage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct Cage {
3434
pub cwd: RwLock<Arc<PathBuf>>,
3535
// Reverse mapping for shared memory of addresses in cage to shmid, used for attaching and deattaching
3636
// shared memory segments
37-
pub rev_shm: Mutex<Vec<(u32, i32)>>,
37+
pub rev_shm: Mutex<Vec<(u64, i32)>>,
3838
// signalhandler is a hash map where the key is a signal number, and the value is a SigactionStruct, which
3939
// defines how the cage should handle a specific signal. Interacts with sigaction_syscall() to register or
4040
// retrieve the handler for a specific signal.

src/cage/src/memory/shared.rs

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,6 @@ pub fn timestamp() -> u64 {
5151
.as_secs()
5252
}
5353

54-
/// Calls the system `mmap` via `libc` and returns the mapped address as a 32-bit integer.
55-
///
56-
/// In Lind-WASM, addresses are represented as 32-bit values within the linear memory of a
57-
/// Wasm module. The return value is truncated to 32 bits to match this representation.
58-
/// On error, returns `-1` (corresponding to `MAP_FAILED`).
59-
///
60-
/// # Safety
61-
/// This function uses raw pointers and directly invokes `libc::mmap`. The caller must ensure
62-
/// that all arguments are valid and that using the returned address as a 32-bit pointer is safe
63-
/// in the Lind-WASM context.
64-
pub fn libc_mmap(addr: *mut u8, len: usize, prot: i32, flags: i32, fildes: i32, off: i64) -> i32 {
65-
return ((unsafe { libc::mmap(addr as *mut c_void, len, prot, flags, fildes, off) } as i64)
66-
& 0xffffffff) as i32;
67-
}
68-
6954
// Mimic shared memory in Linux by creating a file backing and truncating it to the segment size
7055
// We can then safely unlink the file while still holding a descriptor to that segment,
7156
// which we can use to map shared across cages.
@@ -149,7 +134,7 @@ impl ShmSegment {
149134
}
150135
// mmap shared segment into cage, and increase attachments
151136
// increase in cage references within attached_cages map
152-
pub fn map_shm(&mut self, shmaddr: *mut u8, prot: i32, cageid: u64) -> i32 {
137+
pub fn map_shm(&mut self, shmaddr: *mut u8, prot: i32, cageid: u64) -> usize {
153138
let fobjfdno = self.filebacking.as_fd_handle_raw_int();
154139
self.shminfo.shm_nattch += 1;
155140
self.shminfo.shm_atime = timestamp() as isize;
@@ -162,27 +147,34 @@ impl ShmSegment {
162147
vacant.insert(1);
163148
}
164149
};
165-
libc_mmap(
166-
shmaddr,
167-
self.size as usize,
168-
prot,
169-
(MAP_SHARED as i32) | (MAP_FIXED as i32),
170-
fobjfdno,
171-
0,
172-
)
150+
151+
unsafe {
152+
(libc::mmap(
153+
shmaddr as *mut c_void,
154+
self.size as usize,
155+
prot,
156+
(MAP_SHARED as i32) | (MAP_FIXED as i32),
157+
fobjfdno,
158+
0,
159+
) as usize)
160+
}
173161
}
174162

175163
// unmap shared segment, decrease attachments
176164
// decrease references within attached cages map
177165
pub fn unmap_shm(&mut self, shmaddr: *mut u8, cageid: u64) {
178-
libc_mmap(
179-
shmaddr,
180-
self.size as usize,
181-
PROT_NONE,
182-
(MAP_PRIVATE as i32) | (MAP_ANONYMOUS as i32) | (MAP_FIXED as i32),
183-
-1,
184-
0,
185-
);
166+
let mmap_ret = unsafe {
167+
(libc::mmap(
168+
shmaddr as *mut c_void,
169+
self.size as usize,
170+
PROT_NONE,
171+
(MAP_PRIVATE as i32) | (MAP_ANONYMOUS as i32) | (MAP_FIXED as i32),
172+
-1,
173+
0,
174+
) as usize)
175+
};
176+
assert!(mmap_ret == shmaddr as usize);
177+
186178
self.shminfo.shm_nattch -= 1;
187179
self.shminfo.shm_dtime = timestamp() as isize;
188180
match self.attached_cages.entry(cageid) {
@@ -269,9 +261,9 @@ pub fn unmap_shm_mappings(cageid: u64) {
269261
/// # Returns
270262
/// * `Some(index)` if the address is found in the vector.
271263
/// * `None` if the address does not exist in the mapping.
272-
pub fn rev_shm_find_index_by_addr(rev_shm: &Vec<(u32, i32)>, shmaddr: u32) -> Option<usize> {
264+
pub fn rev_shm_find_index_by_addr(rev_shm: &Vec<(u64, i32)>, shmaddr: u64) -> Option<usize> {
273265
for (index, val) in rev_shm.iter().enumerate() {
274-
if val.0 == shmaddr as u32 {
266+
if val.0 == shmaddr as u64 {
275267
return Some(index);
276268
}
277269
}
@@ -285,9 +277,9 @@ pub fn rev_shm_find_index_by_addr(rev_shm: &Vec<(u32, i32)>, shmaddr: u32) -> Op
285277
/// * `shmid` – The shared memory ID to search for.
286278
///
287279
/// # Returns
288-
/// * A vector of all addresses (`u32`) associated with the given `shmid`.
280+
/// * A vector of all addresses (`u64`) associated with the given `shmid`.
289281
/// * Returns an empty vector if no addresses are found.
290-
pub fn rev_shm_find_addrs_by_shmid(rev_shm: &Vec<(u32, i32)>, shmid: i32) -> Vec<u32> {
282+
pub fn rev_shm_find_addrs_by_shmid(rev_shm: &Vec<(u64, i32)>, shmid: i32) -> Vec<u64> {
291283
let mut addrvec = Vec::new();
292284
for val in rev_shm.iter() {
293285
if val.1 == shmid as i32 {
@@ -308,15 +300,15 @@ pub fn rev_shm_find_addrs_by_shmid(rev_shm: &Vec<(u32, i32)>, shmid: i32) -> Vec
308300
/// * `Some((base_addr, shmid))` if `search_addr` falls within the range of a known segment.
309301
/// * `None` if the address is not within any tracked region.
310302
pub fn search_for_addr_in_region(
311-
rev_shm: &Vec<(u32, i32)>,
312-
search_addr: u32,
313-
) -> Option<(u32, i32)> {
303+
rev_shm: &Vec<(u64, i32)>,
304+
search_addr: u64,
305+
) -> Option<(u64, i32)> {
314306
let metadata = &SHM_METADATA;
315307
for val in rev_shm.iter() {
316308
let addr = val.0;
317309
let shmid = val.1;
318310
if let Some(segment) = metadata.shmtable.get_mut(&shmid) {
319-
let range = addr..(addr + segment.size as u32);
311+
let range = addr..(addr + segment.size as u64);
320312
if range.contains(&search_addr) {
321313
return Some((addr, shmid));
322314
}
@@ -329,7 +321,7 @@ pub fn search_for_addr_in_region(
329321
///
330322
/// # Arguments
331323
/// * `cageid` – ID of the calling cage.
332-
/// * `shmaddr` – Address where the shared memory segment should be mapped.
324+
/// * `shmaddr` – System Address where the shared memory segment should be mapped.
333325
/// * `shmflg` – Flags controlling access (e.g., `SHM_RDONLY`).
334326
/// * `shmid` – Identifier of the shared memory segment to attach.
335327
///
@@ -339,9 +331,9 @@ pub fn search_for_addr_in_region(
339331
/// invalid, it returns an error.
340332
///
341333
/// # Returns
342-
/// * On success – the mapped address as a `u32`.
343-
/// * On error – a negative errno value as a `u32`.
344-
pub fn shmat_helper(cageid: u64, shmaddr: *mut u8, shmflg: i32, shmid: i32) -> u32 {
334+
/// * On success – the mapped address as a `usize`.
335+
/// * On error – a negative errno value as a `usize`.
336+
pub fn shmat_helper(cageid: u64, shmaddr: *mut u8, shmflg: i32, shmid: i32) -> usize {
345337
let metadata = &SHM_METADATA;
346338
let prot: i32;
347339

@@ -354,20 +346,20 @@ pub fn shmat_helper(cageid: u64, shmaddr: *mut u8, shmflg: i32, shmid: i32) -> u
354346
prot = PROT_READ | PROT_WRITE;
355347
}
356348
let mut rev_shm = cage.rev_shm.lock();
357-
rev_shm.push((shmaddr as u32, shmid));
349+
rev_shm.push((shmaddr as u64, shmid));
358350
drop(rev_shm);
359351

360-
segment.map_shm(shmaddr, prot, cageid) as u32
352+
segment.map_shm(shmaddr, prot, cageid) as usize
361353
} else {
362-
syscall_error(Errno::EINVAL, "shmat", "Invalid shmid value") as u32
354+
syscall_error(Errno::EINVAL, "shmat", "Invalid shmid value") as usize
363355
}
364356
}
365357

366358
/// Helper function that detaches a shared memory segment from the calling cage’s address space.
367359
///
368360
/// # Arguments
369361
/// * `cageid` – ID of the calling cage.
370-
/// * `shmaddr` – Address of the shared memory segment to detach.
362+
/// * `shmaddr` – System Address of the shared memory segment to detach.
371363
///
372364
/// This function searches the cage’s reverse mapping table for the segment mapped at `shmaddr`,
373365
/// detaches it with `unmap_shm`, and removes the reverse mapping entry. If the segment was marked
@@ -382,7 +374,7 @@ pub fn shmdt_helper(cageid: u64, shmaddr: *mut u8) -> i32 {
382374
let mut rm = false;
383375
let cage = get_cage(cageid).unwrap();
384376
let mut rev_shm = cage.rev_shm.lock();
385-
let rev_shm_index = rev_shm_find_index_by_addr(&rev_shm, shmaddr as u32);
377+
let rev_shm_index = rev_shm_find_index_by_addr(&rev_shm, shmaddr as u64);
386378

387379
if let Some(index) = rev_shm_index {
388380
let shmid = rev_shm[index].1;

src/cage/src/memory/vmmap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ impl VmmapOps for Vmmap {
868868
///
869869
/// Arguments:
870870
/// - npages: Number of pages needed
871-
/// - hint: Starting address to search from
871+
/// - hint: Starting address (in pages) to search from
872872
///
873873
/// Returns:
874874
/// - Some(Interval) containing the found space
@@ -932,7 +932,7 @@ impl VmmapOps for Vmmap {
932932
/// Arguments:
933933
/// - num_pages: Number of pages needed
934934
/// - pages_per_map: Alignment requirement in pages
935-
/// - hint: Starting address to search from
935+
/// - hint: Starting address (in pages) to search from
936936
///
937937
/// Returns:
938938
/// - Some(Interval) containing aligned space

src/glibc/sysdeps/unix/sysv/linux/shmctl.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include <errno.h>
2424
#include <linux/posix_types.h> /* For __kernel_mode_t. */
2525

26+
#include <syscall-template.h>
27+
#include <lind_syscall_num.h>
28+
2629
/* POSIX states ipc_perm mode should have type of mode_t. */
2730
_Static_assert (sizeof ((struct shmid_ds){0}.shm_perm.mode)
2831
== sizeof (mode_t),
@@ -73,12 +76,8 @@ typedef struct kernel_shmid64_ds shmctl_arg_t;
7376
static int
7477
shmctl_syscall (int shmid, int cmd, shmctl_arg_t *buf)
7578
{
76-
#ifdef __ASSUME_DIRECT_SYSVIPC_SYSCALLS
77-
return INLINE_SYSCALL_CALL (shmctl, shmid, cmd | __IPC_64, buf);
78-
#else
79-
return INLINE_SYSCALL_CALL (ipc, IPCOP_shmctl, shmid, cmd | __IPC_64, 0,
80-
buf);
81-
#endif
79+
// #endif
80+
return MAKE_SYSCALL(SHMCTL_SYSCALL, "syscall|shmctl", (uint64_t) shmid, (uint64_t) cmd, (uint64_t) buf, NOTUSED, NOTUSED, NOTUSED);
8281
}
8382

8483
/* Provide operations to control over shared memory segments. */
@@ -277,15 +276,7 @@ int
277276
attribute_compat_text_section
278277
__old_shmctl (int shmid, int cmd, struct __old_shmid_ds *buf)
279278
{
280-
#if defined __ASSUME_DIRECT_SYSVIPC_SYSCALLS \
281-
&& !defined __ASSUME_SYSVIPC_DEFAULT_IPC_64
282-
/* For architecture that have wire-up shmctl but also have __IPC_64 to a
283-
value different than default (0x0), it means the compat symbol used the
284-
__NR_ipc syscall. */
285-
return INLINE_SYSCALL_CALL (shmctl, shmid, cmd, buf);
286-
#else
287-
return INLINE_SYSCALL_CALL (ipc, IPCOP_shmctl, shmid, cmd, 0, buf);
288-
#endif
279+
return MAKE_SYSCALL(SHMCTL_SYSCALL, "syscall|shmctl", (uint64_t) shmid, (uint64_t) cmd, (uint64_t) buf, NOTUSED, NOTUSED, NOTUSED);
289280
}
290281
compat_symbol (libc, __old_shmctl, shmctl, GLIBC_2_0);
291282
#endif

0 commit comments

Comments
 (0)