Skip to content

Commit 6053633

Browse files
committed
Utilize C99 flexible array members to allow for FORTIFY/ASAN
1 parent dea03eb commit 6053633

11 files changed

Lines changed: 94 additions & 16 deletions

File tree

.github/workflows/compilation_on_android_ubuntu.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,48 @@ jobs:
369369
ctest --output-on-failure
370370
working-directory: tests/unit
371371

372+
build_array_bounds_warning_check:
373+
needs: [build_llvm_libraries_on_ubuntu_2204]
374+
runs-on: ${{ matrix.os }}
375+
strategy:
376+
matrix:
377+
os: [ubuntu-22.04]
378+
include:
379+
- os: ubuntu-22.04
380+
llvm_cache_key: ${{ needs.build_llvm_libraries_on_ubuntu_2204.outputs.cache_key }}
381+
steps:
382+
- name: checkout
383+
uses: actions/checkout@v6.0.2
384+
with:
385+
submodules: recursive
386+
387+
- name: Get LLVM libraries
388+
id: retrieve_llvm_libs
389+
uses: actions/cache@v5
390+
with:
391+
path: |
392+
./core/deps/llvm/build/bin
393+
./core/deps/llvm/build/include
394+
./core/deps/llvm/build/lib
395+
./core/deps/llvm/build/libexec
396+
./core/deps/llvm/build/share
397+
key: ${{ matrix.llvm_cache_key }}
398+
399+
- name: Quit if cache miss
400+
if: steps.retrieve_llvm_libs.outputs.cache-hit != 'true'
401+
run: echo "::error::can not get prebuilt llvm libraries" && exit 1
402+
403+
- name: Build shared-utils with array-bounds warnings as errors
404+
run: |
405+
mkdir build && cd build
406+
cmake .. \
407+
-DCMAKE_C_COMPILER=clang \
408+
-DCMAKE_CXX_COMPILER=clang++ \
409+
-DCMAKE_C_FLAGS="-Werror=array-bounds" \
410+
-DCMAKE_CXX_FLAGS="-Werror=array-bounds"
411+
cmake --build . --target shared_utils_test --parallel 4
412+
working-directory: tests/unit
413+
372414
build_regression_tests:
373415
needs: [build_llvm_libraries_on_ubuntu_2204]
374416
runs-on: ${{ matrix.os }}

core/iwasm/aot/aot_runtime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ typedef struct AOTFrame {
432432
* currently local's ref flags are stored in AOTModule,
433433
* here we only reserve the padding bytes
434434
*/
435-
uint32 lp[1];
435+
BH_FLEXIBLE_ARRAY_MEMBER(uint32, lp);
436436
} AOTFrame;
437437

438438
#if WASM_ENABLE_STATIC_PGO != 0

core/iwasm/common/gc/gc_object.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ typedef uintptr_t WASMI31ObjectRef;
7777
typedef struct WASMStructObject {
7878
/* Must be pointer of WASMRttObject of struct type */
7979
WASMObjectHeader header;
80-
uint8 field_data[1];
80+
BH_FLEXIBLE_ARRAY_MEMBER(uint8, field_data);
8181
} WASMStructObject, *WASMStructObjectRef;
8282

8383
/* Representation of WASM array objects */
@@ -89,7 +89,7 @@ typedef struct WASMArrayObject {
8989
* elem_size = 2 ^ (length & 0x3)
9090
*/
9191
uint32 length;
92-
uint8 elem_data[1];
92+
BH_FLEXIBLE_ARRAY_MEMBER(uint8, elem_data);
9393
} WASMArrayObject, *WASMArrayObjectRef;
9494

9595
#define WASM_ARRAY_LENGTH_SHIFT 2

core/iwasm/compilation/aot.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ typedef struct AOTTableInitData {
133133
/* Function index count */
134134
uint32 value_count;
135135
/* Function index array */
136-
InitializerExpression init_values[1];
136+
BH_FLEXIBLE_ARRAY_MEMBER(InitializerExpression, init_values);
137137
} AOTTableInitData;
138138

139139
/**

core/iwasm/interpreter/wasm.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,13 @@ typedef union WASMValue {
270270
typedef struct WASMStructNewInitValues {
271271
uint32 type_idx;
272272
uint32 count;
273-
WASMValue fields[1];
273+
BH_FLEXIBLE_ARRAY_MEMBER(WASMValue, fields);
274274
} WASMStructNewInitValues;
275275

276276
typedef struct WASMArrayNewInitValues {
277277
uint32 type_idx;
278278
uint32 length;
279-
WASMValue elem_data[1];
279+
BH_FLEXIBLE_ARRAY_MEMBER(WASMValue, elem_data);
280280
} WASMArrayNewInitValues;
281281

282282
typedef struct InitializerExpression {
@@ -444,7 +444,7 @@ typedef struct WASMFuncType {
444444
/* types of params and results, only store the first byte
445445
* of the type, if it cannot be described with one byte,
446446
* then the full type info is stored in ref_type_maps */
447-
uint8 types[1];
447+
BH_FLEXIBLE_ARRAY_MEMBER(uint8, types);
448448
} WASMFuncType;
449449

450450
#if WASM_ENABLE_GC != 0
@@ -486,7 +486,7 @@ typedef struct WASMStructType {
486486
* the first byte of the field type, if it cannot be described
487487
* with one byte, then the full field type info is stored in
488488
* ref_type_maps */
489-
WASMStructFieldType fields[1];
489+
BH_FLEXIBLE_ARRAY_MEMBER(WASMStructFieldType, fields);
490490
} WASMStructType;
491491

492492
typedef struct WASMArrayType {
@@ -862,7 +862,7 @@ typedef struct BrTableCache {
862862
/* Address of br_table opcode */
863863
uint8 *br_table_op_addr;
864864
uint32 br_count;
865-
uint32 br_depths[1];
865+
BH_FLEXIBLE_ARRAY_MEMBER(uint32, br_depths);
866866
} BrTableCache;
867867

868868
#if WASM_ENABLE_DEBUG_INTERP != 0

core/iwasm/interpreter/wasm_interp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ typedef struct WASMInterpFrame {
5050
#if WASM_ENABLE_GC != 0
5151
uint8 *frame_ref;
5252
#endif
53-
uint32 operand[1];
53+
BH_FLEXIBLE_ARRAY_MEMBER(uint32, operand);
5454
#else /* else of WASM_ENABLE_FAST_INTERP != 0 */
5555
/* Operand stack top pointer of the current frame. The bottom of
5656
the stack is the next cell after the last local variable. */
@@ -71,7 +71,7 @@ typedef struct WASMInterpFrame {
7171
* whether each cell in local and stack area is a GC obj
7272
* jit spill cache: only available for fast jit
7373
*/
74-
uint32 lp[1];
74+
BH_FLEXIBLE_ARRAY_MEMBER(uint32, lp);
7575
#endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
7676
} WASMInterpFrame;
7777

core/iwasm/interpreter/wasm_runtime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ struct WASMTableInstance {
188188
/* Maximum size */
189189
uint32 max_size;
190190
/* Table elements */
191-
table_elem_type_t elems[1];
191+
BH_FLEXIBLE_ARRAY_MEMBER(table_elem_type_t, elems);
192192
};
193193

194194
struct WASMGlobalInstance {

core/shared/platform/include/platform_common.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ BH_VPRINTF(const char *format, va_list ap);
9999
#endif
100100
#endif
101101

102+
/* Prefer C99 flexible array members for FORTIFY/ASAN compatibility */
103+
#ifndef BH_FLEXIBLE_ARRAY_MEMBER
104+
#if !defined(__cplusplus) && defined(__STDC_VERSION__) \
105+
&& __STDC_VERSION__ >= 199901L
106+
#define BH_FLEXIBLE_ARRAY_MEMBER(type, name) type name[]
107+
#else
108+
#define BH_FLEXIBLE_ARRAY_MEMBER(type, name) type name[1]
109+
#endif
110+
#endif
111+
102112
typedef uint8_t uint8;
103113
typedef int8_t int8;
104114
typedef uint16_t uint16;

core/shared/utils/bh_bitmap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ typedef struct bh_bitmap {
2323
uintptr_t end_index;
2424

2525
/* The bitmap. */
26-
uint8 map[1];
26+
BH_FLEXIBLE_ARRAY_MEMBER(uint8, map);
2727
} bh_bitmap;
2828

2929
/**

core/shared/utils/bh_hashmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct HashMap {
2222
KeyEqualFunc key_equal_func;
2323
KeyDestroyFunc key_destroy_func;
2424
ValueDestroyFunc value_destroy_func;
25-
HashMapElem *elements[1];
25+
BH_FLEXIBLE_ARRAY_MEMBER(HashMapElem *, elements);
2626
};
2727

2828
HashMap *

0 commit comments

Comments
 (0)