Skip to content

Commit

Permalink
AP_Scripting: fixed alignment errors for 64 bit lua on ChibiOS
Browse files Browse the repository at this point in the history
cope with sizeof(void*) being less than sizeof(lua_Number)
  • Loading branch information
tridge committed Jun 9, 2023
1 parent 72d1d19 commit a3184d7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
8 changes: 8 additions & 0 deletions libraries/AP_Scripting/lua/src/lgc.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ static void removeentry (Node *n) {
setdeadvalue(wgkey(n)); /* unused and unmarked key; remove it */
}

/*
the GC code assumed pointer alignment is at least as large as
lua_Number. The memory allocator does align to at least 8 bytes, but
the compiler doesn't know this so need to suppress cast-align warnings
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"

/*
** tells whether a key or value can be cleared from a weak
Expand Down Expand Up @@ -526,6 +533,7 @@ static lu_mem traverseLclosure (global_State *g, LClosure *cl) {
return sizeLclosure(cl->nupvalues);
}

#pragma GCC diagnostic pop

static lu_mem traversethread (global_State *g, lua_State *th) {
StkId o = th->stack;
Expand Down
38 changes: 19 additions & 19 deletions libraries/AP_Scripting/lua/src/lobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
/*
** Common type for all collectable objects
*/
typedef struct GCObject GCObject;
typedef struct GCObject GCObject ALIGNED_NUMBER;


/*
Expand All @@ -84,7 +84,7 @@ typedef struct GCObject GCObject;
*/
struct GCObject {
CommonHeader;
};
} ALIGNED_NUMBER;



Expand All @@ -104,15 +104,15 @@ typedef union Value {
lua_CFunction f; /* light C functions */
lua_Integer i; /* integer numbers */
lua_Number n; /* float numbers */
} Value;
} Value ALIGNED_NUMBER;


#define TValuefields Value value_; int tt_


typedef struct lua_TValue {
TValuefields;
} TValue;
TValuefields;
} TValue ALIGNED_NUMBER;



Expand Down Expand Up @@ -309,7 +309,7 @@ typedef struct TString {
size_t lnglen; /* length for long strings */
struct TString *hnext; /* linked list for hash table */
} u;
} TString;
} TString ALIGNED_NUMBER;


/*
Expand All @@ -318,7 +318,7 @@ typedef struct TString {
typedef union UTString {
L_Umaxalign dummy; /* ensures maximum alignment for strings */
TString tsv;
} UTString;
} UTString ALIGNED_NUMBER;


/*
Expand Down Expand Up @@ -349,7 +349,7 @@ typedef struct Udata {
struct Table *metatable;
size_t len; /* number of bytes */
union Value user_; /* user value */
} Udata;
} Udata ALIGNED_NUMBER;


/*
Expand All @@ -358,7 +358,7 @@ typedef struct Udata {
typedef union UUdata {
L_Umaxalign dummy; /* ensures maximum alignment for 'local' udata */
Udata uv;
} UUdata;
} UUdata ALIGNED_NUMBER;


/*
Expand Down Expand Up @@ -387,7 +387,7 @@ typedef struct Upvaldesc {
TString *name; /* upvalue name (for debug information) */
lu_byte instack; /* whether it is in stack (register) */
lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
} Upvaldesc;
} Upvaldesc ALIGNED_NUMBER;


/*
Expand All @@ -398,7 +398,7 @@ typedef struct LocVar {
TString *varname;
int startpc; /* first point where variable is active */
int endpc; /* first point where variable is dead */
} LocVar;
} LocVar ALIGNED_NUMBER;


/*
Expand Down Expand Up @@ -426,7 +426,7 @@ typedef struct Proto {
struct LClosure *cache; /* last-created closure with this prototype */
TString *source; /* used for debug information */
GCObject *gclist;
} Proto;
} Proto ALIGNED_NUMBER;



Expand All @@ -441,26 +441,26 @@ typedef struct UpVal UpVal;
*/

#define ClosureHeader \
CommonHeader; lu_byte nupvalues; GCObject *gclist
CommonHeader; lu_byte nupvalues; GCObject *gclist ALIGNED_NUMBER

typedef struct CClosure {
ClosureHeader;
lua_CFunction f;
TValue upvalue[1]; /* list of upvalues */
} CClosure;
} CClosure ALIGNED_NUMBER;


typedef struct LClosure {
ClosureHeader;
struct Proto *p;
UpVal *upvals[1]; /* list of upvalues */
} LClosure;
} LClosure ALIGNED_NUMBER;


typedef union Closure {
CClosure c;
LClosure l;
} Closure;
} Closure ALIGNED_NUMBER;


#define isLfunction(o) ttisLclosure(o)
Expand All @@ -478,7 +478,7 @@ typedef union TKey {
int next; /* for chaining (offset for next node) */
} nk;
TValue tvk;
} TKey;
} TKey ALIGNED_NUMBER;


/* copy a value into a key without messing up field 'next' */
Expand All @@ -491,7 +491,7 @@ typedef union TKey {
typedef struct Node {
TValue i_val;
TKey i_key;
} Node;
} Node ALIGNED_NUMBER;


typedef struct Table {
Expand All @@ -504,7 +504,7 @@ typedef struct Table {
Node *lastfree; /* any free position is before this position */
struct Table *metatable;
GCObject *gclist;
} Table;
} Table ALIGNED_NUMBER;



Expand Down
10 changes: 5 additions & 5 deletions libraries/AP_Scripting/lua/src/lstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ typedef struct stringtable {
TString **hash;
int nuse; /* number of elements */
int size;
} stringtable;
} stringtable ALIGNED_NUMBER;


/*
Expand Down Expand Up @@ -107,7 +107,7 @@ typedef struct CallInfo {
ptrdiff_t extra;
short nresults; /* expected number of results from this function */
unsigned short callstatus;
} CallInfo;
} CallInfo ALIGNED_NUMBER;


/*
Expand Down Expand Up @@ -169,7 +169,7 @@ typedef struct global_State {
TString *tmname[TM_N]; /* array with tag-method names */
struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
} global_State;
} global_State ALIGNED_NUMBER;


/*
Expand Down Expand Up @@ -199,7 +199,7 @@ struct lua_State {
unsigned short nCcalls; /* number of nested C calls */
l_signalT hookmask;
lu_byte allowhook;
};
} ALIGNED_NUMBER;


#define G(L) (L->l_G)
Expand All @@ -216,7 +216,7 @@ union GCUnion {
struct Table h;
struct Proto p;
struct lua_State th; /* thread */
};
} ALIGNED_NUMBER;


#define cast_u(o) cast(union GCUnion *, (o))
Expand Down
3 changes: 3 additions & 0 deletions libraries/AP_Scripting/lua/src/luaconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
// use 32 bit number and integer types if we don't have hardware double supportx
#if !HAL_HAVE_HARDWARE_DOUBLE
#define LUA_32BITS 1
#define ALIGNED_NUMBER
#else
// ChibiOS does not define LLONG_MAX
#ifndef LLONG_MAX
#define LLONG_MAX 9223372036854775807LL
#define LLONG_MIN (-LLONG_MAX - 1)
#endif
#pragma GCC diagnostic error "-Wframe-larger-than=1400"
#define ALIGNED_NUMBER __attribute__((aligned(8)))
#endif // HAL_HAVE_HARDWARE_DOUBLE

/*
Expand Down

0 comments on commit a3184d7

Please sign in to comment.