undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,258 @@
/*
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @library ../ /test/lib
* @requires jdk.foreign.linker != "FALLBACK"
* @build NativeTestHelper
* @run testng/othervm/native --enable-native-access=ALL-UNNAMED TestNested
*/
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.GroupLayout;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SegmentAllocator;
import java.lang.foreign.StructLayout;
import java.lang.foreign.UnionLayout;
import java.lang.invoke.MethodHandle;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
public class TestNested extends NativeTestHelper {
static {
System.loadLibrary("Nested");
}
@Test(dataProvider = "nestedLayouts")
public void testNested(GroupLayout layout) throws Throwable {
try (Arena arena = Arena.ofConfined()) {
Random random = new Random(0);
TestValue testValue = genTestValue(random, layout, arena);
String funcName = "test_" + layout.name().orElseThrow();
FunctionDescriptor downcallDesc = FunctionDescriptor.of(layout, layout, C_POINTER);
FunctionDescriptor upcallDesc = FunctionDescriptor.of(layout, layout);
MethodHandle downcallHandle = downcallHandle(funcName, downcallDesc);
AtomicReference<Object[]> returnBox = new AtomicReference<>();
MemorySegment stub = makeArgSaverCB(upcallDesc, arena, returnBox, 0);
MemorySegment returned = (MemorySegment) downcallHandle.invokeExact(
(SegmentAllocator) arena, (MemorySegment) testValue.value(), stub);
testValue.check().accept(returnBox.get()[0]);
testValue.check().accept(returned);
}
}
@DataProvider
public static Object[][] nestedLayouts() {
List<GroupLayout> layouts = List.of(
S1, U1, U17, S2, S3, S4, S5, S6, U2, S7, U3, U4, U5, U6, U7, S8, S9, U8, U9, U10, S10,
U11, S11, U12, S12, U13, U14, U15, S13, S14, U16, S15);
return layouts.stream().map(l -> new Object[]{l}).toArray(Object[][]::new);
}
static final StructLayout S1 = MemoryLayout.structLayout(
C_DOUBLE.withName("f0"),
C_LONG_LONG.withName("f1"),
C_DOUBLE.withName("f2"),
C_INT.withName("f3"),
MemoryLayout.paddingLayout(4)
).withName("S1");
static final UnionLayout U1 = MemoryLayout.unionLayout(
C_SHORT.withName("f0"),
C_LONG_LONG.withName("f1"),
C_SHORT.withName("f2"),
MemoryLayout.sequenceLayout(4, MemoryLayout.sequenceLayout(3, C_CHAR)).withName("f3"),
MemoryLayout.paddingLayout(16)
).withName("U1");
static final UnionLayout U17 = MemoryLayout.unionLayout(
C_CHAR.withName("f0"),
C_CHAR.withName("f1"),
C_LONG_LONG.withName("f2"),
C_DOUBLE.withName("f3")
).withName("U17");
static final StructLayout S2 = MemoryLayout.structLayout(
U17.withName("f0"),
MemoryLayout.sequenceLayout(4, C_LONG_LONG).withName("f1"),
C_SHORT.withName("f2"),
MemoryLayout.paddingLayout(6)
).withName("S2");
static final StructLayout S3 = MemoryLayout.structLayout(
C_FLOAT.withName("f0"),
C_INT.withName("f1"),
U1.withName("f2"),
S2.withName("f3")
).withName("S3");
static final StructLayout S4 = MemoryLayout.structLayout(
MemoryLayout.sequenceLayout(2, C_SHORT).withName("f0"),
MemoryLayout.paddingLayout(4),
S1.withName("f1")
).withName("S4");
static final StructLayout S5 = MemoryLayout.structLayout(
C_FLOAT.withName("f0"),
MemoryLayout.paddingLayout(4),
C_POINTER.withName("f1"),
S4.withName("f2")
).withName("S5");
static final StructLayout S6 = MemoryLayout.structLayout(
S5.withName("f0")
).withName("S6");
static final UnionLayout U2 = MemoryLayout.unionLayout(
C_FLOAT.withName("f0"),
C_SHORT.withName("f1"),
C_POINTER.withName("f2"),
C_FLOAT.withName("f3")
).withName("U2");
static final StructLayout S7 = MemoryLayout.structLayout(
C_DOUBLE.withName("f0"),
C_SHORT.withName("f1"),
C_SHORT.withName("f2"),
MemoryLayout.paddingLayout(4),
C_LONG_LONG.withName("f3")
).withName("S7");
static final UnionLayout U3 = MemoryLayout.unionLayout(
C_POINTER.withName("f0"),
U2.withName("f1"),
C_LONG_LONG.withName("f2"),
S7.withName("f3")
).withName("U3");
static final UnionLayout U4 = MemoryLayout.unionLayout(
C_FLOAT.withName("f0")
).withName("U4");
static final UnionLayout U5 = MemoryLayout.unionLayout(
U3.withName("f0"),
MemoryLayout.sequenceLayout(3, C_LONG_LONG).withName("f1"),
U4.withName("f2"),
C_FLOAT.withName("f3")
).withName("U5");
static final UnionLayout U6 = MemoryLayout.unionLayout(
C_SHORT.withName("f0"),
C_FLOAT.withName("f1"),
U5.withName("f2"),
C_SHORT.withName("f3")
).withName("U6");
static final UnionLayout U7 = MemoryLayout.unionLayout(
C_SHORT.withName("f0")
).withName("U7");
static final StructLayout S8 = MemoryLayout.structLayout(
MemoryLayout.sequenceLayout(3, C_DOUBLE).withName("f0"),
U7.withName("f1"),
MemoryLayout.paddingLayout(6),
C_POINTER.withName("f2"),
C_POINTER.withName("f3")
).withName("S8");
static final StructLayout S9 = MemoryLayout.structLayout(
C_CHAR.withName("f0"),
MemoryLayout.paddingLayout(7),
MemoryLayout.sequenceLayout(2, C_DOUBLE).withName("f1"),
C_CHAR.withName("f2"),
MemoryLayout.paddingLayout(7),
S8.withName("f3")
).withName("S9");
static final UnionLayout U8 = MemoryLayout.unionLayout(
C_LONG_LONG.withName("f0"),
C_POINTER.withName("f1"),
S9.withName("f2")
).withName("U8");
static final UnionLayout U9 = MemoryLayout.unionLayout(
C_INT.withName("f0"),
C_DOUBLE.withName("f1"),
MemoryLayout.sequenceLayout(2, C_SHORT).withName("f2"),
C_LONG_LONG.withName("f3")
).withName("U9");
static final UnionLayout U10 = MemoryLayout.unionLayout(
C_LONG_LONG.withName("f0"),
U9.withName("f1"),
C_CHAR.withName("f2"),
C_FLOAT.withName("f3")
).withName("U10");
static final StructLayout S10 = MemoryLayout.structLayout(
MemoryLayout.sequenceLayout(4, C_DOUBLE).withName("f0")
).withName("S10");
static final UnionLayout U11 = MemoryLayout.unionLayout(
MemoryLayout.sequenceLayout(3, S10).withName("f0")
).withName("U11");
static final StructLayout S11 = MemoryLayout.structLayout(
C_SHORT.withName("f0"),
C_CHAR.withName("f1"),
MemoryLayout.paddingLayout(1)
).withName("S11");
static final UnionLayout U12 = MemoryLayout.unionLayout(
C_FLOAT.withName("f0"),
S11.withName("f1"),
C_CHAR.withName("f2"),
C_CHAR.withName("f3")
).withName("U12");
static final StructLayout S12 = MemoryLayout.structLayout(
U12.withName("f0"),
C_FLOAT.withName("f1")
).withName("S12");
static final UnionLayout U13 = MemoryLayout.unionLayout(
C_FLOAT.withName("f0"),
S12.withName("f1")
).withName("U13");
static final UnionLayout U14 = MemoryLayout.unionLayout(
C_INT.withName("f0"),
MemoryLayout.sequenceLayout(2, C_POINTER).withName("f1"),
MemoryLayout.sequenceLayout(2, MemoryLayout.sequenceLayout(3, C_FLOAT)).withName("f2")
).withName("U14");
static final UnionLayout U15 = MemoryLayout.unionLayout(
C_POINTER.withName("f0"),
C_LONG_LONG.withName("f1"),
MemoryLayout.sequenceLayout(1, C_DOUBLE).withName("f2"),
C_LONG_LONG.withName("f3")
).withName("U15");
static final StructLayout S13 = MemoryLayout.structLayout(
C_INT.withName("f0"),
C_CHAR.withName("f1"),
MemoryLayout.paddingLayout(3),
C_POINTER.withName("f2"),
C_CHAR.withName("f3"),
MemoryLayout.paddingLayout(7)
).withName("S13");
static final StructLayout S14 = MemoryLayout.structLayout(
C_LONG_LONG.withName("f0")
).withName("S14");
static final UnionLayout U16 = MemoryLayout.unionLayout(
MemoryLayout.sequenceLayout(4, C_SHORT).withName("f0"),
C_INT.withName("f1"),
S13.withName("f2"),
S14.withName("f3")
).withName("U16");
static final StructLayout S15 = MemoryLayout.structLayout(
U16.withName("f0"),
C_FLOAT.withName("f1"),
C_INT.withName("f2"),
C_LONG_LONG.withName("f3")
).withName("S15");
}

View file

@ -0,0 +1,98 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "export.h"
#ifdef _AIX
#pragma align (natural)
#endif
struct S1{ double f0; long long f1; double f2; int f3; };
union U1{ short f0; long long f1; short f2; char f3[4][3]; };
union U17{ char f0; char f1; long long f2; double f3; };
struct S2{ union U17 f0; long long f1[4]; short f2; };
struct S3{ float f0; int f1; union U1 f2; struct S2 f3; };
struct S4{ short f0[2]; struct S1 f1; };
struct S5{ float f0; void* f1; struct S4 f2; };
struct S6{ struct S5 f0; };
union U2{ float f0; short f1; void* f2; float f3; };
struct S7{ double f0; short f1; short f2; long long f3; };
union U3{ void* f0; union U2 f1; long long f2; struct S7 f3; };
union U4{ float f0; };
union U5{ union U3 f0; long long f1[3]; union U4 f2; float f3; };
union U6{ short f0; float f1; union U5 f2; short f3; };
union U7{ short f0; };
struct S8{ double f0[3]; union U7 f1; void* f2; void* f3; };
struct S9{ char f0; double f1[2]; char f2; struct S8 f3; };
union U8{ long long f0; void* f1; struct S9 f2; };
union U9{ int f0; double f1; short f2[2]; long long f3; };
union U10{ long long f0; union U9 f1; char f2; float f3; };
struct S10{ double f0[4]; };
union U11{ struct S10 f0[3]; };
struct S11{ short f0; char f1; };
union U12{ float f0; struct S11 f1; char f2; char f3; };
struct S12{ union U12 f0; float f1; };
union U13{ float f0; struct S12 f1; };
union U14{ int f0; void* f1[2]; float f2[2][3]; };
union U15{ void* f0; long long f1; double f2[1]; long long f3; };
struct S13{ int f0; char f1; void* f2; char f3; };
struct S14{ long long f0; };
union U16{ short f0[4]; int f1; struct S13 f2; struct S14 f3; };
struct S15{ union U16 f0; float f1; int f2; long long f3; };
EXPORT struct S1 test_S1(struct S1 arg, struct S1(*cb)(struct S1)) { return cb(arg); }
EXPORT union U1 test_U1(union U1 arg, union U1(*cb)(union U1)) { return cb(arg); }
EXPORT union U17 test_U17(union U17 arg, union U17(*cb)(union U17)) { return cb(arg); }
EXPORT struct S2 test_S2(struct S2 arg, struct S2(*cb)(struct S2)) { return cb(arg); }
EXPORT struct S3 test_S3(struct S3 arg, struct S3(*cb)(struct S3)) { return cb(arg); }
EXPORT struct S4 test_S4(struct S4 arg, struct S4(*cb)(struct S4)) { return cb(arg); }
EXPORT struct S5 test_S5(struct S5 arg, struct S5(*cb)(struct S5)) { return cb(arg); }
EXPORT struct S6 test_S6(struct S6 arg, struct S6(*cb)(struct S6)) { return cb(arg); }
EXPORT union U2 test_U2(union U2 arg, union U2(*cb)(union U2)) { return cb(arg); }
EXPORT struct S7 test_S7(struct S7 arg, struct S7(*cb)(struct S7)) { return cb(arg); }
EXPORT union U3 test_U3(union U3 arg, union U3(*cb)(union U3)) { return cb(arg); }
EXPORT union U4 test_U4(union U4 arg, union U4(*cb)(union U4)) { return cb(arg); }
EXPORT union U5 test_U5(union U5 arg, union U5(*cb)(union U5)) { return cb(arg); }
EXPORT union U6 test_U6(union U6 arg, union U6(*cb)(union U6)) { return cb(arg); }
EXPORT union U7 test_U7(union U7 arg, union U7(*cb)(union U7)) { return cb(arg); }
EXPORT struct S8 test_S8(struct S8 arg, struct S8(*cb)(struct S8)) { return cb(arg); }
EXPORT struct S9 test_S9(struct S9 arg, struct S9(*cb)(struct S9)) { return cb(arg); }
EXPORT union U8 test_U8(union U8 arg, union U8(*cb)(union U8)) { return cb(arg); }
EXPORT union U9 test_U9(union U9 arg, union U9(*cb)(union U9)) { return cb(arg); }
EXPORT union U10 test_U10(union U10 arg, union U10(*cb)(union U10)) { return cb(arg); }
EXPORT struct S10 test_S10(struct S10 arg, struct S10(*cb)(struct S10)) { return cb(arg); }
EXPORT union U11 test_U11(union U11 arg, union U11(*cb)(union U11)) { return cb(arg); }
EXPORT struct S11 test_S11(struct S11 arg, struct S11(*cb)(struct S11)) { return cb(arg); }
EXPORT union U12 test_U12(union U12 arg, union U12(*cb)(union U12)) { return cb(arg); }
EXPORT struct S12 test_S12(struct S12 arg, struct S12(*cb)(struct S12)) { return cb(arg); }
EXPORT union U13 test_U13(union U13 arg, union U13(*cb)(union U13)) { return cb(arg); }
EXPORT union U14 test_U14(union U14 arg, union U14(*cb)(union U14)) { return cb(arg); }
EXPORT union U15 test_U15(union U15 arg, union U15(*cb)(union U15)) { return cb(arg); }
EXPORT struct S13 test_S13(struct S13 arg, struct S13(*cb)(struct S13)) { return cb(arg); }
EXPORT struct S14 test_S14(struct S14 arg, struct S14(*cb)(struct S14)) { return cb(arg); }
EXPORT union U16 test_U16(union U16 arg, union U16(*cb)(union U16)) { return cb(arg); }
EXPORT struct S15 test_S15(struct S15 arg, struct S15(*cb)(struct S15)) { return cb(arg); }
#ifdef _AIX
#pragma align (reset)
#endif