Skip to content

Commit

Permalink
[PAL] Return 0 on success in PalStreamSetLength()
Browse files Browse the repository at this point in the history
Previously, there was a bug of returning the supplied length. However,
the PAL API explicitly states that this function returns 0 on success,
and LibOS relied on this behavior.

Signed-off-by: Dmitrii Kuvaiskii <dmitrii.kuvaiskii@intel.com>
  • Loading branch information
dimakuv authored and mkow committed Dec 17, 2023
1 parent 7e260b1 commit 5d38bb1
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 25 deletions.
6 changes: 3 additions & 3 deletions pal/include/pal_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ struct handle_ops {
int (*map)(PAL_HANDLE handle, void* address, pal_prot_flags_t prot, uint64_t offset,
uint64_t size);

/* 'setlength' is used by PalStreamFlush. It truncate the stream to certain size. */
int64_t (*setlength)(PAL_HANDLE handle, uint64_t length);
/* 'setlength' is used by PalStreamFlush. It truncates the stream to certain size. */
int (*setlength)(PAL_HANDLE handle, uint64_t length);

/* 'flush' is used by PalStreamFlush. It syncs the stream to the device */
int (*flush)(PAL_HANDLE handle);
Expand Down Expand Up @@ -177,7 +177,7 @@ int _PalStreamAttributesQuery(const char* uri, PAL_STREAM_ATTR* attr);
int _PalStreamAttributesQueryByHandle(PAL_HANDLE hdl, PAL_STREAM_ATTR* attr);
int _PalStreamMap(PAL_HANDLE handle, void* addr, pal_prot_flags_t prot, uint64_t offset,
uint64_t size);
int64_t _PalStreamSetLength(PAL_HANDLE handle, uint64_t length);
int _PalStreamSetLength(PAL_HANDLE handle, uint64_t length);
int _PalStreamFlush(PAL_HANDLE handle);
int _PalSendHandle(PAL_HANDLE target_process, PAL_HANDLE cargo);
int _PalReceiveHandle(PAL_HANDLE source_process, PAL_HANDLE* out_cargo);
Expand Down
4 changes: 2 additions & 2 deletions pal/src/host/linux-sgx/pal_devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ static int dev_delete(PAL_HANDLE handle, enum pal_delete_mode delete_mode) {
return ret < 0 ? unix_to_pal_error(ret) : ret;
}

static int64_t dev_setlength(PAL_HANDLE handle, uint64_t length) {
static int dev_setlength(PAL_HANDLE handle, uint64_t length) {
assert(handle->hdr.type == PAL_TYPE_DEV);

int ret = ocall_ftruncate(handle->dev.fd, length);
return ret < 0 ? unix_to_pal_error(ret) : (int64_t)length;
return ret < 0 ? unix_to_pal_error(ret) : 0;
}

static int dev_flush(PAL_HANDLE handle) {
Expand Down
4 changes: 2 additions & 2 deletions pal/src/host/linux-sgx/pal_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,13 @@ static int file_map(PAL_HANDLE handle, void* addr, pal_prot_flags_t prot, uint64
}

/* 'setlength' operation for file stream. */
static int64_t file_setlength(PAL_HANDLE handle, uint64_t length) {
static int file_setlength(PAL_HANDLE handle, uint64_t length) {
int ret = ocall_ftruncate(handle->file.fd, length);
if (ret < 0)
return unix_to_pal_error(ret);

handle->file.total = length;
return (int64_t)length;
return 0;
}

/* 'flush' operation for file stream. */
Expand Down
4 changes: 2 additions & 2 deletions pal/src/host/linux/pal_devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ static int dev_delete(PAL_HANDLE handle, enum pal_delete_mode delete_mode) {
return ret < 0 ? unix_to_pal_error(ret) : ret;
}

static int64_t dev_setlength(PAL_HANDLE handle, uint64_t length) {
static int dev_setlength(PAL_HANDLE handle, uint64_t length) {
assert(handle->hdr.type == PAL_TYPE_DEV);

int ret = DO_SYSCALL(ftruncate, handle->dev.fd, length);
return ret < 0 ? unix_to_pal_error(ret) : (int64_t)length;
return ret < 0 ? unix_to_pal_error(ret) : 0;
}

static int dev_map(PAL_HANDLE handle, void* addr, pal_prot_flags_t prot, uint64_t offset,
Expand Down
4 changes: 2 additions & 2 deletions pal/src/host/linux/pal_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ static int file_map(PAL_HANDLE handle, void* addr, pal_prot_flags_t prot, uint64
}

/* 'setlength' operation for file stream. */
static int64_t file_setlength(PAL_HANDLE handle, uint64_t length) {
static int file_setlength(PAL_HANDLE handle, uint64_t length) {
int ret = DO_SYSCALL(ftruncate, handle->file.fd, length);

if (ret < 0)
return (ret == -EINVAL || ret == -EBADF) ? -PAL_ERROR_BADHANDLE
: -PAL_ERROR_DENIED;

return (int64_t)length;
return 0;
}

/* 'flush' operation for file stream. */
Expand Down
2 changes: 1 addition & 1 deletion pal/src/host/skeleton/pal_devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static int dev_delete(PAL_HANDLE handle, enum pal_delete_mode delete_mode) {
return -PAL_ERROR_NOTIMPLEMENTED;
}

static int64_t dev_setlength(PAL_HANDLE handle, uint64_t length) {
static int dev_setlength(PAL_HANDLE handle, uint64_t length) {
return -PAL_ERROR_NOTIMPLEMENTED;
}

Expand Down
2 changes: 1 addition & 1 deletion pal/src/host/skeleton/pal_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static int file_map(PAL_HANDLE handle, void* addr, pal_prot_flags_t prot, uint64
}

/* 'setlength' operation for file stream. */
static int64_t file_setlength(PAL_HANDLE handle, uint64_t length) {
static int file_setlength(PAL_HANDLE handle, uint64_t length) {
return -PAL_ERROR_NOTIMPLEMENTED;
}

Expand Down
14 changes: 2 additions & 12 deletions pal/src/pal_streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,7 @@ int PalStreamMap(PAL_HANDLE handle, void* addr, pal_prot_flags_t prot, uint64_t
return _PalStreamMap(handle, addr, prot, offset, size);
}

/* _PalStreamSetLength for internal use. This function truncate the stream to certain length. This
* call might not be support for certain streams */
int64_t _PalStreamSetLength(PAL_HANDLE handle, uint64_t length) {
int _PalStreamSetLength(PAL_HANDLE handle, uint64_t length) {
const struct handle_ops* ops = HANDLE_OPS(handle);

if (!ops)
Expand All @@ -328,15 +326,7 @@ int PalStreamSetLength(PAL_HANDLE handle, uint64_t length) {
if (!handle) {
return -PAL_ERROR_INVAL;
}

int64_t ret = _PalStreamSetLength(handle, length);

if (ret < 0) {
return ret;
}

assert((uint64_t)ret == length);
return 0;
return _PalStreamSetLength(handle, length);
}

/* _PalStreamFlush for internal use. This function sync up the handle with devices. Some streams may
Expand Down

0 comments on commit 5d38bb1

Please sign in to comment.