Skip to content

Commit

Permalink
bugfix: _datum_heap_read() handler may be called on misaligned address
Browse files Browse the repository at this point in the history
issue is reported at heterodb#719
  • Loading branch information
kaigai committed Jan 27, 2024
1 parent 3bd4304 commit 325b6b4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/xpu_basetype.cu
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ PGSTROM_SQLTYPE_OPERATORS(bool,true,1,sizeof(bool));
xpu_##NAME##_t *result = (xpu_##NAME##_t *)__result; \
\
result->expr_ops = &xpu_##NAME##_ops; \
result->value = *((const BASETYPE *)addr); \
__FetchStore(result->value, (const BASETYPE *)addr); \
return true; \
} \
STATIC_FUNCTION(bool) \
Expand Down
26 changes: 23 additions & 3 deletions src/xpu_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,27 +193,47 @@ __runtime_file_name(const char *path)
#define __FILE_NAME__ __runtime_file_name(__FILE__)
#endif

#ifdef __CUDACC__
#ifdef __cplusplus
template <typename T>
INLINE_FUNCTION(T)
__Fetch(const T *ptr)
{
T temp;

if ((sizeof(T) & (sizeof(T)-1)) == 0 &&
(((uintptr_t)ptr) & (sizeof(T)-1)) == 0)
{
return *ptr;
}
memcpy(&temp, ptr, sizeof(T));

return temp;
}

template <typename T>
INLINE_FUNCTION(void)
__FetchStore(T &dest, const T *ptr)
{
if ((sizeof(T) & (sizeof(T)-1)) == 0 &&
(((uintptr_t)ptr) & (sizeof(T)-1)) == 0)
{
dest = *ptr;
}
else
{
memcpy(&dest, ptr, sizeof(T));
}
}

template <typename T>
INLINE_FUNCTION(T)
__volatileRead(const volatile T *ptr)
{
return *ptr;
}

#else
#else /* __cplusplus */
#define __Fetch(PTR) (*(PTR))
#define __FetchStore(DEST,PTR) do { (DEST) = *(PTR); } while(0)
#define __volatileRead(PTR) (*(PTR))
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/xpu_misclib.cu
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ xpu_money_datum_heap_read(kern_context *kcxt,
xpu_money_t *result = (xpu_money_t *)__result;

result->expr_ops = &xpu_money_ops;
result->value = *((Cash *)addr);
__FetchStore(result->value, (Cash *)addr);
return true;
}

Expand Down
8 changes: 4 additions & 4 deletions src/xpu_timelib.cu
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ xpu_date_datum_heap_read(kern_context *kcxt,
xpu_date_t *result = (xpu_date_t *)__result;

result->expr_ops = &xpu_date_ops;
result->value = *((const DateADT *)addr);
__FetchStore(result->value, (const DateADT *)addr);
return true;
}

Expand Down Expand Up @@ -224,7 +224,7 @@ xpu_time_datum_heap_read(kern_context *kcxt,
xpu_time_t *result = (xpu_time_t *)__result;

result->expr_ops = &xpu_time_ops;
result->value = *((const TimeADT *)addr);
__FetchStore(result->value, (const TimeADT *)addr);
return true;
}

Expand Down Expand Up @@ -517,7 +517,7 @@ xpu_timestamp_datum_heap_read(kern_context *kcxt,
xpu_timestamp_t *result = (xpu_timestamp_t *)__result;

result->expr_ops = &xpu_timestamp_ops;
result->value = *((int64_t *)addr);
__FetchStore(result->value, (int64_t *)addr);
return true;
}

Expand Down Expand Up @@ -697,7 +697,7 @@ xpu_timestamptz_datum_heap_read(kern_context *kcxt,
xpu_timestamptz_t *result = (xpu_timestamptz_t *)__result;

result->expr_ops = &xpu_timestamptz_ops;
result->value = *((const TimestampTz *)addr);
__FetchStore(result->value, (const TimestampTz *)addr);
return true;
}

Expand Down

0 comments on commit 325b6b4

Please sign in to comment.